diff --git a/doc/lispref/backups.texi b/doc/lispref/backups.texi index 8d0f3806646..a55b0a6aed2 100644 --- a/doc/lispref/backups.texi +++ b/doc/lispref/backups.texi @@ -777,6 +777,16 @@ Reverting may or may not run this hook. @end defvar +@defvar revert-buffer-restore-functions +The value of this variable specifies a list of functions that preserve +the state of the buffer. Before the revert operation each function from +this list is called without arguments, and it should return a lambda +that preserves some particular state (for example, the read-only state). +After the revert operation each lambda will be called one by one in the +order of the list, and it should restore the saved state in the reverted +buffer. +@end defvar + Emacs can revert buffers automatically. It does that by default for buffers visiting files. The following describes how to add support for auto-reverting new types of buffers. diff --git a/etc/NEWS b/etc/NEWS index e5a63cc8a67..571ed359924 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2704,6 +2704,10 @@ treesitter grammar. ** New buffer-local variable 'tabulated-list-groups'. It controls display and separate sorting of groups of entries. ++++ +** New variable 'revert-buffer-restore-functions'. +It helps to preserve various states after reverting the buffer. + --- ** New text property 'context-menu-functions'. Like the variable with the same name, it adds menus from the list that diff --git a/lisp/files.el b/lisp/files.el index b25cca00bb3..210cd0fa7ad 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -6848,6 +6848,24 @@ revert-buffer-internal-hook ;; `preserve-modes' argument of `revert-buffer'. (defvar revert-buffer-preserve-modes) +(defvar revert-buffer-restore-functions '(revert-buffer-restore-read-only) + "Functions to preserve any state during `revert-buffer'. +The value of this variable is a list of functions that are called before +reverting the buffer. Each of these functions are called without +arguments and should return a lambda that can restore a previous state +of the buffer. Then after reverting the buffer each of these lambdas +will be called one by one in the order of the list to restore previous +states of the buffer. An example of the buffer state is keeping the +buffer read-only, or keeping minor modes, etc.") + +(defun revert-buffer-restore-read-only () + "Preserve read-only state for `revert-buffer'." + (when-let ((state (and (boundp 'read-only-mode--state) + (list read-only-mode--state)))) + (lambda () + (setq buffer-read-only (car state)) + (setq-local read-only-mode--state (car state))))) + (defun revert-buffer (&optional ignore-auto noconfirm preserve-modes) "Replace current buffer text with the text of the visited file on disk. This undoes all changes since the file was visited or saved. @@ -6897,14 +6915,13 @@ revert-buffer (interactive (list (not current-prefix-arg))) (let ((revert-buffer-in-progress-p t) (revert-buffer-preserve-modes preserve-modes) - (state (and (boundp 'read-only-mode--state) - (list read-only-mode--state)))) + restore-functions) + (run-hook-wrapped 'revert-buffer-restore-functions + (lambda (f) (push (funcall f) restore-functions) nil)) ;; Return whatever 'revert-buffer-function' returns. (prog1 (funcall (or revert-buffer-function #'revert-buffer--default) ignore-auto noconfirm) - (when state - (setq buffer-read-only (car state)) - (setq-local read-only-mode--state (car state)))))) + (mapc #'funcall (delq nil restore-functions))))) (defun revert-buffer--default (ignore-auto noconfirm) "Default function for `revert-buffer'.