]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
Rename background buffer
[gnu-emacs-elpa] / diff-hl.el
1 ;;; diff-hl.el --- VC diff highlighting in the buffer window
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 (require 'cl))
24
25 (defface diff-hl-insert
26 '((t :inherit diff-added))
27 "Face used to highlight inserted lines."
28 :group 'diff-hl)
29
30 (defface diff-hl-delete
31 '((t :inherit diff-removed))
32 "Face used to highlight deleted lines."
33 :group 'diff-hl)
34
35 (defface diff-hl-change
36 '((((class color) (min-colors 88) (background light))
37 :background "#ddddff")
38 (((class color) (min-colors 88) (background dark))
39 :background "#333355"))
40 "Face used to highlight changed lines."
41 :group 'diff-hl)
42
43 (define-fringe-bitmap 'diff-hl-empty [0] 1 1 'center)
44
45 (defun diff-hl-changes ()
46 (let* ((buf-name " *vc-diff-hl* ")
47 (vc-git-diff-switches nil)
48 (vc-hg-diff-switches nil)
49 (vc-diff-switches '("-U0"))
50 (vc-disable-async-diff t)
51 (file (buffer-file-name))
52 (backend (vc-backend file))
53 res)
54 (when backend
55 (vc-call-backend backend 'diff (list file) nil nil buf-name)
56 (with-current-buffer buf-name
57 (goto-char (point-min))
58 (unless (eobp)
59 (diff-beginning-of-hunk t)
60 (while (looking-at diff-hunk-header-re-unified)
61 (let ((line (string-to-number (match-string 3)))
62 (len (let ((m (match-string 4)))
63 (if m (string-to-number m) 1)))
64 (beg (point)))
65 (diff-end-of-hunk)
66 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
67 (deletes (diff-count-matches "^-" beg (point)))
68 (type (cond ((zerop deletes) 'insert)
69 ((zerop inserts) 'delete)
70 (t 'change))))
71 (push (list line len type) res)))))))
72 (nreverse res)))
73
74 (eval-and-compile
75 (dolist (type '(insert delete change))
76 (let* ((type-str (symbol-name type))
77 (spec-sym (intern (concat "diff-hl-" type-str "-spec")))
78 (face-sym (intern (concat "diff-hl-" type-str))))
79 (eval `(defconst ,spec-sym
80 ,(propertize " " 'display
81 `((left-fringe diff-hl-empty ,face-sym))))))))
82
83 (defun diff-hl-update ()
84 (let ((changes (diff-hl-changes))
85 (current-line 1))
86 (save-excursion
87 (goto-char (point-min))
88 (mapc (lambda (o) (when (overlay-get o 'diff-hl) (delete-overlay o)))
89 (overlays-in (point-min) (point-max)))
90 (dolist (c changes)
91 (destructuring-bind (line len type) c
92 (when (eq type 'delete)
93 (setq len 1)
94 (incf line))
95 (forward-line (- line current-line))
96 (setq current-line line)
97 (while (plusp len)
98 (let ((o (make-overlay (point) (line-end-position))))
99 (overlay-put o 'diff-hl t)
100 (overlay-put o 'before-string
101 (case type
102 ('insert diff-hl-insert-spec)
103 ('delete diff-hl-delete-spec)
104 ('change diff-hl-change-spec)))
105 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
106 (overlay-put o 'insert-in-front-hooks '(diff-hl-overlay-modified)))
107 (forward-line 1)
108 (incf current-line)
109 (decf len)))))))
110
111 (defun diff-hl-overlay-modified (ov after-p beg end &optional length)
112 ;; Do the simplest possible thing, for now.
113 (when after-p (delete-overlay ov)))
114
115 (defvar diff-hl-timer nil)
116
117 (defun diff-hl-edit (beg end len)
118 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
119 (when undo-in-progress
120 (when diff-hl-timer
121 (cancel-timer diff-hl-timer))
122 (setq diff-hl-timer
123 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
124
125 (defun diff-hl-after-undo (buffer)
126 (with-current-buffer buffer
127 (unless (buffer-modified-p)
128 (diff-hl-update))))
129
130 ;;;###autoload
131 (define-minor-mode diff-hl-mode
132 "Toggle display of VC diff indicators in the left fringe."
133 :after-hook (diff-hl-update)
134 (if diff-hl-mode
135 (progn
136 (add-hook 'after-save-hook 'diff-hl-update nil t)
137 (add-hook 'after-change-functions 'diff-hl-edit nil t))
138 (remove-hook 'after-save-hook 'diff-hl-update t)
139 (remove-hook 'after-change-functions 'diff-hl-edit t)))
140
141 (defun turn-on-diff-hl-mode ()
142 (when (buffer-file-name)
143 (diff-hl-mode 1)))
144
145 ;;;###autoload
146 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
147 turn-on-diff-hl-mode)
148
149 (provide 'diff-hl)