4

I usually setup a nontrivial compile command, then frequently call 'recompile. Is there a way to suppress the prompt for saving files similar to the approach in auctex invoked with (setq TeX-save-query nil)? I find it very annoying to always have to save manually, or press y at the prompt. When I hit recompile, I want all files saved if necessary.

4
compilation-ask-about-save is a variable defined in `compile.el'.
Its value is t

Documentation:
Non-nil means M-x compile asks which buffers to save before compiling.
Otherwise, it saves all modified buffers without asking.

You can customize this variable.

Here is the function recompile, which demonstrates that it also uses the variable compilation-ask-about-save:

(defun recompile (&optional edit-command)
  "Re-compile the program including the current buffer.
If this is run in a Compilation mode buffer, re-use the arguments from the
original use.  Otherwise, recompile using `compile-command'.
If the optional argument `edit-command' is non-nil, the command can be edited."
  (interactive "P")
  (save-some-buffers (not compilation-ask-about-save)
                     compilation-save-buffers-predicate)
  (let ((default-directory (or compilation-directory default-directory))
    (command (eval compile-command)))
    (when edit-command
      (setq command (compilation-read-command (or (car compilation-arguments)
                          command)))
      (if compilation-arguments (setcar compilation-arguments command)))
    (apply 'compilation-start (or compilation-arguments (list command)))))

See also the doc-string for save-some-buffers -- "Optional argument (the prefix) non-nil means save all with no questions."

save-some-buffers is an interactive compiled Lisp function in
`files.el'.

It is bound to C-x s.

(save-some-buffers &optional ARG PRED)

Save some modified file-visiting buffers.  Asks user about each one.
You can answer `y' to save, `n' not to save, `C-r' to look at the
buffer in question with `view-buffer' before deciding or `d' to
view the differences using `diff-buffer-with-file'.

This command first saves any buffers where `buffer-save-without-query' is
non-nil, without asking.

Optional argument (the prefix) non-nil means save all with no questions.
Optional second argument PRED determines which buffers are considered:
If PRED is nil, all the file-visiting buffers are considered.
If PRED is t, then certain non-file buffers will also be considered.
If PRED is a zero-argument function, it indicates for each buffer whether
to consider it or not when called with that buffer current.

See `save-some-buffers-action-alist' if you want to
change the additional actions you can take on files.
  • For the impatient: I put (setq compilation-ask-about-save nil) into my .emacs and it works. – Fred Schoen Aug 20 '14 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.