Subject: bug#12321: closed (Re: bug#12321: 24.2.50; `read-regexp' parameter PROMPT)

From: [email-masked] (GNU bug Tracking System)
Date: Thu, 20 Sep 2012 22:03:03 +0000

Your bug report

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

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
12321: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=12321
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#12321: 24.2.50; `read-regexp' parameter PROMPT
From: Juri Linkov <[email-masked]>
To: Stefan Monnier <[email-masked]>
Cc: [email-masked], Drew Adams <[email-masked]>
Date: Fri, 21 Sep 2012 00:59:08 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

> That looks OK, tho please use `cond' then.

Done.

Subject: 24.2.50; `read-regexp' parameter DEFAULT-VALUE and the calculated defaults
From: "Drew Adams" <[email-masked]>
To: <[email-masked]>
Date: Fri, 31 Aug 2012 15:38:45 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

A couple of things seem odd to me about `read-regexp' as currently
defined.
 
First, there is the business of automatically handling `: ' for the
PROMPT.  I thought we had finally gotten away from this kind of thing.
It just gives callers less control.  And it has sometimes resulted in
confusion and extra work.  I don't see this as a win.  But whatever.
 
More importantly -
 
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?
 
The current version provides a list of defaults, but they are all
hard-coded.  A given calling context might well have its own set of
defaults that it would like to provide.  But it cannot do so (except for
one).
 
Here is a version I am using, which lets the arg be a list of defaults.
That gives callers more control.  Perhaps you might consider it.
(I have not changed the prompt behavior here.)
 
(defun read-regexp (prompt &optional defaults)
  "Read and return a regular expression as a string.
Prompt with PROMPT, which should not include a final `: '.
 
Non-nil optional arg DEFAULTS is a string or a list of strings that
are prepended to a list of standard default values, which include the
string at point, the last isearch regexp, the last isearch string, and
the last replacement regexp."
  (when (and defaults  (atom defaults)) (setq defaults  (list defaults)))
  (let* ((deflts (append
                  defaults
                  (list (regexp-quote
                         (or (funcall
                              (or find-tag-default-function
                                  (get major-mode 'find-tag-default-function)
                                  'find-tag-default))
                             ""))
                        (car regexp-search-ring)
                        (regexp-quote (or (car search-ring)  ""))
                        (car (symbol-value
                              query-replace-from-history-variable)))))
         (deflts (delete-dups (delq nil (delete "" deflts))))
         ;; Do not automatically add INPUT to the history, in case it is "".
         (history-add-new-input  nil)
         (input (read-from-minibuffer
                 (if defaults
                     (format "%s (default `%s'): " prompt
                             (mapconcat 'isearch-text-char-description
                                        (car deflts) ""))
                   (format "%s: " prompt))
                 nil nil nil 'regexp-history deflts t)))
    (if (equal input "")
        (or (car defaults)  input)
      (prog1 input (add-to-history 'regexp-history input)))))
 
Finally, I wonder if it is really appropriate to be filtering out empty
input ("") here.  Perhaps we should let `read-regexp' return "" under
some conditions?  For example, if DEFAULTS contains "", indicating an
explicit intention that the user be allowed to choose an empty regexp?
Seems like something like whether to allow "" should be up to the
caller, not to `read-regexp'.
 
It used to be the case, for instance, that you could enter empty input
for the `occur' regexp, and thus get all of the buffer lines in Occur
mode.  Whether or not that is something useful for `occur', you can
imagine that some caller of `read-regexp' might well want to allow for
reading an empty string as the user input.

In GNU Emacs 24.2.50.1 (i386-mingw-nt5.1.2600)
 of 2012-08-26 on MARVIN
Bzr revision: 109788 [email-masked]
Windowing system distributor `Microsoft Corp.', version 5.1.2600
Configured using:
 `configure --with-gcc (4.6) --no-opt --enable-checking --cflags
 -ID:/devel/emacs/libs/libXpm-3.5.8/include
 -ID:/devel/emacs/libs/libXpm-3.5.8/src
 -ID:/devel/emacs/libs/libpng-dev_1.4.3-1/include
 -ID:/devel/emacs/libs/zlib-dev_1.2.5-2/include
 -ID:/devel/emacs/libs/giflib-4.1.4-1/include
 -ID:/devel/emacs/libs/jpeg-6b-4/include
 -ID:/devel/emacs/libs/tiff-3.8.2-1/include
 -ID:/devel/emacs/libs/gnutls-3.0.9/include
 -ID:/devel/emacs/libs/libiconv-1.13.1-1-dev/include
 -ID:/devel/emacs/libs/libxml2-2.7.8/include/libxml2'