2 ;; David Kågedal <davidk@lysator.liu.se>
3 ;; Message-ID: <87iren2vqx.fsf@morpheus.local>
15 (push (concat "#" (car r
) (car g
) (car b
)) colors
)
21 (defvar git-blame-dark-colors
22 (color-scale '("00" "04" "08" "0c"
25 "30" "34" "38" "3c")))
27 (defvar git-blame-light-colors
28 (color-scale '("c0" "c4" "c8" "cc"
31 "f0" "f4" "f8" "fc")))
33 (defvar git-blame-ancient-color
"dark green")
35 (defvar git-blame-overlays nil
)
36 (defvar git-blame-cache nil
)
38 (defvar git-blame-mode nil
)
39 (make-variable-buffer-local 'git-blame-mode
)
40 (push (list 'git-blame-mode
" blame") minor-mode-alist
)
42 (defun git-blame-mode (&optional arg
)
45 (setq git-blame-mode
(eq arg
1))
46 (setq git-blame-mode
(not git-blame-mode
)))
47 (make-local-variable 'git-blame-overlays
)
48 (make-local-variable 'git-blame-colors
)
49 (make-local-variable 'git-blame-cache
)
50 (let ((bgmode (cdr (assoc 'background-mode
(frame-parameters)))))
52 (setq git-blame-colors git-blame-dark-colors
)
53 (setq git-blame-colors git-blame-light-colors
)))
58 (defun git-blame-run ()
59 (let* ((display-buf (current-buffer))
60 (blame-buf (get-buffer-create
61 (concat " git blame for " (buffer-name))))
62 (proc (start-process "git-blame" blame-buf
63 "git" "blame" "--incremental"
64 (file-name-nondirectory buffer-file-name
))))
65 (mapcar 'delete-overlay git-blame-overlays
)
66 (setq git-blame-overlays nil
)
67 (setq git-blame-cache
(make-hash-table :test
'equal
))
68 (with-current-buffer blame-buf
70 (make-local-variable 'git-blame-file
)
71 (make-local-variable 'git-blame-current
)
72 (setq git-blame-file display-buf
)
73 (setq git-blame-current nil
))
74 (set-process-filter proc
'git-blame-filter
)
75 (set-process-sentinel proc
'git-blame-sentinel
)))
77 (defun git-blame-cleanup ()
78 "Remove all blame properties"
79 (mapcar 'delete-overlay git-blame-overlays
)
80 (setq git-blame-overlays nil
)
81 (let ((modified (buffer-modified-p)))
82 (remove-text-properties (point-min) (point-max) '(point-entered nil
))
83 (set-buffer-modified-p modified
)))
85 (defun git-blame-sentinel (proc status
)
86 ;;(kill-buffer (process-buffer proc))
87 (message "git blame finished"))
89 (defvar in-blame-filter nil
)
91 (defun git-blame-filter (proc str
)
93 (set-buffer (process-buffer proc
))
94 (goto-char (process-mark proc
))
95 (insert-before-markers str
)
97 (unless in-blame-filter
101 (setq more
(git-blame-parse)))))))
103 (defun git-blame-parse ()
104 (cond ((looking-at "\\([0-9a-f]\\{40\\}\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)\n")
105 (let ((hash (match-string 1))
106 (src-line (string-to-number (match-string 2)))
107 (res-line (string-to-number (match-string 3)))
108 (num-lines (string-to-number (match-string 4))))
109 (setq git-blame-current
110 (git-blame-new-commit
111 hash src-line res-line num-lines
)))
112 (delete-region (point) (match-end 0))
114 ((looking-at "filename \\(.+\\)\n")
115 (let ((filename (match-string 1)))
116 (git-blame-add-info "filename" filename
))
117 (delete-region (point) (match-end 0))
119 ((looking-at "\\([a-z-]+\\) \\(.+\\)\n")
120 (let ((key (match-string 1))
121 (value (match-string 2)))
122 (git-blame-add-info key value
))
123 (delete-region (point) (match-end 0))
125 ((looking-at "boundary\n")
126 (setq git-blame-current nil
)
127 (delete-region (point) (match-end 0))
133 (defun git-blame-new-commit (hash src-line res-line num-lines
)
135 (set-buffer git-blame-file
)
136 (let ((info (gethash hash git-blame-cache
))
137 (inhibit-point-motion-hooks t
))
139 (let ((color (pop git-blame-colors
)))
141 (setq color git-blame-ancient-color
))
142 (setq info
(list hash src-line res-line num-lines
143 (cons 'color color
))))
144 (puthash hash info git-blame-cache
))
146 (while (> num-lines
0)
147 (if (get-text-property (point) 'git-blame
)
149 (let* ((start (point))
150 (end (progn (forward-line 1) (point)))
151 (ovl (make-overlay start end
)))
152 (push ovl git-blame-overlays
)
153 (overlay-put ovl
'git-blame info
)
154 (overlay-put ovl
'help-echo hash
)
155 (overlay-put ovl
'face
(list :background
156 (cdr (assq 'color
(cddddr info
)))))
157 ;;(overlay-put ovl 'point-entered
158 ;; `(lambda (x y) (git-blame-identify ,hash)))
159 (let ((modified (buffer-modified-p)))
160 (put-text-property (if (= start
1) start
(1- start
)) (1- end
)
162 `(lambda (x y
) (git-blame-identify ,hash
)))
163 (set-buffer-modified-p modified
))))
164 (setq num-lines
(1- num-lines
))))))
166 (defun git-blame-add-info (key value
)
167 (if git-blame-current
168 (nconc git-blame-current
(list (cons (intern key
) value
)))))
170 (defun git-blame-current-commit ()
171 (let ((info (get-char-property (point) 'git-blame
)))
174 (error "No commit info"))))
176 (defun git-blame-identify (&optional hash
)
179 (format "git log -1 --pretty=oneline %s" (or hash
180 (git-blame-current-commit)))))