@matheusbsbs / matheus-bagliano / commits / 3cc5ca3923
Fork 0

git-blame.el: pick a set of random colors for each git-blame turn

I thought it would be cool to have different set of colors for each git-blame-mode. Function `git-blame-new-commit' does this for us picking when possible, a random colors based on the set we build on startup. When it fails, `git-blame-ancient-color' will be used. We also take care not to use the same color more than once (thank you David Kågedal, really). * Prevent (future possible) namespace clash by renaming `color-scale' into `git-blame-color-scale'. Definition has been changed to be more in the "lisp" way (thanks for help to #emacs). Also added a small description of what it does. * Added docstrings at some point and instructed defvar when a variable was candidate to customisation by users. * Added missing defvar to silent byte-compilers (git-blame-file, git-blame-current) * Do not require 'cl at startup * Added more informations on compatibility Signed-off-by: Xavier Maillard <zedek@gnu.org> Acked-by: David Kågedal <davidk@lysator.liu.se> Signed-off-by: Junio C Hamano <junkio@cox.net>

Xavier Maillard committed Mar 28, 2007 at 18:44 UTC 3cc5ca3923c4db78d638fb94a3dfb70f103267bf
1 file changed +55 -28
contrib/emacs/git-blame.el
+55 -28
@@ -8,8 +8,8 @@
8 ;; License: GPL
9 ;; Keywords: git, version control, release management
10 ;;
11 -;; Compatibility: Emacs21
12 -
11 +;; Compatibility: Emacs21, Emacs22 and EmacsCVS
12 +;; Git 1.5 and up
13
14 ;; This file is *NOT* part of GNU Emacs.
15 ;; This file is distributed under the same terms as GNU Emacs.
@@ -61,8 +61,9 @@
61
62 ;;; Compatibility:
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:
67 ;;
68 ;; (overlay-put ovl 'face (list :background
69 ;; (cdr (assq 'color (cddddr info)))))
@@ -77,30 +78,51 @@
78 ;;
79 ;;; Code:
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))
107
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).")
113
114 (defvar git-blame-light-colors
101 - (color-scale '("c4" "d4" "cc" "dc" "f4" "e4" "fc" "ec")))
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
103 -(defvar git-blame-ancient-color "dark green")
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.")
126
127 (defvar git-blame-autoupdate t
128 "*Automatically update the blame display while editing")
@@ -125,6 +147,10 @@
147 "A queue of update requests")
148 (make-variable-buffer-local 'git-blame-update-queue)
149
150 +;; FIXME: docstrings
151 +(defvar git-blame-file nil)
152 +(defvar git-blame-current nil)
153 +
154 (defvar git-blame-mode nil)
155 (make-variable-buffer-local 'git-blame-mode)
156
@@ -177,7 +203,7 @@ See also function `git-blame-mode'."
203 "Recalculate all blame information in the current buffer"
204 (interactive)
205 (unless git-blame-mode
180 - (error "git-blame is not active"))
206 + (error "Git-blame is not active"))
207
208 (git-blame-cleanup)
209 (git-blame-run))
@@ -294,7 +320,6 @@ See also function `git-blame-mode'."
320 (t
321 nil)))
322
297 -
323 (defun git-blame-new-commit (hash src-line res-line num-lines)
324 (save-excursion
325 (set-buffer git-blame-file)
@@ -302,9 +327,11 @@ See also function `git-blame-mode'."
327 (inhibit-point-motion-hooks t)
328 (inhibit-modification-hooks t))
329 (when (not info)
305 - (let ((color (pop git-blame-colors)))
306 - (unless color
307 - (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)))
335 (setq info (list hash src-line res-line num-lines
336 (git-describe-commit hash)
337 (cons 'color color))))