Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bug#14405: 24.3.50; read-regexp-defaults-function

1 view
Skip to first unread message

Juri Linkov

unread,
May 14, 2013, 7:51:20 PM5/14/13
to 14...@debbugs.gnu.org
This is a continuation from bug#13892 and bug#13687
closed and archived two months ago. As the unfinished
discussion indicates there are pending enhancements
to create a single option to define the same defaulting
behavior for all regexp-reading commands and to group
the existing defaults to separate functions that can be
overridden by customizing that option.

This is a preliminary implementation:

=== modified file 'lisp/replace.el'
--- lisp/replace.el 2013-04-22 04:17:30 +0000
+++ lisp/replace.el 2013-05-14 23:50:29 +0000
@@ -580,6 +580,41 @@ (defvar regexp-history nil
(defvar occur-collect-regexp-history '("\\1")
"History of regexp for occur's collect operation")

+(defcustom read-regexp-defaults-function nil
+ "Function that provides default regexp(s) for regexp reading commands.
+This function should take no arguments and return one of nil, a
+regexp or a list of regexps. The return value of this function is used
+as DEFAULTS param of `read-regexp'. This function is called only during
+interactive use.
+
+You can customize `read-regexp-defaults-function' to the value
+`find-tag-default-as-regexp' to highlight a symbol at point."
+ :type '(choice
+ (const :tag "No default regexp reading function" nil)
+ (choice :tag "Function to provide default for read-regexp"
+ (function-item :tag "Tag at point" find-tag-default)
+ (function-item :tag "Symbol at point" find-tag-default-as-regexp)
+ (function-item :tag "Latest history" (lambda () (car regexp-history)))
+ function))
+ :group 'matching
+ :version "24.4")
+
+(defun read-regexp-defaults ()
+ (if (functionp read-regexp-defaults-function)
+ (funcall read-regexp-defaults-function)))
+
+(defun read-regexp-defaults-history ()
+ "Return the latest regexp from `regexp-history'.
+See `read-regexp-defaults-function' for details."
+ (or (read-regexp-defaults)
+ (car regexp-history)))
+
+(defun read-regexp-defaults-tag ()
+ "Return the regexp that matches the default tag at point.
+See `read-regexp-defaults-function' for details."
+ (or (read-regexp-defaults)
+ (find-tag-default-as-regexp)))
+
(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 \": \".
@@ -636,7 +671,7 @@ (defun keep-lines-read-args (prompt)
"Read arguments for `keep-lines' and friends.
Prompt for a regexp with PROMPT.
Value is a list, (REGEXP)."
- (list (read-regexp prompt) nil nil t))
+ (list (read-regexp prompt (read-regexp-defaults-history)) nil nil t))

(defun keep-lines (regexp &optional rstart rend interactive)
"Delete all lines except those containing matches for REGEXP.
@@ -1143,32 +1178,35 @@ (defcustom occur-excluded-properties
:group 'matching
:version "22.1")

-(defvar occur-read-regexp-defaults-function
- 'occur-read-regexp-defaults
+(defun occur-read-regexp-defaults ()
"Function that provides default regexp(s) for occur commands.
-This function should take no arguments and return one of nil, a
+This function takes no arguments and returns one of nil, a
regexp or a list of regexps for use with occur commands -
`occur', `multi-occur' and `multi-occur-in-matching-buffers'.
The return value of this function is used as DEFAULTS param of
`read-regexp' while executing the occur command. This function
is called only during interactive use.

-For example, to check for occurrence of symbol at point use
-
- \(setq occur-read-regexp-defaults-function
- 'find-tag-default-as-regexp\).")
-
-(defun occur-read-regexp-defaults ()
- "Return the latest regexp from `regexp-history'.
-See `occur-read-regexp-defaults-function' for details."
- (car regexp-history))
+You can customize `read-regexp-defaults-function' to the value
+`find-tag-default-as-regexp' to highlight a symbol at point."
+ (read-regexp-defaults-history))
+
+(defun occur-collect-read-regexp-defaults ()
+ "Return the latest regexp from `occur-collect-regexp-history'.
+This function is used to read a regexp to collect.
+See `read-regexp-defaults-function' for details.
+
+You can customize `read-regexp-defaults-function' to the value
+`find-tag-default-as-regexp' to highlight a symbol at point."
+ (or (read-regexp-defaults)
+ (car occur-collect-regexp-history)))

(defun occur-read-primary-args ()
(let* ((perform-collect (consp current-prefix-arg))
(regexp (read-regexp (if perform-collect
"Collect strings matching regexp"
"List lines matching regexp")
- (funcall occur-read-regexp-defaults-function))))
+ (occur-read-regexp-defaults))))
(list regexp
(if perform-collect
;; Perform collect operation
@@ -1176,7 +1214,7 @@ (defun occur-read-primary-args ()
;; No subexpression so collect the entire match.
"\\&"
;; Get the regexp for collection pattern.
- (let ((default (car occur-collect-regexp-history)))
+ (let ((default (occur-collect-read-regexp-defaults)))
(read-regexp
(format "Regexp to collect (default %s): " default)
default 'occur-collect-regexp-history)))

=== modified file 'lisp/hi-lock.el'
--- lisp/hi-lock.el 2013-04-22 04:17:30 +0000
+++ lisp/hi-lock.el 2013-05-14 23:50:37 +0000
@@ -279,26 +279,6 @@ (defvar hi-lock-map
map)
"Key map for hi-lock.")

-(defvar hi-lock-read-regexp-defaults-function
- 'hi-lock-read-regexp-defaults
- "Function that provides default regexp(s) for highlighting commands.
-This function should take no arguments and return one of nil, a
-regexp or a list of regexps for use with highlighting commands -
-`hi-lock-face-phrase-buffer', `hi-lock-line-face-buffer' and
-`hi-lock-face-buffer'. The return value of this function is used
-as DEFAULTS param of `read-regexp' while executing the
-highlighting command. This function is called only during
-interactive use.
-
-For example, to highlight at symbol at point use
-
- \(setq hi-lock-read-regexp-defaults-function
- 'find-tag-default-as-regexp\)
-
-If you need different defaults for different highlighting
-operations, use `this-command' to identify the command under
-execution.")
-
;; Visible Functions

;;;###autoload
@@ -422,7 +402,7 @@ (defalias 'highlight-lines-matching-rege
(defun hi-lock-line-face-buffer (regexp &optional face)
"Set face of all lines containing a match of REGEXP to FACE.
Interactively, prompt for REGEXP then FACE. Use
-`hi-lock-read-regexp-defaults-function' to retrieve default
+`hi-lock-read-regexp-defaults' to retrieve default
value(s) of REGEXP. Use the global history list for FACE.

Use Font lock mode, if enabled, to highlight REGEXP. Otherwise,
@@ -432,7 +412,7 @@ (defun hi-lock-line-face-buffer (regexp
(list
(hi-lock-regexp-okay
(read-regexp "Regexp to highlight line"
- (funcall hi-lock-read-regexp-defaults-function)))
+ (hi-lock-read-regexp-defaults)))
(hi-lock-read-face-name)))
(or (facep face) (setq face 'hi-yellow))
(unless hi-lock-mode (hi-lock-mode 1))
@@ -448,7 +428,7 @@ (defalias 'highlight-regexp 'hi-lock-fac
(defun hi-lock-face-buffer (regexp &optional face)
"Set face of each match of REGEXP to FACE.
Interactively, prompt for REGEXP then FACE. Use
-`hi-lock-read-regexp-defaults-function' to retrieve default
+`hi-lock-read-regexp-defaults' to retrieve default
value(s) REGEXP. Use the global history list for FACE.

Use Font lock mode, if enabled, to highlight REGEXP. Otherwise,
@@ -458,7 +438,7 @@ (defun hi-lock-face-buffer (regexp &opti
(list
(hi-lock-regexp-okay
(read-regexp "Regexp to highlight"
- (funcall hi-lock-read-regexp-defaults-function)))
+ (hi-lock-read-regexp-defaults)))
(hi-lock-read-face-name)))
(or (facep face) (setq face 'hi-yellow))
(unless hi-lock-mode (hi-lock-mode 1))
@@ -470,7 +450,7 @@ (defalias 'highlight-phrase 'hi-lock-fac
(defun hi-lock-face-phrase-buffer (regexp &optional face)
"Set face of each match of phrase REGEXP to FACE.
Interactively, prompt for REGEXP then FACE. Use
-`hi-lock-read-regexp-defaults-function' to retrieve default
+`hi-lock-read-regexp' to retrieve default
value(s) of REGEXP. Use the global history list for FACE. When
called interactively, replace whitespace in user provided regexp
with arbitrary whitespace and make initial lower-case letters
@@ -484,7 +464,7 @@ (defun hi-lock-face-phrase-buffer (regex
(hi-lock-regexp-okay
(hi-lock-process-phrase
(read-regexp "Phrase to highlight"
- (funcall hi-lock-read-regexp-defaults-function))))
+ (hi-lock-read-regexp-defaults))))
(hi-lock-read-face-name)))
(or (facep face) (setq face 'hi-yellow))
(unless hi-lock-mode (hi-lock-mode 1))
@@ -651,9 +631,22 @@ (defun hi-lock-regexp-okay (regexp)
regexp))

(defun hi-lock-read-regexp-defaults ()
- "Return the latest regexp from `regexp-history'.
-See `hi-lock-read-regexp-defaults-function' for details."
- (car regexp-history))
+ "Function that provides default regexp(s) for highlighting commands.
+This function takes no arguments and returns one of nil, a
+regexp or a list of regexps for use with highlighting commands -
+`hi-lock-face-phrase-buffer', `hi-lock-line-face-buffer' and
+`hi-lock-face-buffer'. The return value of this function is used
+as DEFAULTS param of `read-regexp' while executing the
+highlighting command. This function is called only during
+interactive use.
+
+You can customize `read-regexp-defaults-function' to the value
+`find-tag-default-as-regexp' to highlight a symbol at point.
+
+If you need different defaults for different highlighting
+operations, redefine this function and use `this-command'
+to identify the command under execution."
+ (read-regexp-defaults-history))

(defun hi-lock-read-face-name ()
"Return face for interactive highlighting.

=== modified file 'lisp/progmodes/grep.el'
--- lisp/progmodes/grep.el 2013-02-12 07:57:04 +0000
+++ lisp/progmodes/grep.el 2013-05-14 23:50:07 +0000
@@ -829,9 +829,13 @@ (defun grep-expand-template (template &o
"")
t t command))))))

+(defun grep-read-regexp-defaults ()
+ (or (read-regexp-defaults)
+ (grep-tag-default)))
+
(defun grep-read-regexp ()
"Read regexp arg for interactive grep."
- (let ((default (grep-tag-default)))
+ (let ((default (grep-read-regexp-defaults)))
(read-regexp
(concat "Search for"
(if (and default (> (length default) 0))




Juri Linkov

unread,
May 15, 2013, 6:58:33 PM5/15/13
to 14...@debbugs.gnu.org
> There are pending enhancements to create a single option to define
> the same defaulting behavior for all regexp-reading commands
> and to group the existing defaults to separate functions
> that can be overridden by customizing that option.

But note that the option `read-regexp-defaults-function' has
limited usefulness. For example, in case when someone wants
`M-x man' to provide the default value not an entry near point
but the last element from the history, there is no way to do
this by customizing a common option because `man' uses the
specific variable `Man-topic-history'.

I see the only way to customize this by using `advice' like:

(advice-add 'Man-default-man-entry :override
(lambda () (car Man-topic-history)))

or even two default values (the last history and the original):

(advice-add 'Man-default-man-entry :filter-return
(lambda (r) (delq nil (list (car Man-topic-history) r))))

If advices are the preferable method of customization then the
design goal would be to add more functions returning default values
that users can customize using advices. For example, for `grep'
there is already `grep-tag-default' that could be customized like:

(advice-add 'grep-tag-default :override
(lambda () (car grep-regexp-history)))

as a better advice for
http://stackoverflow.com/questions/15161592/make-emacs-rgrep-default-to-last-search-term-rather-than-word-at-point

Other commands require adding more functions for advice-based customization
like `occur-read-regexp-defaults' and `hi-lock-read-regexp-defaults'.



Juri Linkov

unread,
May 18, 2013, 7:28:14 PM5/18/13
to 14...@debbugs.gnu.org
> +(defcustom read-regexp-defaults-function nil
> + :type '(choice
> + (const :tag "No default regexp reading function" nil)
> + (choice :tag "Function to provide default for read-regexp"
> + (function-item :tag "Tag at point" find-tag-default)
> + (function-item :tag "Symbol at point" find-tag-default-as-regexp)
> + (function-item :tag "Latest history" (lambda () (car regexp-history)))

Actually there are two problems in this defcustom:

1. `find-tag-default' doesn't return a regexp. To fix this problem,
we need two functions returning a regexp: as a non-symbol regexp and
as a symbol regexp. I propose the following implementations:

(defun find-tag-default-as-regexp ()
"Return regexp that matches the default tag at point."
(let ((tag (funcall (or find-tag-default-function
(get major-mode 'find-tag-default-function)
'find-tag-default))))
(if tag (regexp-quote tag))))

(defun find-tag-default-as-symbol-regexp ()
"Return regexp that matches the default tag at point as symbol."
(let ((tag-regexp (find-tag-default-as-regexp)))
(if (and tag-regexp
(eq (or find-tag-default-function
(get major-mode 'find-tag-default-function)
'find-tag-default)
'find-tag-default))
(format "\\_<%s\\_>" tag-regexp)
tag-regexp)))

2. The second problem is that `(lambda () (car regexp-history))'
can't be used in defcustom because many commands use history other than
`regexp-history'.

As a possible solution we could add a new special symbol
`default-last-history' to handle it in `read-regexp' that
has access to the actual value of the history variable in its arg
`history' using `(symbol-value (or history 'regexp-history))' below:

=== modified file 'lisp/replace.el'
--- lisp/replace.el 2013-03-24 21:47:52 +0000
+++ lisp/replace.el 2013-05-18 23:27:38 +0000
@@ -580,6 +580,24 @@ (defvar regexp-history nil
(defvar occur-collect-regexp-history '("\\1")
"History of regexp for occur's collect operation")

+(defcustom read-regexp-defaults-function nil
+ "Function that provides default regexp(s) for regexp reading commands."
+ :type '(choice
+ (const :tag "No default regexp reading function" nil)
+ (const :tag "Latest history" default-last-history)
+ (function-item :tag "Tag at point" find-tag-default-as-regexp)
+ (function-item :tag "Symbol at point" find-tag-default-as-symbol-regexp)
+ (function :tag "Function to provide default for read-regexp"))
+ :group 'matching
+ :version "24.4")
+
(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 \": \".
@@ -591,13 +609,23 @@ (defun read-regexp (prompt &optional def

Optional arg 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))
+ (let* ((defaults
+ (if (and defaults (symbolp defaults))
+ (cond
+ ((eq (or read-regexp-defaults-function defaults)
+ 'default-last-history)
+ (car (symbol-value (or history 'regexp-history))))
+ ((functionp (or read-regexp-defaults-function defaults))
+ (funcall (or read-regexp-defaults-function defaults))))
+ defaults))
+ (default (if (consp defaults) (car defaults) defaults))
(suggestions (if (listp defaults) defaults (list defaults)))
(suggestions
(append
suggestions
(list
(find-tag-default-as-regexp)
+ (find-tag-default-as-symbol-regexp)
(car regexp-search-ring)
(regexp-quote (or (car search-ring) ""))
(car (symbol-value query-replace-from-history-variable)))))
@@ -1143,9 +1170,9 @@
(defun occur-read-primary-args ()
(let* ((perform-collect (consp current-prefix-arg))
(regexp (read-regexp (if perform-collect
"Collect strings matching regexp"
"List lines matching regexp")
- (funcall occur-read-regexp-defaults-function))))
+ 'default-last-history)))
(list regexp
(if perform-collect
;; Perform collect operation

=== modified file 'lisp/hi-lock.el'
--- lisp/hi-lock.el 2013-03-31 13:34:35 +0000
+++ lisp/hi-lock.el 2013-05-18 23:27:23 +0000
@@ -431,8 +411,7 @@ (defun hi-lock-line-face-buffer (regexp
(interactive
(list
(hi-lock-regexp-okay
- (read-regexp "Regexp to highlight line"
- (funcall hi-lock-read-regexp-defaults-function)))
+ (read-regexp "Regexp to highlight line" 'default-last-history))
(hi-lock-read-face-name)))
(or (facep face) (setq face 'hi-yellow))
(unless hi-lock-mode (hi-lock-mode 1))
@@ -457,8 +436,7 @@ (defun hi-lock-face-buffer (regexp &opti
(interactive
(list
(hi-lock-regexp-okay
- (read-regexp "Regexp to highlight"
- (funcall hi-lock-read-regexp-defaults-function)))
+ (read-regexp "Regexp to highlight" 'default-last-history))
(hi-lock-read-face-name)))
(or (facep face) (setq face 'hi-yellow))
(unless hi-lock-mode (hi-lock-mode 1))
@@ -483,8 +481,7 @@ (defun hi-lock-face-phrase-buffer (regex
(list
(hi-lock-regexp-okay
(hi-lock-process-phrase
- (read-regexp "Phrase to highlight"
- (funcall hi-lock-read-regexp-defaults-function))))
+ (read-regexp "Phrase to highlight" 'default-last-history)))
(hi-lock-read-face-name)))
(or (facep face) (setq face 'hi-yellow))
(unless hi-lock-mode (hi-lock-mode 1))

=== modified file 'lisp/progmodes/grep.el'
--- lisp/progmodes/grep.el 2013-02-12 04:46:18 +0000
+++ lisp/progmodes/grep.el 2013-05-18 23:23:30 +0000
@@ -817,12 +831,7 @@ (defun grep-expand-template (template &o

(defun grep-read-regexp ()
"Read regexp arg for interactive grep."
- (let ((default (grep-tag-default)))
- (read-regexp
- (concat "Search for"
- (if (and default (> (length default) 0))
- (format " (default \"%s\"): " default) ": "))
- default 'grep-regexp-history)))
+ (read-regexp "Search for" 'grep-tag-default 'grep-regexp-history))

(defun grep-read-files (regexp)
"Read files arg for interactive grep."




Jambunathan K

unread,
Dec 20, 2013, 9:39:45 PM12/20/13
to Juri Linkov, 14...@debbugs.gnu.org

My workflow is M-s o searches for symbol at point. Your recent commit
revno: 115651 closed this behaviour for me. Just wanted to check
whether you overlooked the following diff.

The following works for me.

(custom-set-variables
'(read-regexp-defaults-function
(quote find-tag-default-as-symbol-regexp)))

Juri Linkov

unread,
Dec 21, 2013, 4:25:41 PM12/21/13
to Jambunathan K, 14...@debbugs.gnu.org
> My workflow is M-s o searches for symbol at point. Your recent commit
> revno: 115651 closed this behaviour for me. Just wanted to check
> whether you overlooked the following diff.

For historical reasons, `M-s o' provides the previous history element as
the default. I had no intention to change this default (because I recall
there were objections to changing the default behavior). So if you want
`M-s o' to provide a symbol at point, you can customize
`read-regexp-defaults-function' to `find-tag-default-as-symbol-regexp'.



Jambunathan K

unread,
Dec 21, 2013, 5:28:40 PM12/21/13
to Juri Linkov, 14...@debbugs.gnu.org
The diff attached with my mail indicates what the problem is. Please
take a second look.



Juri Linkov

unread,
Dec 22, 2013, 4:39:10 PM12/22/13
to Jambunathan K, 14...@debbugs.gnu.org
> The diff attached with my mail indicates what the problem is. Please
> take a second look.

Maybe I'm missing something, maybe you're missing something,
but note that `read-regexp' already takes care about handling
`read-regexp-defaults-function' whose non-nil value
can override the default value in `occur-read-primary-args',
so no more additional changes are needed.



Jambunathan K

unread,
Dec 22, 2013, 9:38:22 PM12/22/13
to Juri Linkov, 14...@debbugs.gnu.org
Juri Linkov <ju...@jurta.org> writes:

> so no more additional changes are needed

True. Sorry, my error.



Jambunathan K

unread,
Dec 24, 2013, 1:25:26 PM12/24/13
to Juri Linkov, 14...@debbugs.gnu.org

I depend on occur and rgrep. For occur, I want the symbol delimiters.
For rgrep, a mere tag at point will do. If one were to use multi-occur
from Buffer menu mode, a symbol match doesn't make sense.

Just a note. I can fix .el files locally.



Juri Linkov

unread,
Dec 25, 2013, 3:56:30 PM12/25/13
to Jambunathan K, 14...@debbugs.gnu.org
> I depend on occur and rgrep. For occur, I want the symbol delimiters.
> For rgrep, a mere tag at point will do. If one were to use multi-occur
> from Buffer menu mode, a symbol match doesn't make sense.

You can use `this-command' for different defaults of different commands
in a customized function like you wrote in the docstring that is
still preserved in `read-regexp-defaults-function'.



Jambunathan K

unread,
Dec 27, 2013, 12:22:31 AM12/27/13
to Juri Linkov, 14...@debbugs.gnu.org
For the benefit of googler out there.

(setq read-regexp-defaults-function
(lambda nil
(pcase this-command
(`rgrep (find-tag-default-as-regexp))
(_ (find-tag-default-as-symbol-regexp)))))

It would be a good idea to choose a different name for
find-tag-default-*? The function retrieves the tag-at-point but doesn't
find it (as in M-.). So "find" is misleading.



0 new messages