Commit a45717da authored by Miles Bader's avatar Miles Bader
Browse files

(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.
parent 93aca633
...@@ -67,6 +67,10 @@ will be parsed and highlighted as soon as you try to move to them." ...@@ -67,6 +67,10 @@ will be parsed and highlighted as soon as you try to move to them."
(defcustom grep-command nil (defcustom grep-command nil
"The default grep command for \\[grep]. "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'; The default value of this variable is set up by `grep-compute-defaults';
call that function before using this variable in your program." call that function before using this variable in your program."
:type 'string :type 'string
...@@ -75,6 +79,21 @@ call that function before using this variable in your program." ...@@ -75,6 +79,21 @@ call that function before using this variable in your program."
(progn (grep-compute-defaults) grep-command))) (progn (grep-compute-defaults) grep-command)))
:group 'compilation) :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 (defcustom grep-find-command nil
"The default find command for \\[grep-find]. "The default find command for \\[grep-find].
The default value of this variable is set up by `grep-compute-defaults'; The default value of this variable is set up by `grep-compute-defaults';
...@@ -574,15 +593,38 @@ to a function that generates a unique name." ...@@ -574,15 +593,38 @@ to a function that generates a unique name."
(cons msg code))))) (cons msg code)))))
(defun grep-compute-defaults () (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 (unless grep-command
(setq grep-command (setq grep-command
(if (equal (condition-case nil ; in case "grep" isn't in exec-path (let ((required-options (if grep-use-null-device "-n" "-nH")))
(call-process grep-program nil nil nil (if (equal (condition-case nil ; in case "grep" isn't in exec-path
"-e" "foo" null-device) (call-process grep-program nil nil nil
(error nil)) "-e" "foo" null-device)
1) (error nil))
(format "%s -n -e " grep-program) 1)
(format "%s -n " grep-program)))) (format "%s %s -e " grep-program required-options)
(format "%s %s " grep-program required-options)))))
(unless grep-find-use-xargs (unless grep-find-use-xargs
(setq grep-find-use-xargs (setq grep-find-use-xargs
(if (and (if (and
...@@ -622,7 +664,8 @@ in the grep command history (or into `grep-command' ...@@ -622,7 +664,8 @@ in the grep command history (or into `grep-command'
if that history list is empty)." if that history list is empty)."
(interactive (interactive
(let (grep-default (arg current-prefix-arg)) (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)) (grep-compute-defaults))
(when arg (when arg
(let ((tag-default (let ((tag-default
...@@ -646,7 +689,7 @@ if that history list is empty)." ...@@ -646,7 +689,7 @@ if that history list is empty)."
;; Setting process-setup-function makes exit-message-function work ;; Setting process-setup-function makes exit-message-function work
;; even when async processes aren't supported. ;; even when async processes aren't supported.
(let* ((compilation-process-setup-function 'grep-process-setup) (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) (concat command-args " " null-device)
command-args) command-args)
"No more grep hits" "grep" "No more grep hits" "grep"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment