From 4eac3196449db433cfe921eef30818d3cf114b62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 14 Aug 2020 17:00:19 +0200 Subject: vis-lua: make file.modified assignable We fake a modification by doing an insertion followed by a deletion which does not actually change the buffer content. Close #855 --- vis-lua.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/vis-lua.c b/vis-lua.c index 53ffe59..8a2740a 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -2100,6 +2100,27 @@ static int file_index(lua_State *L) { return index_common(L); } +static int file_newindex(lua_State *L) { + File *file = obj_ref_check(L, 1, VIS_LUA_TYPE_FILE); + + if (lua_isstring(L, 2)) { + const char *key = lua_tostring(L, 2); + + if (strcmp(key, "modified") == 0) { + bool modified = lua_isboolean(L, 3) && lua_toboolean(L, 3); + if (modified) { + text_insert(file->text, 0, " ", 1); + text_delete(file->text, 0, 1); + } else { + text_save(file->text, NULL); + } + return 0; + } + } + + return newindex_common(L); +} + /*** * Insert data at position. * @function insert @@ -2281,7 +2302,7 @@ static int file_text_object(lua_State *L) { static const struct luaL_Reg file_funcs[] = { { "__index", file_index }, - { "__newindex", newindex_common }, + { "__newindex", file_newindex }, { "insert", file_insert }, { "delete", file_delete }, { "lines_iterator", file_lines_iterator }, -- cgit v1.2.3