I'll assume that you want to echo any literal Set
operation that occurs in input, even if it is not on a line by itself.
$Pre
This may work for you:
$Pre =
Function[
main,
Unevaluated[main] /. Set -> Function[, Print@HoldForm[# = #2]; # = #2, HoldFirst],
HoldAll
];
Now:
{a = 2 + 2, b = 10/2, c = Sqrt[9]};
a = 4
b = 5
c = 3
{a, b, c}
{4, 5, 3}
echo
function
Alternatively, since I cannot imagine the echo being practical for every Set
you might do it like this:
$Pre =. (* clear the prior definition for $Pre *)
echo =
Function[
main,
Unevaluated[main] /. Set -> Function[, Print@HoldForm[# = #2]; # = #2, HoldFirst],
HoldAll
];
Then:
{a = 2 + 2, b = 10/2, c = Sqrt[9]} // echo
CellEvaluationFunction
If you want to echo some but not all Set
s, and you do not want to have // echo
appear, you could perhaps use CellEvaluationFunction
for individual Cell
s.
As an example, this code generates a new input Cell into which any Set
that is typed will be echoed:
CellPrint[ExpressionCell[Placeholder[], "Input", CellEvaluationFunction ->
(ReleaseHold[
MakeExpression@# /.
Set -> Function[, Print@HoldForm[# = #2]; # = #2, HoldFirst]
] &)
]]
The cell that is created can be copied and pasted to make more, or you can use the Option Inspector to set the value of CellEvaluationFunction
for existing cells.
$Post
and$Pre
in the Mathematica documentation. $\endgroup$$Post
and$PrePrint
(nuby here!). $\endgroup$