diff --git a/config/init.lisp b/config/init.lisp index ff3bcf9..bda52df 100644 --- a/config/init.lisp +++ b/config/init.lisp @@ -16,7 +16,7 @@ ;; KEY MAPPING (map-key "CTRL-q" editor-quit) -(map-key "CTRL-s" editor-save) +(map-key "CTRL-d" editor-save) (map-key "ARROW-UP" '(move-cursor "up")) (map-key "ARROW-DOWN" '(move-cursor "down")) (map-key "ARROW-RIGHT" '(move-cursor "right")) @@ -30,5 +30,5 @@ (map-key "PAGE-DOWN" move-cursor-page-down) (map-key "CTRL-o" editor-open-file) (map-key "CTRL-k" editor-del-row) - +(map-key "CTRL-s" editor-find) diff --git a/include/builtins.h b/include/builtins.h index b6cc0a1..2f1670f 100644 --- a/include/builtins.h +++ b/include/builtins.h @@ -32,4 +32,7 @@ Lisp editorPrintC(Lisp args, LispError *e, LispContext ctx); Lisp addPackage(Lisp args, LispError *e, LispContext ctx); Lisp editorDelRow_L(Lisp args, LispError *e, LispContext ctx); + +Lisp editorFind_L(Lisp args, LispError *e, LispContext ctx); + #endif diff --git a/include/file_io.h b/include/file_io.h index 771ef08..f8ee453 100644 --- a/include/file_io.h +++ b/include/file_io.h @@ -17,4 +17,6 @@ void editorOpen(char *filename); void editorSave(); +void editorFind(); + #endif // FILE_IO_H_ diff --git a/include/row_op.h b/include/row_op.h index 9a486db..80973ad 100644 --- a/include/row_op.h +++ b/include/row_op.h @@ -10,6 +10,8 @@ int editorRowCxToRx(erow *row, int cursor_x); +int editorRowRxToCx(erow *row, int rx); + void editorUpdateRow(erow *row); void editorInsertRow(int at, char *s, size_t len); diff --git a/src/builtins.c b/src/builtins.c index c20abaf..0403e19 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -162,3 +162,7 @@ Lisp editorDelRow_L(Lisp args, LispError *e, LispContext ctx) { return lisp_null(); } +Lisp editorFind_L(Lisp args, LispError *e, LispContext ctx) { + editorFind(); + return lisp_null(); +} diff --git a/src/file_io.c b/src/file_io.c index 47aea85..1f98d63 100644 --- a/src/file_io.c +++ b/src/file_io.c @@ -110,3 +110,21 @@ void editorSave() { free(buf); editorSetStatusMessage("Can't save! I/O error: %s", strerror(errno)); } + +void editorFind() { + char *query = editorPrompt("Search: %s (ESC to cancel)"); + if (query == NULL) return; + int i; + for (i = 0; i < E.numrows; i++) { + erow *row = &E.row[i]; + char *match = strstr(row->render, query); + if (match) { + E.cursor_y = i; + E.cursor_x = editorRowRxToCx(row, match - row->render); + E.row_offset = E.numrows; + break; + } + } + free(query); +} + diff --git a/src/init.c b/src/init.c index 9e07fc0..f9860ba 100644 --- a/src/init.c +++ b/src/init.c @@ -33,6 +33,7 @@ void initBuiltins() { registerBuiltin("EDITOR-INSERT-CHAR", editorPrintC); registerBuiltin("ADD-PACKAGE", addPackage); registerBuiltin("EDITOR-DEL-ROW", editorDelRow_L); + registerBuiltin("EDITOR-FIND", editorFind_L); } void initEditor() { diff --git a/src/row_op.c b/src/row_op.c index 5502025..29bd22d 100644 --- a/src/row_op.c +++ b/src/row_op.c @@ -18,6 +18,18 @@ int editorRowCxToRx(erow *row, int cursor_x) { return render_x; } +int editorRowRxToCx(erow *row, int rx) { + int cur_rx = 0; + int cx; + for (cx = 0; cx < row->size; cx++) { + if (row->chars[cx] == '\t') + cur_rx += (E.constantes.TAB_LENGTH - 1) - (cur_rx % E.constantes.TAB_LENGTH); + cur_rx++; + if (cur_rx > rx) return cx; + } + return cx; +} + /** * \fn editorUpdateRow(erow *row) * \brief Copy content of \p row in \p row->render.