]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-amend.el
diff-hl-amend: Add explicit (require)
[gnu-emacs-elpa] / diff-hl-amend.el
1 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
2 ;; URL: https://github.com/dgutov/diff-hl
3
4 ;; This file is not part of GNU Emacs.
5
6 ;; This file is free software: you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation, either version 3 of the License, or
9 ;; (at your option) any later version.
10
11 ;; This file is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
15
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
18
19 ;;; Commentary:
20
21 ;; Toggle in the current buffer with `M-x diff-hl-amend-mode'.
22 ;; Toggle in all buffers with `M-x global-diff-hl-amend-mode'.
23
24 ;;; Code:
25
26 (require 'diff-hl)
27
28 ;;;###autoload
29 (define-minor-mode diff-hl-amend-mode
30 "Show changes against the second-last revision in `diff-hl-mode'.
31 Most useful with VCSes that support rewriting local commits, and
32 'amending' the most recent one in particular.
33 Currently only supports Git, Mercurial and Bazaar."
34 :lighter " Amend"
35 (if diff-hl-amend-mode
36 (progn
37 (if vc-mode
38 (diff-hl-amend-setup)
39 (add-hook 'find-file-hook 'diff-hl-amend-setup nil t))
40 (add-hook 'after-revert-hook 'diff-hl-amend-setup nil t))
41 (remove-hook 'find-file-hook 'diff-hl-amend-setup t)
42 (remove-hook 'after-revert-hook 'diff-hl-amend-setup t)
43 (setq-local diff-hl-reference-revision nil))
44 (when diff-hl-mode
45 (diff-hl-update)))
46
47 (defun diff-hl-amend-setup ()
48 (let ((backend (vc-backend buffer-file-name)))
49 (when backend
50 (setq-local diff-hl-reference-revision
51 (cl-case backend
52 (Git
53 "HEAD^")
54 (Hg
55 "-2")
56 (Bzr
57 "revno:-2"))))))
58
59 ;;;###autoload
60 (define-globalized-minor-mode global-diff-hl-amend-mode diff-hl-amend-mode
61 turn-on-diff-hl-amend-mode)
62
63 (defun turn-on-diff-hl-amend-mode ()
64 "Turn on `diff-hl-amend-mode' in a buffer if appropriate."
65 (and buffer-file-name (diff-hl-amend-mode 1)))
66
67 (provide 'diff-hl-amend)
68
69 ;;; diff-hl-amend.el ends here