Function: grep-compute-defaults

grep-compute-defaults is an autoloaded and byte-compiled function defined in grep.el.gz.

Signature

(grep-compute-defaults)

Documentation

Compute the defaults for the grep command.

The value depends on grep-command, grep-template, grep-use-null-device, grep-find-command, grep-find-template, grep-use-null-filename-separator, grep-find-use-xargs, grep-highlight-matches, and grep-quoting-style.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/grep.el.gz
;;;###autoload
(defun grep-compute-defaults ()
  "Compute the defaults for the `grep' command.
The value depends on `grep-command', `grep-template',
`grep-use-null-device', `grep-find-command', `grep-find-template',
`grep-use-null-filename-separator', `grep-find-use-xargs',
`grep-highlight-matches', and `grep-quoting-style'."
  ;; Keep default values.
  (unless grep-host-defaults-alist
    (add-to-list
     'grep-host-defaults-alist
     (cons nil
	   `((grep-command ,grep-command)
	     (grep-template ,grep-template)
	     (grep-use-null-device ,grep-use-null-device)
	     (grep-find-command ,grep-find-command)
	     (grep-find-template ,grep-find-template)
             (grep-use-null-filename-separator
              ,grep-use-null-filename-separator)
	     (grep-find-use-xargs ,grep-find-use-xargs)
	     (grep-highlight-matches ,grep-highlight-matches)
             (grep-quoting-style ,grep-quoting-style)))))
  (let* ((remote (file-remote-p default-directory))
         (host-id (intern (or remote "localhost")))
	 (host-defaults (assq host-id grep-host-defaults-alist))
	 (defaults (assq nil grep-host-defaults-alist))
         (quot-braces (shell-quote-argument "{}" remote))
         (quot-scolon (shell-quote-argument ";" remote)))
    ;; There are different defaults on different hosts.  They must be
    ;; computed for every host once.
    (dolist (setting '(grep-command grep-template
		       grep-use-null-device grep-find-command
                       grep-use-null-filename-separator
                       grep-find-template grep-find-use-xargs
		       grep-highlight-matches))
      (set setting
	   (cadr (or (assq setting host-defaults)
		     (assq setting 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 (grep-hello-file)))
                (prog1
		    (not
		     (and (if grep-command
			      ;; `grep-command' is already set, so
			      ;; use that for testing.
			      (grep-probe
                               grep-command
			       `(nil t nil "^Copyright"
                                     ,(file-local-name hello-file))
			       #'process-file-shell-command)
			    ;; otherwise use `grep-program'
			    (grep-probe
                             grep-program
			     `(nil t nil "-nH" "^Copyright"
                                   ,(file-local-name hello-file))))
                          (progn
			    (goto-char (point-min))
			    (looking-at
			     (concat (regexp-quote (file-local-name hello-file))
				     ":[0-9]+:Copyright")))))
                  (when (file-remote-p hello-file) (delete-file hello-file)))))))

    (when (eq grep-use-null-filename-separator 'auto-detect)
      (setq grep-use-null-filename-separator
            (with-temp-buffer
              (let* ((hello-file (grep-hello-file))
                     (args `("--null" "-ne" "^Copyright"
                             ,(file-local-name hello-file))))
                (if grep-use-null-device
                    (setq args (append args (list (null-device))))
                  (push "-H" args))
                (prog1
                    (and (grep-probe grep-program `(nil t nil ,@args))
                         (progn
                           (goto-char (point-min))
                           (looking-at
                            (concat (regexp-quote (file-local-name hello-file))
                                    "\0[0-9]+:Copyright"))))
                  (when (file-remote-p hello-file) (delete-file hello-file)))))))

    (when (eq grep-highlight-matches 'auto-detect)
      (setq grep-highlight-matches
	    (with-temp-buffer
              ;; The "grep --help" exit status varies; pay no attention to it.
              (grep-probe grep-program '(nil t nil "--help"))
	      (goto-char (point-min))
	      (and (let ((case-fold-search nil))
                     (re-search-forward (rx "--color" (not (in "a-z"))) nil t))
                   ;; Windows and DOS pipes fail `isatty' detection in Grep.
		   (if (memq system-type '(windows-nt ms-dos))
		       'always 'auto)))))

    (unless (and grep-command grep-find-command
		 grep-template grep-find-template)
      (let ((grep-options
	     (concat (if grep-use-null-device "-n" "-nH")
                     (if grep-use-null-filename-separator " --null")
                     (when (grep-probe grep-program
                                       `(nil nil nil "-e" "foo" ,(null-device))
                                       nil 1)
                       " -e"))))
	(unless grep-command
	  (setq grep-command
		(format "%s %s %s " grep-program
                        (or
                         (and grep-highlight-matches
                              (grep-probe
                               grep-program
                               `(nil nil nil "--color" "x" ,(null-device))
                               nil 1)
                              (if (eq grep-highlight-matches 'always)
                                  "--color=always" "--color=auto"))
                         "")
                        grep-options)))
	(unless grep-template
	  (setq grep-template
		(format "%s <X> <C> %s <R> <F>" grep-program grep-options)))
	(unless grep-find-use-xargs
	  (setq grep-find-use-xargs
		(cond
                 ;; For performance, we want:
                 ;; A. Run grep on batches of files (instead of one grep per file)
                 ;; B. If the directory is large and we need multiple batches,
                 ;;    run find in parallel with a running grep.
                 ;; "find | xargs grep" gives both A and B
		 ((and
                   (not (eq system-type 'windows-nt))
		   (grep-probe
                    find-program `(nil nil nil ,(null-device) "-print0"))
		   (grep-probe xargs-program '(nil nil nil "-0" "echo")))
		  'gnu)
                 ;; "find -exec {} +" gives A but not B
		 ((grep-probe find-program
			      `(nil nil nil ,(null-device) "-exec" "echo"
				    "{}" "+"))
		  'exec-plus)
                 ;; "find -exec {} ;" gives neither A nor B.
		 (t
		  'exec))))
	(unless grep-find-command
	  (setq grep-find-command
		(cond ((eq grep-find-use-xargs 'gnu)
		       ;; Windows shells need the program file name
		       ;; after the pipe symbol be quoted if they use
		       ;; forward slashes as directory separators.
		       (format "%s . -type f -print0 | \"%s\" -0 %s"
			       find-program xargs-program grep-command))
		      ((eq grep-find-use-xargs 'gnu-sort)
		       (format "%s . -type f -print0 | sort -z | \"%s\" -0 %s"
			       find-program xargs-program grep-command))
		      ((memq grep-find-use-xargs '(exec exec-plus))
		       (let ((cmd0 (format "%s . -type f -exec %s"
					   find-program grep-command))
			     (null (if grep-use-null-device
				       (format "%s " (null-device))
				     "")))
			 (cons
			  (if (eq grep-find-use-xargs 'exec-plus)
			      (format "%s %s%s +" cmd0 null quot-braces)
			    (format "%s %s %s%s"
                                    cmd0 quot-braces null quot-scolon))
			  (1+ (length cmd0)))))
		      (t
		       (format "%s . -type f -print | \"%s\" %s"
			       find-program xargs-program grep-command)))))
	(unless grep-find-template
	  (setq grep-find-template
		(let ((gcmd (format "%s <C> %s <R>"
				    grep-program grep-options))
		      (null (if grep-use-null-device
                                (format "%s " (null-device))
                              "")))
                  (cond ((eq grep-find-use-xargs 'gnu)
                         (format "%s -H <D> <X> -type f <F> -print0 | \"%s\" -0 %s"
                                 find-program xargs-program gcmd))
                        ((eq grep-find-use-xargs 'gnu-sort)
                         (format "%s -H <D> <X> -type f <F> -print0 | sort -z | \"%s\" -0 %s"
                                 find-program xargs-program gcmd))
                        ((eq grep-find-use-xargs 'exec)
                         (format "%s -H <D> <X> -type f <F> -exec %s %s %s%s"
                                 find-program gcmd quot-braces null quot-scolon))
                        ((eq grep-find-use-xargs 'exec-plus)
                         (format "%s -H <D> <X> -type f <F> -exec %s %s%s +"
                                 find-program gcmd null quot-braces))
                        (t
                         (format "%s -H <D> <X> -type f <F> -print | \"%s\" %s"
                                 find-program xargs-program gcmd))))))

        (setq grep-quoting-style (and remote 'posix))))

    ;; Save defaults for this host.
    (setq grep-host-defaults-alist
	  (delete (assq host-id grep-host-defaults-alist)
		  grep-host-defaults-alist))
    (add-to-list
     'grep-host-defaults-alist
     (cons host-id
	   `((grep-command ,grep-command)
	     (grep-template ,grep-template)
	     (grep-use-null-device ,grep-use-null-device)
	     (grep-find-command ,grep-find-command)
	     (grep-find-template ,grep-find-template)
	     (grep-use-null-filename-separator
	      ,grep-use-null-filename-separator)
	     (grep-find-use-xargs ,grep-find-use-xargs)
	     (grep-highlight-matches ,grep-highlight-matches)
             (grep-quoting-style ,grep-quoting-style))))))