VimArea - Day 2 - aixu
In the last article I've implemented the basic navigation commands hjkl. This time I'll continue to implement the manipulation commands aixu.

Observing the behaviours

The way undo command works seemed to be a series of recorded sessions. That is when you get into the -- INSERT -- mode and type some words. The time you press Esc a state is the created for this session.

However, when you pressed arrow keys in a session, this session will be split into two. This could be also treated as the following consecutive commands:
Esc -> h/j/k/l -> i

Recording Sessions

One way of doing this is to store the entire content for each session but this consumes much memories if it is editing a big file, although the implementation was not meant for serious coding. I could assume that all sessions are small content editing. However, the calculation needed for each key stroke is already heavy. I need to minimize the delay as much as possible.

I am not a smart person. But I manage to solve this after a day:

So the minimal variables needed to undo / redo a session is to record the starting position after an insert session, the length of content inserted and the content removed.

By running the "Undo / Redo" function repeatedly I could switch between the state before and after the insert session. But this is not finished yet. For the u & Ctrl + r command to work I need an array to store these states.
1. Array of the Undo Stack
    [ Undo, Undo, Undo, Undo ] CurrentState

2. Pressing u
    [ Undo, Undo, Undo, CurrentState, Redo ]

3. Press u again
    [ Undo, Undo, CurrentState, Redo, Redo ]

4. Press Ctrl + r
    [ Undo, Undo, Undo, CurrentState, Redo ]

5. Press a, remove the states after current state
    [ Undo, Undo, Undo ] CurrentState

Cursor position

The cursor position before and after Undo / Redo - ing seems to be the absolute position just before the session start. So I'll need another variable just to store that.

Phantom Space

A phantom space is a space character that append at the very end of a line when you press a.


See how the cursor moved to a non-existent character? That I called a phantom space. It exists only on certain mode.


Here is a special case, the phantom space is not inserted because there is no room for that. Once you typed in some character it is appended again!

The next step is to implement the -- VISUAL -- command.

( Recently I kind of get distracted constantly by watching youtube video about wood working and lock picking. Don't know why but I found those fascinating. )

Articles in the series


Useful Links

Project home | Demo
Tag(s): vim BotanJS
Profile picture
斟酌 鵬兄
Wed Mar 16 2016 17:43:52 GMT+0000 (Coordinated Universal Time)
Last modified: Sun Apr 10 2022 07:49:05 GMT+0000 (Coordinated Universal Time)
Comments
No comments here.
Do you even comment?
website: 
Not a valid website
Invalid email format
Please enter your email
*Name: 
Please enter a name
Submit
抱歉,Google Recaptcha 服務被牆掉了,所以不能回覆了