(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
(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';
(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
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
;; 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"