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

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

bug#51016: 28.0.50; 'diff-font-lock-prettify' breaks display of outline


From: Matthias Meulien
Subject: bug#51016: 28.0.50; 'diff-font-lock-prettify' breaks display of outline headers
Date: Mon, 08 Nov 2021 23:38:43 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Lars Ingebrigtsen <larsi@gnus.org> writes:

> Juri Linkov <juri@linkov.net> writes:
>
>> This patch broke vc-diff.  For example, after 'C-x v L' type 'd',
>> and check that *vc-diff* buffer doesn't have buffer-local variables
>> diff-vc-backend, diff-vc-revisions anymore, because they were defined
>> in vc-diff-internal after diff-mode, but now the postponed diff-mode
>> kills these buffer-local variables:
>>
>>      (set-buffer buffer)
>> -    (diff-mode)
>>      (setq-local diff-vc-backend (car vc-fileset))
>>      (setq-local diff-vc-revisions (list rev1 rev2))
>
> Right.  Matthias -- can this be fixed in a different way that doesn't
> postpone changing the major mode?

Thanks Juri!

I moved the call to `diff-mode` since in its implementation one
initialize `diff-buffer-type` by searching:

  (save-excursion
    (setq-local diff-buffer-type
                (if (re-search-forward "^diff --git" nil t)
                    'git
                  nil)))

I suggest to restore the original call to `diff-mode` (before the
asynchronous operation completes) and keep the initialization of
`diff-buffer-type` by searching but make it available to
`vc-diff-finish' which is called when the asynchronous operation
completes.

See the attached patch for details.

>From 61d0a96df15f3fe4c44ade01747246b05fed2a48 Mon Sep 17 00:00:00 2001
From: Matthias Meulien <orontee@gmail.com>
Date: Mon, 8 Nov 2021 23:37:33 +0100
Subject: [PATCH] Fix local variables overwritten when diff-mode is set

* lisp/vc/diff-mode.el (diff-mode):
(diff-setup-buffer-type): Defun to initialize `diff-buffer-type'

* lisp/vc/vc.el (vc-diff-finish): Set `diff-buffer-type' after content
inserted
(vc-diff-internal): Restore `diff-mode' being set before local variables
---
 lisp/vc/diff-mode.el | 26 ++++++++++++++++----------
 lisp/vc/vc.el        |  6 +++---
 2 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index 746f76b46c..e68aa2257d 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -1540,16 +1540,7 @@ diff-mode
                 #'diff--filter-substring)
   (unless buffer-file-name
     (hack-dir-local-variables-non-file-buffer))
-  (save-excursion
-    (setq-local diff-buffer-type
-                (if (re-search-forward "^diff --git" nil t)
-                    'git
-                  nil)))
-  (when (eq diff-buffer-type 'git)
-    (setq diff-outline-regexp
-          (concat "\\(^diff --git.*\n\\|" diff-hunk-header-re "\\)"))
-    (setq-local outline-level #'diff--outline-level))
-  (setq-local outline-regexp diff-outline-regexp))
+  (diff-setup-buffer-type))
 
 ;;;###autoload
 (define-minor-mode diff-minor-mode
@@ -1585,6 +1576,21 @@ diff-setup-whitespace
                     "^[-+!] .*?\\([\t ]+\\)$"
                   "^[-+!<>].*?\\([\t ]+\\)$"))))
 
+(defun diff-setup-buffer-type ()
+  "Try to guess the `diff-buffer-type' from content of current Diff mode 
buffer.
+`outline-regexp' is updated accordingly."
+  (save-excursion
+    (goto-char (point-min))
+    (setq-local diff-buffer-type
+                (if (re-search-forward "^diff --git" nil t)
+                    'git
+                  nil)))
+  (when (eq diff-buffer-type 'git)
+    (setq diff-outline-regexp
+          (concat "\\(^diff --git.*\n\\|" diff-hunk-header-re "\\)"))
+    (setq-local outline-level #'diff--outline-level))
+  (setq-local outline-regexp diff-outline-regexp))
+
 (defun diff-delete-if-empty ()
   ;; An empty diff file means there's no more diffs to integrate, so we
   ;; can just remove the file altogether.  Very handy for .rej files if we
diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el
index c9500f454a..87137d8ede 100644
--- a/lisp/vc/vc.el
+++ b/lisp/vc/vc.el
@@ -1739,6 +1739,7 @@ vc-diff-finish
               (insert (cdr messages) ".\n")
               (message "%s" (cdr messages))))
        (diff-setup-whitespace)
+        (diff-setup-buffer-type)
        (goto-char (point-min))
        (when window
          (shrink-window-if-larger-than-buffer window)))
@@ -1804,6 +1805,7 @@ vc-diff-internal
         (setq files (nreverse filtered))))
     (vc-call-backend (car vc-fileset) 'diff files rev1 rev2 buffer async)
     (set-buffer buffer)
+    (diff-mode)
     (setq-local diff-vc-backend (car vc-fileset))
     (setq-local diff-vc-revisions (list rev1 rev2))
     (setq-local revert-buffer-function
@@ -1825,9 +1827,7 @@ vc-diff-internal
       ;; after `pop-to-buffer'; the former assumes the diff buffer is
       ;; shown in some window.
       (let ((buf (current-buffer)))
-        (vc-run-delayed (progn
-                          (vc-diff-finish buf (when verbose messages))
-                          (diff-mode))))
+        (vc-run-delayed (vc-diff-finish buf (when verbose messages))))
       ;; In the async case, we return t even if there are no differences
       ;; because we don't know that yet.
       t)))
-- 
2.30.2

-- 
Matthias

reply via email to

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