Subject: bug#13687: /srv/bzr/emacs/trunk r111878: * lisp/replace.el(read-regexp): Let-bind `default' to the first

From: Jambunathan K <[email-masked]>
Date: Sat, 09 Mar 2013 14:17:22 +0530

Jambunathan K <[email-masked]> writes:

> "Drew Adams" <[email-masked]> writes:
>
>> E.g., in the code I cited, if a user does not want the same defaulting
>> behavior for commands `occur', `how-many', etc., she can set option
>> `search/replace-default-fn' to a function that distinguishes them
>> (e.g., using `this-command', as Jambunathan suggested).
>
> Interesting suggestion there.
>
> This makes me think that there is no need for multiple
> `hi-lock-read-regexp-defaults-function' and a separate
> `occur-read-regexp-defaults-function' etc.  But a single
> `read-regexp-defaults-function' that cases on `this-command'.
>
> The function can return a symbol token like `t' for `this-command's
> which it doesn't want to meddle with but return nil or a regexp or list
> of regexps for commands it wants to insinuate.
>
> Is there any problem with this `read-regexp-defaults-function'
> approach?

EXPERIMENTAL and ABANDONED PATCH

Use of `this-command' is very fragile and flaky.  Consider
`multi-occur-in-matching-buffers' which does multiple `read-regexp' -
one for the buffers and one for the actual regexp.  It is not possible
to return a two different regexps for the same `this-command'.

Interestingly, I am attaching a long from *Messages* buffer and it looks
like `this-command' is not reliable (Do you see `exit-minibuffer' in the
logs.)

So `this-command' could work for simple commands like highlighting
commands but will be flaky to be applied in general.

Anyways good to experiment and see where an idea takes us...

----------------------------------------------------------------
Customization

    (custom-set-variables
     '(read-regexp-user-defaults 
       (quote ((highlight-regexp find-tag-default-as-regexp)
               (highlight-phrase find-tag-default)
               (multi-occur-in-matching-buffers find-tag-default)))))

----------------------------------------------------------------

=== modified file 'lisp/replace.el'
--- lisp/replace.el	2013-03-08 04:18:16 +0000
+++ lisp/replace.el	2013-03-09 08:23:57 +0000
@@ -580,6 +580,39 @@ of `history-length', which see.")
 (defvar occur-collect-regexp-history '("\\1")
   "History of regexp for occur's collect operation")
 
+(defcustom read-regexp-user-defaults nil
+  ""
+  :type '(choice
+	  (const :tag "Use system defaults" nil)
+	  (repeat :tag "Per-command defaults"
+		  (list (radio :tag "Command"
+			       (function-item highlight-regexp)
+			       (function-item highlight-phrase)
+			       (function-item highlight-lines-matching-regexp)
+			       (function :tag "Command"))
+			(choice :tag "Function to retrieve the regexp"
+				(const :tag "Use no defaults" nil)
+				(const :tag "Use system defaults" t)
+				(radio
+				 (function-item find-tag-default-as-regexp)
+				 (function-item find-tag-default)
+				 (function-item :tag "Regexp history"
+						(lambda nil
+						  "Use regexp history."
+						  (car regexp-history)))
+				 function)))))
+  :group 'matching
+  :version "24.4")
+
+(defun read-regexp-defaults ()
+  (if (not read-regexp-user-defaults) t
+    (let ((user-default (assoc this-command read-regexp-user-defaults)))
+      (pcase user-default
+	(`(,cmd ,(and (pred functionp) getter))
+	 (funcall getter))
+	(`nil nil)
+	(_ t)))))
+
 (defun read-regexp (prompt &optional defaults history)
   "Read and return a regular expression as a string.
 When PROMPT doesn't end with a colon and space, it adds a final \": \".
@@ -597,6 +630,11 @@ and the last replacement regexp.
 
 Optional arg HISTORY is a symbol to use for the history list.
 If HISTORY is nil, `regexp-history' is used."
+  (let ((user-defaults (read-regexp-defaults)))
+    (unless (eq user-defaults t)
+      (setq defaults user-defaults)
+      (message "cmd: %s defaults: %S" this-command defaults)))
+
   (let* ((default     (if (consp defaults) (car defaults) defaults))
 	 (suggestions (if (listp defaults) defaults (list defaults)))
 	 (suggestions

----------------------------------------------------------------

*Messages*

Global-Hi-Lock mode enabled
Mark saved where search started
cmd: highlight-regexp defaults: "\\_<hi-yellow\\_>"
cmd: highlight-regexp defaults: "\\_<defface\\_>"
cmd: highlight-phrase defaults: "min-colors"
cmd: multi-occur-in-matching-buffers defaults: ":background"
cmd: exit-minibuffer defaults: nil
Searched 1 buffer; 10 matches for `yellow'