From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#30397: Random numbers in grep mode-line Date: Sun, 11 Feb 2018 23:40:05 +0200 Organization: LINKOV.NET Message-ID: <87h8qnb3yi.fsf@mail.linkov.net> References: <87tvurtbek.fsf@mail.linkov.net> <87zi4gpm2r.fsf@mail.linkov.net> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1518385509 1695 195.159.176.226 (11 Feb 2018 21:45:09 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 11 Feb 2018 21:45:09 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (x86_64-pc-linux-gnu) Cc: 30397@debbugs.gnu.org, Noam Postavsky To: Drew Adams Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Feb 11 22:45:05 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ekzQU-0007Vn-Jl for geb-bug-gnu-emacs@m.gmane.org; Sun, 11 Feb 2018 22:44:46 +0100 Original-Received: from localhost ([::1]:59452 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ekzSW-00064d-Be for geb-bug-gnu-emacs@m.gmane.org; Sun, 11 Feb 2018 16:46:52 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:52516) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ekzRm-0005hd-Hw for bug-gnu-emacs@gnu.org; Sun, 11 Feb 2018 16:46:10 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ekzRk-0004mL-6c for bug-gnu-emacs@gnu.org; Sun, 11 Feb 2018 16:46:06 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:59014) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ekzRk-0004mD-1e for bug-gnu-emacs@gnu.org; Sun, 11 Feb 2018 16:46:04 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ekzRj-0001pR-Ry for bug-gnu-emacs@gnu.org; Sun, 11 Feb 2018 16:46:03 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 11 Feb 2018 21:46:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 30397 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 30397-submit@debbugs.gnu.org id=B30397.15183855506954 (code B ref 30397); Sun, 11 Feb 2018 21:46:03 +0000 Original-Received: (at 30397) by debbugs.gnu.org; 11 Feb 2018 21:45:50 +0000 Original-Received: from localhost ([127.0.0.1]:38670 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ekzRW-0001o6-42 for submit@debbugs.gnu.org; Sun, 11 Feb 2018 16:45:50 -0500 Original-Received: from sub3.mail.dreamhost.com ([69.163.253.7]:42742 helo=homiemail-a100.g.dreamhost.com) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ekzRU-0001ny-Gr for 30397@debbugs.gnu.org; Sun, 11 Feb 2018 16:45:48 -0500 Original-Received: from homiemail-a100.g.dreamhost.com (localhost [127.0.0.1]) by homiemail-a100.g.dreamhost.com (Postfix) with ESMTP id BE67031A073; Sun, 11 Feb 2018 13:45:47 -0800 (PST) Original-Received: from localhost.linkov.net (m91-129-100-74.cust.tele2.ee [91.129.100.74]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: jurta@jurta.org) by homiemail-a100.g.dreamhost.com (Postfix) with ESMTPSA id 7160B31A070; Sun, 11 Feb 2018 13:45:46 -0800 (PST) In-Reply-To: (Drew Adams's message of "Sat, 10 Feb 2018 14:01:38 -0800 (PST)") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:143178 Archived-At: --=-=-= Content-Type: text/plain >> Yes, some adjustment is needed for grep. That reminded me >> about two unclosed feature requests: bug#13417 and bug#14017 >> that proposed to display these numbers also at the bottom of >> output buffers. But the showstopper was to decide on the >> final format of such messages. Although this looks good: >> >> Grep finished with 42 matches in 5 lines at Thu Jul 21 15:02:15 >> >> Than the mode-line will display two numbers: the number of matches >> and the number of matching lines (in green). > > Is the total number of lines (in the search space) also > available? If so, would that be useful? Maybe something > like this? > > Grep finished at Thu Jul 21 15:02:15 - 42 matches in 5/113 lines > ^^^^ Unfortunately the total number of lines is not available. There is even problems with getting the right number of matches. When grep doesn't highlight matches, we can't count them. Another problem is that grep matches to be printed at the end of the grep buffer can't be counted in grep-regexp-alist because it is based on font-lock which is invoked at unpredictable times when grep process is already finished. This leaves only one way to count matches in grep-filter in this patch. For the same reason, printing the number of compilation errors at the end of the compilation buffer can't be implemented for bug#13417. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=grep-matches-mode-line.patch diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el index 9ce4ff8..23de8aa 100644 --- a/lisp/progmodes/grep.el +++ b/lisp/progmodes/grep.el @@ -425,6 +425,14 @@ grep-match-face (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 @@ grep-mode-font-lock-keywords ;; 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\\)\\|with \\(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 @@ grep-process-setup (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 @@ grep-filter (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 @@ grep-mode 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\\`")) --=-=-=--