nano-devel
[Top][All Lists]
Advanced

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

[Nano-devel] [PATCH] tweaks: rename and condense some things


From: Benno Schulenberg
Subject: [Nano-devel] [PATCH] tweaks: rename and condense some things
Date: Fri, 9 Feb 2018 19:45:22 +0100

And strip what seems to be unneeded.
---
 src/nano.h   |  8 +++---
 src/prompt.c |  2 +-
 src/proto.h  |  2 +-
 src/search.c | 93 +++++++++++++++++++++++-------------------------------------
 4 files changed, 42 insertions(+), 63 deletions(-)

diff --git a/src/nano.h b/src/nano.h
index d00e5d04..79d416d2 100644
--- a/src/nano.h
+++ b/src/nano.h
@@ -144,8 +144,8 @@
 #define STEPWISE  3
 
 /* Modes for incremental search. */
-#define SEARCH_FROM_START 1
-#define SEARCH_NEXT 2
+#define FROM_START 1
+#define FROM_HERE 2
 
 /* Enumeration types. */
 typedef enum {
@@ -496,7 +496,6 @@ enum
 enum
 {
        DONTUSE,
-       INCREMENTAL_SEARCH,
        CASE_SENSITIVE,
        CONSTANT_SHOW,
        NO_HELP,
@@ -538,7 +537,8 @@ enum
        SHOW_CURSOR,
        LINE_NUMBERS,
        NO_PAUSES,
-       AT_BLANKS
+       AT_BLANKS,
+       INCREMENTAL_SEARCH
 };
 
 /* Flags for the menus in which a given function should be present. */
diff --git a/src/prompt.c b/src/prompt.c
index 85bf021c..ee77ec08 100644
--- a/src/prompt.c
+++ b/src/prompt.c
@@ -573,7 +573,7 @@ functionptrtype acquire_an_answer(int *actual, bool 
allow_tabs,
                        break;
 
                if (ISSET(INCREMENTAL_SEARCH) && currmenu == MWHEREIS)
-                       search_text(answer, SEARCH_FROM_START);
+                       advance_to(answer, FROM_START);
 
                update_the_statusbar();
 
diff --git a/src/proto.h b/src/proto.h
index 853af40d..2086851a 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -493,7 +493,7 @@ void do_findprevious(void);
 void do_findnext(void);
 #endif
 void do_research(void);
-void search_text(char *needle, int modus);
+void advance_to(char *needle, int modus);
 void go_looking(void);
 ssize_t do_replace_loop(const char *needle, bool whole_word_only,
                const filestruct *real_current, size_t *real_current_x);
diff --git a/src/search.c b/src/search.c
index 50534990..11e706b0 100644
--- a/src/search.c
+++ b/src/search.c
@@ -30,9 +30,9 @@ static bool came_full_circle = FALSE;
                /* Have we reached the starting line again while searching? */
 static bool regexp_compiled = FALSE;
                /* Have we compiled any regular expressions? */
-static filestruct *saved_current;
-static size_t saved_x;
-               /* Position to be restored because of incremental search */
+static filestruct *starting_line;
+static size_t starting_x;
+               /* The position where the user started an incremental search. */
 
 /* Compile the given regular expression and store it in search_regexp.
  * Return TRUE if the expression is valid, and FALSE otherwise. */
@@ -108,35 +108,29 @@ void search_replace_abort(void)
 /* Repostion the cursor */
 void reposition_cursor(filestruct *was_current, size_t was_current_x, bool 
refresh)
 {
-               openfile->current = was_current;
-               openfile->current_x = was_current_x;
-               openfile->placewewant = xplustabs();
-               if(refresh)
-                       edit_refresh();
+       openfile->current = was_current;
+       openfile->current_x = was_current_x;
+       if (refresh)
+               edit_refresh();
 }
 
-/* Searches, highlights (or beeps) and positions the cursor.
+/* Search for the needle and highlight it, or beep when not found.
  * Possible modes are:
- *  - SEARCH_NEXT: searches for next occurrence or
- *  - SEARCH_FROM_START: search from the saved position.
- * Return true if found, false otherwise. */
-void search_text(char *needle, int modus)
+ *  - FROM_START: search from the original position;
+ *  - FROM_HERE: search for the next occurrence.
+ * Return TRUE if found, and FALSE otherwise. */
+void advance_to(char *needle, int modus)
 {
-       filestruct *was_current = openfile->current;
+       filestruct *was_line = openfile->current;
        size_t was_x = openfile->current_x;
-           /* Initial position for incremental search */
-
-       bool skipone = (modus == SEARCH_NEXT);
-
-       bool didfind, was_full_circle = came_full_circle;
+       bool didfind;
        size_t len;
 
-       if(modus == SEARCH_FROM_START)
-               reposition_cursor(saved_current, saved_x, FALSE);
+       if (modus == FROM_START)
+               reposition_cursor(starting_line, starting_x, FALSE);
 
        if (needle[0] == '\0') {
-               reposition_cursor(saved_current, saved_x, TRUE);
-               highlight(FALSE, 0);
+               reposition_cursor(starting_line, starting_x, TRUE);
                return;
        }
 
@@ -144,18 +138,13 @@ void search_text(char *needle, int modus)
                return;
 
        came_full_circle = FALSE;
-       didfind = findnextstr(needle, FALSE, STEPWISE, &len, skipone,
+       didfind = findnextstr(needle, FALSE, STEPWISE, &len, TRUE,
                                openfile->current, openfile->current_x);
-       came_full_circle = was_full_circle;
-
-       if (ISSET(USE_REGEXP))
-               regexp_cleanup();
 
        if (didfind)
                highlight(TRUE, len);
        else {
-               if(modus == SEARCH_FROM_START)
-                       reposition_cursor(was_current, was_x, FALSE);
+               reposition_cursor(was_line, was_x, FALSE);
                beep();
        }
 }
@@ -167,13 +156,14 @@ void search_init(bool replacing, bool keep_the_answer)
 {
        char *thedefault;
                /* What will be searched for when the user typed nothing. */
+       bool didnext = FALSE;
 
        /* When starting a new search, clear the current answer. */
        if (!keep_the_answer)
                answer = mallocstrcpy(answer, NULL);
 
-       saved_current = openfile->current;
-       saved_x = openfile->current_x;
+       starting_line = openfile->current;
+       starting_x = openfile->current_x;
 
        /* If something was searched for earlier, include it in the prompt. */
        if (*last_search != '\0') {
@@ -207,6 +197,8 @@ void search_init(bool replacing, bool keep_the_answer)
 #endif
                                        _(" (to replace)") : "", 
ISSET(INCREMENTAL_SEARCH) ? "" : thedefault);
 
+               highlight(FALSE, 0);
+
                /* If the search was cancelled, or we have a blank answer and
                 * nothing was searched for yet during this session, get out. */
                if (i == -1 || (i == -2 && *last_search == '\0')) {
@@ -214,8 +206,8 @@ void search_init(bool replacing, bool keep_the_answer)
                        search_replace_abort();
                        free(thedefault);
 
-                       if(ISSET(INCREMENTAL_SEARCH))
-                               reposition_cursor(saved_current, saved_x, TRUE);
+                       if (ISSET(INCREMENTAL_SEARCH))
+                               reposition_cursor(starting_line, starting_x, 
TRUE);
 
                        return;
                }
@@ -241,16 +233,16 @@ void search_init(bool replacing, bool keep_the_answer)
 
                        if (replacing)
                                ask_for_replacement();
-                       else if (ISSET(INCREMENTAL_SEARCH))
-                               highlight(FALSE, 0);
-                       else
+                       else if (!ISSET(INCREMENTAL_SEARCH) && !didnext)
                                go_looking();
 
+                       openfile->placewewant = xplustabs();
                        search_replace_abort();
                        return;
                }
 
                func = func_from_key(&i);
+               didnext = FALSE;
 
                /* If we're here, one of the five toggles was pressed, or
                 * a shortcut was executed. */
@@ -262,17 +254,11 @@ void search_init(bool replacing, bool keep_the_answer)
                        TOGGLE(USE_REGEXP);
                } else if (func == flip_replace) {
                        replacing = !replacing;
-                       if (ISSET(INCREMENTAL_SEARCH)) {
-                               reposition_cursor(saved_current, saved_x, TRUE);
-                               if (!replacing)
-                                       search_text(answer, FALSE);
-                       }
-               } else if (func == do_research_void) {
-                       search_text(answer, SEARCH_NEXT);
                } else if (func == inc_search_void) {
                        TOGGLE(INCREMENTAL_SEARCH);
-                       if (!ISSET(INCREMENTAL_SEARCH))
-                               reposition_cursor(saved_current, saved_x, TRUE);
+               } else if (func == do_research_void) {
+                       advance_to(answer, FROM_HERE);
+                       didnext = TRUE;
                } else {
                        if (func == flip_goto)
                                do_gotolinecolumn(openfile->current->lineno,
@@ -282,13 +268,10 @@ void search_init(bool replacing, bool keep_the_answer)
                        return;
                }
 
-               /* Handle some cases where we have to search again. */
-               if(ISSET(INCREMENTAL_SEARCH) && (func == case_sens_void ||
-                                                                               
 func == backwards_void ||
-                                                                               
 func == regexp_void    ||
-                                                                               
 func == inc_search_void)) {
-                       search_text(answer, SEARCH_FROM_START);
-               }
+               if (ISSET(INCREMENTAL_SEARCH) && !replacing && !didnext)
+                       advance_to(answer, FROM_START);
+               else if (!didnext)
+                       reposition_cursor(starting_line, starting_x, TRUE);
        }
 }
 
@@ -521,10 +504,6 @@ void go_looking(void)
        didfind = findnextstr(last_search, FALSE, JUSTFIND, NULL, TRUE,
                                                                
openfile->current, openfile->current_x);
 
-       /* Unmark highlighted text. */
-       if (didfind && ISSET(INCREMENTAL_SEARCH))
-               highlight(FALSE, 0);
-
        /* If we found something, and we're back at the exact same spot
         * where we started searching, then this is the only occurrence. */
        if (didfind == 1 && openfile->current == was_current &&
-- 
2.14.3




reply via email to

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