git-blame: Change installation instructions
[git] / contrib / emacs / git-blame.el
1 ;;; git-blame.el --- Minor mode for incremental blame for Git  -*- coding: utf-8 -*-
2 ;;
3 ;; Copyright (C) 2007  David Kågedal
4 ;;
5 ;; Authors:    David Kågedal <davidk@lysator.liu.se>
6 ;; Created:    31 Jan 2007
7 ;; Message-ID: <87iren2vqx.fsf@morpheus.local>
8 ;; License:    GPL
9 ;; Keywords:   git, version control, release management
10 ;;
11 ;; Compatibility: Emacs21
12
13
14 ;; This file is *NOT* part of GNU Emacs.
15 ;; This file is distributed under the same terms as GNU Emacs.
16
17 ;; This program is free software; you can redistribute it and/or
18 ;; modify it under the terms of the GNU General Public License as
19 ;; published by the Free Software Foundation; either version 2 of
20 ;; the License, or (at your option) any later version.
21
22 ;; This program is distributed in the hope that it will be
23 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
24 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
25 ;; PURPOSE.  See the GNU General Public License for more details.
26
27 ;; You should have received a copy of the GNU General Public
28 ;; License along with this program; if not, write to the Free
29 ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 ;; MA 02111-1307 USA
31
32 ;; http://www.fsf.org/copyleft/gpl.html
33
34
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 ;;
37 ;;; Commentary:
38 ;;
39 ;; Here is an Emacs implementation of incremental git-blame.  When you
40 ;; turn it on while viewing a file, the editor buffer will be updated by
41 ;; setting the background of individual lines to a color that reflects
42 ;; which commit it comes from.  And when you move around the buffer, a
43 ;; one-line summary will be shown in the echo area.
44
45 ;;; Installation:
46 ;;
47 ;; To use this package, put it somewhere in `load-path' (or add
48 ;; directory with git-blame.el to `load-path'), and add the following
49 ;; line to your .emacs:
50 ;;
51 ;;    (require 'git-blame)
52 ;;
53 ;; If you do not want to load this package before it is necessary, you
54 ;; can make use of the `autoload' feature, e.g. by adding to your .emacs
55 ;; the following lines
56 ;;
57 ;;    (autoload 'git-blame-mode "git-blame"
58 ;;              "Minor mode for incremental blame for Git." t)
59 ;;
60 ;; Then first use of `M-x git-blame-mode' would load the package.
61
62 ;;; Compatibility:
63 ;;
64 ;; It requires GNU Emacs 21.  If you'are using Emacs 20, try
65 ;; changing this:
66 ;;
67 ;;            (overlay-put ovl 'face (list :background
68 ;;                                         (cdr (assq 'color (cddddr info)))))
69 ;;
70 ;; to
71 ;;
72 ;;            (overlay-put ovl 'face (cons 'background-color
73 ;;                                         (cdr (assq 'color (cddddr info)))))
74
75
76 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
77 ;;
78 ;;; Code:
79
80 (require 'cl)                         ; to use `push', `pop'
81
82 (defun color-scale (l)
83   (let* ((colors ())
84          r g b)
85     (setq r l)
86     (while r
87       (setq g l)
88       (while g
89         (setq b l)
90         (while b
91           (push (concat "#" (car r) (car g) (car b)) colors)
92           (pop b))
93         (pop g))
94       (pop r))
95     colors))
96
97 (defvar git-blame-dark-colors
98   (color-scale '("00" "04" "08" "0c"
99                  "10" "14" "18" "1c"
100                  "20" "24" "28" "2c"
101                  "30" "34" "38" "3c")))
102
103 (defvar git-blame-light-colors
104   (color-scale '("c0" "c4" "c8" "cc"
105                  "d0" "d4" "d8" "dc"
106                  "e0" "e4" "e8" "ec"
107                  "f0" "f4" "f8" "fc")))
108
109 (defvar git-blame-ancient-color "dark green")
110
111 (defvar git-blame-overlays nil)
112 (defvar git-blame-cache nil)
113
114 (defvar git-blame-mode nil)
115 (make-variable-buffer-local 'git-blame-mode)
116 (unless (assq 'git-blame-mode minor-mode-alist)
117   (setq minor-mode-alist
118         (cons (list 'git-blame-mode " blame")
119               minor-mode-alist)))
120
121 ;;;###autoload
122 (defun git-blame-mode (&optional arg)
123   (interactive "P")
124   (if arg
125       (setq git-blame-mode (eq arg 1))
126     (setq git-blame-mode (not git-blame-mode)))
127   (make-local-variable 'git-blame-overlays)
128   (make-local-variable 'git-blame-colors)
129   (make-local-variable 'git-blame-cache)
130   (let ((bgmode (cdr (assoc 'background-mode (frame-parameters)))))
131     (if (eq bgmode 'dark)
132         (setq git-blame-colors git-blame-dark-colors)
133       (setq git-blame-colors git-blame-light-colors)))
134   (if git-blame-mode
135       (git-blame-run)
136     (git-blame-cleanup)))
137
138 (defun git-blame-run ()
139   (let* ((display-buf (current-buffer))
140          (blame-buf (get-buffer-create
141                      (concat " git blame for " (buffer-name))))
142          (proc (start-process "git-blame" blame-buf
143                              "git" "blame" "--incremental"
144                              (file-name-nondirectory buffer-file-name))))
145     (mapcar 'delete-overlay git-blame-overlays)
146     (setq git-blame-overlays nil)
147     (setq git-blame-cache (make-hash-table :test 'equal))
148     (with-current-buffer blame-buf
149       (erase-buffer)
150       (make-local-variable 'git-blame-file)
151       (make-local-variable 'git-blame-current)
152       (setq git-blame-file display-buf)
153       (setq git-blame-current nil))
154     (set-process-filter proc 'git-blame-filter)
155     (set-process-sentinel proc 'git-blame-sentinel)))
156
157 (defun git-blame-cleanup ()
158   "Remove all blame properties"
159     (mapcar 'delete-overlay git-blame-overlays)
160     (setq git-blame-overlays nil)
161     (let ((modified (buffer-modified-p)))
162       (remove-text-properties (point-min) (point-max) '(point-entered nil))
163       (set-buffer-modified-p modified)))
164
165 (defun git-blame-sentinel (proc status)
166   ;;(kill-buffer (process-buffer proc))
167   (message "git blame finished"))
168
169 (defvar in-blame-filter nil)
170
171 (defun git-blame-filter (proc str)
172   (save-excursion
173     (set-buffer (process-buffer proc))
174     (goto-char (process-mark proc))
175     (insert-before-markers str)
176     (goto-char 0)
177     (unless in-blame-filter
178       (let ((more t)
179             (in-blame-filter t))
180         (while more
181           (setq more (git-blame-parse)))))))
182
183 (defun git-blame-parse ()
184   (cond ((looking-at "\\([0-9a-f]\\{40\\}\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)\n")
185          (let ((hash (match-string 1))
186                (src-line (string-to-number (match-string 2)))
187                (res-line (string-to-number (match-string 3)))
188                (num-lines (string-to-number (match-string 4))))
189            (setq git-blame-current
190                  (git-blame-new-commit
191                   hash src-line res-line num-lines)))
192          (delete-region (point) (match-end 0))
193          t)
194         ((looking-at "filename \\(.+\\)\n")
195          (let ((filename (match-string 1)))
196            (git-blame-add-info "filename" filename))
197          (delete-region (point) (match-end 0))
198          t)
199         ((looking-at "\\([a-z-]+\\) \\(.+\\)\n")
200          (let ((key (match-string 1))
201                (value (match-string 2)))
202            (git-blame-add-info key value))
203          (delete-region (point) (match-end 0))
204          t)
205         ((looking-at "boundary\n")
206          (setq git-blame-current nil)
207          (delete-region (point) (match-end 0))
208          t)
209         (t
210          nil)))
211
212
213 (defun git-blame-new-commit (hash src-line res-line num-lines)
214   (save-excursion
215     (set-buffer git-blame-file)
216     (let ((info (gethash hash git-blame-cache))
217           (inhibit-point-motion-hooks t))
218       (when (not info)
219         (let ((color (pop git-blame-colors)))
220           (unless color
221             (setq color git-blame-ancient-color))
222           (setq info (list hash src-line res-line num-lines
223                            (cons 'color color))))
224         (puthash hash info git-blame-cache))
225       (goto-line res-line)
226       (while (> num-lines 0)
227         (if (get-text-property (point) 'git-blame)
228             (forward-line)
229           (let* ((start (point))
230                  (end (progn (forward-line 1) (point)))
231                  (ovl (make-overlay start end)))
232             (push ovl git-blame-overlays)
233             (overlay-put ovl 'git-blame info)
234             (overlay-put ovl 'help-echo hash)
235             (overlay-put ovl 'face (list :background
236                                          (cdr (assq 'color (cddddr info)))))
237             ;;(overlay-put ovl 'point-entered
238             ;;             `(lambda (x y) (git-blame-identify ,hash)))
239             (let ((modified (buffer-modified-p)))
240               (put-text-property (if (= start 1) start (1- start)) (1- end)
241                                  'point-entered
242                                  `(lambda (x y) (git-blame-identify ,hash)))
243               (set-buffer-modified-p modified))))
244         (setq num-lines (1- num-lines))))))
245
246 (defun git-blame-add-info (key value)
247   (if git-blame-current
248       (nconc git-blame-current (list (cons (intern key) value)))))
249
250 (defun git-blame-current-commit ()
251   (let ((info (get-char-property (point) 'git-blame)))
252     (if info
253         (car info)
254       (error "No commit info"))))
255
256 (defun git-blame-identify (&optional hash)
257   (interactive)
258   (shell-command
259    (format "git log -1 --pretty=oneline %s" (or hash
260                                                 (git-blame-current-commit)))))
261
262 (provide 'git-blame)
263
264 ;;; git-blame.el ends here