[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#69511: Restore any state after revert-buffer
From: |
Juri Linkov |
Subject: |
bug#69511: Restore any state after revert-buffer |
Date: |
Mon, 03 Jun 2024 09:35:32 +0300 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) |
>> >> revert-buffer-preserve-functions
>> >> revert-buffer-preserve-modes
>> >> revert-buffer-preserve-read-only
>> >> revert-buffer-preserve-outlines
>> >
>> > How about revert-buffer-restore- ?
>>
>> "restore" would be only part of the truth,
>> because these functions first save the state,
>> only their second task is to restore the saved state
>> afterwards.
>
> IMO, "restore" is better than "state", because you don't really
> restore any state: a buffer has no state, per se. "Restore" is also
> better than "preserve".
>
> What is attractive in "restore" is that it is general and generic
> enough to include all the meanings you have shown in your examples.
> So I think "restore" is the best candidate till now. If someone has
> better suggestions, please speak up.
Since no one has better suggestions, here is the patch that uses
"restore". This will create two different name prefixes:
revert-buffer-preserve-modes
revert-buffer-restore-read-only
but this is fine. So here is a complete patch with docs:
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'.
- bug#69511: Restore any state after revert-buffer,
Juri Linkov <=