vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 785893070ccb550eac18ab6acb9917de2bf83ea8 parent 237b4b21d93f7a06fb638fe3179a22a41296a189 Author: Marc André Tanner <mat@brain-dump.org> Date: Wed, 1 Mar 2017 18:00:42 +0100 vis-lua: document cursor behavior Diffstat:
| M | vis-lua.c | | | 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 50 insertions(+), 0 deletions(-)
diff --git a/vis-lua.c b/vis-lua.c @@ -1504,7 +1504,57 @@ static const struct luaL_Reg window_cursors_funcs[] = { /*** * A cursor object. + * + * Cursors are represented as absolute byte offsets from the start of the file. + * Valid cursor placements are within the closed interval `[0, file.size]`. + * Cursors are currently implemented using character marks into the underlying + * persistent [text management data structure](http://google.ch/). + * This has a few consequences you should be aware of: + * + * - A cursor becomes invalid when the underlying text range it is referencing + * is deleted: + * + * -- leaves cursor in an invalid state + * win.file:delete(win.cursor.pos, 1) + * assert(win.cursor.pos == nil) + * + * Like a regular mark it will become valid again when the text is reverted + * to the state before the deletion. + * + * - Inserts after the cursor position (`> cursor.pos`) will not affect the + * cursor postion. + * + * local pos = win.cursor.pos + * win.file:insert(pos+1, "-") + * assert(win.cursor.pos == pos) + * + * - Non-cached inserts before the cursor position (`<= cursor.pos`) will + * affect the mark and adjust the cursor postion by the number of bytes + * which were inserted. + * + * local pos = win.cursor.pos + * win.file:insert(pos, "-") + * assert(win.cursor.pos == pos+1) + * + * - Cached inserts before the cursor position (`<= cursor.pos`) will + * not affect the cursor position because the underlying text is replaced + * inplace. + * + * For these reasons it is generally recommended to update the cursor position + * after a modification. The general procedure amounts to: + * + * 1. Read out the current cursor position + * 2. Perform text modifications + * 3. Update the cursor postion + * + * This is what @{Vis:insert} and @{Vis:replace} do internally. + * * @type Cursor + * @usage + * local data = "new text" + * local pos = win.cursor.pos + * win.file:insert(pos, data) + * win.cursor.pos = pos + #data */ /***