A line editor with undo
The problem
Build a small text editor that keeps the lines of a document in a doubly linked list, and can undo every change.
Requirements
- The lines are kept in a doubly linked list, with a cursor on the current line
- Commands insert a line after the cursor, delete the current line, and move the cursor up or down
- Every change is recorded on a stack, and an undo command reverses the most recent one
- Undo with no history gives a clear message
- A print command shows the document with the current line marked
- All memory is freed when the program ends
Technologies
- C
- Doubly linked lists
- Stacks
- Strings
- malloc and free
Step by step
- Define the line node, and write functions that insert after the cursor and delete the current line.
- Write the print command, and test inserting and deleting at the first, a middle and the last line.
- Define an undo record that says what was done and holds what is needed to reverse it.
- Push a record on every insert and every delete.
- Write undo: pop a record and apply the opposite change, putting the cursor back where it was.
- Free the list and the undo stack at the end.
- Test a long run of changes followed by the same number of undos, which must give back the empty document.
Expected result
A working line editor in which any sequence of edits can be undone step by step, with no memory errors.
How it is evaluated
- Insert and delete are correct at every position
- Undo restores both the text and the cursor
- No node is used after it is freed, and nothing is leaked
- Every command except print runs in O(1)
Ways to improve it
- Add redo with a second stack
- Save the document to a file and load it again
- Add a command that finds a word