Subject: 24.3.50; read-regexp-defaults-function

From: Juri Linkov <[email-masked]>
Date: Wed, 15 May 2013 02:51:20 +0300

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))