mirror of
https://git.savannah.gnu.org/git/emacs.git
synced 2025-01-28 19:42:02 +00:00
* lisp/files.el (revert-buffer-function): Use a non-nil default.
(revert-buffer-preserve-modes): Declare var to provide access to the `preserve-modes' argument. (revert-buffer): Let-bind it. (revert-buffer--default): New function, extracted from revert-buffer.
This commit is contained in:
parent
2cdeb903c5
commit
ac93e56b69
@ -1,3 +1,11 @@
|
|||||||
|
2013-07-24 Stefan Monnier <monnier@iro.umontreal.ca>
|
||||||
|
|
||||||
|
* files.el (revert-buffer-function): Use a non-nil default.
|
||||||
|
(revert-buffer-preserve-modes): Declare var to
|
||||||
|
provide access to the `preserve-modes' argument.
|
||||||
|
(revert-buffer): Let-bind it.
|
||||||
|
(revert-buffer--default): New function, extracted from revert-buffer.
|
||||||
|
|
||||||
2013-07-24 Stefan Monnier <monnier@iro.umontreal.ca>
|
2013-07-24 Stefan Monnier <monnier@iro.umontreal.ca>
|
||||||
|
|
||||||
* lpr.el: Signal print errors more prominently.
|
* lpr.el: Signal print errors more prominently.
|
||||||
|
220
lisp/files.el
220
lisp/files.el
@ -5246,10 +5246,12 @@ comparison."
|
|||||||
|
|
||||||
|
|
||||||
(put 'revert-buffer-function 'permanent-local t)
|
(put 'revert-buffer-function 'permanent-local t)
|
||||||
(defvar revert-buffer-function nil
|
(defvar revert-buffer-function #'revert-buffer--default
|
||||||
"Function to use to revert this buffer, or nil to do the default.
|
"Function to use to revert this buffer, or nil to do the default.
|
||||||
The function receives two arguments IGNORE-AUTO and NOCONFIRM,
|
The function receives two arguments IGNORE-AUTO and NOCONFIRM,
|
||||||
which are the arguments that `revert-buffer' received.")
|
which are the arguments that `revert-buffer' received.
|
||||||
|
It also has access to the `preserve-modes' argument of `revert-buffer'
|
||||||
|
via the `revert-buffer-preserve-modes' dynamic variable.")
|
||||||
|
|
||||||
(put 'revert-buffer-insert-file-contents-function 'permanent-local t)
|
(put 'revert-buffer-insert-file-contents-function 'permanent-local t)
|
||||||
(defvar revert-buffer-insert-file-contents-function nil
|
(defvar revert-buffer-insert-file-contents-function nil
|
||||||
@ -5296,6 +5298,11 @@ This is true even if a `revert-buffer-function' is being used.")
|
|||||||
|
|
||||||
(defvar revert-buffer-internal-hook)
|
(defvar revert-buffer-internal-hook)
|
||||||
|
|
||||||
|
;; `revert-buffer-function' was defined long ago to be a function of only
|
||||||
|
;; 2 arguments, so we have to use a dynbind variable to pass the
|
||||||
|
;; `preserve-modes' argument of `revert-buffer'.
|
||||||
|
(defvar revert-buffer-preserve-modes)
|
||||||
|
|
||||||
(defun revert-buffer (&optional ignore-auto noconfirm preserve-modes)
|
(defun revert-buffer (&optional ignore-auto noconfirm preserve-modes)
|
||||||
"Replace current buffer text with the text of the visited file on disk.
|
"Replace current buffer text with the text of the visited file on disk.
|
||||||
This undoes all changes since the file was visited or saved.
|
This undoes all changes since the file was visited or saved.
|
||||||
@ -5337,112 +5344,113 @@ non-nil, it is called instead of rereading visited file contents."
|
|||||||
;; reversal of the argument sense. So I'm just changing the user
|
;; reversal of the argument sense. So I'm just changing the user
|
||||||
;; interface, but leaving the programmatic interface the same.
|
;; interface, but leaving the programmatic interface the same.
|
||||||
(interactive (list (not current-prefix-arg)))
|
(interactive (list (not current-prefix-arg)))
|
||||||
(if revert-buffer-function
|
(let ((revert-buffer-in-progress-p t)
|
||||||
(let ((revert-buffer-in-progress-p t))
|
(revert-buffer-preserve-modes preserve-modes))
|
||||||
(funcall revert-buffer-function ignore-auto noconfirm))
|
(funcall (or revert-buffer-function #'revert-buffer--default)
|
||||||
(with-current-buffer (or (buffer-base-buffer (current-buffer))
|
ignore-auto noconfirm)))
|
||||||
(current-buffer))
|
(defun revert-buffer--default (ignore-auto noconfirm)
|
||||||
(let* ((revert-buffer-in-progress-p t)
|
(with-current-buffer (or (buffer-base-buffer (current-buffer))
|
||||||
(auto-save-p (and (not ignore-auto)
|
(current-buffer))
|
||||||
(recent-auto-save-p)
|
(let* ((auto-save-p (and (not ignore-auto)
|
||||||
buffer-auto-save-file-name
|
(recent-auto-save-p)
|
||||||
(file-readable-p buffer-auto-save-file-name)
|
buffer-auto-save-file-name
|
||||||
(y-or-n-p
|
(file-readable-p buffer-auto-save-file-name)
|
||||||
"Buffer has been auto-saved recently. Revert from auto-save file? ")))
|
(y-or-n-p
|
||||||
(file-name (if auto-save-p
|
"Buffer has been auto-saved recently. Revert from auto-save file? ")))
|
||||||
buffer-auto-save-file-name
|
(file-name (if auto-save-p
|
||||||
buffer-file-name)))
|
buffer-auto-save-file-name
|
||||||
(cond ((null file-name)
|
buffer-file-name)))
|
||||||
(error "Buffer does not seem to be associated with any file"))
|
(cond ((null file-name)
|
||||||
((or noconfirm
|
(error "Buffer does not seem to be associated with any file"))
|
||||||
(and (not (buffer-modified-p))
|
((or noconfirm
|
||||||
(catch 'found
|
(and (not (buffer-modified-p))
|
||||||
(dolist (regexp revert-without-query)
|
(catch 'found
|
||||||
(when (string-match regexp file-name)
|
(dolist (regexp revert-without-query)
|
||||||
(throw 'found t)))))
|
(when (string-match regexp file-name)
|
||||||
(yes-or-no-p (format "Revert buffer from file %s? "
|
(throw 'found t)))))
|
||||||
file-name)))
|
(yes-or-no-p (format "Revert buffer from file %s? "
|
||||||
(run-hooks 'before-revert-hook)
|
file-name)))
|
||||||
;; If file was backed up but has changed since,
|
(run-hooks 'before-revert-hook)
|
||||||
;; we should make another backup.
|
;; If file was backed up but has changed since,
|
||||||
(and (not auto-save-p)
|
;; we should make another backup.
|
||||||
(not (verify-visited-file-modtime (current-buffer)))
|
(and (not auto-save-p)
|
||||||
(setq buffer-backed-up nil))
|
(not (verify-visited-file-modtime (current-buffer)))
|
||||||
;; Effectively copy the after-revert-hook status,
|
(setq buffer-backed-up nil))
|
||||||
;; since after-find-file will clobber it.
|
;; Effectively copy the after-revert-hook status,
|
||||||
(let ((global-hook (default-value 'after-revert-hook))
|
;; since after-find-file will clobber it.
|
||||||
(local-hook (when (local-variable-p 'after-revert-hook)
|
(let ((global-hook (default-value 'after-revert-hook))
|
||||||
after-revert-hook))
|
(local-hook (when (local-variable-p 'after-revert-hook)
|
||||||
(inhibit-read-only t))
|
after-revert-hook))
|
||||||
(cond
|
(inhibit-read-only t))
|
||||||
(revert-buffer-insert-file-contents-function
|
(cond
|
||||||
(unless (eq buffer-undo-list t)
|
(revert-buffer-insert-file-contents-function
|
||||||
;; Get rid of all undo records for this buffer.
|
(unless (eq buffer-undo-list t)
|
||||||
(setq buffer-undo-list nil))
|
;; Get rid of all undo records for this buffer.
|
||||||
;; Don't make undo records for the reversion.
|
(setq buffer-undo-list nil))
|
||||||
(let ((buffer-undo-list t))
|
;; Don't make undo records for the reversion.
|
||||||
(funcall revert-buffer-insert-file-contents-function
|
(let ((buffer-undo-list t))
|
||||||
file-name auto-save-p)))
|
(funcall revert-buffer-insert-file-contents-function
|
||||||
((not (file-exists-p file-name))
|
file-name auto-save-p)))
|
||||||
(error (if buffer-file-number
|
((not (file-exists-p file-name))
|
||||||
"File %s no longer exists!"
|
(error (if buffer-file-number
|
||||||
"Cannot revert nonexistent file %s")
|
"File %s no longer exists!"
|
||||||
file-name))
|
"Cannot revert nonexistent file %s")
|
||||||
((not (file-readable-p file-name))
|
file-name))
|
||||||
(error (if buffer-file-number
|
((not (file-readable-p file-name))
|
||||||
"File %s no longer readable!"
|
(error (if buffer-file-number
|
||||||
"Cannot revert unreadable file %s")
|
"File %s no longer readable!"
|
||||||
file-name))
|
"Cannot revert unreadable file %s")
|
||||||
(t
|
file-name))
|
||||||
;; Bind buffer-file-name to nil
|
(t
|
||||||
;; so that we don't try to lock the file.
|
;; Bind buffer-file-name to nil
|
||||||
(let ((buffer-file-name nil))
|
;; so that we don't try to lock the file.
|
||||||
(or auto-save-p
|
(let ((buffer-file-name nil))
|
||||||
(unlock-buffer)))
|
(or auto-save-p
|
||||||
(widen)
|
(unlock-buffer)))
|
||||||
(let ((coding-system-for-read
|
(widen)
|
||||||
;; Auto-saved file should be read by Emacs's
|
(let ((coding-system-for-read
|
||||||
;; internal coding.
|
;; Auto-saved file should be read by Emacs's
|
||||||
(if auto-save-p 'auto-save-coding
|
;; internal coding.
|
||||||
(or coding-system-for-read
|
(if auto-save-p 'auto-save-coding
|
||||||
(and
|
(or coding-system-for-read
|
||||||
buffer-file-coding-system-explicit
|
(and
|
||||||
(car buffer-file-coding-system-explicit))))))
|
buffer-file-coding-system-explicit
|
||||||
(if (and (not enable-multibyte-characters)
|
(car buffer-file-coding-system-explicit))))))
|
||||||
coding-system-for-read
|
(if (and (not enable-multibyte-characters)
|
||||||
(not (memq (coding-system-base
|
coding-system-for-read
|
||||||
coding-system-for-read)
|
(not (memq (coding-system-base
|
||||||
'(no-conversion raw-text))))
|
coding-system-for-read)
|
||||||
;; As a coding system suitable for multibyte
|
'(no-conversion raw-text))))
|
||||||
;; buffer is specified, make the current
|
;; As a coding system suitable for multibyte
|
||||||
;; buffer multibyte.
|
;; buffer is specified, make the current
|
||||||
(set-buffer-multibyte t))
|
;; buffer multibyte.
|
||||||
|
(set-buffer-multibyte t))
|
||||||
|
|
||||||
;; This force after-insert-file-set-coding
|
;; This force after-insert-file-set-coding
|
||||||
;; (called from insert-file-contents) to set
|
;; (called from insert-file-contents) to set
|
||||||
;; buffer-file-coding-system to a proper value.
|
;; buffer-file-coding-system to a proper value.
|
||||||
(kill-local-variable 'buffer-file-coding-system)
|
(kill-local-variable 'buffer-file-coding-system)
|
||||||
|
|
||||||
;; Note that this preserves point in an intelligent way.
|
;; Note that this preserves point in an intelligent way.
|
||||||
(if preserve-modes
|
(if revert-buffer-preserve-modes
|
||||||
(let ((buffer-file-format buffer-file-format))
|
(let ((buffer-file-format buffer-file-format))
|
||||||
(insert-file-contents file-name (not auto-save-p)
|
(insert-file-contents file-name (not auto-save-p)
|
||||||
nil nil t))
|
nil nil t))
|
||||||
(insert-file-contents file-name (not auto-save-p)
|
(insert-file-contents file-name (not auto-save-p)
|
||||||
nil nil t)))))
|
nil nil t)))))
|
||||||
;; Recompute the truename in case changes in symlinks
|
;; Recompute the truename in case changes in symlinks
|
||||||
;; have changed the truename.
|
;; have changed the truename.
|
||||||
(setq buffer-file-truename
|
(setq buffer-file-truename
|
||||||
(abbreviate-file-name (file-truename buffer-file-name)))
|
(abbreviate-file-name (file-truename buffer-file-name)))
|
||||||
(after-find-file nil nil t nil preserve-modes)
|
(after-find-file nil nil t nil revert-buffer-preserve-modes)
|
||||||
;; Run after-revert-hook as it was before we reverted.
|
;; Run after-revert-hook as it was before we reverted.
|
||||||
(setq-default revert-buffer-internal-hook global-hook)
|
(setq-default revert-buffer-internal-hook global-hook)
|
||||||
(if local-hook
|
(if local-hook
|
||||||
(set (make-local-variable 'revert-buffer-internal-hook)
|
(set (make-local-variable 'revert-buffer-internal-hook)
|
||||||
local-hook)
|
local-hook)
|
||||||
(kill-local-variable 'revert-buffer-internal-hook))
|
(kill-local-variable 'revert-buffer-internal-hook))
|
||||||
(run-hooks 'revert-buffer-internal-hook))
|
(run-hooks 'revert-buffer-internal-hook))
|
||||||
t))))))
|
t)))))
|
||||||
|
|
||||||
(defun recover-this-file ()
|
(defun recover-this-file ()
|
||||||
"Recover the visited file--get contents from its last auto-save file."
|
"Recover the visited file--get contents from its last auto-save file."
|
||||||
|
Loading…
Reference in New Issue
Block a user