5

I'm trying to create a notebook where a user can enter a Mathematica expression, and its syntax is analyzed (number of tokens, compressed size, etc.).

Trying this answer,

CellPrint@TextCell[
  Row[{"This is an inline cell with automatic syntax highlighting: ", 
    ExpressionCell[Dynamic@input, "Notebook", "Input", 
     CellFrame -> True]}], "Text"]
Dynamic@input

the Dynamic doesn't update input. If I make an InputField inside the ExpressionCell the syntax highlighting doesn't apply, even if I change the BaseStyle. How can I get both dynamic behavior and syntax highlighting?

Kuba
  • 136,707
  • 13
  • 279
  • 740
lirtosiast
  • 684
  • 6
  • 14

1 Answers1

7

Here is what you can do:

x = ToBoxes@Defer@Plot[x, {x, 0, 1}];

InputField[Dynamic[x], Boxes, 
 BaseStyle -> {"Notebook", "Input", ShowCodeAssist -> True, ShowSyntaxStyles -> True}
]

Dynamic@x
Dynamic@MakeExpression[StripBoxes[x], StandardForm]

enter image description here

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • If I add whitespace inside that field, MakeExpression fails and generates ErrorBoxes; this is frustrating because the user cannot paste in input or add whitespace. Is there a way around that? – lirtosiast Jun 08 '19 at 05:54
  • @lirtosiast I added StripBoxes to be less strict about the content. – Kuba Jun 08 '19 at 06:24