From b08732f9c11abfdb630e6620c108c65f5301ee8b Mon Sep 17 00:00:00 2001 From: Michele Bini Date: Mon, 8 Jan 2024 02:21:56 +0100 Subject: Ctrl-_ for seek to line by number, or random line Improve support of pipe mode diff --git a/nanobox.c b/nanobox.c index fec354c..2242d04 100644 --- a/nanobox.c +++ b/nanobox.c @@ -179,6 +179,7 @@ //config: This option makes the HOME and RETURN key behave differently //config: depending on the current indentation level. //config: +#define ENABLE_FEATURE_NANO_GOTO_LINE 1 #define ENABLE_FEATURE_NANO_DEVTTY 1 #define ENABLE_FEATURE_NANO_STDIO 1 //config:config FEATURE_NANO_STDIO_C @@ -234,6 +235,7 @@ #include #include #include +#include 0) { #if ENABLE_FEATURE_NANO_CRASHME @@ -922,7 +936,9 @@ static void edit_file(char *fn) } //------------------------------------------------------------------- - go_bottom_and_clear_to_eol(); + if (!nano_stdio_mode) { + go_bottom_and_clear_to_eol(); + } cookmode(); #undef cur_line } @@ -2465,6 +2481,21 @@ static void not_implemented(const char *s) static char last_key_code = 0; #endif +static void go_to_line(char*line) +{ + int lineno; + if (strcmp(line, "!") == 0) { + srand(time(NULL)); + lineno = rand() % count_lines(text, end - 1); + } else { + lineno = atoi(line); + if (!(lineno > 1)) return; + lineno--; + } + for (dot = text; lineno > 0 && dot < end; lineno--) dot = next_line(dot); +} + + // show file status on status line static int format_edit_status(void) { @@ -2518,7 +2549,7 @@ static int format_edit_status(void) #if 0 " %ld" #endif - " ^X=Exit ^O=Save ^W=Search ^K=Cut ^U=Paste M-u=Undo", + " ^X=Exit ^O=Save ^W=Search ^K=Cut ^U=Paste ^_=GoTo M-u=Undo", (current_filename != NULL ? current_filename : ""), #if ENABLE_FEATURE_NANO_READONLY (readonly_mode ? " [Readonly]" : ""), @@ -2974,6 +3005,14 @@ static void do_cmd(int c) editing = 0; continue_editing: break; +#if ENABLE_FEATURE_NANO_GOTO_LINE + case CTRL_CODE('_'): + // p = get_input_line("Jump to line [!=random]: ", buf); + p = get_input_line("Go to line [!=random]: ", buf); + strcpy(buf, p); + go_to_line(p); + break; +#endif #if ENABLE_FEATURE_NANO_SEARCH case CTRL_CODE('W'): #define buf search__buf -- cgit v0.10.2 From fb13647e6aef47c175412be7a08f655e016bf587 Mon Sep 17 00:00:00 2001 From: Michele Bini Date: Mon, 8 Jan 2024 03:02:31 +0100 Subject: Some support for 0 in files; reverse search Ctrl-Y 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; -- cgit v0.10.2