Add search function
Build project / build (push) Has been cancelled

This commit is contained in:
arthur
2025-11-03 16:45:23 +01:00
parent 6a201b3ebc
commit 419e924650
8 changed files with 44 additions and 2 deletions
+2 -2
View File
@@ -16,7 +16,7 @@
;; KEY MAPPING ;; KEY MAPPING
(map-key "CTRL-q" editor-quit) (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-UP" '(move-cursor "up"))
(map-key "ARROW-DOWN" '(move-cursor "down")) (map-key "ARROW-DOWN" '(move-cursor "down"))
(map-key "ARROW-RIGHT" '(move-cursor "right")) (map-key "ARROW-RIGHT" '(move-cursor "right"))
@@ -30,5 +30,5 @@
(map-key "PAGE-DOWN" move-cursor-page-down) (map-key "PAGE-DOWN" move-cursor-page-down)
(map-key "CTRL-o" editor-open-file) (map-key "CTRL-o" editor-open-file)
(map-key "CTRL-k" editor-del-row) (map-key "CTRL-k" editor-del-row)
(map-key "CTRL-s" editor-find)
+3
View File
@@ -32,4 +32,7 @@ Lisp editorPrintC(Lisp args, LispError *e, LispContext ctx);
Lisp addPackage(Lisp args, LispError *e, LispContext ctx); Lisp addPackage(Lisp args, LispError *e, LispContext ctx);
Lisp editorDelRow_L(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 #endif
+2
View File
@@ -17,4 +17,6 @@ void editorOpen(char *filename);
void editorSave(); void editorSave();
void editorFind();
#endif // FILE_IO_H_ #endif // FILE_IO_H_
+2
View File
@@ -10,6 +10,8 @@
int editorRowCxToRx(erow *row, int cursor_x); int editorRowCxToRx(erow *row, int cursor_x);
int editorRowRxToCx(erow *row, int rx);
void editorUpdateRow(erow *row); void editorUpdateRow(erow *row);
void editorInsertRow(int at, char *s, size_t len); void editorInsertRow(int at, char *s, size_t len);
+4
View File
@@ -162,3 +162,7 @@ Lisp editorDelRow_L(Lisp args, LispError *e, LispContext ctx) {
return lisp_null(); return lisp_null();
} }
Lisp editorFind_L(Lisp args, LispError *e, LispContext ctx) {
editorFind();
return lisp_null();
}
+18
View File
@@ -110,3 +110,21 @@ void editorSave() {
free(buf); free(buf);
editorSetStatusMessage("Can't save! I/O error: %s", strerror(errno)); 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);
}
+1
View File
@@ -33,6 +33,7 @@ void initBuiltins() {
registerBuiltin("EDITOR-INSERT-CHAR", editorPrintC); registerBuiltin("EDITOR-INSERT-CHAR", editorPrintC);
registerBuiltin("ADD-PACKAGE", addPackage); registerBuiltin("ADD-PACKAGE", addPackage);
registerBuiltin("EDITOR-DEL-ROW", editorDelRow_L); registerBuiltin("EDITOR-DEL-ROW", editorDelRow_L);
registerBuiltin("EDITOR-FIND", editorFind_L);
} }
void initEditor() { void initEditor() {
+12
View File
@@ -18,6 +18,18 @@ int editorRowCxToRx(erow *row, int cursor_x) {
return render_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) * \fn editorUpdateRow(erow *row)
* \brief Copy content of \p row in \p row->render. * \brief Copy content of \p row in \p row->render.