nano-devel
[Top][All Lists]
Advanced

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

[Nano-devel] [PATCH 1/2] search: begin from where we are, to be able to


From: Benno Schulenberg
Subject: [Nano-devel] [PATCH 1/2] search: begin from where we are, to be able to find the first \B
Date: Sun, 15 Jan 2017 20:24:27 +0100

A search should start at the place of the cursor, not one step beyond,
so that the non-word boundary between the current character and the next
will be found.  Starting one step beyond the current character, as was
done until now, will find the first non-word boundary between the next
and the overnext character.
---
 src/search.c | 34 +++++++++++++++++++---------------
 src/utils.c  |  5 -----
 2 files changed, 19 insertions(+), 20 deletions(-)

diff --git a/src/search.c b/src/search.c
index ba59a72b..af36de2b 100644
--- a/src/search.c
+++ b/src/search.c
@@ -245,24 +245,15 @@ int findnextstr(const char *needle, bool whole_word_only, 
size_t *match_len,
     int feedback = 0;
        /* When bigger than zero, show and wipe the "Searching..." message. */
     filestruct *line = openfile->current;
-    const char *from = line->data, *found = NULL;
+       /* The line that we will search through now. */
+    const char *from = line->data + openfile->current_x;
+       /* The point in the line from where we start searching. */
+    const char *found = NULL;
+       /* A pointer to the location of the match, if any. */
     size_t found_x;
        /* The x coordinate of a found occurrence. */
     time_t lastkbcheck = time(NULL);
 
-    /* 'from' might end up 1 character before the start or after the end
-     * of the line.  This is fine because in that case strstrwrapper()
-     * will return immediately and say that no match was found, and
-     * 'from' will be properly set when the search continues on the
-     * previous or next line. */
-    if (ISSET(BACKWARDS_SEARCH)) {
-       if (openfile->current_x == 0)
-           from += -1;
-       else
-           from += move_mbleft(line->data, openfile->current_x);
-    } else
-       from += move_mbright(line->data, openfile->current_x);
-
     enable_nodelay();
 
     if (begin == NULL)
@@ -295,6 +286,19 @@ int findnextstr(const char *needle, bool whole_word_only, 
size_t *match_len,
        /* Search for the needle in the current line. */
        found = strstrwrapper(line->data, needle, from);
 
+       /* Ignore the initial match at the starting position: continue
+        * searching from the next character, or invalidate the match. */
+       if (found == begin->data + begin_x && !came_full_circle) {
+           if (ISSET(BACKWARDS_SEARCH) && from != line->data) {
+               from = line->data + move_mbleft(line->data, from - line->data);
+               continue;
+           } else if (!ISSET(BACKWARDS_SEARCH) && *from != '\0') {
+               from += move_mbright(from, 0);
+               continue;
+           }
+           found = FALSE;
+       }
+
        if (found != NULL) {
 #ifdef HAVE_REGEX_H
            /* When doing a regex search, compute the length of the match. */
@@ -369,7 +373,7 @@ int findnextstr(const char *needle, bool whole_word_only, 
size_t *match_len,
        /* Set the starting x to the start or end of the line. */
        from = line->data;
        if (ISSET(BACKWARDS_SEARCH))
-           from += strlen(line->data);
+           from += strlen(line->data) + 1;
     }
 
     found_x = found - line->data;
diff --git a/src/utils.c b/src/utils.c
index 7a0641bf..bfd9d13c 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -324,11 +324,6 @@ bool is_separate_word(size_t position, size_t length, 
const char *buf)
 const char *strstrwrapper(const char *haystack, const char *needle,
        const char *start)
 {
-    /* start can be 1 character before the start or after the end of the
-     * line.  In either case, we just say no match was found. */
-    if ((start > haystack && *(start - 1) == '\0') || start < haystack)
-       return NULL;
-
 #ifdef HAVE_REGEX_H
     if (ISSET(USE_REGEXP)) {
        if (ISSET(BACKWARDS_SEARCH)) {
-- 
2.11.0




reply via email to

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