From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#69511: Restore any state after revert-buffer Date: Mon, 03 Jun 2024 09:35:32 +0300 Organization: LINKOV.NET Message-ID: <867cf7pqaj.fsf@mail.linkov.net> References: <86y1b0r00p.fsf@mail.linkov.net> <867cikpkpm.fsf@gnu.org> <864jdnpw83.fsf@mail.linkov.net> <86jzmjofdv.fsf@gnu.org> <86il23kywt.fsf@mail.linkov.net> <864jdnnrd6.fsf@gnu.org> <86wmqjjj3w.fsf@mail.linkov.net> <8634t7nofy.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="6649"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) Cc: 69511@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Jun 03 08:56:12 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sE1c4-0001bK-90 for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 03 Jun 2024 08:56:12 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sE1bj-0003zo-2d; Mon, 03 Jun 2024 02:55:51 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sE1bh-0003yX-IT for bug-gnu-emacs@gnu.org; Mon, 03 Jun 2024 02:55:49 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sE1bh-0008JC-Aw for bug-gnu-emacs@gnu.org; Mon, 03 Jun 2024 02:55:49 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sE1bt-0000A4-QC for bug-gnu-emacs@gnu.org; Mon, 03 Jun 2024 02:56:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 03 Jun 2024 06:56:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 69511 X-GNU-PR-Package: emacs Original-Received: via spool by 69511-submit@debbugs.gnu.org id=B69511.1717397717528 (code B ref 69511); Mon, 03 Jun 2024 06:56:01 +0000 Original-Received: (at 69511) by debbugs.gnu.org; 3 Jun 2024 06:55:17 +0000 Original-Received: from localhost ([127.0.0.1]:51776 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sE1bA-00008S-WB for submit@debbugs.gnu.org; Mon, 03 Jun 2024 02:55:17 -0400 Original-Received: from relay9-d.mail.gandi.net ([217.70.183.199]:58967) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sE1Zc-0008Vu-4O for 69511@debbugs.gnu.org; Mon, 03 Jun 2024 02:53:40 -0400 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id A1815FF805; Mon, 3 Jun 2024 06:53:00 +0000 (UTC) In-Reply-To: <8634t7nofy.fsf@gnu.org> (Eli Zaretskii's message of "Sun, 03 Mar 2024 20:46:25 +0200") X-GND-Sasl: juri@linkov.net X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:286422 Archived-At: --=-=-= Content-Type: text/plain >> >> 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: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=revert-buffer-restore-functions.patch 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'. --=-=-=--