nano-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Nano-devel] [PATCH V2] new feature: allow specifying a search string to


From: Benno Schulenberg
Subject: [Nano-devel] [PATCH V2] new feature: allow specifying a search string to "jump to" at startup
Date: Sat, 17 Aug 2019 14:55:32 +0200

V2: * Don't fail to compile with --disable-histories.
    * Report an empty search string instead of sitting on the second character.
    * Properly report it when the string is not found.

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 <address@hidden>

With-help-from: Brand Huntsman <address@hidden>
---
 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);
-- 
2.22.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]