summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichele Bini <michele.bini@gmail.com>2024-01-08 02:02:31 (GMT)
committerMichele Bini <michele.bini@gmail.com>2024-01-08 02:02:31 (GMT)
commitfb13647e6aef47c175412be7a08f655e016bf587 (patch)
tree91c65f15789aa08dbcd9d436121b006b14dfc687
parentb08732f9c11abfdb630e6620c108c65f5301ee8b (diff)
Some support for 0 in files; reverse search Ctrl-Yfrom-alienbuntu
-rw-r--r--nanobox.c86
1 files changed, 84 insertions, 2 deletions
diff --git a/nanobox.c b/nanobox.c
index 2242d04..2e144d2 100644
--- a/nanobox.c
+++ b/nanobox.c
@@ -78,6 +78,9 @@
//config: help
//config: Select this if you wish to be able to do search and replace.
//config:
+#define ENABLE_FEATURE_NANO_CORRECT_SEARCH 1
+//config: bool "Better support 0-chars in text, and perhaps (slightly) more efficent search"
+#define ENABLE_FEATURE_NANO_REVERSE_SEARCH 1
#define ENABLE_FEATURE_NANO_REGEX_SEARCH 0
//config:config FEATURE_NANO_REGEX_SEARCH
//config: bool "Enable regex in search and replace"
@@ -2829,6 +2832,50 @@ static void fold_cmd(void)
// 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
//---------------------------------------------------------------------
+
+#if ENABLE_FEATURE_NANO_REVERSE_SEARCH
+static char *rstrstr(const char * haystack, const char * needle, const char * start) {
+ const char *h, *n;
+ if (start == NULL) {
+ for (start = haystack; *start != 0; start++);
+ }
+ while (--start >= haystack) {
+ for (h = start, n = needle; *h == *n; h++, n++) {
+ if (*n == 0) return (char*)start;
+ }
+ if (*n == 0) return (char*)start;
+ }
+}
+#endif
+
+#if ENABLE_FEATURE_NANO_CORRECT_SEARCH
+#if 0
+static char *strstr(const char * haystack, const char * needle) {
+ const char *h, *n;
+ while (*haystack != 0) {
+ for (h = haystack, n = needle; *h == *n; h++, n++) {
+ if (*n == 0) return (char*)haystack;
+ }
+ if (*n == 0) return (char*)haystack;
+ haystack++;
+ }
+ return NULL;
+}
+#endif
+static char *strstrx(const char * haystack, const char * needle, const char * before) {
+ const char *h, *n;
+ // before shouldn't be NULL!
+ while (haystack < before) {
+ for (h = haystack, n = needle; *h == *n; h++, n++) {
+ if (*n == 0) return (char*)haystack;
+ }
+ if (*n == 0) return (char*)haystack;
+ haystack++;
+ }
+ return NULL;
+}
+#endif
+
//----- Execute a Nano Command -----------------------------------
static void do_cmd(int c)
{
@@ -3014,16 +3061,49 @@ static void do_cmd(int c)
break;
#endif
#if ENABLE_FEATURE_NANO_SEARCH
+#if ENABLE_FEATURE_NANO_REVERSE_SEARCH
+ case CTRL_CODE('Y'):
+#define buf search__buf
+ if (dot == NULL) dot = end; // not sure if this is really needed, segfaults observed during search
+ p = get_input_line("Search: ", buf);
+ strcpy(buf, p);
+ q = dot > text ? rstrstr(text, p, dot) : NULL;
+ char *info_dot = dot;
+ if (q != NULL) {
+ dot = q;
+ } else {
+ q = rstrstr(dot, p, end);
+ if (q != NULL) {
+ status_line_bold("Search wrapped!");
+ dot = q;
+ } else {
+ status_line_bold("String not found: %s", p);
+ }
+ }
+#undef buf
+ break;
+#endif
case CTRL_CODE('W'):
#define buf search__buf
+ if (dot == NULL) dot = end; // not sure if this is really needed, segfaults observed during search!
p = get_input_line("Search: ", buf);
strcpy(buf, p);
- q = dot + 1 < end ? strstr(dot+1, p) : NULL;
+ q = dot + 1 < end ?
+#if ENABLE_FEATURE_NANO_CORRECT_SEARCH
+ strstrx(dot+1, p, end)
+#else
+ strstr(dot+1, p)
+#endif
+ : NULL;
if (q != NULL) {
dot = q;
} else {
- // This may scan parts of the buffer twice but is still correct and much simpler to implement.
+#if ENABLE_FEATURE_NANO_CORRECT_SEARCH
+ q = strstrx(text, p, dot);
+#else
+ // This may scan parts of the buffer twice but is still correct and much simpler to code
q = strstr(text, p);
+#endif
if (q != NULL) {
status_line_bold("Search wrapped!", p);
dot = q;
@@ -3034,9 +3114,11 @@ static void do_cmd(int c)
#undef buf
break;
#endif
+#if !(ENABLE_FEATURE_NANO_SEARCH && ENABLE_FEATURE_NANO_REVERSE_SEARCH)
case 25: // ctrl-Y scroll up one line
dot_scroll(1, -1);
break;
+#endif
case KEYCODE_RIGHT: // Cursor Key Right
dot_right();
break;