From 2d113afece1124e5d04947d4c367451c95c7bbd3 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Mon, 3 Jun 2024 19:55:47 +0300 Subject: [PATCH] New variable 'revert-buffer-restore-functions' (bug#69511) * doc/lispref/backups.texi (Reverting): Add documentation for 'revert-buffer-restore-functions'. * lisp/files.el (revert-buffer-restore-functions): New variable. (revert-buffer-restore-read-only): New function. (revert-buffer): Use 'revert-buffer-restore-functions' with the default value 'revert-buffer-restore-read-only' (bug#69511). * lisp/buff-menu.el (Buffer-menu-mode): Add hook 'revert-buffer-restore-functions' to restore outline-minor-mode highlighting. (cherry picked from commit a525cfb3af0c49c5c64e8af548ab23d086348fed) --- doc/lispref/backups.texi | 10 ++++++++++ etc/NEWS | 4 ++++ lisp/buff-menu.el | 10 +++++++++- lisp/files.el | 27 ++++++++++++++++++++++----- 4 files changed, 45 insertions(+), 6 deletions(-) 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 @@ after inserting the modified contents. A custom @code{revert-buffer-function} 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 a0a98d2b797..eb2f9290725 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2907,6 +2907,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/buff-menu.el b/lisp/buff-menu.el index d59c5b6cf21..b431637414b 100644 --- a/lisp/buff-menu.el +++ b/lisp/buff-menu.el @@ -232,6 +232,8 @@ then the buffer will be displayed in the buffer list.") ["Quit" quit-window :help "Remove the buffer menu from the display"])) +(declare-function outline-minor-mode-highlight-buffer "outline" ()) + (define-derived-mode Buffer-menu-mode tabulated-list-mode "Buffer Menu" "Major mode for Buffer Menu buffers. The Buffer Menu is invoked by the commands \\[list-buffers], @@ -274,7 +276,13 @@ In Buffer Menu mode, the following commands are defined: :interactive nil (setq-local buffer-stale-function (lambda (&optional _noconfirm) 'fast)) - (add-hook 'tabulated-list-revert-hook 'list-buffers--refresh nil t)) + (add-hook 'tabulated-list-revert-hook 'list-buffers--refresh nil t) + (add-hook 'revert-buffer-restore-functions + (lambda () + (when (bound-and-true-p outline-minor-mode) + (lambda () + (outline-minor-mode-highlight-buffer)))) + nil t)) (defun buffer-menu--display-help () (message "%s" diff --git a/lisp/files.el b/lisp/files.el index 85d986cf554..fc26655701b 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -6898,6 +6898,24 @@ A customized `revert-buffer-function' need not run this 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. @@ -6947,14 +6965,13 @@ preserve markers and overlays, at the price of being slower." (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'. -- 2.39.2