Function: revert-buffer--default

revert-buffer--default is a byte-compiled function defined in files.el.gz.

Signature

(revert-buffer--default IGNORE-AUTO NOCONFIRM)

Documentation

Default function for revert-buffer.

The arguments IGNORE-AUTO and NOCONFIRM are as described for revert-buffer. Runs the hooks before-revert-hook and after-revert-hook at the start and end. The function returns non-nil if it reverts the buffer; signals an error if the buffer is not associated with a file.

Calls revert-buffer-insert-file-contents-function to reread the contents of the visited file, with two arguments: the first is the file name, the second is non-nil if reading an auto-save file.

This function handles only buffers that are visiting files. Non-file buffers need a custom function.

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun revert-buffer--default (ignore-auto noconfirm)
  "Default function for `revert-buffer'.
The arguments IGNORE-AUTO and NOCONFIRM are as described for `revert-buffer'.
Runs the hooks `before-revert-hook' and `after-revert-hook' at the
start and end.
The function returns non-nil if it reverts the buffer; signals
an error if the buffer is not associated with a file.

Calls `revert-buffer-insert-file-contents-function' to reread the
contents of the visited file, with two arguments: the first is the file
name, the second is non-nil if reading an auto-save file.

This function handles only buffers that are visiting files.
Non-file buffers need a custom function."
  (with-current-buffer (or (buffer-base-buffer (current-buffer))
                           (current-buffer))
    (let* ((auto-save-p (and (not ignore-auto)
                             (recent-auto-save-p)
                             buffer-auto-save-file-name
                             (file-readable-p buffer-auto-save-file-name)
                             (y-or-n-p
                              "Buffer has been auto-saved recently.  Revert from auto-save file? ")))
           (file-name (if auto-save-p
                          buffer-auto-save-file-name
                        buffer-file-name)))
      (cond ((null file-name)
             (error "Buffer does not seem to be associated with any file"))
            ((or noconfirm
                 (and (not (buffer-modified-p))
                      (catch 'found
                        (dolist (regexp revert-without-query)
                          (when (string-match regexp file-name)
                            (throw 'found t)))))
                 (yes-or-no-p
                  (format (if (buffer-modified-p)
                              "Discard edits and reread from %s? "
                            "Revert buffer from file %s? ")
                          file-name)))
             (run-hooks 'before-revert-hook)
             ;; If file was backed up but has changed since,
             ;; we should make another backup.
             (and (not auto-save-p)
                  (not (verify-visited-file-modtime (current-buffer)))
                  (setq buffer-backed-up nil))
             ;; Effectively copy the after-revert-hook status,
             ;; since after-find-file will clobber it.
             (let ((global-hook (default-value 'after-revert-hook))
                   (local-hook (when (local-variable-p 'after-revert-hook)
                                 after-revert-hook))
                   (inhibit-read-only t))
               ;; FIXME: Throw away undo-log when preserve-modes is nil?
               (funcall
                (or revert-buffer-insert-file-contents-function
                    #'revert-buffer-insert-file-contents--default-function)
                file-name auto-save-p)
               ;; Recompute the truename in case changes in symlinks
               ;; have changed the truename.
               (setq buffer-file-truename
                     (abbreviate-file-name (file-truename buffer-file-name)))
               (after-find-file nil nil t nil revert-buffer-preserve-modes)
               ;; Run after-revert-hook as it was before we reverted.
               (setq-default revert-buffer-internal-hook global-hook)
               (if local-hook
                   (setq-local revert-buffer-internal-hook
                        local-hook)
                 (kill-local-variable 'revert-buffer-internal-hook))
               (run-hooks 'revert-buffer-internal-hook))
             t)))))