diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/kilo.c | 59 | ||||
-rw-r--r-- | src/lua.c | 12 |
2 files changed, 68 insertions, 3 deletions
diff --git a/src/kilo.c b/src/kilo.c index 35aa577..0fdc899 100644 --- a/src/kilo.c +++ b/src/kilo.c @@ -118,6 +118,7 @@ enum KEY_ACTION{ CTRL_D = 4, CTRL_E = 5, CTRL_F = 6, + CTRL_G = 7, CTRL_H = 8, TAB = 9, CTRL_L = 12, @@ -603,6 +604,11 @@ void editorDelRow(int at) { E.dirty++; } +void editorClear(void) { + for (int j = E.numrows-1; j >= 0; j--) + editorDelRow(j); +} + /* Turn the editor rows into a single heap-allocated string. * Returns the pointer to the heap-allocated string and populate the * integer pointed by 'buflen' with the size of the string, escluding @@ -927,12 +933,15 @@ void editorRefreshScreen(void) { /* Create a two rows status. First row: */ abAppend(&ab,"\x1b[0K",4); - abAppend(&ab,"\x1b[7m \x1b[0m ^e \x1b[7m run ",23); + abAppend(&ab,"\x1b[7m ",6); + abAppend(&ab,"\x1b[0m ^e \x1b[7m run ",17); int len = 2 + 4 + 5; if (Previous_error != NULL) { abAppend(&ab,"\x1b[0m ^c \x1b[7m\x1b[0m\x1b[1m abort ",27); len += 4 + 7; } + abAppend(&ab,"\x1b[0m ^g \x1b[7m go ",16); + len += 4 + 4; abAppend(&ab,"\x1b[0m ^s \x1b[7m search ",20); len += 4 + 8; char rstatus[80]; @@ -1160,6 +1169,50 @@ void editorMoveCursor(int key) { } } +extern char* Current_definition; +int definition_exists(lua_State *L, char *name); +void write_definition_to_file(lua_State *L, char *name, char *outfilename); +void read_contents(lua_State *L, char *filename, char *out); +void update_definition(lua_State *L, char *name, char *out); +void save_image(lua_State *L); +int dostring(lua_State *L, const char *s, const char *name); +void editorGo(lua_State* L, int fd) { + char query[KILO_QUERY_LEN+1] = {0}; + int qlen = 0; + + editorSave(); + char new_contents[8192] = {0}; + read_contents(L, "teliva_editbuffer", new_contents); + update_definition(L, Current_definition, new_contents); + save_image(L); + /* reload binding if possible */ + dostring(L, new_contents, Current_definition); + + while(1) { + editorSetStatusMessage("Jump to: %s", query); + editorRefreshScreen(); + + int c = editorReadKey(fd); + if (c == DEL_KEY || c == CTRL_H || c == BACKSPACE) { + if (qlen != 0) query[--qlen] = '\0'; + } else if (c == ESC || c == ENTER) { + editorSetStatusMessage(""); + if (c == ENTER && definition_exists(L, query)) { + Current_definition = query; + write_definition_to_file(L, Current_definition, "teliva_editbuffer"); + editorClear(); + editorOpen("teliva_editbuffer"); + } + return; + } else if (isprint(c)) { + if (qlen < KILO_QUERY_LEN) { + query[qlen++] = c; + query[qlen] = '\0'; + } + } + } +} + /* Process events arriving from the standard input, which is, the user * is typing stuff on the terminal. */ #define KILO_QUIT_TIMES 3 @@ -1181,6 +1234,10 @@ void editorProcessKeypress(lua_State* L, int fd) { editorSave(); Quit = 1; break; + case CTRL_G: + /* Go to a different definition. */ + editorGo(L, fd); + break; case CTRL_F: editorFind(fd); break; diff --git a/src/lua.c b/src/lua.c index e75ba92..9b7cd76 100644 --- a/src/lua.c +++ b/src/lua.c @@ -309,6 +309,15 @@ static int handle_image (lua_State *L, char **argv, int n) { } +int definition_exists (lua_State *L, char *name) { + lua_getglobal(L, "teliva_program"); + lua_getfield(L, -1, name); + const char *contents = lua_tostring(L, -1); + lua_pop(L, 1); + return contents != NULL; +} + + void write_definition_to_file (lua_State *L, char *name, char *outfilename) { lua_getglobal(L, "teliva_program"); lua_getfield(L, -1, name); @@ -368,9 +377,8 @@ void switch_to_editor (lua_State *L, const char *message) { read_contents(L, "teliva_editbuffer", new_contents); update_definition(L, Current_definition, new_contents); save_image(L); - /* reload binding */ + /* reload binding if possible */ dostring(L, new_contents, Current_definition); - /* TODO: handle error */ } execv(Argv[0], Argv); /* never returns */ |