From a9dd73fb16529259bd247de77667e3920b8c62d1 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 9 Aug 2019 14:55:06 +0200 Subject: [PATCH] new feature: allow specifying a search string to "jump to" at startup The string to "jump to" is specified with +/ for a forward search (from the top of the file), or with +? for a backward search (from the bottom of the file). This fulfills https://savannah.gnu.org/bugs/?54535. Requested-by: Derek Wolfe With-help-from: Brand Huntsman --- src/nano.c | 24 ++++++++++++++++++++++++ src/proto.h | 2 ++ 2 files changed, 26 insertions(+) diff --git a/src/nano.c b/src/nano.c index cb16a37b..7b3863fa 100644 --- a/src/nano.c +++ b/src/nano.c @@ -2581,9 +2581,19 @@ int main(int argc, char **argv) /* Read the files mentioned on the command line into new buffers. */ while (optind < argc && (!openfile || read_them_all)) { ssize_t givenline = 0, givencol = 0; + char *searchstring = NULL; /* If there's a +LINE[,COLUMN] argument here, eat it up. */ if (optind < argc - 1 && argv[optind][0] == '+') { + if (argv[optind][1] == '/' || argv[optind][1] == '?') { + if (argv[optind][2]) { + searchstring = mallocstrcpy(NULL, &argv[optind][2]); + if (argv[optind][1] == '?') + SET(BACKWARDS_SEARCH); + } else + statusline(ALERT, _("Empty search string")); + optind++; + } else if (!parse_line_column(&argv[optind++][1], &givenline, &givencol)) statusline(ALERT, _("Invalid line or column number")); } @@ -2600,6 +2610,20 @@ int main(int argc, char **argv) /* If a position was given on the command line, go there. */ if (givenline != 0 || givencol != 0) do_gotolinecolumn(givenline, givencol, FALSE, FALSE); + else if (searchstring != NULL) { + if (ISSET(USE_REGEXP)) + regexp_init(searchstring); + if (!findnextstr(searchstring, FALSE, JUSTFIND, NULL, TRUE, + openfile->filetop, 0)) + not_found_msg(searchstring); + else + wipe_statusbar(); + if (ISSET(USE_REGEXP)) + tidy_up_after_search(); + free(last_search); + last_search = searchstring; + searchstring = NULL; + } #ifdef ENABLE_HISTORIES else if (ISSET(POSITIONLOG) && openfile->filename[0] != '\0') { ssize_t savedline, savedcol; diff --git a/src/proto.h b/src/proto.h index 39af51f4..aa9cd31f 100644 --- a/src/proto.h +++ b/src/proto.h @@ -479,6 +479,8 @@ void do_rcfiles(void); #endif /* ENABLE_NANORC */ /* Most functions in search.c. */ +bool regexp_init(const char *regexp); +void tidy_up_after_search(void); int findnextstr(const char *needle, bool whole_word_only, int modus, size_t *match_len, bool skipone, const linestruct *begin, size_t begin_x); void do_search(void);