]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/grep.el
Detect remote uid and gid in tramp-gvfs.el
[gnu-emacs] / lisp / progmodes / grep.el
index 5d1b68a192d4dcd69a72baa8e645d7819fc30d8f..2b44b58f245e162e45ce6de4f7ca95989e619d5e 100644 (file)
@@ -227,6 +227,22 @@ to determine whether cdr should not be excluded."
                 (const :tag "No ignored files" nil))
   :group 'grep)
 
+(defcustom grep-save-buffers 'ask
+  "If non-nil, save buffers before running the grep commands.
+If `ask', ask before saving.  If a function, call it with no arguments
+with each buffer current, as a predicate to determine whether that
+buffer should be saved or not.  E.g., one can set this to
+  (lambda ()
+    (string-prefix-p my-grep-root (file-truename (buffer-file-name))))
+to limit saving to files located under `my-grep-root'."
+  :version "25.2"
+  :type '(choice
+          (const :tag "Ask before saving" ask)
+          (const :tag "Don't save buffers" nil)
+          function
+          (other :tag "Save all buffers" t))
+  :group 'grep)
+
 (defcustom grep-error-screen-columns nil
   "If non-nil, column numbers in grep hits are screen columns.
 See `compilation-error-screen-columns'"
@@ -411,34 +427,21 @@ Notice that using \\[next-error] or \\[compile-goto-error] modifies
 This gets tacked on the end of the generated expressions.")
 
 ;;;###autoload
-(defcustom grep-program (purecopy "grep")
+(defvar grep-program (purecopy "grep")
   "The default grep program for `grep-command' and `grep-find-command'.
-This variable's value takes effect when `grep-compute-defaults' is called."
-  :type 'string
-  :version "25.1"
-  :group 'grep)
+This variable's value takes effect when `grep-compute-defaults' is called.")
 
 ;;;###autoload
-(defcustom grep-find-program (purecopy "find")
+(defvar find-program (purecopy "find")
   "The default find program.
 This is used by commands like `grep-find-command', `find-dired'
-and others."
-  :type 'string
-  :version "25.1"
-  :group 'grep)
-
-(define-obsolete-variable-alias 'find-program 'grep-find-program "25.1")
+and others.")
 
 ;;;###autoload
-(defcustom grep-xargs-program (purecopy "xargs")
+(defvar xargs-program (purecopy "xargs")
   "The default xargs program for `grep-find-command'.
 See `grep-find-use-xargs'.
-This variable's value takes effect when `grep-compute-defaults' is called."
-  :type 'string
-  :version "25.1"
-  :group 'grep)
-
-(define-obsolete-variable-alias 'xargs-program 'grep-xargs-program "25.1")
+This variable's value takes effect when `grep-compute-defaults' is called.")
 
 ;;;###autoload
 (defvar grep-find-use-xargs nil
@@ -608,14 +611,13 @@ This function is called from `compilation-filter-hook'."
        (unless grep-find-use-xargs
          (setq grep-find-use-xargs
                (cond
-                ((grep-probe grep-find-program
+                ((grep-probe find-program
                              `(nil nil nil ,null-device "-exec" "echo"
                                    "{}" "+"))
                  'exec-plus)
                 ((and
-                  (grep-probe grep-find-program
-                               `(nil nil nil ,null-device "-print0"))
-                  (grep-probe grep-xargs-program `(nil nil nil "-0" "echo")))
+                  (grep-probe find-program `(nil nil nil ,null-device "-print0"))
+                  (grep-probe xargs-program `(nil nil nil "-0" "echo")))
                  'gnu)
                 (t
                  'exec))))
@@ -626,11 +628,10 @@ This function is called from `compilation-filter-hook'."
                       ;; after the pipe symbol be quoted if they use
                       ;; forward slashes as directory separators.
                       (format "%s . -type f -print0 | \"%s\" -0 %s"
-                              grep-find-program grep-xargs-program
-                               grep-command))
+                              find-program xargs-program grep-command))
                      ((memq grep-find-use-xargs '(exec exec-plus))
                       (let ((cmd0 (format "%s . -type f -exec %s"
-                                          grep-find-program grep-command))
+                                          find-program grep-command))
                             (null (if grep-use-null-device
                                       (format "%s " null-device)
                                     "")))
@@ -642,8 +643,7 @@ This function is called from `compilation-filter-hook'."
                          (1+ (length cmd0)))))
                      (t
                       (format "%s . -type f -print | \"%s\" %s"
-                              grep-find-program grep-xargs-program
-                               grep-command)))))
+                              find-program xargs-program grep-command)))))
        (unless grep-find-template
          (setq grep-find-template
                (let ((gcmd (format "%s <C> %s <R>"
@@ -653,17 +653,17 @@ This function is called from `compilation-filter-hook'."
                              "")))
                  (cond ((eq grep-find-use-xargs 'gnu)
                         (format "%s <D> <X> -type f <F> -print0 | \"%s\" -0 %s"
-                                grep-find-program grep-xargs-program gcmd))
+                                find-program xargs-program gcmd))
                        ((eq grep-find-use-xargs 'exec)
                         (format "%s <D> <X> -type f <F> -exec %s {} %s%s"
-                                grep-find-program gcmd null
+                                find-program gcmd null
                                 (shell-quote-argument ";")))
                        ((eq grep-find-use-xargs 'exec-plus)
                         (format "%s <D> <X> -type f <F> -exec %s %s{} +"
-                                grep-find-program gcmd null))
+                                find-program gcmd null))
                        (t
                         (format "%s <D> <X> -type f <F> -print | \"%s\" %s"
-                                grep-find-program grep-xargs-program gcmd))))))))
+                                find-program xargs-program gcmd))))))))
 
     ;; Save defaults for this host.
     (setq grep-host-defaults-alist
@@ -744,6 +744,12 @@ This function is called from `compilation-filter-hook'."
        grep-error-screen-columns)
   (add-hook 'compilation-filter-hook 'grep-filter nil t))
 
+(defun grep--save-buffers ()
+  (when grep-save-buffers
+    (save-some-buffers (and (not (eq grep-save-buffers 'ask))
+                            (not (functionp grep-save-buffers)))
+                       (and (functionp grep-save-buffers)
+                            grep-save-buffers))))
 
 ;;;###autoload
 (defun grep (command-args)
@@ -775,6 +781,7 @@ list is empty)."
                                  'grep-history
                                  (if current-prefix-arg nil default))))))
 
+  (grep--save-buffers)
   ;; Setting process-setup-function makes exit-message-function work
   ;; even when async processes aren't supported.
   (compilation-start (if (and grep-use-null-device null-device)
@@ -968,6 +975,7 @@ This command shares argument histories with \\[rgrep] and \\[grep]."
        (let ((default-directory dir))
          ;; Setting process-setup-function makes exit-message-function work
          ;; even when async processes aren't supported.
+          (grep--save-buffers)
          (compilation-start (if (and grep-use-null-device null-device)
                                 (concat command " " null-device)
                               command)
@@ -1030,6 +1038,7 @@ to specify a command to run."
                    (read-from-minibuffer "Confirm: "
                                          command nil nil 'grep-find-history))
            (add-to-history 'grep-find-history command))
+          (grep--save-buffers)
          (let ((default-directory dir))
            (compilation-start command 'grep-mode))
          ;; Set default-directory if we started rgrep in the *grep* buffer.