From: Dmitry Gutov Date: Sun, 31 Jan 2016 01:30:14 +0000 (+0300) Subject: Implement integration with Magit 2.4 X-Git-Url: https://code.delx.au/gnu-emacs-elpa/commitdiff_plain/fdbf34a93d6b249ba20d9e7501dfa026aa88ac04?ds=sidebyside Implement integration with Magit 2.4 Closes #60 https://github.com/magit/magit/issues/2491 https://github.com/magit/magit/pull/2530 --- diff --git a/README.md b/README.md index ab8c9d81e..8de2bf0bc 100644 --- a/README.md +++ b/README.md @@ -90,5 +90,11 @@ psvn Magit ----- -If you have a recent enough version installed, it defines -`magit-revert-buffer-hook` (or `magit-not-reverted-hook`), which we use. +If you're using a version before 2.4.0, it defines `magit-revert-buffer-hook` +(or `magit-not-reverted-hook`), which we use. + +When using Magit 2.4 or newer, add this to your init script: + +```lisp +(add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh t) +``` diff --git a/diff-hl.el b/diff-hl.el index 63c2cde18..9c6f877bd 100644 --- a/diff-hl.el +++ b/diff-hl.el @@ -488,8 +488,8 @@ in the source file, or the last line of the hunk above it." ;; doesn't care about changed VC state. ;; https://github.com/magit/magit/issues/603 (add-hook 'magit-revert-buffer-hook 'diff-hl-update nil t) - ;; Magit 2+ doesn't do the above and calls this instead, - ;; but only when it doesn't call `revert-buffer': + ;; Magit versions 2.0-2.3 don't do the above and call this + ;; instead, but only when they dosn't call `revert-buffer': (add-hook 'magit-not-reverted-hook 'diff-hl-update nil t) (add-hook 'auto-revert-mode-hook 'diff-hl-update nil t) (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t)) @@ -517,6 +517,32 @@ in the source file, or the last line of the hunk above it." (scan diff-hl-command-map) (smartrep-define-key diff-hl-mode-map diff-hl-command-prefix smart-keys)))) +(declare-function magit-toplevel "magit-git") +(declare-function magit-unstaged-files "magit-git") + +(defun diff-hl-magit-post-refresh () + (let* ((topdir (magit-toplevel)) + (modified-files + (mapcar (lambda (file) (expand-file-name file topdir)) + (magit-unstaged-files t))) + (unmodified-states '(up-to-date ignored unregistered))) + (dolist (buf (buffer-list)) + (when (and (buffer-local-value 'diff-hl-mode buf) + (not (buffer-modified-p buf)) + (file-in-directory-p (buffer-file-name buf) topdir)) + (with-current-buffer buf + (let* ((file buffer-file-name) + (backend (vc-backend file))) + (when backend + (cond + ((member file modified-files) + (when (memq (vc-state file) unmodified-states) + (vc-state-refresh file backend)) + (diff-hl-update)) + ((not (memq (vc-state file backend) unmodified-states)) + (vc-state-refresh file backend) + (diff-hl-update)))))))))) + (defun diff-hl-dir-update () (dolist (pair (if (vc-dir-marked-files) (vc-dir-marked-only-files-and-states)