What is in your Mathematica tool bag?
Todd Gayley (Wolfram Research) just send me a nice hack which allows to "wrap" built-in functions with arbitrary code. I feel that I have to share this useful instrument. The following is Todd's answer on my question
.
A bit of interesting (?) history: That style of hack for "wrapping" a built-in function was invented around 1994 by Robby Villegas and I, ironically for the function Message, in a package called ErrorHelp that I wrote for the Mathematica Journal back then. It has been used many times, by many people, since then. It's a bit of an insider's trick, but I think it's fair to say that it has become the canonical way of injecting your own code into the definition of a built-in function. It gets the job done nicely. You can, of course, put the $inMsg variable into any private context you wish.
Unprotect[Message];
Message[args___] := Block[{$inMsg = True, result},
"some code here";
result = Message[args];
"some code here";
result] /; ! TrueQ[$inMsg]
Protect[Message];
One of the nice things about the Mathematica notebook interface is that it can evaluate expressions in any language, not just Mathematica. As a simple example, consider creating a new Shell input cell type that passes the contained expression to the operating system shell for evaluation.
First, define a function that delegates evaluation of a textual command to the external shell:
shellEvaluate[cmd_, _] := Import["!"~~cmd, "Text"]
The second argument is needed and ignored for reasons that will become apparent later. Next, we want to create a new style called Shell:
- Open a new notebook.
- Select the menu item Format/Edit Stylesheet...
- In the dialog, beside Enter a style name: type
Shell
. - Select the cell bracket beside the new style.
- Select the menu item Cell/Show Expression
- Overwrite the cell expression with the Step 6 Text given below.
- Once again, select the menu item Cell/Show Expression
- Close the dialog.
Use the following cell expression as the Step 6 Text:
Cell[StyleData["Shell"],
CellFrame->{{0, 0}, {0.5, 0.5}},
CellMargins->{{66, 4}, {0, 8}},
Evaluatable->True,
StripStyleOnPaste->True,
CellEvaluationFunction->shellEvaluate,
CellFrameLabels->{{None, "Shell"}, {None, None}},
Hyphenation->False,
AutoQuoteCharacters->{},
PasteAutoQuoteCharacters->{},
LanguageCategory->"Formula",
ScriptLevel->1,
MenuSortingValue->1800,
FontFamily->"Courier"]
Most of this expression was copied directly form the built-in Program style. The key changes are these lines:
Evaluatable->True,
CellEvaluationFunction->shellEvaluate,
CellFrameLabels->{{None, "Shell"}, {None, None}},
Evaluatable
enables the SHIFT+ENTER functionality for the cell. Evaluation will call the CellEvaluationFunction
passing the cell content and content type as arguments (shellEvaluate
ignores the latter argument). CellFrameLabels
is just a nicety that let's the user identify that this cell is unusual.
With all of this in place, we can now enter and evaluate a shell expression:
- In the notebook created in the steps above, create an empty cell and select the cell bracket.
- Select the menu item Format/Style/Shell.
- Type a valid operating system shell command into the cell (e.g. 'ls' on Unix or 'dir' on Windows).
- Press SHIFT+ENTER.
It is best to keep this defined style in a centrally located stylesheet. Furthermore, evaluation functions like shellEvaluate
are best defined as stubs using DeclarePackage in init.m
. The details of both of these activities are beyond the scope of this response.
With this functionality, one can create notebooks that contain input expressions in any syntax of interest. The evaluation function can be written in pure Mathematica, or delegate any or all parts of the evaluation to an external agency. Be aware that there are other hooks that relate to cell evaluation, like CellEpilog
, CellProlog
and CellDynamicExpression
.
A common pattern involves writing the input expression text to a temporary file, compiling the file in some language, running the program and capturing the output for ultimate display in the output cell. There are plenty of details to address when implementing a full solution of this kind (like capturing error messages properly), but one must appreciate the fact that it is not only possible to do things like this, but practical.
On a personal note, it is features like this that makes the notebook interface the center of my programming universe.
Update
The following helper function is useful for creating such cells:
evaluatableCell[label_String, evaluationFunction_] :=
( CellPrint[
TextCell[
""
, "Program"
, Evaluatable -> True
, CellEvaluationFunction -> (evaluationFunction[#]&)
, CellFrameLabels -> {{None, label}, {None, None}}
, CellGroupingRules -> "InputGrouping"
]
]
; SelectionMove[EvaluationNotebook[], All, EvaluationCell]
; NotebookDelete[]
; SelectionMove[EvaluationNotebook[], Next, CellContents]
)
It is used thus:
shellCell[] := evaluatableCell["shell", Import["!"~~#, "Text"] &]
Now, if shellCell[]
is evaluated, the input cell will be deleted and replaced with a new input cell that evaluates its contents as a shell command.