From 8665ff31a174d6bf8648f70d68c49eec9a6cf752 Mon Sep 17 00:00:00 2001 From: Jackson Ray Hamilton Date: Sat, 31 Jan 2015 17:22:17 -0800 Subject: [PATCH] Simplify `context-coloring-set-colors'. --- README.md | 43 +++++++----------- context-coloring.el | 83 ++++++++++++++++------------------- test/context-coloring-test.el | 39 +++++++++++++++- 3 files changed, 94 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index e380b43f7..14f4f1aa7 100644 --- a/README.md +++ b/README.md @@ -65,38 +65,28 @@ make compile ## Customizing -You can adjust the colors to your liking using -`context-coloring-set-colors`. The first argument is an alist of levels, and the -optional second argument is the new total number of levels. This plugin does not -figure out the total for you; you need to specify it if your number of colors is -different from the default (`7`). +You can adjust the colors to your liking using `context-coloring-set-colors`. I like to take the colors from an existing theme and use those to create a -rainbow that matches that theme. The end result is consistent, and usually looks -as good as the theme does. Here's an example for `tango`: +rainbow that matches that theme. Here's an example for [`zenburn`][zenburn] (which is the +theme used in the screenshot above). ```lisp ;; ~/.emacs -(load-theme 'tango) +(load-theme 'zenburn t) (require 'context-coloring) -(defun jrh-context-coloring-tango () - (interactive) - (context-coloring-set-colors - '((0 . "#2e3436") ; Globals. - (1 . "#346604") - (2 . "#204a87") - (3 . "#5c3566") - (4 . "#a40000") - (5 . "#b35000") - (6 . "#c4a000") - (7 . "#8ae234") ; "You're screwed" colors. - (8 . "#8cc4ff") - (9 . "#ad7fa8") - (10 . "#ef2929") - (11 . "#fcaf3e") - (12 . "#fce94f")) - 13)) -(jrh-context-coloring-tango) +(context-coloring-set-colors + "#DCDCCC" + "#93E0E3" + "#BFEBBF" + "#F0DFAF" + "#DFAF8F" + "#CC9393" + "#DC8CC3" + "#94BFF3" + "#9FC59F" + "#D0BF8F" + "#DCA3A3") ``` ## Extending @@ -135,6 +125,7 @@ into an array like the one above. [linter]: http://jshint.com/about/ [flycheck]: http://www.flycheck.org/ +[zenburn]: http://github.com/bbatsov/zenburn-emacs [point]: http://www.gnu.org/software/emacs/manual/html_node/elisp/Point.html [js2-mode]: https://github.com/mooz/js2-mode [node]: http://nodejs.org/download/ diff --git a/context-coloring.el b/context-coloring.el index d0579d18c..f0b18657c 100644 --- a/context-coloring.el +++ b/context-coloring.el @@ -91,62 +91,57 @@ used.") ;;; Faces -(defmacro context-coloring-defface (level tty light dark) +(defun context-coloring-defface (level tty light dark) (let ((face (intern (format "context-coloring-level-%s-face" level))) (doc (format "Context coloring face, level %s." level))) - `(defface ,face - '((((type tty)) (:foreground ,tty)) - (((background light)) (:foreground ,light)) - (((background dark)) (:foreground ,dark))) - ,doc - :group 'context-coloring))) - -(context-coloring-defface 0 "white" "#000000" "#ffffff") -(context-coloring-defface 1 "yellow" "#007f80" "#ffff80") -(context-coloring-defface 2 "green" "#001580" "#cdfacd") -(context-coloring-defface 3 "cyan" "#550080" "#d8d8ff") -(context-coloring-defface 4 "blue" "#802b00" "#e7c7ff") -(context-coloring-defface 5 "magenta" "#6a8000" "#ffcdcd") -(context-coloring-defface 6 "red" "#008000" "#ffe390") - -(defcustom context-coloring-face-count 8 - "Number of faces defined for highlighting levels. -Determines level at which to cycle through faces again." - :group 'context-coloring) - -(defvar context-coloring-max-level (- context-coloring-face-count 1)) - -(defun context-coloring-defface-doom (level) - (eval (macroexpand `(context-coloring-defface ,level "white" "#3f3f3f" "#cdcdcd")))) - -(context-coloring-defface-doom context-coloring-max-level) - + (eval (macroexpand `(defface ,face + '((((type tty)) (:foreground ,tty)) + (((background light)) (:foreground ,light)) + (((background dark)) (:foreground ,dark))) + ,doc + :group 'context-coloring))))) + +(defvar context-coloring-face-count nil + "Number of faces available for context coloring.") + +(defun context-coloring-defface-default (level) + (context-coloring-defface level "white" "#3f3f3f" "#cdcdcd")) + +(defun context-coloring-set-colors-default () + (context-coloring-defface 0 "white" "#000000" "#ffffff") + (context-coloring-defface 1 "yellow" "#007f80" "#ffff80") + (context-coloring-defface 2 "green" "#001580" "#cdfacd") + (context-coloring-defface 3 "cyan" "#550080" "#d8d8ff") + (context-coloring-defface 4 "blue" "#802b00" "#e7c7ff") + (context-coloring-defface 5 "magenta" "#6a8000" "#ffcdcd") + (context-coloring-defface 6 "red" "#008000" "#ffe390") + (context-coloring-defface-default 7) + (setq context-coloring-face-count 8)) + +(context-coloring-set-colors-default) ;;; Face functions (defsubst context-coloring-face-symbol (level) "Returns a symbol for a face with LEVEL." + ;; `concat' is faster than `format' here. (intern-soft (concat "context-coloring-level-" (number-to-string level) "-face"))) -(defun context-coloring-set-colors (pairs &optional count) - "Set an alist of PAIRS for different levels' colors. Also sets -`context-coloring-face-count' to COUNT, if specified." - (when count - (setq context-coloring-face-count count) - (setq context-coloring-max-level (- count 1)) - ;; Ensure there are available faces to contain new colors. - (let ((current context-coloring-max-level)) - (while (not (context-coloring-face-symbol current)) - (context-coloring-defface-doom current) - (setq current (- current 1))))) - (dolist (pair pairs) - (let ((level (car pair)) - (color (cdr pair))) - (set-face-foreground (context-coloring-face-symbol level) color)))) +(defun context-coloring-set-colors (&rest colors) + "Set context coloring's levels' coloring to COLORS, where the +Nth element of COLORS is level N's color." + (setq context-coloring-face-count (length colors)) + (let ((level 0)) + (dolist (color colors) + ;; Ensure there are available faces to contain new colors. + (when (not (context-coloring-face-symbol level)) + (context-coloring-defface-default level)) + (set-face-foreground (context-coloring-face-symbol level) color) + (setq level (+ level 1))))) (defsubst context-coloring-level-face (level) "Returns the face name for LEVEL." - (context-coloring-face-symbol (min level context-coloring-max-level))) + (context-coloring-face-symbol (min level context-coloring-face-count))) ;;; Colorization utilities diff --git a/test/context-coloring-test.el b/test/context-coloring-test.el index 64ed6e40f..b7dda7614 100644 --- a/test/context-coloring-test.el +++ b/test/context-coloring-test.el @@ -20,7 +20,8 @@ (defun context-coloring-test-cleanup () (setq context-coloring-comments-and-strings t) (setq context-coloring-after-colorize-hook nil) - (setq context-coloring-js-block-scopes nil)) + (setq context-coloring-js-block-scopes nil) + (context-coloring-set-colors-default)) (defmacro context-coloring-test-with-fixture (fixture &rest body) "Evaluate BODY in a temporary buffer with the relative @@ -139,6 +140,42 @@ to run arbitrary code before the mode is invoked." (context-coloring-test-assert-message "Context coloring is not available for this major mode"))) +(defun context-coloring-test-assert-face (level foreground) + (let* ((face (context-coloring-face-symbol level)) + actual-foreground) + (when (not face) + (ert-fail (format "Expected face for level `%s' to exist; but it didn't" level))) + (setq actual-foreground (face-attribute face :foreground)) + (when (not (string-equal foreground actual-foreground)) + (ert-fail (format "Expected face for level `%s' to have foreground `%s'; but it was `%s'" + level foreground actual-foreground))))) + +(ert-deftest context-coloring-test-set-colors () + ;; This test has an irreversible side-effect in that it defines faces beyond + ;; 7. Faces 0 through 7 are reset to their default states, so it might not + ;; matter, but be aware anyway. + (context-coloring-set-colors + "#000000" + "#111111" + "#222222" + "#333333" + "#444444" + "#555555" + "#666666" + "#777777" + "#888888" + "#999999") + (context-coloring-test-assert-face 0 "#000000") + (context-coloring-test-assert-face 1 "#111111") + (context-coloring-test-assert-face 2 "#222222") + (context-coloring-test-assert-face 3 "#333333") + (context-coloring-test-assert-face 4 "#444444") + (context-coloring-test-assert-face 5 "#555555") + (context-coloring-test-assert-face 6 "#666666") + (context-coloring-test-assert-face 7 "#777777") + (context-coloring-test-assert-face 8 "#888888") + (context-coloring-test-assert-face 9 "#999999")) + (defun context-coloring-test-js-function-scopes () (context-coloring-test-assert-region-level 1 9 0) (context-coloring-test-assert-region-level 9 23 1) -- 2.39.2