Subject: bug#13178: Query-replace fails soon after narrow-to-region

From: Juri Linkov <[email-masked]>
Date: Mon, 02 Mar 2015 22:14:17 +0200

>> Perhaps the "replace" family of commands ought to ignore the region if it's
>> empty?
>
> It should obey use-empty-active-region.

This patch allows query-replace commands to obey use-empty-active-region:

diff --git a/lisp/replace.el b/lisp/replace.el
index e0636e0..8b62b54 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -312,15 +312,15 @@ (defun query-replace (from-string to-string &optional delimited start end backwa
 		   (if current-prefix-arg
 		       (if (eq current-prefix-arg '-) " backward" " word")
 		     "")
-		   (if (and transient-mark-mode mark-active) " in region" ""))
+		   (if (use-region-p) " in region" ""))
 	   nil)))
      (list (nth 0 common) (nth 1 common) (nth 2 common)
 	   ;; These are done separately here
 	   ;; so that command-history will record these expressions
 	   ;; rather than the values they had this time.
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-beginning))
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-end))
 	   (nth 3 common))))
   (perform-replace from-string to-string t nil delimited nil nil start end backward))
@@ -392,15 +392,15 @@ (defun query-replace-regexp (regexp to-string &optional delimited start end back
 		       (if (eq current-prefix-arg '-) " backward" " word")
 		     "")
 		   " regexp"
-		   (if (and transient-mark-mode mark-active) " in region" ""))
+		   (if (use-region-p) " in region" ""))
 	   t)))
      (list (nth 0 common) (nth 1 common) (nth 2 common)
 	   ;; These are done separately here
 	   ;; so that command-history will record these expressions
 	   ;; rather than the values they had this time.
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-beginning))
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-end))
 	   (nth 3 common))))
   (perform-replace regexp to-string t t delimited nil nil start end backward))
@@ -469,9 +469,9 @@ (defun query-replace-regexp-eval (regexp to-expr &optional delimited start end)
        ;; and the user might enter a single token.
        (replace-match-string-symbols to)
        (list from (car to) current-prefix-arg
-	     (if (and transient-mark-mode mark-active)
+	     (if (use-region-p)
 		 (region-beginning))
-	     (if (and transient-mark-mode mark-active)
+	     (if (use-region-p)
 		 (region-end))))))
   (perform-replace regexp (cons 'replace-eval-replacement to-expr)
 		   t 'literal delimited nil nil start end))
@@ -507,9 +507,9 @@ (defun map-query-replace-regexp (regexp to-strings &optional n start end)
      (list from to
 	   (and current-prefix-arg
 		(prefix-numeric-value current-prefix-arg))
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-beginning))
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-end)))))
   (let (replacements)
     (if (listp to-strings)
@@ -571,12 +571,12 @@ (defun replace-string (from-string to-string &optional delimited start end backw
 		       (if (eq current-prefix-arg '-) " backward" " word")
 		     "")
 		   " string"
-		   (if (and transient-mark-mode mark-active) " in region" ""))
+		   (if (use-region-p) " in region" ""))
 	   nil)))
      (list (nth 0 common) (nth 1 common) (nth 2 common)
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-beginning))
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-end))
 	   (nth 3 common))))
   (perform-replace from-string to-string nil nil delimited nil nil start end backward))
@@ -645,12 +645,12 @@ (defun replace-regexp (regexp to-string &optional delimited start end backward)
 		       (if (eq current-prefix-arg '-) " backward" " word")
 		     "")
 		   " regexp"
-		   (if (and transient-mark-mode mark-active) " in region" ""))
+		   (if (use-region-p) " in region" ""))
 	   t)))
      (list (nth 0 common) (nth 1 common) (nth 2 common)
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-beginning))
-	   (if (and transient-mark-mode mark-active)
+	   (if (use-region-p)
 	       (region-end))
 	   (nth 3 common))))
   (perform-replace regexp to-string nil t delimited nil nil start end backward))
@@ -816,7 +816,7 @@ (defun keep-lines (regexp &optional rstart rend interactive)
 		  (unless (or (bolp) (eobp))
 		    (forward-line 0))
 		  (point-marker)))))
-    (if (and interactive transient-mark-mode mark-active)
+    (if (and interactive (use-region-p))
 	(setq rstart (region-beginning)
 	      rend (progn
 		     (goto-char (region-end))
@@ -885,7 +885,7 @@ (defun flush-lines (regexp &optional rstart rend interactive)
       (progn
 	(goto-char (min rstart rend))
 	(setq rend (copy-marker (max rstart rend))))
-    (if (and interactive transient-mark-mode mark-active)
+    (if (and interactive (use-region-p))
 	(setq rstart (region-beginning)
 	      rend (copy-marker (region-end)))
       (setq rstart (point)
@@ -935,7 +935,7 @@ (defun how-many (regexp &optional rstart rend interactive)
               (setq rend (max rstart rend)))
           (goto-char rstart)
           (setq rend (point-max)))
-      (if (and interactive transient-mark-mode mark-active)
+      (if (and interactive (use-region-p))
 	  (setq rstart (region-beginning)
 		rend (region-end))
 	(setq rstart (point)