unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: Drew Adams <drew.adams@oracle.com>
Cc: 30397@debbugs.gnu.org, Noam Postavsky <npostavs@users.sourceforge.net>
Subject: bug#30397: Random numbers in grep mode-line
Date: Sun, 11 Feb 2018 23:40:05 +0200	[thread overview]
Message-ID: <87h8qnb3yi.fsf@mail.linkov.net> (raw)
In-Reply-To: <da3638f9-1711-4191-bfa6-75b7523de064@default> (Drew Adams's message of "Sat, 10 Feb 2018 14:01:38 -0800 (PST)")

[-- Attachment #1: Type: text/plain, Size: 1396 bytes --]

>> 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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: grep-matches-mode-line.patch --]
[-- Type: text/x-diff, Size: 3990 bytes --]

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\\`"))

  reply	other threads:[~2018-02-11 21:40 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-08 21:32 bug#30397: Random numbers in grep mode-line Juri Linkov
2018-02-08 21:48 ` Drew Adams
2018-02-09  9:51   ` Eli Zaretskii
2018-02-08 23:00 ` Noam Postavsky
2018-02-10 21:32   ` Juri Linkov
2018-02-10 22:01     ` Drew Adams
2018-02-11 21:40       ` Juri Linkov [this message]
2018-02-12  4:54         ` Drew Adams
2018-02-12 15:47         ` Eli Zaretskii
2018-02-12 21:39           ` Juri Linkov
2018-02-11 20:45     ` Richard Stallman
2018-02-12 16:34       ` Eli Zaretskii
2018-02-09  9:50 ` Eli Zaretskii
     [not found] <<87tvurtbek.fsf@mail.linkov.net>
     [not found] ` <<702f1621-529b-47b0-a15d-898a2fd81f79@default>
     [not found]   ` <<83eflu4hjx.fsf@gnu.org>
2018-02-09 15:27     ` Drew Adams
2018-02-09 15:35       ` Eli Zaretskii
     [not found] ` <<83fu6a4hlh.fsf@gnu.org>
2018-02-09 15:43   ` Drew Adams
     [not found] <<<87tvurtbek.fsf@mail.linkov.net>
     [not found] ` <<<702f1621-529b-47b0-a15d-898a2fd81f79@default>
     [not found]   ` <<<83eflu4hjx.fsf@gnu.org>
     [not found]     ` <<3e9d0fd8-b859-4eec-8f34-54185dd6c0f3@default>
     [not found]       ` <<83zi4i2n2o.fsf@gnu.org>
2018-02-09 15:59         ` Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h8qnb3yi.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=30397@debbugs.gnu.org \
    --cc=drew.adams@oracle.com \
    --cc=npostavs@users.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).