Subject: bug#77732: grep-edit-buffer errors with incionsistent behaviour

From: "Paul D. Nelson" <[email-masked]>
Date: Mon, 21 Apr 2025 16:27:09 +0200

I took a look at the manual entry for compilation-mode, which contains:

       Sometimes ‘compilation-error-regexp-alist’ doesn't correctly
    determine the filename that is the source of the error.  Use user option
    ‘compilation-transform-file-match-alist’ to make any necessary
    adjustments, such as adding or changing a directory component, or even
    considering certain compiler messages not error messages at all.

This suggests a surgical fix (attached) that addresses the cases of the
bug that I had been able to reproduce.  Perhaps Johann can check if it
addresses his cases, too.

>From 2efcd27bb75c104b93116b2678ba2569dddebd9a Mon Sep 17 00:00:00 2001
From: Paul Nelson <[email-masked]>
Date: Mon, 21 Apr 2025 16:25:06 +0200
Subject: [PATCH] Fix grep buffer display of "finished" lines as matches

* lisp/progmodes/grep.el
(grep-compilation-transform-finished-rules): New variable
containing rules to prevent "Grep finished" lines from being
misinterpreted as matches.
(grep-mode): Add these rules to
compilation-transform-file-match-alist (bug#77732).
---
 lisp/progmodes/grep.el | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el
index b0105f08ea2..ed64d8a5bc8 100644
--- a/lisp/progmodes/grep.el
+++ b/lisp/progmodes/grep.el
@@ -542,6 +542,13 @@ grep-mode-font-lock-keywords
    "Additional things to highlight in grep output.
 This gets tacked on the end of the generated expressions.")
 
+(defvar grep-compilation-transform-finished-rules
+  '(("^Grep[/a-zA-Z]* finished with \\(?:\\(\\(?:[0-9]+ \\)?match\\(?:es\\)? found\\)\\|\\(no matches found\\)\\).*" . nil)
+    ("^Grep[/a-zA-Z]* \\(exited abnormally\\|interrupt\\|killed\\|terminated\\)\\(?:.*with code \\([0-9]+\\)\\)?.*" . nil))
+  "Rules added to `compilation-transform-file-match-alist' in `grep-mode'
+These prevent the \"Grep finished\" lines from being misinterpreted as
+matches (bug#77732).")
+
 ;;;###autoload
 (defvar grep-program "grep"
   "The default grep program for `grep-command' and `grep-find-command'.
@@ -971,6 +978,9 @@ grep-mode
               grep-hit-face)
   (setq-local compilation-error-regexp-alist
               grep-regexp-alist)
+  (setq-local compilation-transform-file-match-alist
+              (append grep-compilation-transform-finished-rules
+                      compilation-transform-file-match-alist))
   (setq-local compilation-mode-line-errors
               grep-mode-line-matches)
   ;; compilation-directory-matcher can't be nil, so we set it to a regexp that
-- 
2.39.3 (Apple Git-145)