Subject: bug#57247: [PATCH] em-unix.el: only pass -H option to grep

From: "Brendan O'Dea" <[email-masked]>
Date: Tue, 16 Aug 2022 13:17:40 -0400

Tags: patch

A recent change to eshell-grep added the -H option to cmd in order to
ensure that the filename is always included in the output.

This is fine for grep/egrep/fgrep, but breaks other uses if they don't
support that flag: e.g. agrep/glimpse appear to use -H for a
completely different purpose, one which requires an argument.

In my case, I'm using it to define some eshell commands for some
internal search tools which don't support the flag.

Attached is a patch which moves the addition of the -H option only to
the functions which call grep.

--bod
diff --git a/lisp/eshell/em-unix.el b/lisp/eshell/em-unix.el
index 68276b22d95..40b83010f94 100644
--- a/lisp/eshell/em-unix.el
+++ b/lisp/eshell/em-unix.el
@@ -755,26 +755,21 @@ eshell-grep
 				      (eshell-stringify-list
 				       (flatten-tree args)))
 			      " "))
-	     (cmd (format "%s -nH %s"
-			  (pcase command
-			    ("egrep" "grep -E")
-			    ("fgrep" "grep -F")
-			    (x x))
-			  args))
+	     (cmd (format "%s -n %s" command args))
 	     compilation-scroll-output)
 	(grep cmd)))))
 
 (defun eshell/grep (&rest args)
   "Use Emacs grep facility instead of calling external grep."
-  (eshell-grep "grep" args t))
+  (eshell-grep "grep" (append '("-H") args) t))
 
 (defun eshell/egrep (&rest args)
   "Use Emacs grep facility instead of calling external grep -E."
-  (eshell-grep "egrep" args t))
+  (eshell-grep "grep" (append '("-EH") args) t))
 
 (defun eshell/fgrep (&rest args)
   "Use Emacs grep facility instead of calling external grep -F."
-  (eshell-grep "fgrep" args t))
+  (eshell-grep "grep" (append '("-FH") args) t))
 
 (defun eshell/agrep (&rest args)
   "Use Emacs grep facility instead of calling external agrep."