title: help on formatting my notebook back as it was. From 'code' style to Input style
body: <p>I did something silly I think.</p>
  
  <p>I changed my cell style to 'code' and starting changing indentation and moving code around the way I like to, (since it is easier to do that in 'code' cell style, as the notebook interface does not get in the way). But after trying it for a while, I find other issues with this with this style.</p>
  
  <p>Now I changed the style back to 'input'. But now all the code has all these extra spaces in them, and code does not look good any more as it was in the original 'input' style.</p>
  
  <p>I made too many edits and manual formatting to the code.</p>
  
  <p>Is there a way to tell Mathematica to 'auto-format' the code back to the default input style? i.e. remove all the extra space where needed, realign things the way they were, etc...</p>
  
  <p>If I have to do this by hand, will take me forever. I could not find such as option. I tried many options there on the menues, and nothing helps.</p>
  
  <p>ps. in Matlab, this is trivial to do, just select all code, then select 'smart-ident' from a menu, and it will do that. </p>
  
  <p>ps. I tried emacs Mathematica mode, and they have no prety-print either.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I tried these: Select cell, then Cell->Convert To, and selected 'InputForm' and also selected 'input Form Display'.</p>
  
  <p>This seems to have removed all the extra white spaces. Which is good. Now I only need to go and just hit returns in all the right places to get it back to the original form.</p>
  
  <p>At least this is much better than having to delete spaces by hand. So, disaster seems to be contained for now.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>screen shot after converting 'code' style back to 'standard form'. very hard to read.</p>
  
  <p><img src="https://i.stack.imgur.com/KfwST.png" alt="enter image description here">
  thanks,</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>To make sure what the question is, given some Mathematica code, which is 'mangled' up, and formatted by hand with manual space inserting and manual inserting of returns all over, and now if I just put it in a notebook with default style, I want it to smart-indent back to the default setting, and have all the line-wrapping, etc... as if it was originally written in the default style. i.e. pretty-print to what it would be if it was written using the 'input' code style.</p>
  
  <p>here is a small function as an example that I formatted by hand. It will look not well formatted even more when I paste it here. If you copy this code to your notebook, default style, then how to auto-format it back to the default settings without doing it by hand?</p>
  
  <pre><code>If[Abs@omega &lt;= $MachineEpsilon,
      (
        data = {{0, 0}};
        p    = ListPlot[data, Sequence@plotOptions]
       ),
      (
        driverPeriod            = 2. Pi/Abs@omega;
        valuesOfDriverAmplitude = 
      Range[aStart, aStart + aLen, aLen/aIntervals];
        timeValues              = 
      Range[initialDriverPeriod*driverPeriod, 
       finalDriverPeriod*driverPeriod, driverPeriod ];
  
        data  = 
      Table[0, {Length[valuesOfDriverAmplitude]}, {Length[timeValues ]}];
        total = Length[timeValues]*Length[valuesOfDriverAmplitude];
        bifurcationProgress = 0.;
        count = 0.;
  
        Do[
           (
             {x1, x2, x3} = 
        solve[q, valuesOfDriverAmplitude [[i]], omega, phase, 
         initialDriverPeriod*driverPeriod,
                                  (finalDriverPeriod)*driverPeriod, x10,
          x20, isDamped, 4, 4];
             currentTorqueAmplitude = valuesOfDriverAmplitude[[i]];
             Do[
                (
  
         data[[i, j]] = {currentTorqueAmplitude, x2[timeValues[[j]]  ]};
                  count      += 1;
                  bifurcationProgress = count/total;
                 ),
                  {j, 1, Length[timeValues]}
                ]
            ),
           {i, 1, Length[valuesOfDriverAmplitude]}
           ];
  
       p = ListPlot[Flatten[data, 1], Sequence@plotOptions]
       )
    ];
  </code></pre>
  
  <p><strong>EDIT</strong>
  6 PM</p>
  
  <p>1) Saved the notebook as m file (mathematica package) called aclFix.m
  2) then I did the following</p>
  
  <pre><code>str=Import["aclFix.m")
  </code></pre>
  
  <p>also tried</p>
  
  <pre><code>str=Get["aclFix.m"];
  </code></pre>
  
  <p>but in both cases, all what I get is the actual Manipulate on the screen showing up. And the command shown below (StringReplace etc....) does not work on it as it is not a string. I get lots of errors, such as</p>
  
  <pre><code>    StringReplace::strse: String or list of strings expected at position 1 in 
  StringReplace[Manipulate[&lt;&lt;1&gt;&gt;,
  {{aStart,0.9,},0,2,0.01,ImageSize-&gt;Tiny,ImagePadding-&gt;0,ControlPlacement-&gt;1},
  {{aLen,0.2,},0.01,2,0.01,ImageSize-&gt;Tiny,ImagePadding-&gt;0,ControlPlacement-&gt;2},
  &lt;&lt;45&gt;&gt;,SynchronousUpdating-&gt;False,ContinuousAction-&gt;False,&lt;&lt;6&gt;&gt;],
  {(x_/;x==FromCharacterCode[32])..-&gt;,(x_/;x==&lt;&lt;17&gt;&gt;[9])..-&gt;}]. &gt;&gt;
  </code></pre>
  
  <p>may be I misunderstood the solution..</p>
  
  <p><strong>EDIT</strong>
  1 AM</p>
  
  <p>I exported the notebook as "m" file, Imported it back as</p>
  
  <pre><code>str = Import["aclFix.m", "Text"];
  </code></pre>
  
  <p>then</p>
  
  <pre><code>code=StringReplace[str, {(x_ /; x == FromCharacterCode[32]) .. -&gt; 
     "", (x_ /; x == FromCharacterCode[9]) .. -&gt; ""}]
  </code></pre>
  
  <p>then</p>
  
  <pre><code>Export["fix.m", code]
  </code></pre>
  
  <p>But it is still a string. When I import it back, it is still a string. I tried
  ToExpression[code] but it did nothing. How do convert it to actual code? This is an example looking at the .m using text editor. I used the above code to test it on. May be we are getting close here?</p>
  
  <p>Will look more at it.</p>
  
  <pre><code>(* Created by Wolfram Mathematica 8.0 for Students - Personal Use Only : www.wolfram.com *)
  
  "If[Abs@omega&lt;=$MachineEpsilon,\n(\ndata={{0,0}};\np=ListPlot[data,Sequence@p\
  
  lotOptions]\n),\n(\ndriverPeriod=2.Pi/Abs@omega;\nvaluesOfDriverAmplitude=\nR\
  
  ange[aStart,aStart+aLen,aLen/aIntervals];\ntimeValues=\nRange[initialDriverPe\
  
  riod*driverPeriod,\nfinalDriverPeriod*driverPeriod,driverPeriod];\n\ndata=\nT\
  
  able[0,{Length[valuesOfDriverAmplitude]},{Length[timeValues]}];\ntotal=Length\
  
  [timeValues]*Length[valuesOfDriverAmplitude];\nbifurcationProgress=0.;\ncount\
  
  =0.;\n\nDo[\n(\n{x1,x2,x3}=\nsolve[q,valuesOfDriverAmplitude[[i]],omega,phase\
  
  ,\ninitialDriverPeriod*driverPeriod,\n(finalDriverPeriod)*driverPeriod,x10,\n\
  
  x20,isDamped,4,4];\ncurrentTorqueAmplitude=valuesOfDriverAmplitude[[i]];\nDo[\
  
  \n(\n\ndata[[i,j]]={currentTorqueAmplitude,x2[timeValues[[j]]]};\ncount+=1;\n\
  
  bifurcationProgress=count/total;\n),\n{j,1,Length[timeValues]}\n]\n),\n{i,1,L\
  
  ength[valuesOfDriverAmplitude]}\n];\n\np=ListPlot[Flatten[data,1],Sequence@pl\
  
  otOptions]\n)\n];"
  </code></pre>