1
$\begingroup$

Can Mathematica, natively or otherwise, behave more like a conventional text editing tool such as Sublime Text or Notepad++ when it comes to code formatting and syntax highlighting?

I have seen some users use Mathematica through power shell or terminal – I don't want to do this as it seems to defeat the instant feedback in terms of plotting one gets in a Notebook, but formatting code in the more traditional way would be nice.

At the moment I put everything in code blocks (Alt + 8) which mostly does the trick. There also exist packages in Sublime Text that allow syntax highlighting and code formatting, but then you have to copy your text over to a Mathematica Notebook – tedious!

Here is an example of Sublime Text with Wolfram Language syntax highlighting, I'm not necessarily looking for this exact style, and I would prefer to be able to keep the ability of working in Mathematica rather than coding in one environment an execute in another:

enter image description here

$\endgroup$
6
  • $\begingroup$ What portion of code formatting are you looking for? That’s a broad topic. If you want block indentation or autoindentation, it’s possible to get that baked into the system. $\endgroup$
    – b3m2a1
    Nov 15, 2019 at 18:33
  • $\begingroup$ Syntax highlighting, block indentation, the dashed lines one sees in Sublime to show bracket pairs...as close as to sublime text really -- in my view Sublime Text is near perfect when it comes to code formatting. $\endgroup$
    – user27119
    Nov 16, 2019 at 13:10
  • $\begingroup$ Syntax highlighting already mostly works. What about that would you like to see changed? I’m reasonably certain I can customize any of these things, just wondering what you’d like different. The dashed lines can also be set up (sorta) using a setting that will display spaces and tabs as very light dots. That specific look however is probably not possible. $\endgroup$
    – b3m2a1
    Nov 16, 2019 at 18:14
  • $\begingroup$ The dashed lines would be nice, a more consistent form of tabulation and indentation would be nice -- exactly what one gets in Sublime. Proper syntax highlighting, as far as I know the only syntax highlighting in native Mathematica is the variables in functions turning green. Any method of keeping code and clean, readable. I love Mathematica, but it's notebook and coding formatting could do with some work! $\endgroup$
    – user27119
    Nov 17, 2019 at 16:59
  • 3
    $\begingroup$ It seems like you're looking for an IDE for the Wolfram language. If yes, please have a look at Wolfram Workbench (Eclipse plug-in) or Mathematica plug-in for IntelliJ IDEA or PyCharm. Both have nice integration with MMA kernel, so you don't need to copy your text. $\endgroup$ Nov 18, 2019 at 6:33

1 Answer 1

7
$\begingroup$

To be honest, I still don't know what you're looking for since 90% of what is in your screenshot is already in Mathematica, but if you want to customize, here's something that'll do that on a single Notebook:

enter image description here

SetOptions[
 EvaluationNotebook[],
 With[
  {
   operatorStyle = {FontColor -> GrayLevel[.3]},
   commandStyle = {FontColor -> Orange},
   blockStyle = {FontWeight -> Bold},
   typeStyle = {FontColor -> Hue[.5 , .5, .5]},
   constStyle = {FontColor -> Hue[.1 , 1, .6]},
   globalStyle = {FontColor -> Pink},
   systemStyle = {FontColor -> Black},
   customSymbolStyle = {FontColor -> Hue[.5 , .7, .2]},
   codeStyle = {
     TabFilling -> StringRepeat[" ", 11] <> "|",
     TabSpacings -> 2,
     InitializationCell -> False
     }
   },
  {
   StyleDefinitions ->
    Notebook[{
      Cell[StyleData[StyleDefinitions -> "Default.nb"]],
      Cell[StyleData["OperatorStyle"], Sequence @@ operatorStyle],
      Cell[StyleData["CommandStyle"], Sequence @@ commandStyle],
      Cell[StyleData["BlockStyle"], Sequence @@ blockStyle],
      Cell[StyleData["TypeStyle"], Sequence @@ typeStyle],
      Cell[StyleData["ConstantStyle"], Sequence @@ constStyle],
      Cell[StyleData["GlobalStyle"], Sequence @@ globalStyle],
      Cell[StyleData["SystemStyle"], Sequence @@ systemStyle],
      Cell[StyleData["CustomSymbolStyle"], Sequence @@ customSymbolStyle],
      Cell[StyleData["Code"], Sequence @@ codeStyle]
      }],
   AutoStyleOptions ->
    {
     "FunctionLocalVariableStyle" -> {FontColor -> Blue},
     "LocalVariableStyle" -> {FontColor -> Purple},
     "PatternVariableStyle" -> {FontColor -> Darker@Green, 
       FontSlant -> "Italic"},
     "SymbolContextStyles" -> {
       "System`" -> "SystemStyle",
       "Global`" -> "GlobalStyle",
       Automatic -> "CustomSymbolStyle"
       }
     },
   AutoStyleWords -> {
     "@" -> "OperatorStyle", "//" -> "OperatorStyle", "~" -> "OperatorStyle", 
     ";" -> "OperatorStyle", "," -> "OperatorStyle", "-" -> "OperatorStyle", 
     "+" -> "OperatorStyle", "/" -> "OperatorStyle", "*" -> "OperatorStyle", 
     "(" -> "OperatorStyle", ")" -> "OperatorStyle", "[" -> "OperatorStyle", 
     "]" -> "OperatorStyle", "{" -> "OperatorStyle", "}" -> "OperatorStyle", 
     "[[" -> "OperatorStyle", "]]" -> "OperatorStyle", "<" -> "OperatorStyle",
      ">" -> "OperatorStyle", ":" -> "OperatorStyle", "::" -> "OperatorStyle",
      "." -> "OperatorStyle", ".." -> "OperatorStyle", 
     "..." -> "OperatorStyle", "_" -> "OperatorStyle", 
     "__" -> "OperatorStyle", "___" -> "OperatorStyle", 
     "?" -> "OperatorStyle", "&" -> "OperatorStyle", "&&" -> "OperatorStyle", 
     "~~" -> "OperatorStyle", "\[Rule]" -> "OperatorStyle", 
     "\[RuleDelayed]" -> "OperatorStyle", "->" -> "OperatorStyle", 
     ":>" -> "OperatorStyle", "|" -> "OperatorStyle", ":=" -> "OperatorStyle",
      "^:=" -> "OperatorStyle", "/:" -> "OperatorStyle", 
     "/;" -> "OperatorStyle",
     "Return" -> "CommandStyle", "Break" -> "CommandStyle", 
     "DialogReturn" -> "CommandStyle", "Goto" -> "CommandStyle", 
     "Throw" -> "CommandStyle", "Catch" -> "CommandStyle", 
     "Message" -> "CommandStyle", "Print" -> "CommandStyle", 
     "Assert" -> "CommandStyle", "Begin" -> "CommandStyle", 
     "BeginPackage" -> "CommandStyle", "End" -> "CommandStyle", 
     "EndPackage" -> "CommandStyle",
     "With" -> "BlockStyle", "Module" -> "BlockStyle", 
     "Block" -> "BlockStyle", "If" -> "BlockStyle", "Switch" -> "BlockStyle", 
     "Which" -> "BlockStyle", "Do" -> "BlockStyle", "Table" -> "BlockStyle", 
     "For" -> "BlockStyle", "While" -> "BlockStyle", 
     "Replace" -> "BlockStyle", "ReplaceAll" -> "BlockStyle", 
     "ReplaceRepeated" -> "BlockStyle", "ReplacePart" -> "BlockStyle", 
     "Map" -> "BlockStyle", "MapIndexed" -> "BlockStyle", 
     "MapThread" -> "BlockStyle", "Entity" -> "TypeStyle", 
     "Quantity" -> "TypeStyle", "ChannelObject" -> "TypeStyle", 
     "CloudObject" -> "TypeStyle", "CloudExpression" -> "TypeStyle", 
     "CellObject" -> "TypeStyle", "Notebook" -> "TypeStyle", 
     "Cell" -> "TypeStyle", "ResourceObject" -> "TypeStyle", 
     "LocalObject" -> "TypeStyle", "FormObject" -> "TypeStyle", 
     "$Failed" -> "ConstantStyle", "False" -> "ConstantStyle", 
     "True" -> "ConstantStyle", "$Canceled" -> "ConstantStyle", 
     "All" -> "ConstantStyle"
     }
   }
  ]
 ]

This is based off of something I do for my own stuff, but there I put all of this in a SyntaxHighlighting.nb stylesheet and then link off of that. That way if you just want to make a minor adjustment to one style, you can only change that one piece. Here's a sample for you to work from. Here's an example of a stylesheet deriving from it.

I'd recommend that flow to you, moving forward, but my guess is you aren't familiar with working with Mathematica stylesheets, so the function above will get you started for for some on-the-fly testing.

$\endgroup$
3
  • $\begingroup$ This already goes quite along way to what I am interested in having, so thanks for taking the time for putting together examples. $\endgroup$
    – user27119
    Nov 18, 2019 at 18:20
  • $\begingroup$ Can you tell me what you’d like to have specifically? I don’t know what’s left from your screenshot except the vertical bars and v12 at least seems to be buggy and not having them display $\endgroup$
    – b3m2a1
    Nov 18, 2019 at 18:33
  • $\begingroup$ I'll edit my question to include a wishlist. $\endgroup$
    – user27119
    Nov 18, 2019 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.