What is in your Mathematica tool bag? [closed]

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];

Leave a Comment