Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to design undo & redo in text editor?

Part of my project is to write a text editor that is used for typing some rules, compiling my application and running it. Writing compiler was end and release beta version. In the final version we must add undo and redo to the text editor. I use a file and save it periodically for the text editor. How to design undo and redo to my text editor? What is changed in the structure of persistent of file?

like image 977
Sajad Bahmani Avatar asked Aug 27 '10 12:08

Sajad Bahmani


People also ask

How do you implement undo?

To implement Undo , you work backward from the tail of the linked-list, using a 'current-node' pointer or index: where the change was insert , you do a delete but without updating the linked-list; and where it was a delete you insert the data from the data in the linked-list buffer.

How do you implement undo/redo stack?

If “UNDO” string is encountered, pop the top element from Undo stack and push it to Redo stack. If “REDO” string is encountered, pop the top element of Redo stack and push it into the Undo stack.

What is the Command for undo?

To undo an action press Ctrl+Z. If you prefer your mouse, click Undo on the Quick Access Toolbar. You can press Undo (or CTRL+Z) repeatedly if you want to undo multiple steps.


1 Answers

You can model your actions as commands, that you keep in two stacks. One for undo, another for redo. You can compose your commands to create more high-level commands, like when you want to undo the actions of a macro, for example; or if you want to group individual keystrokes of a single word, or phrase, in one action.

Each action in your editor (or a redo action) generates a new undo command that goes into the undo stack (and also clears the redo stack). Each undo action generates the corresponding redo command that goes into the redo stack.

You can also, as mentioned in the comments by derekerdmann, combine both undo and redo commands into one type of command, that knows how to undo and redo its action.

like image 68
Jordão Avatar answered Oct 03 '22 11:10

Jordão