commit a9dd73fb16529259bd247de77667e3920b8c62d1
parent 073bd3ad6e3dd25a0ea882e76fe2524aea704b6b
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Fri, 9 Aug 2019 14:55:06 +0200
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 <dwwolfe1@gmail.com>
With-help-from: Brand Huntsman <alpha@qzx.com>
Diffstat:
2 files changed, 26 insertions(+), 0 deletions(-)
diff --git 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
@@ -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);