diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2021-11-28 10:43:28 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2021-11-28 10:44:40 -0800 |
commit | d3d8c13828b78d4252b5a2d530b4b0dd38babcb7 (patch) | |
tree | 148bfb64337b76bf36b704ab8cd4003e4c371ac7 /src | |
parent | 6388bc08c46c096b3df91a5deddd2ac740cd6d38 (diff) | |
download | teliva-d3d8c13828b78d4252b5a2d530b4b0dd38babcb7.tar.gz |
make look_up_definition more composable
Diffstat (limited to 'src')
-rw-r--r-- | src/lua.c | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/src/lua.c b/src/lua.c index 6035a62..71fee90 100644 --- a/src/lua.c +++ b/src/lua.c @@ -311,7 +311,10 @@ int is_special_history_key(const char *key) { } -static const char *look_up_definition (lua_State *L, const char *name) { +/* when found, return 1 and leave string on top of stack + * when not found, return 0 + * caller is responsible for cleaning up the stack. */ +static int look_up_definition (lua_State *L, const char *name) { lua_getglobal(L, "teliva_program"); int history_array = lua_gettop(L); /* iterate over mutations in teliva_program history in reverse order */ @@ -332,11 +335,12 @@ static const char *look_up_definition (lua_State *L, const char *name) { } if (is_special_history_key(key)) continue; if (strcmp(key, name) == 0) - return lua_tostring(L, -1); + return 1; } + lua_pop(L, 1); } lua_pop(L, 1); - return NULL; + return 0; } @@ -410,13 +414,14 @@ void save_snapshot (int rowoff, int coloff, int cy, int cx) { } void save_to_current_definition_and_editor_buffer (lua_State *L, const char *definition) { + int current_stack_index = lua_gettop(L); strncpy(Current_definition, definition, CURRENT_DEFINITION_LEN); - const char *contents = look_up_definition(L, Current_definition); + int status = look_up_definition(L, Current_definition); FILE *out = fopen("teliva_editbuffer", "w"); - if (contents != NULL) - fprintf(out, "%s", contents); + if (status) + fprintf(out, "%s", lua_tostring(L, -1)); fclose(out); - lua_settop(L, 0); /* reclaim contents */ + lua_settop(L, current_stack_index); } |