bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#69511: Restore any state after revert-buffer


From: Eli Zaretskii
Subject: bug#69511: Restore any state after revert-buffer
Date: Sun, 03 Mar 2024 19:43:17 +0200

> From: Juri Linkov <juri@linkov.net>
> Cc: 69511@debbugs.gnu.org
> Date: Sun, 03 Mar 2024 19:28:34 +0200
> 
> > Apart of the "state" thing, which I think is sub-optimal for the
> > reasons explained above, this is fine, thanks.
> 
> Like Michael suggested a set of functions should be created.
> These functions should share the same prefix.
> 
> Then revert-buffer-state-read-only will looks like this:
> 
> diff --git a/lisp/files.el b/lisp/files.el
> index 3460c327bb4..82ad748d192 100644
> --- a/lisp/files.el
> +++ b/lisp/files.el
> @@ -6805,6 +6805,24 @@ revert-buffer-internal-hook
>  ;; `preserve-modes' argument of `revert-buffer'.
>  (defvar revert-buffer-preserve-modes)
>  
> +(defvar revert-buffer-state-functions '(revert-buffer-state-read-only)
> +  "Functions to save and restore 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-state-read-only ()
> +  "Save and restore 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.
> @@ -6854,14 +6872,12 @@ 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))))
> +        (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))))))
> +      (mapc #'funcall state-functions))))
>  
>  (defun revert-buffer--default (ignore-auto noconfirm)
>    "Default function for `revert-buffer'.
> 
> So the whole set of functions will be:
> 
> revert-buffer-state-functions
> revert-buffer-state-read-only
> revert-buffer-state-outlines
> ...
> 
> Or like the variable 'preserve-modes' hints above,
> the common prefix could be like this:
> 
> revert-buffer-preserve-functions
> revert-buffer-preserve-modes
> revert-buffer-preserve-read-only
> revert-buffer-preserve-outlines
> ...
> 
> Which prefix would you prefer?

How about revert-buffer-restore- ?





reply via email to

[Prev in Thread] Current Thread [Next in Thread]