From e76ec27418c4bf7599ce9ecc93ea1227fbc2db57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Thu, 21 Apr 2016 22:20:39 +0200 Subject: vis-lua: also accept a range as argument for file:delete and file:content --- README.md | 18 +++++++++++------- vis-lua.c | 30 +++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 1abd8b2..de79e8c 100644 --- a/README.md +++ b/README.md @@ -597,9 +597,9 @@ At this time there exists no API stability guarantees. - `command_register(name, function(argv, force, win, cursor, range))` hook up a Lua function to `:name` command - `map(mode, key, function)` map a Lua function to `key` in `mode` - `file` - - `content(pos, len)` + - `content(pos, len)` or `content({start, finish})` - `insert(pos, data)` - - `delete(pos, len)` + - `delete(pos, len)` or `delete({start, finish})` - `lines_iterator()` - `name` - `lines[0..#lines+1]` array giving read/write access to lines @@ -612,11 +612,15 @@ At this time there exists no API stability guarantees. - `cursor` primary cursor - `syntax` lexer name used for syntax highlighting or `nil` - `cursor` - - `line` (1 based), `col` (1 based) - - `to(line, col)` - - `pos` bytes from start of file (0 based) - - `number` zero based index of cursor - - `selection` either `nil` or a table `{start, finish}` + - `line` (1 based), `col` (1 based) + - `to(line, col)` + - `pos` bytes from start of file (0 based) + - `number` one based index of cursor + - `selection` a `range` + - `range` denoted by absolute postions in bytes from the start of the file, + an invalid range is represented as `nil` + - `start` + - `finish` Most of the exposed objects are managed by the C core. Allthough there is a simple object life time management mechanism in place, it is still diff --git a/vis-lua.c b/vis-lua.c index 03392dd..d2a6bf0 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -246,6 +246,22 @@ static void pushrange(lua_State *L, Filerange *r) { lua_settable(L, -3); } +static Filerange getrange(lua_State *L, int index) { + Filerange range = text_range_empty(); + if (lua_istable(L, index)) { + lua_getfield(L, index, "start"); + range.start = luaL_checkunsigned(L, -1); + lua_pop(L, 1); + lua_getfield(L, index, "finish"); + range.end = luaL_checkunsigned(L, -1); + lua_pop(L, 1); + } else { + range.start = luaL_checkunsigned(L, index); + range.end = range.start + luaL_checkunsigned(L, index+1); + } + return range; +} + static const char *keymapping(Vis *vis, const char *keys, const Arg *arg) { lua_State *L = vis->lua; if (!func_ref_get(L, arg->v)) @@ -815,10 +831,8 @@ static int file_insert(lua_State *L) { static int file_delete(lua_State *L) { File *file = obj_ref_check(L, 1, "vis.file"); if (file) { - size_t pos = luaL_checkunsigned(L, 2); - size_t len = luaL_checkunsigned(L, 3); - bool ret = text_delete(file->text, pos, len); - lua_pushboolean(L, ret); + Filerange range = getrange(L, 2); + lua_pushboolean(L, text_delete_range(file->text, &range)); } else { lua_pushboolean(L, false); } @@ -859,12 +873,14 @@ static int file_content(lua_State *L) { File *file = obj_ref_check(L, 1, "vis.file"); if (!file) goto err; - size_t pos = luaL_checkunsigned(L, 2); - size_t len = luaL_checkunsigned(L, 3); + Filerange range = getrange(L, 2); + if (!text_range_valid(&range)) + goto err; + size_t len = text_range_size(&range); char *data = malloc(len); if (!data) goto err; - len = text_bytes_get(file->text, pos, len, data); + len = text_bytes_get(file->text, range.start, len, data); lua_pushlstring(L, data, len); free(data); return 1; -- cgit v1.2.3