aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2017-03-01 18:04:15 +0100
committerMarc André Tanner <mat@brain-dump.org>2017-03-01 18:22:13 +0100
commit17786fb0c6982888534885792b390d5ff9f9a342 (patch)
tree5c573cab1e1f66fa218a1535a7b565a41db612ae
parent32ed959c6e2e7729ec4fba11a0274680703e4974 (diff)
downloadvis-17786fb0c6982888534885792b390d5ff9f9a342.tar.gz
vis-17786fb0c6982888534885792b390d5ff9f9a342.tar.xz
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.
-rw-r--r--lua/vis-std.lua4
-rw-r--r--vis-lua.c19
2 files changed, 15 insertions, 8 deletions
diff --git a/lua/vis-std.lua b/lua/vis-std.lua
index 5da6020..6499f24 100644
--- 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
index b31d584..8a4f54c 100644
--- 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;
}