8
$\begingroup$

I'm interested to implement a custom input style which would appear (mostly?) like a normal "Input" Cell, but which would perform custom evaluation on shift-return or when evaluating a cell as a part of a group of cells. I'm interested of the custom evaluator, but also on easy way of creating such cells (keyboard shortcut?) or converting ordinary input cells to this custom variant.

How to approach this problem? My first assumption is that setting $Pre and applying custom evaluation on basis of either Cell style or CellTags values would be the way to go. I'm a little uncertain if this is the most sensible approach since there are also CellEvaluationFunction and CellProlog. What are the caveats of these approaches?

$\endgroup$
1
  • 2
    $\begingroup$ CellEvaluationFunction seems by far the best approach. $\endgroup$ May 21, 2019 at 10:31

1 Answer 1

11
$\begingroup$

You can use StyleKeyMapping to easily switch back and forth from "Input" cells to your "CustomInput" cell. For example:

Cell[StyleData["Input"],
    StyleKeyMapping->{
        "=" -> "WolframAlphaShort",
        "*" -> "Item",
        ">" -> "ExternalLanguage",
        "Tab" -> "CustomInput"
    }
]

Cell[StyleData["CustomInput"],
    StyleKeyMapping->{"Tab" -> "Input"}
]

For a custom evaluation function, you can use CellProlog or CellEvaluationFunction. For example:

CellEvaluationFunction -> Function @ Replace[#,
    {
    BoxData[foo_List] :> ToExpression[
        BoxData[Map[RowBox[{"goo", "[", #, "]"}]& , foo]],
        #2
    ],
    BoxData[foo_] :> ToExpression[BoxData[RowBox[{"goo", "[", foo, "]"}]], #2]
    }
]

would add the wrapper goo around every input before evaluation. The one downside with this approach is that only the last line of input will get an output, the others are automatically suppressed. If that is a problem, then I could give you a CellProlog approach instead.

$\endgroup$

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.