From 9b4b4a8355506a0253d8a4943e0a9aa87f9e92eb Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Fri, 26 Jun 2015 20:21:50 +0300 Subject: [PATCH] Add --color Grep option to the command dynamically * lisp/progmodes/grep.el (grep-template, grep-find-template): Update the description for . (Bug#20728) (grep-compute-defaults): Don't add the --color option to grep-options. Only add it to grep-command. (grep-expand-keywords): Expand the env value opts into . (grep-expand-template): Replace cf in the env with the opts list, that can include -i and --color. * lisp/progmodes/xref.el (xref-collect-matches): Do not remove "--color=always" from the template, because we don't have to. --- etc/NEWS | 9 +++++++++ lisp/progmodes/grep.el | 35 +++++++++++++++++++++++------------ lisp/progmodes/xref.el | 9 ++------- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 84636552973..1f8cbbc1b98 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -883,6 +883,15 @@ word syntax, use `\sw' instead. ** The `diff' command uses the unified format now. To restore the old behavior, set `diff-switches' to `-c'. +** `grep-template' and `grep-find-template' values don't include the +--color argument anymore. It's added at the place holder position +dynamically. + +** `grep-template' and `grep-find-template' values don't include the +--color argument anymore. It's added at the place holder position +dynamically. Any third-party code that changes these templates should +be updated accordingly. + * Lisp Changes in Emacs 25.1 diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el index cc6662f3bf3..e20e5bd4c40 100644 --- a/lisp/progmodes/grep.el +++ b/lisp/progmodes/grep.el @@ -130,7 +130,7 @@ Customize or call the function `grep-apply-setting'." (defcustom grep-template nil "The default command to run for \\[lgrep]. The following place holders should be present in the string: - - place to put -i if case insensitive grep. + - place to put the options like -i and --color. - file names and wildcards to search. - file names and wildcards to exclude. - the regular expression searched for. @@ -177,7 +177,7 @@ The following place holders should be present in the string: - base directory for find - find options to restrict or expand the directory list - find options to limit the files matched - - place to put -i if case insensitive grep + - place to put the grep options like -i and --color - the regular expression searched for. In interactive usage, the actual value of this variable is set up by `grep-compute-defaults'; to change the default value, use @@ -572,20 +572,22 @@ This function is called from `compilation-filter-hook'." (unless (and grep-command grep-find-command grep-template grep-find-template) (let ((grep-options - (concat (and grep-highlight-matches - (grep-probe grep-program - `(nil nil nil "--color" "x" ,null-device) - nil 1) - (if (eq grep-highlight-matches 'always) - "--color=always " "--color ")) - (if grep-use-null-device "-n" "-nH") + (concat (if grep-use-null-device "-n" "-nH") (if (grep-probe grep-program `(nil nil nil "-e" "foo" ,null-device) nil 1) " -e")))) (unless grep-command (setq grep-command - (format "%s %s " grep-program grep-options))) + (format "%s %s %s " grep-program grep-options + (or + (and grep-highlight-matches + (grep-probe grep-program + `(nil nil nil "--color" "x" ,null-device) + nil 1) + (if (eq grep-highlight-matches 'always) + "--color=always" "--color")) + "")))) (unless grep-template (setq grep-template (format "%s %s " grep-program grep-options))) @@ -791,7 +793,7 @@ easily repeat a find command." ;; User-friendly interactive API. (defconst grep-expand-keywords - '(("" . (and cf (isearch-no-upper-case-p regexp t) "-i")) + '(("" . (mapconcat #'identity opts " ")) ("" . (or dir ".")) ("" . files) ("" . null-device) @@ -804,7 +806,16 @@ substitution string. Note dynamic scoping of variables.") (defun grep-expand-template (template &optional regexp files dir excl) "Patch grep COMMAND string replacing , , , , and ." (let* ((command template) - (env `((cf . ,case-fold-search) + (env `((opts . ,(let (opts) + (when (and case-fold-search + (isearch-no-upper-case-p regexp t)) + (push "-i" opts)) + (cond + ((eq grep-highlight-matches 'always) + (push "--color=always" opts)) + ((eq grep-highlight-matches 'auto) + (push "--color" opts))) + opts)) (excl . ,excl) (dir . ,dir) (files . ,files) diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el index 469f65d4fa6..50d52d01efe 100644 --- a/lisp/progmodes/xref.el +++ b/lisp/progmodes/xref.el @@ -749,13 +749,8 @@ tools are used, and when." (require 'semantic/fw) (grep-compute-defaults) (defvar grep-find-template) - (let* ((grep-find-template - (replace-regexp-in-string - ;; Override the use ot '--color=always' on MS-Windows. - "--color=always" "" - (replace-regexp-in-string "-e " "-E " - grep-find-template t t) - t t)) + (let* ((grep-find-template (replace-regexp-in-string "-e " "-E " + grep-find-template t t)) (command (rgrep-default-command (xref--regexp-to-extended regexp) "*.*" dir)) (orig-buffers (buffer-list))