diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2017-03-01 18:00:42 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2017-03-01 18:22:13 +0100 |
| commit | 32ed959c6e2e7729ec4fba11a0274680703e4974 (patch) | |
| tree | 0968b14c4ba71d6e5cdfc3740d4f6e8a7ab590c4 /vis-lua.c | |
| parent | 81deff2851c90245d8ed997b68f0e3c4e8ecc123 (diff) | |
| download | vis-32ed959c6e2e7729ec4fba11a0274680703e4974.tar.gz vis-32ed959c6e2e7729ec4fba11a0274680703e4974.tar.xz | |
vis-lua: document cursor behavior
Diffstat (limited to 'vis-lua.c')
| -rw-r--r-- | vis-lua.c | 50 |
1 files changed, 50 insertions, 0 deletions
@@ -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 */ /*** |
