UPDATE
The latest version of the shortInputForm function can be found here .
Original post
Here is another best solution (Mathematica 5 compatible):
myInputForm[expr_] := Block[{oldContexts, output, interpretation, skeleton}, output = ToString[expr, InputForm]; oldContexts = {$Context, $ContextPath}; $Context = "myTemp`"; $ContextPath = {$Context}; output = DisplayForm@ToBoxes [ToExpression[output] /. {myTemp`interpretation -> If[$VersionNumber >= 6, System`Interpretation, System` First@ {#} &], myTemp`Row -> System`Row, myTemp`skeleton -> System`Skeleton, myTemp`sequence :> (System`Sequence @@ # &)}, StandardForm]; {$Context, $ContextPath} = oldContexts; output] shortInputForm[expr_] := myInputForm[expr /. {{} -> Sequence[], lst : {x_ /; VectorQ[x, NumberQ], y__} /; (MatrixQ[lst, NumberQ] && Length[lst] > 3) :> {x /. v : {a_, b__} /; Length[v] > 3 :> {a, interpretation[skeleton[Length[{b}]], sequence@ {b}]}, interpretation[skeleton[Length[{y}]], sequence@ {y}]}, lst : {x_, y__} /; VectorQ[lst, NumberQ] && Length[lst] > 3 :> {x, interpretation[skeleton[Length[{y}]], sequence@ {y}]}}]
How it works
This solution is based on a simple idea: we need to block the conversion of things such as Graphics , Point and others for a set of expressions so that they are displayed in the internal form (in the form of expressions suitable for input). Fortunately, if we do this, we get the result of StandardForm , which will only be formatted (two-dimensional) InputForm original expression. This is what you need!
But how to do that? First of all, this conversion is done by FormatValues defined for Symbol as Graphics , Point , etc. You can get a complete list of such Symbol by evaluating the following:
list = Symbol /@ Select[DeleteCases[Names["*"], "I" | "Infinity"], ToExpression[
My first idea was just the Block all these Symbol (and it works!):
myInputForm[expr_] := With[{list = list}, Block[list, RawBoxes@MakeBoxes @expr]]
But this method evaluates all of these Symbol , and also evaluates all FormatValues for all Symbol in $ContextPath . I think this should be avoided.
Another way to block these FormatValues is to simply remove the "System`" context from $ContextPath . But it only works if these Symbol are not yet allowed in the context of "System`" . Therefore, we need to first convert our expression to String , and then remove the "System`" context from $ContextPath and finally convert the string back to the original expression. Then all new Symbol will be associated with the current $Context (and Graphics , Point , etc.), since they are not in $ContextPath ). To prevent conflicts of contextual shadow copying and clogging the "Global`" context "Global`" I switch $Context to "myTemp`" , which can be easily cleaned if necessary.
This is how myInputForm works.
Now about shortInputForm . The idea is not only to display the abbreviated version of myInputForm , but also to retain the ability to select and copy parts of the abbreviated code to a new input cell and use this code, since it will be a complete code without abbreviations. In version 6 and above, it can be achieved with Interpretation . For compatibility with pre-6 versions of Mathematica I added a code snippet that removes this ability if $VersionNumber less than 6.
The only problem I encountered while working with Interpretation is that it does not have the SequenceHold attribute, so we cannot just specify Sequence as the second argument to Interpretation . But this problem can be easily avoided by wrapping the sequence in a List , and then Apply ing Sequence to it:
System`Sequence @@
Please note that I need to specify the exact context for all the built-in Symbol that I use, because at the time they are called, the "System`" context is not in $ContextPath .
This completes the custom decisions I made when developing these features. Suggestions and comments are welcome!
