calc: Undo Tutorial
3.1.3 Undo and Redo
-------------------
If you make a mistake, you can usually correct it by pressing shift-‘U’,
the “undo” command. First, clear the stack (‘M-0 <DEL>’) and exit and
restart Calc (‘C-x * * C-x * *’) to make sure things start off with a
clean slate. Now:
1: 2 2: 2 1: 8 2: 2 1: 6
. 1: 3 . 1: 3 .
. .
2 <RET> 3 ^ U *
You can undo any number of times. Calc keeps a complete record of
all you have done since you last opened the Calc window. After the
above example, you could type:
1: 6 2: 2 1: 2 . .
. 1: 3 .
.
(error)
U U U U
You can also type ‘D’ to “redo” a command that you have undone
mistakenly.
. 1: 2 2: 2 1: 6 1: 6
. 1: 3 . .
.
(error)
D D D D
It was not possible to redo past the ‘6’, since that was placed there by
something other than an undo command.
You can think of undo and redo as a sort of “time machine.” Press ‘U’
to go backward in time, ‘D’ to go forward. If you go backward and do
something (like ‘*’) then, as any science fiction reader knows, you have
changed your future and you cannot go forward again. Thus, the
inability to redo past the ‘6’ even though there was an earlier undo
command.
You can always recall an earlier result using the Trail. We’ve
ignored the trail so far, but it has been faithfully recording
everything we did since we loaded the Calculator. If the Trail is not
displayed, press ‘t d’ now to turn it on.
Let’s try grabbing an earlier result. The ‘8’ we computed was undone
by a ‘U’ command, and was lost even to Redo when we pressed ‘*’, but
it’s still there in the trail. There should be a little ‘>’ arrow (the
“trail pointer”) resting on the last trail entry. If there isn’t, press
‘t ]’ to reset the trail pointer. Now, press ‘t p’ to move the arrow
onto the line containing ‘8’, and press ‘t y’ to “yank” that number back
onto the stack.
If you press ‘t ]’ again, you will see that even our Yank command
went into the trail.
Let’s go further back in time. Earlier in the tutorial we computed a
huge integer using the formula ‘2^3^4’. We don’t remember what it was,
but the first digits were “241”. Press ‘t r’ (which stands for
trail-search-reverse), then type ‘241’. The trail cursor will jump back
to the next previous occurrence of the string “241” in the trail. This
is just a regular Emacs incremental search; you can now press ‘C-s’ or
‘C-r’ to continue the search forwards or backwards as you like.
To finish the search, press <RET>. This halts the incremental search
and leaves the trail pointer at the thing we found. Now we can type ‘t
y’ to yank that number onto the stack. If we hadn’t remembered the
“241”, we could simply have searched for ‘2^3^4’, then pressed ‘<RET> t
n’ to halt and then move to the next item.
You may have noticed that all the trail-related commands begin with
the letter ‘t’. (The store-and-recall commands, on the other hand, all
began with ‘s’.) Calc has so many commands that there aren’t enough
keys for all of them, so various commands are grouped into two-letter
sequences where the first letter is called the “prefix” key. If you
type a prefix key by accident, you can press ‘C-g’ to cancel it. (In
fact, you can press ‘C-g’ to cancel almost anything in Emacs.) To get
help on a prefix key, press that key followed by ‘?’. Some prefixes
have several lines of help, so you need to press ‘?’ repeatedly to see
them all. You can also type ‘h h’ to see all the help at once.
Try pressing ‘t ?’ now. You will see a line of the form,
trail/time: Display; Fwd, Back; Next, Prev, Here, [, ]; Yank: [MORE] t-
The word “trail” indicates that the ‘t’ prefix key contains
trail-related commands. Each entry on the line shows one command, with
a single capital letter showing which letter you press to get that
command. We have used ‘t n’, ‘t p’, ‘t ]’, and ‘t y’ so far. The
‘[MORE]’ means you can press ‘?’ again to see more ‘t’-prefix commands.
Notice that the commands are roughly divided (by semicolons) into
related groups.
When you are in the help display for a prefix key, the prefix is
still active. If you press another key, like ‘y’ for example, it will
be interpreted as a ‘t y’ command. If all you wanted was to look at the
help messages, press ‘C-g’ afterwards to cancel the prefix.
One more way to correct an error is by editing the stack entries.
The actual Stack buffer is marked read-only and must not be edited
directly, but you can press ‘`’ (grave accent) to edit a stack entry.
Try entering ‘3.141439’ now. If this is supposed to represent ‘pi’,
it’s got several errors. Press ‘`’ to edit this number. Now use the
normal Emacs cursor motion and editing keys to change the second 4 to a
5, and to transpose the 3 and the 9. When you press <RET>, the number
on the stack will be replaced by your new number. This works for
formulas, vectors, and all other types of values you can put on the
stack. The ‘`’ key also works during entry of a number or algebraic
formula.