]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
lexical-binding: t
[gnu-emacs-elpa] / diff-hl.el
1 ;;; diff-hl.el --- VC diff fringe highlighting -*- lexical-binding: t -*-
2
3 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
4 ;; Keywords: vc, diff
5
6 ;; This file is not part of GNU Emacs.
7
8 ;; This file is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This file is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20
21 (require 'diff-mode)
22 (require 'vc)
23 (eval-when-compile
24 (require 'cl)
25 (require 'vc-git)
26 (require 'vc-hg))
27
28 (defface diff-hl-insert
29 '((t :inherit diff-added))
30 "Face used to highlight inserted lines."
31 :group 'diff-hl)
32
33 (defface diff-hl-delete
34 '((t :inherit diff-removed))
35 "Face used to highlight deleted lines."
36 :group 'diff-hl)
37
38 (defface diff-hl-change
39 '((((class color) (min-colors 88) (background light))
40 :background "#ddddff")
41 (((class color) (min-colors 88) (background dark))
42 :background "#333355"))
43 "Face used to highlight changed lines."
44 :group 'diff-hl)
45
46 (define-fringe-bitmap 'diff-hl-empty [0] 1 1 'center)
47
48 (defun diff-hl-changes ()
49 (let* ((buf-name " *vc-diff-hl* ")
50 (vc-git-diff-switches nil)
51 (vc-hg-diff-switches nil)
52 (vc-diff-switches '("-U0"))
53 (vc-disable-async-diff t)
54 (file (buffer-file-name))
55 (backend (vc-backend file))
56 res)
57 (when backend
58 (vc-call-backend backend 'diff (list file) nil nil buf-name)
59 (with-current-buffer buf-name
60 (goto-char (point-min))
61 (unless (eobp)
62 (diff-beginning-of-hunk t)
63 (while (looking-at diff-hunk-header-re-unified)
64 (let ((line (string-to-number (match-string 3)))
65 (len (let ((m (match-string 4)))
66 (if m (string-to-number m) 1)))
67 (beg (point)))
68 (diff-end-of-hunk)
69 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
70 (deletes (diff-count-matches "^-" beg (point)))
71 (type (cond ((zerop deletes) 'insert)
72 ((zerop inserts) 'delete)
73 (t 'change))))
74 (push (list line len type) res)))))))
75 (nreverse res)))
76
77 (eval-and-compile
78 (dolist (type '(insert delete change))
79 (let* ((type-str (symbol-name type))
80 (spec-sym (intern (concat "diff-hl-" type-str "-spec")))
81 (face-sym (intern (concat "diff-hl-" type-str))))
82 (eval `(defconst ,spec-sym
83 ,(propertize " " 'display
84 `((left-fringe diff-hl-empty ,face-sym))))))))
85
86 (defun diff-hl-update ()
87 (let ((changes (diff-hl-changes))
88 (current-line 1))
89 (save-excursion
90 (goto-char (point-min))
91 (mapc (lambda (o) (when (overlay-get o 'diff-hl) (delete-overlay o)))
92 (overlays-in (point-min) (point-max)))
93 (dolist (c changes)
94 (destructuring-bind (line len type) c
95 (when (eq type 'delete)
96 (setq len 1)
97 (incf line))
98 (forward-line (- line current-line))
99 (setq current-line line)
100 (while (plusp len)
101 (let ((o (make-overlay (point) (line-end-position))))
102 (overlay-put o 'diff-hl t)
103 (overlay-put o 'before-string
104 (case type
105 ('insert diff-hl-insert-spec)
106 ('delete diff-hl-delete-spec)
107 ('change diff-hl-change-spec)))
108 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
109 (overlay-put o 'insert-in-front-hooks '(diff-hl-overlay-modified)))
110 (forward-line 1)
111 (incf current-line)
112 (decf len)))))))
113
114 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
115 ;; Do the simplest possible thing, for now.
116 (when after-p (delete-overlay ov)))
117
118 (defvar diff-hl-timer nil)
119
120 (defun diff-hl-edit (_beg _end _len)
121 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
122 (when undo-in-progress
123 (when diff-hl-timer
124 (cancel-timer diff-hl-timer))
125 (setq diff-hl-timer
126 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
127
128 (defun diff-hl-after-undo (buffer)
129 (with-current-buffer buffer
130 (unless (buffer-modified-p)
131 (diff-hl-update))))
132
133 ;;;###autoload
134 (define-minor-mode diff-hl-mode
135 "Toggle display of VC diff indicators in the left fringe."
136 :after-hook (diff-hl-update)
137 (if diff-hl-mode
138 (progn
139 (add-hook 'after-save-hook 'diff-hl-update nil t)
140 (add-hook 'after-change-functions 'diff-hl-edit nil t))
141 (remove-hook 'after-save-hook 'diff-hl-update t)
142 (remove-hook 'after-change-functions 'diff-hl-edit t)))
143
144 (defun turn-on-diff-hl-mode ()
145 (when (buffer-file-name)
146 (diff-hl-mode 1)))
147
148 ;;;###autoload
149 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
150 turn-on-diff-hl-mode)
151
152 (provide 'diff-hl)