unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#69511: Restore any state after revert-buffer
@ 2024-03-02 17:55 Juri Linkov
  2024-03-02 18:11 ` Eli Zaretskii
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Juri Linkov @ 2024-03-02 17:55 UTC (permalink / raw)
  To: 69511

[-- Attachment #1: Type: text/plain, Size: 169 bytes --]

This patch adds a new variable 'revert-buffer-state-functions'
that will allow any state to be saved and restored,
not only the currently hard-coded 'read-only' state:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: revert-buffer-state-functions.patch --]
[-- Type: text/x-diff, Size: 1704 bytes --]

diff --git a/lisp/files.el b/lisp/files.el
index ed18bc5841e..e8ecb351759 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -6804,6 +6805,13 @@ revert-buffer-internal-hook
 ;; `preserve-modes' argument of `revert-buffer'.
 (defvar revert-buffer-preserve-modes)
 
+(defvar-local revert-buffer-state-functions nil
+  "Functions to save and restore any state during `revert-buffer'.
+This variable is a list of functions that are called before
+reverting the buffer.  These functions should return a lambda
+that will be called after reverting the buffer
+to restore a previous state saved in that lambda.")
+
 (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.
@@ -6854,13 +6862,16 @@ revert-buffer
   (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))))
+                    (list read-only-mode--state)))
+        (state-functions
+         (delq nil (mapcar #'funcall revert-buffer-state-functions))))
     ;; 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))))))
+        (setq-local read-only-mode--state (car state)))
+      (mapc #'funcall state-functions))))
 
 (defun revert-buffer--default (ignore-auto noconfirm)
   "Default function for `revert-buffer'.

[-- Attachment #3: Type: text/plain, Size: 691 bytes --]


One of numerous examples is to save and restore
hidden outlines after doing 'revert-buffer':

(add-hook 'Buffer-menu-mode-hook
          (lambda ()
            (add-hook 'revert-buffer-state-functions
                      (lambda ()
                        (let ((regexp (outline-hidden-headings-regexp)))
                          (when regexp
                            (lambda ()
                              (outline-hide-by-heading-regexp regexp))))))))

Currently this handles outlines only in the buffer list.
But probably this settings should be added to outline-minor-mode.

And here is the immediate need to use it to rehighlight the outlines
after reverting the buffer list:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: outline-minor-mode-highlight-buffer.patch --]
[-- Type: text/x-diff, Size: 702 bytes --]

diff --git a/lisp/buff-menu.el b/lisp/buff-menu.el
index ec5337e3fda..ae3f3e0035e 100644
--- a/lisp/buff-menu.el
+++ b/lisp/buff-menu.el
@@ -274,7 +276,12 @@ Buffer-menu-mode
   :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-state-functions
+            (lambda ()
+              (when (bound-and-true-p outline-minor-mode)
+                (lambda ()
+                  (outline-minor-mode-highlight-buffer))))))
 
 (defun buffer-menu--display-help ()
   (message "%s"

^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2024-06-04  6:37 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-02 17:55 bug#69511: Restore any state after revert-buffer Juri Linkov
2024-03-02 18:11 ` Eli Zaretskii
2024-03-03  7:59   ` Juri Linkov
2024-03-03  9:04     ` Eli Zaretskii
2024-03-03 17:28       ` Juri Linkov
2024-03-03 17:43         ` Eli Zaretskii
2024-03-03 17:55           ` Juri Linkov
2024-03-03 18:46             ` Eli Zaretskii
2024-06-03  6:35               ` Juri Linkov
2024-06-03 16:55                 ` Juri Linkov
2024-03-03  2:46 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-03  7:55   ` Juri Linkov
2024-03-03  9:03     ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-06-04  6:37 ` Juri Linkov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).