4
$\begingroup$

This question is a follow up from Formatting notebook files

In Programmatic formatting for Mathematica code - possible? masterxilo proposed to use <<GeneralUtilities` then GeneralUtilities`PrintDefinitions@f whereby f is some mathematica code to get a pretty-printed, formatted version of ??f.

Leonid Shifrin wrote a very nice software which is aimed to format mathematica code: https://github.com/lshifr/CodeFormatter

Concerning GeneralUtilities Leonid Shifrin wrote to masterxilo:

 "I have to admit that the formatter implemented in GeneralUtilities impressed me a lot ...".

I used Leonid Shifrins code as well tried to use GeneralUtilities as masterxilo proposed with the following one-liner:

Do[x = i; Do[y = j; Do[z = k;, {k, 1, 3}];, {j, 1, 3}];, {i, 1, 3}]

Leonid Shifrins code gives nearly what I expect:

Import["https://raw.githubusercontent.com/lshifr/CodeFormatter/master/\
CodeFormatter.m"]

    Needs["CodeFormatter`"]

    prn = CellPrint[Cell[BoxData[#], "Input"]] &; prn@
     FullCodeFormat@
      CodeFormatterMakeBoxes@
       Do[x = i; Do[y = j; Do[z = k;, {k, 1, 3}];, {j, 1, 3}];, {i, 1, 3}]

Result:
enter image description here

Why does the Null appears?

masterxilos proposal opens up a new notebook with the information: Attributes[Null] := {Protected};:

<< GeneralUtilities` 

GeneralUtilities`PrintDefinitions@
 Do[x = i; Do[y = j; Do[z = k;, {k, 1, 3}];, {j, 1, 3}];, {i, 1, 3}]

enter image description here

What do I wrong?

$\endgroup$
  • 1
    $\begingroup$ For the last part: Try using f[] := Do[x = i; Do[y = j; Do[z = k;, {k, 1, 3}];, {j, 1, 3}];, {i, 1, 3}] with PrintDefinitionsLocal[f]. $\endgroup$ – Karsten 7. Sep 27 '16 at 11:19
  • $\begingroup$ @Karsten 7: This is great, I tried it with a relatively long code and the result is nearly perfect. I wonder if there are possibilities to make some adjustments in PrintDefinitionsLocal. $\endgroup$ – mrz Sep 27 '16 at 13:57
  • 1
    $\begingroup$ Based on PrintDefinitionsLocal@PrintDefinitionsLocal, there are no options settings to do adjustments. However, you can inspect it a little deeper and figure out that, e.g., Block[{GeneralUtilities`Debugging`PackagePrivate`$PrintDefinitionsBackground = Orange}, PrintDefinitionsLocal[f]] will produce an orange background for the printed cells. $\endgroup$ – Karsten 7. Sep 27 '16 at 14:14
  • $\begingroup$ When I use ?GeneralUtilities`` *` I do not see Debugging listed. How did you find it? $\endgroup$ – mrz Sep 27 '16 at 14:39
  • 1
    $\begingroup$ By clicking through the definitions. This answer contains a short description of PrintDefinitions. $\endgroup$ – Karsten 7. Sep 27 '16 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.