Function: save-some-buffers

save-some-buffers is an interactive and byte-compiled function defined in files.el.gz.

Signature

(save-some-buffers &optional ARG PRED)

Documentation

Save some modified file-visiting buffers. Asks user about each one.

You can answer \y or \SPC to save, \n or \DEL not to save,
\M-~ not to save and also mark the buffer as unmodified, \C-r
to look at the buffer in question with view-buffer before deciding, \d to view the differences using diff-buffer-with-file, \! to save the buffer and all remaining buffers without any further querying, \. to save only the current buffer and skip the remaining ones and \q or \RET to exit the function without saving any more buffers. \C-h displays a help message describing these options.

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

Optional argument ARG (interactively, prefix argument) 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 function, it is called with no argument in each buffer and should return non-nil if that buffer should be considered. PRED defaults to the value of save-some-buffers-default-predicate.

See save-some-buffers-action-alist if you want to change the additional actions you can take on files.

The functions in save-some-buffers-functions will be called after saving the buffers.

View in manual

Probably introduced at or before Emacs version 19.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun save-some-buffers (&optional arg pred)
  "Save some modified file-visiting buffers.  Asks user about each one.
You can answer \\`y' or \\`SPC' to save, \\`n' or \\`DEL' not to save,
\\`M-~' not to save and also mark the buffer as unmodified, \\`C-r'
to look at the buffer in question with `view-buffer' before
deciding, \\`d' to view the differences using
`diff-buffer-with-file', \\`!' to save the buffer and all remaining
buffers without any further querying, \\`.' to save only the
current buffer and skip the remaining ones and \\`q' or \\`RET' to exit
the function without saving any more buffers.  \\`C-h' displays a
help message describing these options.

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

Optional argument ARG (interactively, prefix argument) 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 function, it is called with no argument in each buffer and
should return non-nil if that buffer should be considered.
PRED defaults to the value of `save-some-buffers-default-predicate'.

See `save-some-buffers-action-alist' if you want to
change the additional actions you can take on files.

The functions in `save-some-buffers-functions' will be called
after saving the buffers."
  (interactive "P")
  (unless pred
    (setq pred
          ;; Allow `pred' to be a function that returns a predicate
          ;; with lexical bindings in its original environment (bug#46374).
          (if (and (symbolp save-some-buffers-default-predicate)
                   (get save-some-buffers-default-predicate
                        'save-some-buffers-function))
              (funcall save-some-buffers-default-predicate)
            save-some-buffers-default-predicate)))
  (let* ((switched-buffer nil)
         (save-some-buffers--switch-window-callback
          (lambda (buffer)
            (setq switched-buffer buffer)))
         queried autosaved-buffers
	 files-done inhibit-message)
    (unwind-protect
        (save-window-excursion
          (dolist (buffer (buffer-list))
	    ;; First save any buffers that we're supposed to save
	    ;; unconditionally.  That way the following code won't ask
	    ;; about them.
	    (with-current-buffer buffer
	      (when (and buffer-save-without-query (buffer-modified-p))
                (push (buffer-name) autosaved-buffers)
                (save-buffer))))
          ;; Ask about those buffers that merit it,
          ;; and record the number thus saved.
          (setq files-done
                (map-y-or-n-p
                 (lambda (buffer)
                   (if arg
                       t
                     (setq queried t)
                     (if (buffer-file-name buffer)
                         (if (or
                              (equal (buffer-name buffer)
                                     (file-name-nondirectory
                                      (buffer-file-name buffer)))
                              (string-match
                               (concat "\\<"
                                       (regexp-quote
                                        (file-name-nondirectory
                                         (buffer-file-name buffer)))
                                       "<[^>]*>\\'")
                               (buffer-name buffer)))
                             ;; The buffer name is similar to the file
                             ;; name.
                             (format "Save file %s? "
                                     (buffer-file-name buffer))
                           ;; The buffer and file names are dissimilar;
                           ;; display both.
                           (format "Save file %s (buffer %s)? "
                                   (buffer-file-name buffer)
                                   (buffer-name buffer)))
                       ;; No file name.
                       (format "Save buffer %s? " (buffer-name buffer)))))
                 (lambda (buffer)
                   (with-current-buffer buffer
                     (save-buffer)))
                 (files--buffers-needing-to-be-saved pred)
                 '("buffer" "buffers" "save")
                 save-some-buffers-action-alist))
          ;; Allow other things to be saved at this time, like abbrevs.
          (dolist (func save-some-buffers-functions)
            (setq inhibit-message (or (funcall func nil arg) inhibit-message)))
          (or queried (> files-done 0) inhibit-message
	      (cond
	       ((null autosaved-buffers)
                (when (called-interactively-p 'any)
                  (files--message "(No files need saving)")))
	       ((= (length autosaved-buffers) 1)
                (files--message "(Saved %s)" (car autosaved-buffers)))
	       (t
                (files--message
                 "(Saved %d files: %s)" (length autosaved-buffers)
                 (mapconcat 'identity autosaved-buffers ", "))))))
      (when switched-buffer
        (pop-to-buffer-same-window switched-buffer)))))