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

From: Juri Linkov <[email-masked]>
Date: Sun, 10 Mar 2013 20:28:14 +0200

> EXPERIMENTAL and ABANDONED PATCH

I think your patch is useful, please don't abandon it
except its `this-command' part.  As you already noted
`this-command' is very fragile and flaky.  Removing everything
related to `this-command' would leave other useful parts of your patch
that adds a new defcustom `read-regexp-defaults' and especially this part:

+               (choice :tag "Function to retrieve the regexp"
+                       (const :tag "Use no defaults" nil)
+                       (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)))))

and a new function `read-regexp-defaults'.

Instead of using `this-command', look for ideas to other similar features.
For example, many invocations of minibuffer functions specify their
HISTORY argument as a symbol that divides history variables into groups.
The DEFAULTS argument could use a similar grouping, i.e. when
`read-regexp' uses the symbol `regexp-history' in a call like:

  (read-regexp "Regexp to highlight" (car regexp-history) 'regexp-history)

This could be changed to specify DEFAULTS as the symbol `read-regexp-defaults':

  (read-regexp "Regexp to highlight" 'read-regexp-defaults 'regexp-history)

where `read-regexp-defaults' is a symbol name of the function that uses
the value of the defcustom `read-regexp-defaults' the get the default value
or returns nil.

We could add as many additional default-providing functions
as the number of places that call `read-regexp'.  But I think
it should be enough to have just two functions:

(defun read-regexp-defaults-or-history ()
  (or (read-regexp-defaults)
      (car regexp-history)))

(defun read-regexp-defaults-or-tag ()
  (or (read-regexp-defaults)
      (find-tag-default-as-regexp)))

These two functions are necessary to keep the current status quo
where some commands traditionally provide the last history element
as the default (`highlight-regexp', `occur-read-primary-args', `how-many',
`flush-lines', `keep-lines'), and other commands provide the tag at point
(`rgrep', `query-replace', `multi-occur-in-matching-buffers').
This is an artificial division existing solely for historical reasons.
These functions could be used e.g. in `occur-read-primary-args' as:

  (read-regexp "List lines matching regexp" 'read-regexp-defaults-or-history 'regexp-history)

and e.g. in `grep-read-regexp':

  (read-regexp "Search for" 'read-regexp-defaults-or-tag 'regexp-history)

This implementation will satisfy three goals:

1. Backward compatibility for the current traditional defaults,
2. Allow easy customization in one place (defcustom `read-regexp-defaults'),
3. Allow fine-tuning with function redefinitions, i.e. when
   the users will ask for more specific functions they could be added as:

(defun occur-read-regexp-defaults ()
  (read-regexp-defaults-or-history))

(defun grep-read-regexp-defaults ()
  (read-regexp-defaults-or-tag))

and can be used in `occur-read-primary-args':

  (read-regexp "List lines matching regexp" 'occur-read-regexp-defaults 'regexp-history)

and in `grep-read-regexp':

  (read-regexp "Search for" 'grep-read-regexp-defaults 'regexp-history)