From 24da72738b54b7a247bf14dd704166056af815bf Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Fri, 20 Dec 2013 21:55:56 +0200 Subject: [PATCH] * lisp/replace.el (read-regexp-defaults-function): New defcustom. (read-regexp-suggestions): New function. (read-regexp): Use `read-regexp-defaults-function' to get default values. Use `read-regexp-suggestions'. Add non-empty default to history for empty input. (occur-read-regexp-defaults-function): Remove function. (occur-read-primary-args): Use `regexp-history-last' instead of `occur-read-regexp-defaults-function'. * lisp/hi-lock.el (hi-lock-read-regexp-defaults-function): Remove function. (hi-lock-line-face-buffer, hi-lock-face-buffer) (hi-lock-face-phrase-buffer): Use `regexp-history-last' instead of `hi-lock-read-regexp-defaults-function'. Doc fix. (hi-lock-face-symbol-at-point): Replace `find-tag-default-as-regexp' with `find-tag-default-as-symbol-regexp'. Doc fix. (hi-lock-read-regexp-defaults): Remove function. (hi-lock-regexp-okay): Add check for null. * lisp/progmodes/grep.el (grep-read-regexp): Use `grep-tag-default' for the arg DEFAULTS. Move formatting of the prompt to `read-regexp'. * lisp/subr.el (find-tag-default-as-symbol-regexp): New function. (find-tag-default-as-regexp): Move symbol regexp formatting to `find-tag-default-as-symbol-regexp'. Fixes: debbugs:14405 --- etc/NEWS | 11 ++++- lisp/ChangeLog | 27 +++++++++++++ lisp/hi-lock.el | 55 +++++++------------------ lisp/progmodes/grep.el | 7 +--- lisp/replace.el | 92 ++++++++++++++++++++++++++---------------- lisp/subr.el | 28 +++++++++---- 6 files changed, 130 insertions(+), 90 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 57516bfafb4..4a5a3f2b243 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -331,9 +331,18 @@ Use `electric-indent-mode' instead. ** jit-lock-debug-mode lets you use the debuggers on code run via jit-lock. -** completing-read-multiple's separator can now be a regexp. +** Minibuffer + +*** completing-read-multiple's separator can now be a regexp. The default separator is changed to allow surrounding spaces around the comma. +*** New option `read-regexp-defaults-function' defines the function +that provides the default value for commands that ask for a regexp: +`rgrep', `lgrep' `occur', `multi-occur', `multi-occur-in-matching-buffers', +`highlight-regexp', `highlight-lines-matching-regexp', `highlight-phrase'. +This option can be customized to values that provide a default value +from the regexp last history element or from the symbol found at point. + ** The backtrace debugger and local variables: *** The debugger's `e' command evaluates the code in the context at point. This includes using the lexical environment at point, which means that diff --git a/lisp/ChangeLog b/lisp/ChangeLog index a5447fb5624..ca34fd6e482 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,30 @@ +2013-12-20 Juri Linkov + + * replace.el (read-regexp-defaults-function): New defcustom (bug#14405). + (read-regexp-suggestions): New function. + (read-regexp): Use `read-regexp-defaults-function' to get default values. + Use `read-regexp-suggestions'. Add non-empty default to history + for empty input. + (occur-read-regexp-defaults-function): Remove function. + (occur-read-primary-args): Use `regexp-history-last' instead of + `occur-read-regexp-defaults-function'. + + * hi-lock.el (hi-lock-read-regexp-defaults-function): Remove function. + (hi-lock-line-face-buffer, hi-lock-face-buffer) + (hi-lock-face-phrase-buffer): Use `regexp-history-last' instead of + `hi-lock-read-regexp-defaults-function'. Doc fix. + (hi-lock-face-symbol-at-point): Replace `find-tag-default-as-regexp' + with `find-tag-default-as-symbol-regexp'. Doc fix. + (hi-lock-read-regexp-defaults): Remove function. + (hi-lock-regexp-okay): Add check for null. + + * progmodes/grep.el (grep-read-regexp): Use `grep-tag-default' for + the arg DEFAULTS. Move formatting of the prompt to `read-regexp'. + + * subr.el (find-tag-default-as-symbol-regexp): New function. + (find-tag-default-as-regexp): Move symbol regexp formatting to + `find-tag-default-as-symbol-regexp'. + 2013-12-20 E Sabof (tiny change) * hi-lock.el (hi-lock-set-pattern): Check for `font-lock-specified-p'. diff --git a/lisp/hi-lock.el b/lisp/hi-lock.el index bb5c54e7152..53855e99395 100644 --- a/lisp/hi-lock.el +++ b/lisp/hi-lock.el @@ -284,26 +284,6 @@ a library is being loaded.") 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 @@ -431,7 +411,7 @@ versions before 22 use the following in your init file: (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 +`read-regexp-defaults-function' to customize default value(s) of REGEXP. Use the global history list for FACE. Use Font lock mode, if enabled, to highlight REGEXP. Otherwise, @@ -440,8 +420,7 @@ highlighting will not update as you type." (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" 'regexp-history-last)) (hi-lock-read-face-name))) (or (facep face) (setq face 'hi-yellow)) (unless hi-lock-mode (hi-lock-mode 1)) @@ -457,8 +436,8 @@ highlighting will not update as you type." (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 -value(s) REGEXP. Use the global history list for FACE. +`read-regexp-defaults-function' to customize default +value(s) of REGEXP. Use the global history list for FACE. Use Font lock mode, if enabled, to highlight REGEXP. Otherwise, use overlays for highlighting. If overlays are used, the @@ -466,8 +445,7 @@ highlighting will not update as you type." (interactive (list (hi-lock-regexp-okay - (read-regexp "Regexp to highlight" - (funcall hi-lock-read-regexp-defaults-function))) + (read-regexp "Regexp to highlight" 'regexp-history-last)) (hi-lock-read-face-name))) (or (facep face) (setq face 'hi-yellow)) (unless hi-lock-mode (hi-lock-mode 1)) @@ -479,7 +457,7 @@ highlighting will not update as you type." (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 +`read-regexp-defaults-function' to customize 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 @@ -492,8 +470,7 @@ highlighting will not update as you type." (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" 'regexp-history-last))) (hi-lock-read-face-name))) (or (facep face) (setq face 'hi-yellow)) (unless hi-lock-mode (hi-lock-mode 1)) @@ -504,7 +481,7 @@ highlighting will not update as you type." ;;;###autoload (defun hi-lock-face-symbol-at-point () "Set face of each match of the symbol at point. -Use `find-tag-default-as-regexp' to retrieve the symbol at point. +Use `find-tag-default-as-symbol-regexp' to retrieve the symbol at point. Use non-nil `hi-lock-auto-select-face' to retrieve the next face from `hi-lock-face-defaults' automatically. @@ -513,7 +490,7 @@ Otherwise, use overlays for highlighting. If overlays are used, the highlighting will not update as you type." (interactive) (let* ((regexp (hi-lock-regexp-okay - (find-tag-default-as-regexp))) + (find-tag-default-as-symbol-regexp))) (hi-lock-auto-select-face t) (face (hi-lock-read-face-name))) (or (facep face) (setq face 'hi-yellow)) @@ -676,14 +653,12 @@ and initial lower-case letters made case insensitive." Otherwise signal an error. A pattern that matches the null string is not suitable." - (if (string-match regexp "") - (error "Regexp cannot match an empty string") - 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)) + (cond + ((null regexp) + (error "Regexp cannot match nil")) + ((string-match regexp "") + (error "Regexp cannot match an empty string")) + (t regexp))) (defun hi-lock-read-face-name () "Return face for interactive highlighting. diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el index e63e29df37d..ed443bee640 100644 --- a/lisp/progmodes/grep.el +++ b/lisp/progmodes/grep.el @@ -819,12 +819,7 @@ substitution string. Note dynamic scoping of variables.") (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." diff --git a/lisp/replace.el b/lisp/replace.el index 42f7f3c900a..92191e42a76 100644 --- a/lisp/replace.el +++ b/lisp/replace.el @@ -626,6 +626,44 @@ of `history-length', which see.") (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. + +If you need different defaults for different commands, +use `this-command' to identify the command under execution. + +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) + (const :tag "Latest regexp history" regexp-history-last) + (function-item :tag "Tag at point" + find-tag-default) + (function-item :tag "Tag at point as regexp" + find-tag-default-as-regexp) + (function-item :tag "Tag at point as symbol regexp" + find-tag-default-as-symbol-regexp) + (function :tag "Function to provide default for read-regexp")) + :group 'matching + :version "24.4") + +(defun read-regexp-suggestions () + "Return a list of standard suggestions for `read-regexp'. +By default, the list includes the tag at point, the last isearch regexp, +the last isearch string, and the last replacement regexp. `read-regexp' +appends the list returned by this function to the end of values available +via \\\\[next-history-element]." + (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)))) + (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 \": \". @@ -637,29 +675,31 @@ is returned as the default value when the user enters empty input. SUGGESTIONS is a list of strings that can be inserted into the minibuffer using \\\\[next-history-element]. \ The values supplied in SUGGESTIONS -are prepended to the list of standard suggestions that include -the tag at point, the last isearch regexp, the last isearch string, -and the last replacement regexp. +are prepended to the list of standard suggestions returned by +`read-regexp-suggestions'. The default values can be customized +by `read-regexp-defaults-function'. 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) + 'regexp-history-last) + (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) - (car regexp-search-ring) - (regexp-quote (or (car search-ring) "")) - (car (symbol-value query-replace-from-history-variable))))) + (suggestions (append suggestions (read-regexp-suggestions))) (suggestions (delete-dups (delq nil (delete "" suggestions)))) ;; Do not automatically add default to the history for empty input. (history-add-new-input nil) (input (read-from-minibuffer (cond ((string-match-p ":[ \t]*\\'" prompt) prompt) - (default + ((and default (> (length default) 0)) (format "%s (default %s): " prompt (query-replace-descr default))) (t @@ -667,7 +707,9 @@ If HISTORY is nil, `regexp-history' is used." nil nil nil (or history 'regexp-history) suggestions t))) (if (equal input "") ;; Return the default value when the user enters empty input. - (or default input) + (prog1 (or default input) + (when default + (add-to-history (or history 'regexp-history) default))) ;; Otherwise, add non-empty input to the history and return input. (prog1 input (add-to-history (or history 'regexp-history) input))))) @@ -1192,32 +1234,12 @@ which means to discard all text properties." :group 'matching :version "22.1") -(defvar occur-read-regexp-defaults-function - '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 -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)) - (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)))) + 'regexp-history-last))) (list regexp (if perform-collect ;; Perform collect operation diff --git a/lisp/subr.el b/lisp/subr.el index d86469dd158..2a2068ff295 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -2540,14 +2540,26 @@ If there is no tag at point, return nil. When in a major mode that does not provide its own `find-tag-default-function', return a regexp that matches the symbol at point exactly." - (let* ((tagf (or find-tag-default-function - (get major-mode 'find-tag-default-function) - 'find-tag-default)) - (tag (funcall tagf))) - (cond ((null tag) nil) - ((eq tagf 'find-tag-default) - (format "\\_<%s\\_>" (regexp-quote tag))) - (t (regexp-quote tag))))) + (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. +If there is no tag at point, return nil. + +When in a major mode that does not provide its own +`find-tag-default-function', return a regexp that matches the +symbol at point exactly." + (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))) (defun play-sound (sound) "SOUND is a list of the form `(sound KEYWORD VALUE...)'. -- 2.39.2