Subject: bug#13805: query-replace-regexp has conflicting interactive defaults

From: Juri Linkov <[email-masked]>
Date: Mon, 25 Feb 2013 02:03:37 +0200

> From `emacs -Q':
> M-% . RET ! RET ;; To put a replacement into the history.
> M-b    ;; To place point before the word "buffer" in *scratch*.
> C-M-%  ;; Prompts "Query replace regexp (default . -> !): ".
> RET    ;; Prompts "Query replace regexp \_<buffer\.\_> with: ".
> RET !  ;; Replaces "buffer" with "".

The default logic was broken a week ago in revno:111803.
This patch should restore the previous correct behavior:

=== modified file 'lisp/replace.el'
--- lisp/replace.el	2013-02-22 17:13:05 +0000
+++ lisp/replace.el	2013-02-25 00:01:21 +0000
@@ -592,7 +592,8 @@ (defun read-regexp (prompt &optional def
 
 Non-nil HISTORY is a symbol to use for the history list.
 If HISTORY is nil, `regexp-history' is used."
-  (let* ((defaults
+  (let* ((default (if (consp defaults) (car defaults) defaults))
+	 (defaults
 	   (append
 	    (if (listp defaults) defaults (list defaults))
 	    (list
@@ -610,7 +611,6 @@ (defun read-regexp (prompt &optional def
 		  (car (symbol-value
 			query-replace-from-history-variable)))))
 	 (defaults (delete-dups (delq nil (delete "" defaults))))
-	 (default (car defaults))
 	 ;; Do not automatically add default to the history for empty input.
 	 (history-add-new-input nil)
 	 (input (read-from-minibuffer

To avoid similar confusion in the future, I propose to improve the
terminology and give the variables proper names, i.e. to rename the
variable `defaults' to `suggestions' (a list of values available via `M-n')
as opposed to the variable `default' (a single value used when the user
accepts empty input).  This patch could be applied over the above patch:

=== modified file 'lisp/replace.el'
--- lisp/replace.el	2013-02-25 00:01:21 +0000
+++ lisp/replace.el	2013-02-25 00:02:12 +0000
@@ -593,7 +593,7 @@
 Non-nil HISTORY is a symbol to use for the history list.
 If HISTORY is nil, `regexp-history' is used."
   (let* ((default (if (consp defaults) (car defaults) defaults))
-	 (defaults
+	 (suggestions
 	   (append
 	    (if (listp defaults) defaults (list defaults))
 	    (list
@@ -610,7 +610,7 @@
 		  (regexp-quote (or (car search-ring) ""))
 		  (car (symbol-value
 			query-replace-from-history-variable)))))
-	 (defaults (delete-dups (delq nil (delete "" defaults))))
+	 (suggestions (delete-dups (delq nil (delete "" suggestions))))
 	 ;; Do not automatically add default to the history for empty input.
 	 (history-add-new-input nil)
 	 (input (read-from-minibuffer
@@ -621,7 +621,7 @@
 				 (query-replace-descr default)))
 		       (t
 			(format "%s: " prompt)))
-		 nil nil nil (or history 'regexp-history) defaults t)))
+		 nil nil nil (or history 'regexp-history) suggestions t)))
     (if (equal input "")
 	(or default input)
       (prog1 input