]> git.eshelyaron.com Git - emacs.git/commitdiff
doc-view: Fix error during revert in editing mode
authorTassilo Horn <tsdh@gnu.org>
Sun, 23 Feb 2025 08:46:54 +0000 (09:46 +0100)
committerEshel Yaron <me@eshelyaron.com>
Tue, 4 Mar 2025 20:57:17 +0000 (21:57 +0100)
When in editing mode in a doc-view buffer and then reverting (which can
happen automatically when editing OpenDocument contents), we errored in
the advide function doc-view--revert-buffer because the local
doc-view--buffer-file-name has been killed when switching to the editing
mode.

Also restore doc-view-minor-mode after reverting during being in the
editing mode.

* lisp/doc-view.el (doc-view--revert-buffer): Check that
'doc-view--buffer-file-name' is non-nil.
(doc-view-minor-mode): Add re-enabling function to
'revert-buffer-restore-functions'.

(cherry picked from commit 34362a2a1ccd229b486d87cbf4de2eed66e0010d)

lisp/doc-view.el

index e836c21235649213cb6f4407e336e9a86d8c12eb..44b81706033570ac18bf772d68adb21f8abe82d6 100644 (file)
@@ -588,11 +588,15 @@ Typically \"page-%s.png\".")
   (cl-labels ((revert ()
                 (let ((revert-buffer-preserve-modes t))
                   (apply orig-fun args)
-                  ;; Update the cached version of the pdf file,
-                  ;; too.  This is the one that's used when
-                  ;; rendering (bug#26996).
-                  (unless (equal buffer-file-name
-                                 doc-view--buffer-file-name)
+                  ;; Update the cached version of the pdf file, too.
+                  ;; This is the one that's used when rendering
+                  ;; (bug#26996).  doc-view--buffer-file-name is nil in
+                  ;; the case where we've switched to the editing mode
+                  ;; (bug#76478).  In that case, we'll update the cached
+                  ;; version when switching back to doc-view-mode.
+                  (when (and doc-view--buffer-file-name
+                             (not (equal buffer-file-name
+                                         doc-view--buffer-file-name)))
                     ;; FIXME: Lars says he needed to recreate
                     ;; the dir, we should figure out why.
                     (doc-view-make-safe-dir doc-view-cache-directory)
@@ -2440,7 +2444,20 @@ to the next best mode."
 See the command `doc-view-mode' for more information on this mode."
   :lighter " DocView"
   (when doc-view-minor-mode
-    (add-hook 'change-major-mode-hook (lambda () (doc-view-minor-mode -1)) nil t)
+    (add-hook 'change-major-mode-hook
+              (lambda ()
+                (doc-view-minor-mode -1))
+              nil t)
+    ;; OpenDocuments are archive files, so their editing mode is
+    ;; archive-mode.  When editing and saving a file in that archive,
+    ;; it'll automatically revert the archive buffer.  Take care to
+    ;; re-enable `doc-view-minor-mode' in that case.
+    (add-hook 'revert-buffer-restore-functions
+              (lambda ()
+                (lambda ()
+                  (unless (derived-mode-p 'doc-view-mode)
+                    (doc-view-minor-mode 1))))
+              nil t)
     (message
      "%s"
      (substitute-command-keys