changeset 39599:56800ba6a856

(grep-use-null-device): New variable. (grep-command): Mention `grep-use-null-device'. (grep-compute-defaults): Compute `grep-use-null-device' if necessary. Make computation of `grep-command' respect `grep-use-null-device'. (grep): Respect `grep-use-null-device'. Call `grep-compute-defaults' even if grep-command is set, if grep-use-null-device is still tentative.
author Miles Bader <miles@gnu.org>
date Fri, 05 Oct 2001 12:30:52 +0000
parents 67884544e4c8
children f531a9e9f5d5
files lisp/progmodes/compile.el
diffstat 1 files changed, 52 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/progmodes/compile.el	Fri Oct 05 12:30:20 2001 +0000
+++ b/lisp/progmodes/compile.el	Fri Oct 05 12:30:52 2001 +0000
@@ -67,6 +67,10 @@
 
 (defcustom grep-command nil
   "The default grep command for \\[grep].
+If the grep program used supports an option to always include file names
+in its output (such as the `-H' option to GNU grep), it's a good idea to
+include it when specifying `grep-command'.
+
 The default value of this variable is set up by `grep-compute-defaults';
 call that function before using this variable in your program."
   :type 'string
@@ -75,6 +79,21 @@
 	      (progn (grep-compute-defaults) grep-command)))
   :group 'compilation)
 
+(defcustom grep-use-null-device 'auto-detect
+  "If non-nil, append the value of `null-device' to grep commands.
+This is done to ensure that the output of grep includes the filename of
+any match in the case where only a single file is searched, and is not
+necessary if the grep program used supports the `-H' option.
+
+The default value of this variable is set up by `grep-compute-defaults';
+call that function before using this variable in your program."
+  :type 'boolean
+  :get '(lambda (symbol)
+	  (if (and grep-use-null-device (not (eq grep-use-null-device t)))
+	      (progn (grep-compute-defaults) grep-use-null-device)
+	    grep-use-null-device))
+  :group 'compilation)
+
 (defcustom grep-find-command nil
   "The default find command for \\[grep-find].
 The default value of this variable is set up by `grep-compute-defaults';
@@ -574,15 +593,38 @@
 	   (cons msg code)))))
 
 (defun grep-compute-defaults ()
+  (unless (or (not grep-use-null-device) (eq grep-use-null-device t))
+    (setq grep-use-null-device
+	  (with-temp-buffer
+	    (let ((hello-file (expand-file-name "HELLO" data-directory)))
+	      (not
+	       (and (equal (condition-case nil
+			       (if grep-command
+				   ;; `grep-command' is already set, so
+				   ;; use that for testing.
+				   (call-process-shell-command
+				    grep-command nil t nil
+				    "^English" hello-file)
+				 ;; otherwise use `grep-program'
+				 (call-process grep-program nil t nil
+					       "-nH" "^English" hello-file))
+			     (error nil))
+			   0)
+		    (progn
+		      (goto-char (point-min))
+		      (looking-at
+		       (concat (regexp-quote hello-file)
+			       ":[0-9]+:English")))))))))
   (unless grep-command
     (setq grep-command
-	  (if (equal (condition-case nil ; in case "grep" isn't in exec-path
-			 (call-process grep-program nil nil nil
-				       "-e" "foo" null-device)
-		       (error nil))
-		     1)
-	      (format "%s -n -e " grep-program)
-	    (format "%s -n " grep-program))))
+	  (let ((required-options (if grep-use-null-device "-n" "-nH")))
+	    (if (equal (condition-case nil ; in case "grep" isn't in exec-path
+			   (call-process grep-program nil nil nil
+					 "-e" "foo" null-device)
+			 (error nil))
+		       1)
+		(format "%s %s -e " grep-program required-options)
+	      (format "%s %s " grep-program required-options)))))
   (unless grep-find-use-xargs
     (setq grep-find-use-xargs
 	  (if (and
@@ -622,7 +664,8 @@
 if that history list is empty)."
   (interactive
    (let (grep-default (arg current-prefix-arg))
-     (unless grep-command
+     (unless (and grep-command
+		  (or (not grep-use-null-device) (eq grep-use-null-device t)))
        (grep-compute-defaults))
      (when arg
        (let ((tag-default
@@ -646,7 +689,7 @@
   ;; Setting process-setup-function makes exit-message-function work
   ;; even when async processes aren't supported.
   (let* ((compilation-process-setup-function 'grep-process-setup)
-	 (buf (compile-internal (if null-device
+	 (buf (compile-internal (if (and grep-use-null-device null-device)
 				    (concat command-args " " null-device)
 				  command-args)
 				"No more grep hits" "grep"