vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 3d3f41b42672d6d0d5ef4130ff688aae7aa937f1 parent a6eb2c7c4301aa47af5757b5385f0b82cf371767 Author: Marc André Tanner <mat@brain-dump.org> Date: Wed, 17 Feb 2016 22:17:26 +0100 vis-lua: add file:content(pos, len) function Diffstat:
| M | vis-lua.c | | | 19 | +++++++++++++++++++ |
1 file changed, 19 insertions(+), 0 deletions(-)
diff --git a/vis-lua.c b/vis-lua.c @@ -706,12 +706,31 @@ static int file_lines_iterator_it(lua_State *L) { return 1; } +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); + char *data = malloc(len); + if (!data) + goto err; + len = text_bytes_get(file->text, pos, len, data); + lua_pushlstring(L, data, len); + free(data); + return 1; +err: + lua_pushnil(L); + return 1; +} + static const struct luaL_Reg file_funcs[] = { { "__index", file_index }, { "__newindex", file_newindex }, { "insert", file_insert }, { "delete", file_delete }, { "lines_iterator", file_lines_iterator }, + { "content", file_content }, { NULL, NULL }, };