Subject: Re: bug#12321: 24.2.50; `read-regexp' parameter DEFAULT-VALUE and the calculated defaults

From: Juri Linkov <[email-masked]>
Date: Thu, 20 Sep 2012 11:43:45 +0300

> It seems odd that optional parameter DEFAULT-VALUE is not simply
> included among the `M-n' choices (hence the prompt fiddling and the
> after-read fiddling if empty input).  And it seems odd that you cannot
> pass a list of defaults as the optional parameter.  Why is the argument
> handled separately from the calculated list of "standard" defaults?

This can be fixed by the following patch.  It is based on two
previously sent patches (one that fixes the PROMPT arg, and
another that adds the HISTORY arg to `read-regexp' in bug#7567),
so `read-regexp' has the `history' arg in this third patch.
These three patches could be committed separately.  No doc fix yet,
just confirmed that it works predictably in all my tests.

=== modified file 'lisp/replace.el'
--- lisp/replace.el	2012-09-09 22:15:24 +0000
+++ lisp/replace.el	2012-09-20 08:42:58 +0000
@@ -575,7 +575,7 @@ (defvar regexp-history nil
 (defvar occur-collect-regexp-history '("\\1")
   "History of regexp for occur's collect operation")
 
-(defun read-regexp (prompt &optional default-value history)
+(defun read-regexp (prompt &optional defaults history)
   "Read regexp as a string using the regexp history and some useful defaults.
 Prompt for a regular expression with PROMPT (without a colon and
 space) in the minibuffer.  The optional argument DEFAULT-VALUE
@@ -585,7 +585,10 @@ (defun read-regexp (prompt &optional def
 If HISTORY is nil, `regexp-history' is used.
 Values available via M-n are the string at point, the last isearch
 regexp, the last isearch string, and the last replacement regexp."
-  (let* ((defaults
+  (let* ((default (if (consp defaults) (car defaults) defaults))
+	 (defaults
+	   (append
+	    (if (listp defaults) defaults (list defaults))
 	   (list (regexp-quote
 		  (or (funcall (or find-tag-default-function
 				   (get major-mode 'find-tag-default-function)
@@ -594,7 +597,7 @@ (defun read-regexp (prompt &optional def
 		 (car regexp-search-ring)
 		 (regexp-quote (or (car search-ring) ""))
 		 (car (symbol-value
-		       query-replace-from-history-variable))))
+			query-replace-from-history-variable)))))
 	 (defaults (delete-dups (delq nil (delete "" defaults))))
 	 ;; Don't add automatically the car of defaults for empty input
 	 (history-add-new-input nil)
@@ -602,13 +605,13 @@ (defun read-regexp (prompt &optional def
 	  (read-from-minibuffer
 	   (if (string-match-p ":[ \t]*\\'" prompt)
 	       prompt
-	     (if default-value
+	     (if default
 		 (format "%s (default %s): " prompt
-			 (query-replace-descr default-value))
+			 (query-replace-descr default))
 	       (format "%s: " prompt)))
 	   nil nil nil (or history 'regexp-history) defaults t)))
     (if (equal input "")
-	(or default-value input)
+	(or default input)
       (prog1 input
 	(add-to-history (or history 'regexp-history) input)))))