{"thread":{"id":"7435","subject":"[PATCH] git-blame.el: pick a set of random colors for each git-blame turn","startedAt":"2007-03-27T20:09:46Z","lastAt":"2007-03-27T20:09:46Z","messageCount":1,"participants":["Xavier Maillard"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"38188","messageId":"200703272009.l2RK9k6x010896@localhost.localdomain","threadId":"7435","inReplyTo":null,"subject":"[PATCH] git-blame.el: pick a set of random colors for each git-blame turn","fromName":"Xavier Maillard","fromEmail":"zedek@gnu.org","sentAt":"2007-03-27T20:09:46Z","receivedAt":"2007-03-27T20:09:46Z","isPatch":true,"sender":{"key":"zedek@gnu.org","avatar":null},"body":"\nI thought it would be cool to have different set of colors for each\ngit-blame-mode. Function `git-blame-new-commit' does this for us\npicking when possible, a random colors based on the set we build on\nstartup. When it fails, `git-blame-ancient-color' will be used. We\nalso take care not to use the same color more than once (thank you\nDavid Kågedal).\n\n* Prevent (future possible) namespace clash by renaming `color-scale'\ninto `git-blame-color-scale'. Definition has been changed to be more\nin the \"lisp\" way (thanks for help to #emacs). Also added a small\ndescription of what it does.\n\n* Added docstrings at some point and instructed defvar when a variable\nwas candidate to customisation by users.\n\n* Added missing defvar to silent byte-compilers (git-blame-file,\ngit-blame-current)\n\n* Do not require 'cl at startup\n\n* Added more informations on compatibility\n---\n contrib/emacs/git-blame.el |   82 ++++++++++++++++++++++++++++----------------\n 1 files changed, 52 insertions(+), 30 deletions(-)\n\ndiff --git a/contrib/emacs/git-blame.el b/contrib/emacs/git-blame.el\nindex bd87a86..4703442 100644\n--- a/contrib/emacs/git-blame.el\n+++ b/contrib/emacs/git-blame.el\n@@ -8,8 +8,8 @@\n ;; License:    GPL\n ;; Keywords:   git, version control, release management\n ;;\n-;; Compatibility: Emacs21\n-\n+;; Compatibility: Emacs21, Emacs22 and EmacsCVS\n+;;                Git 1.5 and up\n \n ;; This file is *NOT* part of GNU Emacs.\n ;; This file is distributed under the same terms as GNU Emacs.\n@@ -61,8 +61,9 @@\n \n ;;; Compatibility:\n ;;\n-;; It requires GNU Emacs 21.  If you'are using Emacs 20, try\n-;; changing this:\n+;; It requires GNU Emacs 21 or later and Git 1.5.0 and up\n+;; \n+;; If you'are using Emacs 20, try changing this:\n ;;\n ;;            (overlay-put ovl 'face (list :background\n ;;                                         (cdr (assq 'color (cddddr info)))))\n@@ -77,30 +78,43 @@\n ;;\n ;;; Code:\n \n-(require 'cl)\t\t\t      ; to use `push', `pop'\n-\n-(defun color-scale (l)\n-  (let* ((colors ())\n-         r g b)\n-    (setq r l)\n-    (while r\n-      (setq g l)\n-      (while g\n-        (setq b l)\n-        (while b\n-          (push (concat \"#\" (car r) (car g) (car b)) colors)\n-          (pop b))\n-        (pop g))\n-      (pop r))\n-    colors))\n+(eval-when-compile (require 'cl))\t\t\t      ; to use `push', `pop'\n+\n+\n+(defun git-blame-color-scale (&rest elements)\n+  \"Given a list, returns a list of triples formed with each\n+elements of the list.\n+\n+a b => bbb bba bab baa abb aba aaa aab\"\n+  (let (result)\n+    (dolist (a elements)\n+      (dolist (b elements)\n+        (dolist (c elements)\n+          (setq result (cons (format \"#%s%s%s\" a b c) result)))))\n+    result))\n+\n+;; (git-blame-color-scale \"0c\" \"04\" \"24\" \"1c\" \"2c\" \"34\" \"14\" \"3c\") =>\n+;; (\"#3c3c3c\" \"#3c3c14\" \"#3c3c34\" \"#3c3c2c\" \"#3c3c1c\" \"#3c3c24\"\n+;; \"#3c3c04\" \"#3c3c0c\" \"#3c143c\" \"#3c1414\" \"#3c1434\" \"#3c142c\" ...)\n \n (defvar git-blame-dark-colors\n-  (color-scale '(\"0c\" \"04\" \"24\" \"1c\" \"2c\" \"34\" \"14\" \"3c\")))\n+  (git-blame-color-scale \"0c\" \"04\" \"24\" \"1c\" \"2c\" \"34\" \"14\" \"3c\")\n+  \"*List of colors (format #RGB) to use in a dark environment.\n+\n+To check out the list, evaluate (list-colors-display git-blame-dark-colors).\")\n \n (defvar git-blame-light-colors\n-  (color-scale '(\"c4\" \"d4\" \"cc\" \"dc\" \"f4\" \"e4\" \"fc\" \"ec\")))\n+  (git-blame-color-scale \"c4\" \"d4\" \"cc\" \"dc\" \"f4\" \"e4\" \"fc\" \"ec\")\n+  \"*List of colors (format #RGB) to use in a light environment.\n+\n+To check out the list, evaluate (list-colors-display git-blame-light-colors).\")\n \n-(defvar git-blame-ancient-color \"dark green\")\n+(defvar git-blame-colors '()\n+  \"Colors used by git-blame. The list is built once when activating git-blame\n+minor mode.\")\n+  \n+(defvar git-blame-ancient-color \"dark green\"\n+  \"*Color to be used for ancient commit.\")\n \n (defvar git-blame-autoupdate t\n   \"*Automatically update the blame display while editing\")\n@@ -125,6 +139,10 @@\n   \"A queue of update requests\")\n (make-variable-buffer-local 'git-blame-update-queue)\n \n+;; FIXME: docstrings\n+(defvar git-blame-file nil)\n+(defvar git-blame-current nil)\n+\n (defvar git-blame-mode nil)\n (make-variable-buffer-local 'git-blame-mode)\n \n@@ -177,7 +195,7 @@ See also function `git-blame-mode'.\"\n   \"Recalculate all blame information in the current buffer\"\n   (interactive)\n   (unless git-blame-mode\n-    (error \"git-blame is not active\"))\n+    (error \"Git-blame is not active\"))\n   \n   (git-blame-cleanup)\n   (git-blame-run))\n@@ -294,18 +312,22 @@ See also function `git-blame-mode'.\"\n         (t\n          nil)))\n \n-\n (defun git-blame-new-commit (hash src-line res-line num-lines)\n   (save-excursion\n     (set-buffer git-blame-file)\n     (let ((info (gethash hash git-blame-cache))\n           (inhibit-point-motion-hooks t)\n-          (inhibit-modification-hooks t))\n+          (inhibit-modification-hooks t)\n+\t  (colors git-blame-colors))\n       (when (not info)\n-        (let ((color (pop git-blame-colors)))\n-          (unless color\n-            (setq color git-blame-ancient-color))\n-          (setq info (list hash src-line res-line num-lines\n+\t;; Assign a random color to each new commit info\n+\t;; Take care not to select the same color multiple times\n+\t(let* ((idx (random (length colors)))\n+\t       (color (or (elt colors idx)\n+\t\t\t  git-blame-ancient-color)))\n+\t  (and (assoc color colors)\n+\t       (setq colors (delete idx colors)))\n+\t  (setq info (list hash src-line res-line num-lines\n                            (git-describe-commit hash)\n                            (cons 'color color))))\n         (puthash hash info git-blame-cache))\n-- \n1.5.0.5\n"}]}