tAllow different find direction - ve - a minimal text editor (work in progress) | |
git clone git://src.adamsgaard.dk/ve | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit c25ff23d7f5f66ccd5b8053de791388c3a6b311a | |
parent eedc7316f2cb4c4964aff43cef6055ce428158cb | |
Author: Anders Damsgaard <[email protected]> | |
Date: Wed, 7 Aug 2019 09:32:47 +0200 | |
Allow different find direction | |
Diffstat: | |
M find.c | 64 ++++++++++++++++++++++++-----… | |
M find.h | 3 ++- | |
M input.c | 11 ++++++++++- | |
M terminal.c | 1 + | |
M ve.h | 2 ++ | |
5 files changed, 66 insertions(+), 15 deletions(-) | |
--- | |
diff --git a/find.c b/find.c | |
t@@ -1,29 +1,67 @@ | |
+/* add feature test macro for strdup compatibility */ | |
+#define _DEFAULT_SOURCE | |
+#define _BSD_SOURCE | |
+#define _GNU_SOURCE | |
+ | |
#include <stdlib.h> | |
#include <string.h> | |
#include "input.h" | |
#include "ve.h" | |
#include "row.h" | |
+#include "output.h" | |
+/* find E.find_query from current cursor position moving in E.direction | |
+ * if opposite_direction = 0, and opposite E.direction if | |
+ * opposite_direction = 1 */ | |
void | |
-editor_find() | |
+editor_find_next_occurence(int opposite_direction) | |
{ | |
- char *query, *match; | |
int i; | |
eRow *row; | |
- | |
- query = editor_prompt("/%s"); | |
- if (query == NULL) | |
+ char *match; | |
+ | |
+ if (!E.find_query) | |
return; | |
- for (i=0; i<E.num_rows; ++i) { | |
- row = &E.row[i]; | |
- match = strstr(row->rchars, query); | |
- if (match) { | |
- E.cursor_y = i; | |
- E.cursor_x = editor_row_cursor_rx_to_x(row, match - ro… | |
- /* E.row_offset = E.num_rows; */ /* put line to top of… | |
- break; | |
+ if ((E.find_direction && !opposite_direction) || | |
+ (!E.find_direction && opposite_direction)) { | |
+ for (i=0; i<E.num_rows; ++i) { | |
+ row = &E.row[i]; | |
+ match = strstr(row->rchars, E.find_query); | |
+ if (match) | |
+ break; | |
+ } | |
+ } else { | |
+ for (i=E.num_rows-1; i>=0; --i) { | |
+ row = &E.row[i]; | |
+ match = strstr(row->rchars, E.find_query); | |
+ if (match) | |
+ break; | |
} | |
} | |
+ if (match) { | |
+ E.cursor_y = i; | |
+ E.cursor_x = editor_row_cursor_rx_to_x(row, match - row->rchar… | |
+ /* E.row_offset = E.num_rows; */ /* put line to top of screen … | |
+ } | |
+} | |
+ | |
+/* direction = 1 is forward, 0 is backward */ | |
+void | |
+editor_find(int direction) | |
+{ | |
+ char *query; | |
+ | |
+ if (direction) | |
+ query = editor_prompt("/%s"); | |
+ else | |
+ query = editor_prompt("?%s"); | |
+ if (query == NULL) | |
+ return; | |
+ | |
+ E.find_direction = direction; | |
+ free(E.find_query); | |
+ E.find_query = strdup(query); | |
+ editor_find_next_occurence(0); | |
free(query); | |
} | |
diff --git a/find.h b/find.h | |
t@@ -1,6 +1,7 @@ | |
#ifndef FIND_H_ | |
#define FIND_H_ | |
-void editor_find(); | |
+void editor_find_next_occurence(int opposite_direction); | |
+void editor_find(int direction); | |
#endif | |
diff --git a/input.c b/input.c | |
t@@ -213,7 +213,16 @@ editor_process_keypress() | |
break; | |
case '/': | |
- editor_find(); | |
+ editor_find(1); | |
+ break; | |
+ case '?': | |
+ editor_find(0); | |
+ break; | |
+ case 'n': | |
+ editor_find_next_occurence(0); | |
+ break; | |
+ case 'N': | |
+ editor_find_next_occurence(1); | |
break; | |
} | |
} else if (E.mode == 1) { /* insert mode */ | |
diff --git a/terminal.c b/terminal.c | |
t@@ -122,6 +122,7 @@ init_editor() { | |
E.status_msg_time = 0; | |
E.show_status = 0; | |
E.file_changed = 0; | |
+ E.find_query = NULL; | |
if (get_window_size(&E.screen_rows, &E.screen_columns) == -1) | |
die("get_window_size"); | |
diff --git a/ve.h b/ve.h | |
t@@ -31,6 +31,8 @@ struct editor_config { | |
char status_msg[80]; | |
time_t status_msg_time; | |
int file_changed; | |
+ char *find_query; | |
+ int find_direction; | |
}; | |
extern struct editor_config E; |