~ubuntu-branches/ubuntu/gutsy/git-core/gutsy-updates

« back to all changes in this revision

Viewing changes to contrib/emacs/git-blame.el

  • Committer: Package Import Robot
  • Author(s): Gerrit Pape
  • Date: 2007-05-30 12:38:45 UTC
  • mfrom: (1.1.17)
  • Revision ID: package-import@ubuntu.com-20070530123845-mhso4051d7vx1189
Tags: 1:1.5.2-1
* merge branch debian-experimental.
* new upstream release.
  * gitweb: choose appropriate view for file type if a= parameter missing
    (closes: #410465).
  * git fetch -q is supported (closes: #423165).
* /usr/bin/git transition (thx Ian Beckwith!).
  * debian/git-core.preinst: new; remove /usr/bin/git alternative if
    upgrading from versions older than 1:1.5.2~rc3-2.
  * debian/git-core.prerm, debian/git-core.postinst: remove; no longer
    handle /usr/bin/git alternative through update-alternatives.
  * debian/rules: no longer install git program as git-scm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
;; License:    GPL
9
9
;; Keywords:   git, version control, release management
10
10
;;
11
 
;; Compatibility: Emacs21
12
 
 
 
11
;; Compatibility: Emacs21, Emacs22 and EmacsCVS
 
12
;;                Git 1.5 and up
13
13
 
14
14
;; This file is *NOT* part of GNU Emacs.
15
15
;; This file is distributed under the same terms as GNU Emacs.
61
61
 
62
62
;;; Compatibility:
63
63
;;
64
 
;; It requires GNU Emacs 21.  If you'are using Emacs 20, try
65
 
;; changing this:
 
64
;; It requires GNU Emacs 21 or later and Git 1.5.0 and up
 
65
;;
 
66
;; If you'are using Emacs 20, try changing this:
66
67
;;
67
68
;;            (overlay-put ovl 'face (list :background
68
69
;;                                         (cdr (assq 'color (cddddr info)))))
77
78
;;
78
79
;;; Code:
79
80
 
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))
 
81
(eval-when-compile (require 'cl))                             ; to use `push', `pop'
 
82
 
 
83
 
 
84
(defun git-blame-color-scale (&rest elements)
 
85
  "Given a list, returns a list of triples formed with each
 
86
elements of the list.
 
87
 
 
88
a b => bbb bba bab baa abb aba aaa aab"
 
89
  (let (result)
 
90
    (dolist (a elements)
 
91
      (dolist (b elements)
 
92
        (dolist (c elements)
 
93
          (setq result (cons (format "#%s%s%s" a b c) result)))))
 
94
    result))
 
95
 
 
96
;; (git-blame-color-scale "0c" "04" "24" "1c" "2c" "34" "14" "3c") =>
 
97
;; ("#3c3c3c" "#3c3c14" "#3c3c34" "#3c3c2c" "#3c3c1c" "#3c3c24"
 
98
;; "#3c3c04" "#3c3c0c" "#3c143c" "#3c1414" "#3c1434" "#3c142c" ...)
 
99
 
 
100
(defmacro git-blame-random-pop (l)
 
101
  "Select a random element from L and returns it. Also remove
 
102
selected element from l."
 
103
  ;; only works on lists with unique elements
 
104
  `(let ((e (elt ,l (random (length ,l)))))
 
105
     (setq ,l (remove e ,l))
 
106
     e))
96
107
 
97
108
(defvar git-blame-dark-colors
98
 
  (color-scale '("0c" "04" "24" "1c" "2c" "34" "14" "3c")))
 
109
  (git-blame-color-scale "0c" "04" "24" "1c" "2c" "34" "14" "3c")
 
110
  "*List of colors (format #RGB) to use in a dark environment.
 
111
 
 
112
To check out the list, evaluate (list-colors-display git-blame-dark-colors).")
99
113
 
100
114
(defvar git-blame-light-colors
101
 
  (color-scale '("c4" "d4" "cc" "dc" "f4" "e4" "fc" "ec")))
102
 
 
103
 
(defvar git-blame-ancient-color "dark green")
 
115
  (git-blame-color-scale "c4" "d4" "cc" "dc" "f4" "e4" "fc" "ec")
 
116
  "*List of colors (format #RGB) to use in a light environment.
 
117
 
 
118
To check out the list, evaluate (list-colors-display git-blame-light-colors).")
 
119
 
 
120
(defvar git-blame-colors '()
 
121
  "Colors used by git-blame. The list is built once when activating git-blame
 
122
minor mode.")
 
123
 
 
124
(defvar git-blame-ancient-color "dark green"
 
125
  "*Color to be used for ancient commit.")
104
126
 
105
127
(defvar git-blame-autoupdate t
106
128
  "*Automatically update the blame display while editing")
125
147
  "A queue of update requests")
126
148
(make-variable-buffer-local 'git-blame-update-queue)
127
149
 
 
150
;; FIXME: docstrings
 
151
(defvar git-blame-file nil)
 
152
(defvar git-blame-current nil)
 
153
 
128
154
(defvar git-blame-mode nil)
129
155
(make-variable-buffer-local 'git-blame-mode)
130
 
(unless (assq 'git-blame-mode minor-mode-alist)
131
 
  (setq minor-mode-alist
132
 
        (cons (list 'git-blame-mode " blame")
133
 
              minor-mode-alist)))
 
156
 
 
157
(defvar git-blame-mode-line-string " blame"
 
158
  "String to display on the mode line when git-blame is active.")
 
159
 
 
160
(or (assq 'git-blame-mode minor-mode-alist)
 
161
    (setq minor-mode-alist
 
162
          (cons '(git-blame-mode git-blame-mode-line-string) minor-mode-alist)))
134
163
 
135
164
;;;###autoload
136
165
(defun git-blame-mode (&optional arg)
137
 
  "Minor mode for displaying Git blame"
 
166
  "Toggle minor mode for displaying Git blame
 
167
 
 
168
With prefix ARG, turn the mode on if ARG is positive."
138
169
  (interactive "P")
139
 
  (if arg
140
 
      (setq git-blame-mode (eq arg 1))
141
 
    (setq git-blame-mode (not git-blame-mode)))
 
170
  (cond
 
171
   ((null arg)
 
172
    (if git-blame-mode (git-blame-mode-off) (git-blame-mode-on)))
 
173
   ((> (prefix-numeric-value arg) 0) (git-blame-mode-on))
 
174
   (t (git-blame-mode-off))))
 
175
 
 
176
(defun git-blame-mode-on ()
 
177
  "Turn on git-blame mode.
 
178
 
 
179
See also function `git-blame-mode'."
142
180
  (make-local-variable 'git-blame-colors)
143
181
  (if git-blame-autoupdate
144
182
      (add-hook 'after-change-functions 'git-blame-after-change nil t)
145
183
    (remove-hook 'after-change-functions 'git-blame-after-change t))
146
184
  (git-blame-cleanup)
147
 
  (if git-blame-mode
148
 
      (progn
149
 
        (let ((bgmode (cdr (assoc 'background-mode (frame-parameters)))))
150
 
          (if (eq bgmode 'dark)
151
 
              (setq git-blame-colors git-blame-dark-colors)
152
 
            (setq git-blame-colors git-blame-light-colors)))
153
 
        (setq git-blame-cache (make-hash-table :test 'equal))
154
 
        (git-blame-run))
155
 
    (cancel-timer git-blame-idle-timer)))
 
185
  (let ((bgmode (cdr (assoc 'background-mode (frame-parameters)))))
 
186
    (if (eq bgmode 'dark)
 
187
        (setq git-blame-colors git-blame-dark-colors)
 
188
      (setq git-blame-colors git-blame-light-colors)))
 
189
  (setq git-blame-cache (make-hash-table :test 'equal))
 
190
  (setq git-blame-mode t)
 
191
  (git-blame-run))
 
192
 
 
193
(defun git-blame-mode-off ()
 
194
  "Turn off git-blame mode.
 
195
 
 
196
See also function `git-blame-mode'."
 
197
  (git-blame-cleanup)
 
198
  (if git-blame-idle-timer (cancel-timer git-blame-idle-timer))
 
199
  (setq git-blame-mode nil))
156
200
 
157
201
;;;###autoload
158
202
(defun git-reblame ()
159
203
  "Recalculate all blame information in the current buffer"
 
204
  (interactive)
160
205
  (unless git-blame-mode
161
 
    (error "git-blame is not active"))
162
 
  (interactive)
 
206
    (error "Git-blame is not active"))
 
207
 
163
208
  (git-blame-cleanup)
164
209
  (git-blame-run))
165
210
 
275
320
        (t
276
321
         nil)))
277
322
 
278
 
 
279
323
(defun git-blame-new-commit (hash src-line res-line num-lines)
280
324
  (save-excursion
281
325
    (set-buffer git-blame-file)
283
327
          (inhibit-point-motion-hooks t)
284
328
          (inhibit-modification-hooks t))
285
329
      (when (not info)
286
 
        (let ((color (pop git-blame-colors)))
287
 
          (unless color
288
 
            (setq color git-blame-ancient-color))
 
330
        ;; Assign a random color to each new commit info
 
331
        ;; Take care not to select the same color multiple times
 
332
        (let ((color (if git-blame-colors
 
333
                         (git-blame-random-pop git-blame-colors)
 
334
                       git-blame-ancient-color)))
289
335
          (setq info (list hash src-line res-line num-lines
290
336
                           (git-describe-commit hash)
291
337
                           (cons 'color color))))