From 90c36173fdcc7dbe1fa150784a4784a9c0cb7bbc Mon Sep 17 00:00:00 2001
From: Hugh Brown <aardvark@saintaardvarkthecarpeted.com>
Date: Mon, 29 Feb 2016 16:20:05 +1100
Subject: [PATCH] Save buffers before running grep commands

* lisp/progmodes/grep.el (grep-ask-about-save): New variable (bug#96).
(grep, lgrep, rgrep): Use it (bug#96).

* doc/emacs/building.texi (Grep Searching): Document
`grep-save-buffers'.

* lisp/progmodes/grep.el (grep-save-buffers): Rename from
`grep-ask-about-save'.
(grep--save-buffers): New function.
(grep, lgrep, rgrep): Use it.
---
 doc/emacs/building.texi |  8 ++++++++
 etc/NEWS                |  4 ++++
 lisp/progmodes/grep.el  | 28 ++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+)

diff --git a/doc/emacs/building.texi b/doc/emacs/building.texi
index 3fa89d9062d..03fa0ed83b2 100644
--- a/doc/emacs/building.texi
+++ b/doc/emacs/building.texi
@@ -382,6 +382,14 @@ use of this feature by setting @code{grep-highlight-matches} to
 @code{t}.  When displaying a match in the source buffer, the exact
 match will be highlighted, instead of the entire source line.
 
+  The @command{grep} commands will offer to save buffers before
+running.  This is controlled by the @code{grep-save-buffers} variable.
+The possible values are either @code{nil} (don't save), @code{ask}
+(ask before saving), a function which will be used as a predicate (and
+is called with the file name as the parameter and should return
+non-nil if the buffer is to be saved), and any other non-@code{nil}
+value means that all buffers should be saved without asking.
+
 @findex grep-find
 @findex find-grep
   The command @kbd{M-x grep-find} (also available as @kbd{M-x
diff --git a/etc/NEWS b/etc/NEWS
index 12b98fa989d..8c4fb63fbd4 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1676,6 +1676,10 @@ behavior, set `diff-switches' to `-c'.
 dynamically.  Any third-party code that changes these templates should
 be updated accordingly.
 
+** The grep/rgrep/lgrep functions will now ask about saving files
+before running.  This is controlled by the `grep-save-buffers'
+variable.
+
 +++
 ** ‘(/ N)’ is now equivalent to ‘(/ 1 N)’ rather than to ‘(/ N 1)’.
 The new behavior is compatible with Common Lisp and with XEmacs.
diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el
index f04a7226d18..b035528d4b2 100644
--- a/lisp/progmodes/grep.el
+++ b/lisp/progmodes/grep.el
@@ -227,6 +227,25 @@ to determine whether cdr should not be excluded."
 		 (const :tag "No ignored files" nil))
   :group 'grep)
 
+;;;###autoload
+(defcustom grep-save-buffers 'ask
+  "If non-nil, save buffers before running the grep commands.
+If `ask', ask before saving.  If the variable is a function, it
+will be used as a predicate that should say whether the 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 "Default (ask before saving)" ask)
+          (const :tag "Don't save buffers" nil)
+          (const :tag "Save all buffers" t)
+          function)
+  :type 'boolean
+  :group 'grep)
+
 (defcustom grep-error-screen-columns nil
   "If non-nil, column numbers in grep hits are screen columns.
 See `compilation-error-screen-columns'"
@@ -728,6 +747,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)
@@ -759,6 +784,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)
@@ -952,6 +978,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)
@@ -1014,6 +1041,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.
-- 
2.39.5