14

For example, using newlines after // results in some really weird indentation:

enter image description here

Ideally, I'd love to be able to do something like this:

x //
f1 //
f2 //
f3 //
f4 //
f5

or if possible, this would be even better:

x
// f1
// f2
// f3
// f4
// f5
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
rhennigan
  • 1,783
  • 10
  • 19
  • 7
    The last one is not possible because x on a line by itself is already a complete expression. x// is not a complete expression, so Mathematica will keep reading more from the next time. Similarly, x, newline, +y is two different expressions and not equivalent to x+, newline, y, i.e. x+y. If a line ends with a backslash, Mathematica will keep reading on the next line, but make sure no spaces follow the backslash. This is just a comment, I don't know the answer to your main question. You can use Code cells which don't indent at all but I don't think that's what you want. – Szabolcs Apr 17 '15 at 17:05
  • I cannot recall seeing anything that would let us modify this indentation behavior but I like the question. – Mr.Wizard May 06 '15 at 06:21
  • 2
    x // RightComposition[ f1, f2, f3, f4, f5] can have a relatively good appearance. x // f1 /* f2 /* f3 /* f4 /* f5 unfortunately doesn't. – Karsten7 Jul 05 '15 at 10:39
  • 1
    This would make a lot of sense considering all the operator forms introduced in v10. The current indentation behaviour for // is quite useless. Have you suggested this to Wolfram? – Szabolcs Jul 06 '15 at 12:34

2 Answers2

8

You can use InputAutoReplacements with a TemplateBox to achieve the desired behavior:

CurrentValue[EvaluationNotebook[], {InputAutoReplacements,"//"}] = TemplateBox[
    {},
    "Postfix",
    DisplayFunction->("//"&),
    InterpretationFunction->("//"&),
    SyntaxForm->"\[VerticalSeparator]"
];

The key is to change the SyntaxForm to a symbol that doesn't indent in the undesired fashion, which is most operators. However, it is necessary to use a symbol whose parsing precedence is lower than the default parsing precedence of "//".

Here's a portion of the precedence table from the documentation:

enter image description here

The above explains why I chose \[VerticalSeparator]for the SyntaxForm setting. Now, using // produces the following (as an image so you can see it in action):

enter image description here

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
2

A rough approximation using a string

toExpr[str_String] := 
  str //
    StringReplace[#, "\n" :> ""] & //
   ToExpression;

"
 x
 //f1
 //f2
 //f3
 //f4
 //f5
 " //
 toExpr

f5[f4[f3[f2[f1[x]]]]]

Although, this is just a Composition

Composition[f5, f4, f3, f2, f1]@x

f5[f4[f3[f2[f1[x]]]]]

f5@*f4@*f3@*f2@*f1@x

f5[f4[f3[f2[f1[x]]]]]

% == %% == %%%

True

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198