nano-devel
[Top][All Lists]
Advanced

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

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


From: Benno Schulenberg
Subject: [Nano-devel] [PATCH] possible new feature: allow specifying a string to jump to in a given file
Date: Fri, 9 Aug 2019 15:40:49 +0200

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>
---
 src/nano.c  | 20 ++++++++++++++++++--
 src/proto.h |  2 ++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/nano.c b/src/nano.c
index cb16a37b..77bc13df 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -2581,10 +2581,15 @@ 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 (!parse_line_column(&argv[optind++][1], &givenline, 
&givencol))
+                       if (argv[optind][1] == '/' || argv[optind][1] == '?') {
+                               searchstring = mallocstrcpy(NULL, 
&argv[optind][2]);
+                               if (argv[optind++][1] == '?')
+                                       SET(BACKWARDS_SEARCH);
+                       } else if (!parse_line_column(&argv[optind++][1], 
&givenline, &givencol))
                                statusline(ALERT, _("Invalid line or column 
number"));
                }
 
@@ -2600,8 +2605,19 @@ 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))
+                               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') {
+               } else if (ISSET(POSITIONLOG) && openfile->filename[0] != '\0') 
{
                        ssize_t savedline, savedcol;
                        /* If edited before, restore the last cursor position. 
*/
                        if (has_old_position(argv[optind - 1], &savedline, 
&savedcol))
diff --git a/src/proto.h b/src/proto.h
index 7b58b093..d4cdb97b 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -478,6 +478,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]