Subject: bug#10885: Replace expressions: enhance functionality when searching in filled paragraphs

From: Juri Linkov <[email-masked]>
Date: Wed, 05 Sep 2012 11:38:50 +0300

> This is implemented in the following patch:
> +	      (if replace-lax-whitespace
> +		  (if regexp-flag
> +		      #'re-search-forward-lax-whitespace
> +		    #'search-forward-lax-whitespace)
> +		(if regexp-flag
> +		    #'re-search-forward
> +		  #'search-forward))))

Since query-replace is going in the direction of closer integration with
isearch, the right thing would be to get its search function from isearch.

Before calling `isearch-search-fun', `perform-replace' should bind
the same isearch related variables as in `replace-highlight'.
This will guarantee that highlighted matches are the same that it's
going to replace.

With this change query-replace will also correctly handle replacements of
symbols started from isearch-symbol mode with no additional code
in replace.el to implement this.

This patch is based on the previously sent patch and could be installed
in separate commits:

=== modified file 'lisp/replace.el'
--- lisp/replace.el	2012-09-02 11:27:15 +0000
+++ lisp/replace.el	2012-09-05 08:33:27 +0000
@@ -1786,13 +1791,15 @@ (defun perform-replace (from-string repl
 	  (or (if regexp-flag
 		  replace-re-search-function
 		replace-search-function)
-	      (if replace-lax-whitespace
-		  (if regexp-flag
-		      #'re-search-forward-lax-whitespace
-		    #'search-forward-lax-whitespace)
-		(if regexp-flag
-		    #'re-search-forward
-		  #'search-forward))))
+	      (let ((isearch-regexp regexp-flag)
+		    (isearch-word delimited-flag)
+		    (isearch-lax-whitespace
+		     (and replace-lax-whitespace (not regexp-flag)))
+		    (isearch-regexp-lax-whitespace
+		     (and replace-lax-whitespace regexp-flag))
+		    (isearch-case-fold-search case-fold-search)
+		    (isearch-forward t))
+		(isearch-search-fun))))
          (search-string from-string)
          (real-match-data nil)       ; The match data for the current match.
          (next-replacement nil)
@@ -1846,9 +1853,6 @@ (defun perform-replace (from-string repl
                                (vector repeat-count repeat-count
                                        replacements replacements)))))
 
-    (if delimited-flag
-	(setq search-function 're-search-forward
-	      search-string (word-search-regexp from-string)))
     (when query-replace-lazy-highlight
       (setq isearch-lazy-highlight-last-string nil))
 
@@ -1934,7 +1938,7 @@ (defun perform-replace (from-string repl
 		    (replace-highlight
 		     (nth 0 real-match-data) (nth 1 real-match-data)
 		     start end search-string
-		     (or delimited-flag regexp-flag) case-fold-search))
+		     regexp-flag delimited-flag case-fold-search))
 		  (setq noedit
 			(replace-match-maybe-edit
 			 next-replacement nocasify literal
@@ -1953,7 +1957,7 @@ (defun perform-replace (from-string repl
 		  (replace-highlight
 		   (match-beginning 0) (match-end 0)
 		   start end search-string
-		   (or delimited-flag regexp-flag) case-fold-search)
+		   regexp-flag delimited-flag case-fold-search)
 		  ;; Bind message-log-max so we don't fill up the message log
 		  ;; with a bunch of identical messages.
 		  (let ((message-log-max nil)
@@ -2135,15 +2139,10 @@ (defun perform-replace (from-string repl
 (defvar replace-overlay nil)
 
 (defun replace-highlight (match-beg match-end range-beg range-end
-			  string regexp case-fold)
+			  string regexp word case-fold)
   (if query-replace-highlight
       (if replace-overlay
 	  (move-overlay replace-overlay match-beg match-end (current-buffer))
@@ -2153,9 +2152,7 @@ (defun replace-highlight (match-beg matc
   (if query-replace-lazy-highlight
       (let ((isearch-string string)
 	    (isearch-regexp regexp)
-	    ;; Set isearch-word to nil because word-replace is regexp-based,
-	    ;; so `isearch-search-fun' should not use `word-search-forward'.
-	    (isearch-word nil)
+	    (isearch-word word)
 	    (isearch-lax-whitespace
 	     (and replace-lax-whitespace (not regexp)))
 	    (isearch-regexp-lax-whitespace