From af708843f69dc3c59c76ca525c8048939ba1bf0a Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Sat, 25 Apr 2015 14:52:58 +0000
Subject: [PATCH] Removing the unintended special case for replacing multiple
 occurrences of a literal ^ or $.

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5214 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog    |  4 ++++
 src/proto.h  |  1 -
 src/search.c | 38 +-------------------------------------
 src/utils.c  | 14 ++------------
 4 files changed, 7 insertions(+), 50 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 75d2ed48..12dd260f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2015-04-25  Benno Schulenberg  <bensberg@justemail.net>
+	* src/search.c (do_replace_loop): Remove the unintended special
+	case for replacing multiple occurrences of a literal ^ or $.
+
 2015-04-21  Benno Schulenberg  <bensberg@justemail.net>
 	* src/browser.c (findnextfile): Save the settings of the global
 	case-sens, direction, and regexp flags, and restore them on exit.
diff --git a/src/proto.h b/src/proto.h
index 37aa1d31..b786e118 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -712,7 +712,6 @@ ssize_t ngetdelim(char **lineptr, size_t *n, int delim, FILE *stream);
 #endif
 #endif
 #ifdef HAVE_REGEX_H
-bool regexp_bol_or_eol(const regex_t *preg, const char *string);
 const char *fixbounds(const char *r);
 #endif
 #ifndef DISABLE_SPELLER
diff --git a/src/search.c b/src/search.c
index 0a373174..94d37d66 100644
--- a/src/search.c
+++ b/src/search.c
@@ -632,10 +632,6 @@ ssize_t do_replace_loop(
     ssize_t numreplaced = -1;
     size_t match_len;
     bool replaceall = FALSE;
-#ifdef HAVE_REGEX_H
-    /* The starting-line match and bol/eol regex flags. */
-    bool begin_line = FALSE, bol_or_eol = FALSE;
-#endif
 #ifndef NANO_TINY
     bool old_mark_set = openfile->mark_set;
     filestruct *top, *bot;
@@ -669,16 +665,7 @@ ssize_t do_replace_loop(
 #ifndef DISABLE_SPELLER
 	whole_word,
 #endif
-#ifdef HAVE_REGEX_H
-	/* We should find a bol and/or eol regex only once per line.  If
-	 * the bol_or_eol flag is set, it means that the last search
-	 * found one on the beginning line, so we should skip over the
-	 * beginning line when doing this search. */
-	bol_or_eol
-#else
-	FALSE
-#endif
-	, real_current, *real_current_x, needle, &match_len)) {
+	FALSE, real_current, *real_current_x, needle, &match_len)) {
 	int i = 0;
 
 #ifndef NANO_TINY
@@ -693,22 +680,6 @@ ssize_t do_replace_loop(
 	}
 #endif
 
-#ifdef HAVE_REGEX_H
-	/* If the bol_or_eol flag is set, we've found a match on the
-	 * beginning line already, and we're still on the beginning line
-	 * after the search, it means that we've wrapped around, so
-	 * we're done. */
-	if (bol_or_eol && begin_line && openfile->current == real_current)
-	    break;
-	/* Otherwise, set the begin_line flag if we've found a match on
-	 * the beginning line, reset the bol_or_eol flag, and continue. */
-	else {
-	    if (openfile->current == real_current)
-		begin_line = TRUE;
-	    bol_or_eol = FALSE;
-	}
-#endif
-
 	/* Indicate that we found the search string. */
 	if (numreplaced == -1)
 	    numreplaced = 0;
@@ -741,13 +712,6 @@ ssize_t do_replace_loop(
 	    }
 	}
 
-#ifdef HAVE_REGEX_H
-	/* Set the bol_or_eol flag if we're doing a bol and/or eol regex
-	 * replace ("^", "$", or "^$"). */
-	if (ISSET(USE_REGEXP) && regexp_bol_or_eol(&search_regexp, needle))
-	    bol_or_eol = TRUE;
-#endif
-
 	if (i > 0 || replaceall) {	/* Yes, replace it!!!! */
 	    char *copy;
 	    size_t length_change;
diff --git a/src/utils.c b/src/utils.c
index 9aee7e1b..1d7899d8 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -250,15 +250,6 @@ ssize_t ngetline(char **lineptr, size_t *n, FILE *stream)
 #endif /* !DISABLE_NANORC */
 
 #ifdef HAVE_REGEX_H
-/* Do the compiled regex in preg and the regex in string match the
- * beginning or end of a line? */
-bool regexp_bol_or_eol(const regex_t *preg, const char *string)
-{
-    return (regexec(preg, string, 0, NULL, 0) == 0 &&
-	regexec(preg, string, 0, NULL, REG_NOTBOL | REG_NOTEOL) ==
-	REG_NOMATCH);
-}
-
 /* Fix the regex if we're on platforms which require an adjustment
  * from GNU-style to BSD-style word boundaries. */
 const char *fixbounds(const char *r)
@@ -290,12 +281,11 @@ const char *fixbounds(const char *r)
     fprintf(stderr, "fixbounds(): Ending string = \"%s\"\n", r3);
 #endif
     return (const char *) r3;
-#endif
+#endif /* !GNU_WORDBOUNDS */
 
     return r;
 }
-
-#endif
+#endif /* HAVE_REGEX_H */
 
 #ifndef DISABLE_SPELLER
 /* Is the word starting at position pos in buf a whole word? */
-- 
GitLab