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, 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.

Probably introduced at or before Emacs version 19.20.

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, `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."
  (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 abbrevs-done)
    (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)
                   ;; Note that killing some buffers may kill others via
                   ;; hooks (e.g. Rmail and its viewing buffer).
                   (and (buffer-live-p buffer)
                        (buffer-modified-p buffer)
                        (not (buffer-base-buffer buffer))
                        (or
                         (buffer-file-name buffer)
                         (with-current-buffer buffer
                           (or (eq buffer-offer-save 'always)
                               (and pred buffer-offer-save
                                    (> (buffer-size) 0)))))
                        (or (not (functionp pred))
                            (with-current-buffer buffer (funcall pred)))
                        (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)))
                 (buffer-list)
                 '("buffer" "buffers" "save")
                 save-some-buffers-action-alist))
          ;; Maybe to save abbrevs, and record whether
          ;; we either saved them or asked to.
          (and save-abbrevs abbrevs-changed
	       (progn
                 (if (or arg
                         (eq save-abbrevs 'silently)
                         (y-or-n-p (format "Save abbrevs in %s? "
                                           abbrev-file-name)))
		     (write-abbrev-file nil))
                 ;; Don't keep bothering user if he says no.
                 (setq abbrevs-changed nil)
                 (setq abbrevs-done t)))
          (or queried (> files-done 0) abbrevs-done
	      (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)))))