From 4fa467eefa6c8ef97a4e1526c809cdabeba98878 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Mon, 12 Feb 2018 23:39:28 +0200 Subject: [PATCH] * lisp/progmodes/grep.el (grep-num-matches-found): New variable. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit (grep-mode-line-matches): New defconst. (grep-mode-font-lock-keywords): Update the regexp for “Grep finished” to include the number of matches found. (grep-process-setup): Set grep-num-matches-found to 0. (grep-exit-message): New function with body moved from lambda in grep-process-setup. Use grep-num-matches-found to return the number of matches found. (grep-filter): Increment grep-num-matches-found. (grep-mode): Set compilation-mode-line-errors to grep-mode-line-matches. (Bug#30397, bug#14017) --- lisp/progmodes/grep.el | 46 +++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el index 9ce4ff84627..14e251e0667 100644 --- a/lisp/progmodes/grep.el +++ b/lisp/progmodes/grep.el @@ -425,6 +425,14 @@ See `compilation-error-regexp-alist' for format details.") (defvar grep-context-face 'shadow "Face name to use for grep context lines.") +(defvar grep-num-matches-found 0) + +(defconst grep-mode-line-matches + `(" [" (:propertize (:eval (int-to-string grep-num-matches-found)) + face ,grep-hit-face + help-echo "Number of matches so far") + "]")) + (defvar grep-mode-font-lock-keywords '(;; Command output lines. (": \\(.+\\): \\(?:Permission denied\\|No such \\(?:file or directory\\|device or address\\)\\)$" @@ -432,7 +440,7 @@ See `compilation-error-regexp-alist' for format details.") ;; remove match from grep-regexp-alist before fontifying ("^Grep[/a-zA-z]* started.*" (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t)) - ("^Grep[/a-zA-z]* finished \\(?:(\\(matches found\\))\\|with \\(no matches found\\)\\).*" + ("^Grep[/a-zA-z]* finished with \\(?:\\(\\(?:[0-9]+ \\)?matches found\\)\\|\\(no matches found\\)\\).*" (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t) (1 compilation-info-face nil t) (2 compilation-warning-face nil t)) @@ -503,21 +511,28 @@ Set up `compilation-exit-message-function' and run `grep-setup-hook'." (setenv "GREP_COLOR" "01;31") ;; GREP_COLORS is used in GNU grep 2.5.2 and later versions (setenv "GREP_COLORS" "mt=01;31:fn=:ln=:bn=:se=:sl=:cx=:ne")) + (setq-local grep-num-matches-found 0) (set (make-local-variable 'compilation-exit-message-function) - (lambda (status code msg) - (if (eq status 'exit) - ;; This relies on the fact that `compilation-start' - ;; sets buffer-modified to nil before running the command, - ;; so the buffer is still unmodified if there is no output. - (cond ((and (zerop code) (buffer-modified-p)) - '("finished (matches found)\n" . "matched")) - ((not (buffer-modified-p)) - '("finished with no matches found\n" . "no match")) - (t - (cons msg code))) - (cons msg code)))) + 'grep-exit-message) (run-hooks 'grep-setup-hook)) +(defun grep-exit-message (status code msg) + "Return a status message for grep results." + (if (eq status 'exit) + ;; This relies on the fact that `compilation-start' + ;; sets buffer-modified to nil before running the command, + ;; so the buffer is still unmodified if there is no output. + (cond ((and (zerop code) (buffer-modified-p)) + (if (> grep-num-matches-found 0) + (cons (format "finished with %d matches found\n" grep-num-matches-found) + "matched") + '("finished with matches found\n" . "matched"))) + ((not (buffer-modified-p)) + '("finished with no matches found\n" . "no match")) + (t + (cons msg code))) + (cons msg code))) + (defun grep-filter () "Handle match highlighting escape sequences inserted by the grep process. This function is called from `compilation-filter-hook'." @@ -535,7 +550,8 @@ This function is called from `compilation-filter-hook'." (while (re-search-forward "\033\\[0?1;31m\\(.*?\\)\033\\[[0-9]*m" end 1) (replace-match (propertize (match-string 1) 'face nil 'font-lock-face grep-match-face) - t t)) + t t) + (cl-incf grep-num-matches-found)) ;; Delete all remaining escape sequences (goto-char beg) (while (re-search-forward "\033\\[[0-9;]*[mK]" end 1) @@ -775,6 +791,8 @@ This function is called from `compilation-filter-hook'." grep-hit-face) (set (make-local-variable 'compilation-error-regexp-alist) grep-regexp-alist) + (set (make-local-variable 'compilation-mode-line-errors) + grep-mode-line-matches) ;; compilation-directory-matcher can't be nil, so we set it to a regexp that ;; can never match. (set (make-local-variable 'compilation-directory-matcher) '("\\`a\\`")) -- 2.39.2