From 07875ee72bffac01a1978d0367883d6ceb88cc55 Mon Sep 17 00:00:00 2001 From: Chong Yidong Date: Wed, 25 Apr 2012 23:06:51 +0800 Subject: [PATCH] Fix whitespace highlighting of context diffs. * lisp/vc/diff-mode.el (diff-setup-whitespace): New function. (diff-mode): Use it. * lisp/vc/diff.el (diff-sentinel): * lisp/vc/vc.el (vc-diff-finish): Call diff-setup-whitespace to assign Whitespace mode variables based on diff style. Fixes: debbugs:8612 --- lisp/ChangeLog | 9 +++++++++ lisp/vc/diff-mode.el | 22 +++++++++++++++++----- lisp/vc/diff.el | 3 +++ lisp/vc/vc.el | 21 ++++++++++++--------- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index cad73c2e2e7..4ec3b1934a4 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,12 @@ +2012-04-25 Chong Yidong + + * vc/diff-mode.el (diff-setup-whitespace): New function. + (diff-mode): Use it. + + * vc/diff.el (diff-sentinel): + * vc/vc.el (vc-diff-finish): Call diff-setup-whitespace to assign + Whitespace mode variables based on diff style (Bug#8612). + 2012-04-25 Leo Liu * files.el (auto-mode-alist): Use javascript-mode instead. diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 8b6b85dd22e..c92371fc90b 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -1283,11 +1283,7 @@ a diff with \\[diff-reverse-direction]. (set (make-local-variable 'end-of-defun-function) 'diff-end-of-file) - ;; Set up `whitespace-mode' so that turning it on will show trailing - ;; whitespace problems on the modified lines of the diff. - (set (make-local-variable 'whitespace-style) '(face trailing)) - (set (make-local-variable 'whitespace-trailing-regexp) - "^[-\+!<>].*?\\([\t ]+\\)$") + (diff-setup-whitespace) (setq buffer-read-only diff-default-read-only) ;; setup change hooks @@ -1332,6 +1328,22 @@ the mode if ARG is omitted or nil. ;;; Handy hook functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(defun diff-setup-whitespace () + "Set up Whitespace mode variables for the current Diff mode buffer. +This sets `whitespace-style' and `whitespace-trailing-regexp' so +that Whitespace mode shows trailing whitespace problems on the +modified lines of the diff." + (set (make-local-variable 'whitespace-style) '(face trailing)) + (let ((style (save-excursion + (goto-char (point-min)) + (when (re-search-forward diff-hunk-header-re nil t) + (goto-char (match-beginning 0)) + (diff-hunk-style))))) + (set (make-local-variable 'whitespace-trailing-regexp) + (if (eq style 'context) + "^[-\+!] .*?\\([\t ]+\\)$" + "^[-\+!<>].*?\\([\t ]+\\)$")))) + (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/diff.el b/lisp/vc/diff.el index 05208894356..dd4b4757e88 100644 --- a/lisp/vc/diff.el +++ b/lisp/vc/diff.el @@ -30,6 +30,8 @@ ;;; Code: +(declare-function diff-setup-whitespace "diff-mode" ()) + (eval-when-compile (require 'cl)) (defgroup diff nil @@ -64,6 +66,7 @@ If optional args OLD-TEMP-FILE and/or NEW-TEMP-FILE are non-nil, delete the temporary files so named." (if old-temp-file (delete-file old-temp-file)) (if new-temp-file (delete-file new-temp-file)) + (diff-setup-whitespace) (save-excursion (goto-char (point-max)) (let ((inhibit-read-only t)) diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el index 9ca9e00b8af..433383502da 100644 --- a/lisp/vc/vc.el +++ b/lisp/vc/vc.el @@ -655,6 +655,8 @@ (require 'vc-dispatcher) (require 'ediff) +(declare-function diff-setup-whitespace "diff-mode" ()) + (eval-when-compile (require 'cl) (require 'dired)) @@ -1524,17 +1526,18 @@ to override the value of `vc-diff-switches' and `diff-switches'." ;; possibility of an empty output is for an async process. (when (buffer-live-p buffer) (let ((window (get-buffer-window buffer t)) - (emptyp (zerop (buffer-size buffer)))) + (emptyp (zerop (buffer-size buffer)))) (with-current-buffer buffer - (and messages emptyp - (let ((inhibit-read-only t)) - (insert (cdr messages) ".\n") - (message "%s" (cdr messages)))) - (goto-char (point-min)) - (when window - (shrink-window-if-larger-than-buffer window))) + (and messages emptyp + (let ((inhibit-read-only t)) + (insert (cdr messages) ".\n") + (message "%s" (cdr messages)))) + (diff-setup-whitespace) + (goto-char (point-min)) + (when window + (shrink-window-if-larger-than-buffer window))) (when (and messages (not emptyp)) - (message "%sdone" (car messages)))))) + (message "%sdone" (car messages)))))) (defvar vc-diff-added-files nil "If non-nil, diff added files by comparing them to /dev/null.") -- 2.39.2