Sub topic 5
Redoing an action
This chapter covers redo(), the operation that restores an action you just undid. It assumes you know the stack data structure with push and pop, and that you can read a small Python class. It also assumes you have seen the two-stack model from the earlier chapters and the undo() method.
What redo does
Redo is the mirror image of undo. Undo takes the newest command from the undo stack, reverts it, and pushes it onto the redo stack. Redo takes the newest command from the redo stack, applies it again, and pushes it back onto the undo stack.
A command is never copied or rebuilt. The same object moves between the two stacks, so each stack always holds exactly the commands that fit its role.
The redo method
Here is the full History class. Look at redo() and compare it with undo() above it.
class History:
def __init__(self):
self._undo = []
self._redo = []
def do(self, command):
command.apply()
self._undo.append(command)
self._redo.clear()
def undo(self):
if not self._undo:
return False
command = self._undo.pop()
command.revert()
self._redo.append(command)
return True
def redo(self):
if not self._redo:
return False
command = self._redo.pop()
command.apply()
self._undo.append(command)
return Trueredo() runs in four steps:
- If the redo stack is empty, return
False. There is nothing to redo. - Pop the newest command from the redo stack.
- Call
apply()on it, which repeats the original change. - Push the command onto the undo stack and return
True.
Step 4 matters. After a redo, the user can undo the same action again. Without that push, a redone action would be stuck in the document and could never be undone.
Why order is preserved
Suppose you perform actions A, B and C, then undo twice. Undo reverts C first, then B. The redo stack now holds [C, B], with B on top. A redo pops B, so the actions come back in their original order: B, then C.
This works because a stack reverses order every time you move items through it. Moving items from the undo stack to the redo stack reverses them once. Moving them back reverses them a second time, which restores the original order.
| Step | Undo stack | Redo stack |
|---|---|---|
| do A, B, C | A, B, C | (empty) |
| undo | A, B | C |
| undo | A | C, B |
| redo | A, B | C |
| redo | A, B, C | (empty) |
In each row the rightmost item is the top of the stack.
A redo example
The Insert command below stores what it needs to apply and revert itself. apply() and revert() are exact opposites, which is what makes redo safe.
class Document:
def __init__(self):
self.text = ""
class Insert:
def __init__(self, doc, position, string):
self.doc = doc
self.position = position
self.string = string
def apply(self):
t = self.doc.text
self.doc.text = t[:self.position] + self.string + t[self.position:]
def revert(self):
t = self.doc.text
end = self.position + len(self.string)
self.doc.text = t[:self.position] + t[end:]
doc = Document()
history = History()
history.do(Insert(doc, 0, "Hello"))
history.do(Insert(doc, 5, " world"))
print(doc.text) # Hello world
history.undo()
print(doc.text) # Hello
history.redo()
print(doc.text) # Hello world
print(history.redo()) # FalseThe last line shows the empty-stack case. After the redo, the redo stack is empty, so a second redo() changes nothing and returns False.
Common errors
- Forgetting to push onto the undo stack. The redo appears to work, but the action can no longer be undone.
- Calling
revert()instead ofapply(). This undoes the action a second time and corrupts the document. - Raising an exception on an empty stack. Calling
pop()on an empty list raisesIndexError. Check first and returnFalse, so a user pressing redo too often does not crash the program. - Storing positions that depend on later state. A command must hold everything it needs when it is applied. If
apply()reads a value that has changed since the command was created, redo will not reproduce the original result.
Redo is only valid until the next new action
The redo stack is not a permanent record. Once the user does something new, do() calls self._redo.clear(). The next chapter explains why that is necessary. For now, note that a redo is only possible directly after one or more undos, with no new action in between.
Exercise
Starting with an empty history, you call do(A), do(B), do(C), then undo(), undo(), redo(). What does each stack contain at the end, and what does the next redo() return?
Show answer
The undo stack holds A and B, with B on top. The redo stack holds C. The next redo() pops C, applies it, and returns True. After that the redo stack is empty, so one more redo() would return False.
Exercise
A programmer writes redo() so that it pops from the redo stack and calls apply(), but forgets to append the command to the undo stack. What user-visible bug appears?
Show answer
The redone action takes effect, but it cannot be undone. Pressing undo skips it and reverts an earlier action instead, because the redone command is not on the undo stack.