vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit b3574923732464feae77530967513f0b4fa6c6c6
parent 785893070ccb550eac18ab6acb9917de2bf83ea8
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Wed,  1 Mar 2017 18:04:15 +0100

vis-lua: make cursor.pos return nil if cursor position is invalid

It remains to be seen whether that is a good idea, but at least
it will reveal possible bugs.

Diffstat:
Mlua/vis-std.lua | 4+++-
Mvis-lua.c | 19++++++++++++-------
2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/lua/vis-std.lua b/lua/vis-std.lua @@ -108,7 +108,9 @@ vis.events.subscribe(vis.events.WIN_STATUS, function(win) end local size = file.size - table.insert(right_parts, (size == 0 and "0" or math.ceil(cursor.pos/size*100)).."%") + local pos = cursor.pos + if not pos then pos = 0 end + table.insert(right_parts, (size == 0 and "0" or math.ceil(pos/size*100)).."%") if not win.large then local col = cursor.col diff --git a/vis-lua.c b/vis-lua.c @@ -486,6 +486,13 @@ static size_t checkpos(lua_State *L, int narg) { return luaL_argerror(L, narg, "expected position, got number"); } +static void pushpos(lua_State *L, size_t pos) { + if (pos == EPOS) + lua_pushnil(L); + else + lua_pushunsigned(L, pos); +} + static void pushrange(lua_State *L, Filerange *r) { if (!text_range_valid(r)) { lua_pushnil(L); @@ -1560,6 +1567,7 @@ static const struct luaL_Reg window_cursors_funcs[] = { /*** * The zero based byte position in the file. * + * Might be `nil` if the cursor is in an invalid state. * Setting this field will move the cursor to the given position. * @tfield int pos */ @@ -1592,7 +1600,7 @@ static int window_cursor_index(lua_State *L) { if (lua_isstring(L, 2)) { const char *key = lua_tostring(L, 2); if (strcmp(key, "pos") == 0) { - lua_pushunsigned(L, view_cursors_pos(cur)); + pushpos(L, view_cursors_pos(cur)); return 1; } @@ -2010,6 +2018,7 @@ static const struct luaL_Reg file_lines_funcs[] = { }; static int file_marks_index(lua_State *L) { + size_t pos = EPOS; Vis *vis = lua_touserdata(L, lua_upvalueindex(1)); File *file = obj_ref_check_containerof(L, 1, VIS_LUA_TYPE_MARKS, offsetof(File, marks)); if (!file) @@ -2020,13 +2029,9 @@ static int file_marks_index(lua_State *L) { enum VisMark mark = vis_mark_from(vis, symbol[0]); if (mark == VIS_MARK_INVALID) goto err; - size_t pos = text_mark_get(file->text, file->marks[mark]); - if (pos == EPOS) - goto err; - lua_pushunsigned(L, pos); - return 1; + pos = text_mark_get(file->text, file->marks[mark]); err: - lua_pushnil(L); + pushpos(L, pos); return 1; }