diff options
| author | Juri Linkov | 2018-02-12 23:39:28 +0200 |
|---|---|---|
| committer | Juri Linkov | 2018-02-12 23:39:28 +0200 |
| commit | 4fa467eefa6c8ef97a4e1526c809cdabeba98878 (patch) | |
| tree | 7275f8b85e808287fc5c4f0b670bcae32c1ccb39 | |
| parent | a22820a31c191451334eec101125f7b621d6dbc0 (diff) | |
| download | emacs-4fa467eefa6c8ef97a4e1526c809cdabeba98878.tar.gz emacs-4fa467eefa6c8ef97a4e1526c809cdabeba98878.zip | |
* lisp/progmodes/grep.el (grep-num-matches-found): New variable.
(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)
| -rw-r--r-- | lisp/progmodes/grep.el | 46 |
1 files 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.") | |||
| 425 | (defvar grep-context-face 'shadow | 425 | (defvar grep-context-face 'shadow |
| 426 | "Face name to use for grep context lines.") | 426 | "Face name to use for grep context lines.") |
| 427 | 427 | ||
| 428 | (defvar grep-num-matches-found 0) | ||
| 429 | |||
| 430 | (defconst grep-mode-line-matches | ||
| 431 | `(" [" (:propertize (:eval (int-to-string grep-num-matches-found)) | ||
| 432 | face ,grep-hit-face | ||
| 433 | help-echo "Number of matches so far") | ||
| 434 | "]")) | ||
| 435 | |||
| 428 | (defvar grep-mode-font-lock-keywords | 436 | (defvar grep-mode-font-lock-keywords |
| 429 | '(;; Command output lines. | 437 | '(;; Command output lines. |
| 430 | (": \\(.+\\): \\(?:Permission denied\\|No such \\(?:file or directory\\|device or address\\)\\)$" | 438 | (": \\(.+\\): \\(?:Permission denied\\|No such \\(?:file or directory\\|device or address\\)\\)$" |
| @@ -432,7 +440,7 @@ See `compilation-error-regexp-alist' for format details.") | |||
| 432 | ;; remove match from grep-regexp-alist before fontifying | 440 | ;; remove match from grep-regexp-alist before fontifying |
| 433 | ("^Grep[/a-zA-z]* started.*" | 441 | ("^Grep[/a-zA-z]* started.*" |
| 434 | (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t)) | 442 | (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t)) |
| 435 | ("^Grep[/a-zA-z]* finished \\(?:(\\(matches found\\))\\|with \\(no matches found\\)\\).*" | 443 | ("^Grep[/a-zA-z]* finished with \\(?:\\(\\(?:[0-9]+ \\)?matches found\\)\\|\\(no matches found\\)\\).*" |
| 436 | (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t) | 444 | (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t) |
| 437 | (1 compilation-info-face nil t) | 445 | (1 compilation-info-face nil t) |
| 438 | (2 compilation-warning-face nil t)) | 446 | (2 compilation-warning-face nil t)) |
| @@ -503,21 +511,28 @@ Set up `compilation-exit-message-function' and run `grep-setup-hook'." | |||
| 503 | (setenv "GREP_COLOR" "01;31") | 511 | (setenv "GREP_COLOR" "01;31") |
| 504 | ;; GREP_COLORS is used in GNU grep 2.5.2 and later versions | 512 | ;; GREP_COLORS is used in GNU grep 2.5.2 and later versions |
| 505 | (setenv "GREP_COLORS" "mt=01;31:fn=:ln=:bn=:se=:sl=:cx=:ne")) | 513 | (setenv "GREP_COLORS" "mt=01;31:fn=:ln=:bn=:se=:sl=:cx=:ne")) |
| 514 | (setq-local grep-num-matches-found 0) | ||
| 506 | (set (make-local-variable 'compilation-exit-message-function) | 515 | (set (make-local-variable 'compilation-exit-message-function) |
| 507 | (lambda (status code msg) | 516 | 'grep-exit-message) |
| 508 | (if (eq status 'exit) | ||
| 509 | ;; This relies on the fact that `compilation-start' | ||
| 510 | ;; sets buffer-modified to nil before running the command, | ||
| 511 | ;; so the buffer is still unmodified if there is no output. | ||
| 512 | (cond ((and (zerop code) (buffer-modified-p)) | ||
| 513 | '("finished (matches found)\n" . "matched")) | ||
| 514 | ((not (buffer-modified-p)) | ||
| 515 | '("finished with no matches found\n" . "no match")) | ||
| 516 | (t | ||
| 517 | (cons msg code))) | ||
| 518 | (cons msg code)))) | ||
| 519 | (run-hooks 'grep-setup-hook)) | 517 | (run-hooks 'grep-setup-hook)) |
| 520 | 518 | ||
| 519 | (defun grep-exit-message (status code msg) | ||
| 520 | "Return a status message for grep results." | ||
| 521 | (if (eq status 'exit) | ||
| 522 | ;; This relies on the fact that `compilation-start' | ||
| 523 | ;; sets buffer-modified to nil before running the command, | ||
| 524 | ;; so the buffer is still unmodified if there is no output. | ||
| 525 | (cond ((and (zerop code) (buffer-modified-p)) | ||
| 526 | (if (> grep-num-matches-found 0) | ||
| 527 | (cons (format "finished with %d matches found\n" grep-num-matches-found) | ||
| 528 | "matched") | ||
| 529 | '("finished with matches found\n" . "matched"))) | ||
| 530 | ((not (buffer-modified-p)) | ||
| 531 | '("finished with no matches found\n" . "no match")) | ||
| 532 | (t | ||
| 533 | (cons msg code))) | ||
| 534 | (cons msg code))) | ||
| 535 | |||
| 521 | (defun grep-filter () | 536 | (defun grep-filter () |
| 522 | "Handle match highlighting escape sequences inserted by the grep process. | 537 | "Handle match highlighting escape sequences inserted by the grep process. |
| 523 | This function is called from `compilation-filter-hook'." | 538 | This function is called from `compilation-filter-hook'." |
| @@ -535,7 +550,8 @@ This function is called from `compilation-filter-hook'." | |||
| 535 | (while (re-search-forward "\033\\[0?1;31m\\(.*?\\)\033\\[[0-9]*m" end 1) | 550 | (while (re-search-forward "\033\\[0?1;31m\\(.*?\\)\033\\[[0-9]*m" end 1) |
| 536 | (replace-match (propertize (match-string 1) | 551 | (replace-match (propertize (match-string 1) |
| 537 | 'face nil 'font-lock-face grep-match-face) | 552 | 'face nil 'font-lock-face grep-match-face) |
| 538 | t t)) | 553 | t t) |
| 554 | (cl-incf grep-num-matches-found)) | ||
| 539 | ;; Delete all remaining escape sequences | 555 | ;; Delete all remaining escape sequences |
| 540 | (goto-char beg) | 556 | (goto-char beg) |
| 541 | (while (re-search-forward "\033\\[[0-9;]*[mK]" end 1) | 557 | (while (re-search-forward "\033\\[[0-9;]*[mK]" end 1) |
| @@ -775,6 +791,8 @@ This function is called from `compilation-filter-hook'." | |||
| 775 | grep-hit-face) | 791 | grep-hit-face) |
| 776 | (set (make-local-variable 'compilation-error-regexp-alist) | 792 | (set (make-local-variable 'compilation-error-regexp-alist) |
| 777 | grep-regexp-alist) | 793 | grep-regexp-alist) |
| 794 | (set (make-local-variable 'compilation-mode-line-errors) | ||
| 795 | grep-mode-line-matches) | ||
| 778 | ;; compilation-directory-matcher can't be nil, so we set it to a regexp that | 796 | ;; compilation-directory-matcher can't be nil, so we set it to a regexp that |
| 779 | ;; can never match. | 797 | ;; can never match. |
| 780 | (set (make-local-variable 'compilation-directory-matcher) '("\\`a\\`")) | 798 | (set (make-local-variable 'compilation-directory-matcher) '("\\`a\\`")) |