From 358b8d0361c8152026aeb263f0d0fc29d76d2362 Mon Sep 17 00:00:00 2001 From: Matthias Meulien Date: Tue, 9 Nov 2021 04:46:53 +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 (bug#51016). --- 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 746f76b46c3..9bac3aa67d8 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -1475,6 +1475,21 @@ See `after-change-functions' for the meaning of BEG, END and LEN." ;; Added when diff--font-lock-prettify is non-nil! (cl-pushnew 'display font-lock-extra-managed-props))) +(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)) + (defvar whitespace-style) (defvar whitespace-trailing-regexp) @@ -1540,16 +1555,7 @@ a diff with \\[diff-reverse-direction]. #'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 diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el index c9500f454ae..87137d8ede2 100644 --- a/lisp/vc/vc.el +++ b/lisp/vc/vc.el @@ -1739,6 +1739,7 @@ to override the value of `vc-diff-switches' and `diff-switches'." (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 @@ Return t if the buffer had changes, nil otherwise." (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 @@ Return t if the buffer had changes, nil otherwise." ;; 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.39.5