1 ;;; context-coloring.el --- Highlight by scope -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
5 ;; Author: Jackson Ray Hamilton <jackson@jacksonrayhamilton.com>
7 ;; Keywords: convenience faces tools
8 ;; Package-Requires: ((emacs "24") (js2-mode "20150126"))
9 ;; URL: https://github.com/jacksonrayhamilton/context-coloring
11 ;; This file is part of GNU Emacs.
13 ;; This program is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
28 ;; Highlights code by scope. Top-level scopes are one color, second-level
29 ;; scopes are another color, and so on. Variables retain the color of the scope
30 ;; in which they are defined. A variable defined in an outer scope referenced
31 ;; by an inner scope is colored the same as the outer scope.
33 ;; By default, comments and strings are still highlighted syntactically.
35 ;; To use with js2-mode, add the following to your init file:
37 ;; (require 'context-coloring)
38 ;; (add-hook 'js2-mode-hook 'context-coloring-mode)
40 ;; To use with js-mode or js3-mode, install Node.js 0.10+ and the scopifier
43 ;; $ npm install -g scopifier
52 (defvar-local context-coloring-buffer nil
53 "Reference to this buffer (for timers).")
58 (defun context-coloring-join (strings delimiter)
59 "Join a list of STRINGS with the string DELIMITER."
60 (mapconcat 'identity strings delimiter))
62 (defsubst context-coloring-trim-right (string)
63 "Remove leading whitespace from STRING."
64 (if (string-match "[ \t\n\r]+\\'" string)
65 (replace-match "" t t string)
68 (defsubst context-coloring-trim-left (string)
69 "Remove trailing whitespace from STRING."
70 (if (string-match "\\`[ \t\n\r]+" string)
71 (replace-match "" t t string)
74 (defsubst context-coloring-trim (string)
75 "Remove leading and trailing whitespace from STRING."
76 (context-coloring-trim-left (context-coloring-trim-right string)))
81 (defun context-coloring-defface (level tty light dark)
82 "Define a face for LEVEL with colors for TTY, LIGHT and DARK
84 (let ((face (intern (format "context-coloring-level-%s-face" level)))
85 (doc (format "Context coloring face, level %s." level)))
88 `((((type tty)) (:foreground ,tty))
89 (((background light)) (:foreground ,light))
90 (((background dark)) (:foreground ,dark)))
92 :group 'context-coloring)))
94 (defun context-coloring-defface-neutral (level)
95 "Define a face for LEVEL with the default neutral colors."
96 (context-coloring-defface level nil "#3f3f3f" "#cdcdcd"))
98 (context-coloring-defface 0 nil "#000000" "#ffffff")
99 (context-coloring-defface 1 "yellow" "#008b8b" "#00ffff")
100 (context-coloring-defface 2 "green" "#0000ff" "#87cefa")
101 (context-coloring-defface 3 "cyan" "#483d8b" "#b0c4de")
102 (context-coloring-defface 4 "blue" "#a020f0" "#eedd82")
103 (context-coloring-defface 5 "magenta" "#a0522d" "#98fb98")
104 (context-coloring-defface 6 "red" "#228b22" "#7fffd4")
105 (context-coloring-defface-neutral 7)
107 (defvar context-coloring-maximum-face nil
108 "Index of the highest face available for coloring.")
110 (defvar context-coloring-original-maximum-face nil
111 "Fallback value for `context-coloring-maximum-face' when all
112 themes have been disabled.")
114 (setq context-coloring-maximum-face 7)
116 (setq context-coloring-original-maximum-face
117 context-coloring-maximum-face)
119 ;; Theme authors can have up to 26 levels: 1 (0th) for globals, 24 (1st-24th)
120 ;; for nested levels, and 1 (25th) for infinity.
122 (context-coloring-defface-neutral (+ number context-coloring-maximum-face 1)))
127 (defsubst context-coloring-level-face (level)
128 "Return the symbol for a face with LEVEL."
129 ;; `concat' is faster than `format' here.
131 (concat "context-coloring-level-" (number-to-string level) "-face")))
133 (defsubst context-coloring-bounded-level-face (level)
134 "Return the symbol for a face with LEVEL, bounded by
135 `context-coloring-maximum-face'."
136 (context-coloring-level-face (min level context-coloring-maximum-face)))
139 ;;; Colorization utilities
141 (defsubst context-coloring-colorize-region (start end level)
142 "Color characters from the 1-indexed START point (inclusive) to
143 the END point (exclusive) with the face corresponding to LEVEL."
147 `(face ,(context-coloring-bounded-level-face level))))
149 (defcustom context-coloring-comments-and-strings nil
150 "If non-nil, also color comments and strings using `font-lock'."
151 :group 'context-coloring)
153 (make-obsolete-variable
154 'context-coloring-comments-and-strings
155 "use `context-coloring-syntactic-comments' and
156 `context-coloring-syntactic-strings' instead."
159 (defcustom context-coloring-syntactic-comments t
160 "If non-nil, also color comments using `font-lock'."
161 :group 'context-coloring)
163 (defcustom context-coloring-syntactic-strings t
164 "If non-nil, also color strings using `font-lock'."
165 :group 'context-coloring)
167 (defun context-coloring-font-lock-syntactic-comment-function (state)
168 "Tell `font-lock' to color a comment but not a string."
169 (if (nth 3 state) nil font-lock-comment-face))
171 (defun context-coloring-font-lock-syntactic-string-function (state)
172 "Tell `font-lock' to color a string but not a comment."
173 (if (nth 3 state) font-lock-string-face nil))
175 (defsubst context-coloring-maybe-colorize-comments-and-strings ()
176 "Color the current buffer's comments and strings if
177 `context-coloring-comments-and-strings' is non-nil."
178 (when (or context-coloring-comments-and-strings
179 context-coloring-syntactic-comments
180 context-coloring-syntactic-strings)
181 (let ((old-function font-lock-syntactic-face-function)
184 ((and context-coloring-syntactic-comments
185 (not context-coloring-syntactic-strings))
186 (setq font-lock-syntactic-face-function
187 'context-coloring-font-lock-syntactic-comment-function)
188 (setq saved-function-p t))
189 ((and context-coloring-syntactic-strings
190 (not context-coloring-syntactic-comments))
191 (setq font-lock-syntactic-face-function
192 'context-coloring-font-lock-syntactic-string-function)
193 (setq saved-function-p t)))
195 (font-lock-fontify-syntactically-region (point-min) (point-max)))
196 (when saved-function-p
197 (setq font-lock-syntactic-face-function old-function)))))
200 ;;; js2-mode colorization
202 (defvar-local context-coloring-js2-scope-level-hash-table nil
203 "Associate `js2-scope' structures and with their scope
206 (defcustom context-coloring-js-block-scopes nil
207 "If non-nil, also color block scopes in the scope hierarchy in JavaScript.
209 The block-scoped `let' and `const' are introduced in ES6. Enable
210 this for ES6 code; disable it elsewhere.
212 Supported modes: `js2-mode'"
213 :group 'context-coloring)
215 (defsubst context-coloring-js2-scope-level (scope)
216 "Return the level of SCOPE."
217 (cond ((gethash scope context-coloring-js2-scope-level-hash-table))
220 (current-scope scope)
222 (while (and current-scope
223 (js2-node-parent current-scope)
224 (setq enclosing-scope
225 (js2-node-get-enclosing-scope current-scope)))
226 (when (or context-coloring-js-block-scopes
227 (let ((type (js2-scope-type current-scope)))
228 (or (= type js2-SCRIPT)
229 (= type js2-FUNCTION)
230 (= type js2-CATCH))))
231 (setq level (+ level 1)))
232 (setq current-scope enclosing-scope))
233 (puthash scope level context-coloring-js2-scope-level-hash-table)))))
235 (defsubst context-coloring-js2-local-name-node-p (node)
236 "Determine if NODE is a `js2-name-node' representing a local
238 (and (js2-name-node-p node)
239 (let ((parent (js2-node-parent node)))
240 (not (or (and (js2-object-prop-node-p parent)
241 (eq node (js2-object-prop-node-left parent)))
242 (and (js2-prop-get-node-p parent)
243 ;; For nested property lookup, the node on the left is a
244 ;; `js2-prop-get-node', so this always works.
245 (eq node (js2-prop-get-node-right parent))))))))
247 (defvar-local context-coloring-point-max nil
248 "Cached value of `point-max'.")
250 (defsubst context-coloring-js2-colorize-node (node level)
251 "Color NODE with the color for LEVEL."
252 (let ((start (js2-node-abs-pos node)))
253 (context-coloring-colorize-region
257 (+ start (js2-node-len node))
258 ;; Somes nodes (like the ast when there is an unterminated multiline
259 ;; comment) will stretch to the value of `point-max'.
260 context-coloring-point-max)
263 (defun context-coloring-js2-colorize ()
264 "Color the current buffer using the abstract syntax tree
265 generated by `js2-mode'."
266 ;; Reset the hash table; the old one could be obsolete.
267 (setq context-coloring-js2-scope-level-hash-table (make-hash-table :test 'eq))
268 (setq context-coloring-point-max (point-max))
269 (with-silent-modifications
276 (context-coloring-js2-colorize-node
278 (context-coloring-js2-scope-level node)))
279 ((context-coloring-js2-local-name-node-p node)
280 (let* ((enclosing-scope (js2-node-get-enclosing-scope node))
281 (defining-scope (js2-get-defining-scope
283 (js2-name-node-name node))))
284 ;; The tree seems to be walked lexically, so an entire scope will
285 ;; be colored, including its name nodes, before they are reached.
286 ;; Coloring the nodes defined in that scope would be redundant, so
288 (when (not (eq defining-scope enclosing-scope))
289 (context-coloring-js2-colorize-node
291 (context-coloring-js2-scope-level defining-scope))))))
292 ;; The `t' indicates to search children.
294 (context-coloring-maybe-colorize-comments-and-strings)))
297 ;;; Emacs Lisp colorization
299 (defun context-coloring-make-scope (depth level)
303 :variables (make-hash-table)))
305 (defun context-coloring-scope-get-depth (scope)
306 (plist-get scope :depth))
308 (defun context-coloring-scope-get-level (scope)
309 (plist-get scope :level))
311 (defun context-coloring-scope-add-variable (scope variable)
312 (puthash variable t (plist-get scope :variables)))
314 (defun context-coloring-scope-get-variable (scope variable)
315 (gethash variable (plist-get scope :variables)))
317 (defun context-coloring-get-variable-level (scope-stack variable)
320 (while (and scope-stack (not level))
321 (setq scope (car scope-stack))
323 ((context-coloring-scope-get-variable scope variable)
324 (setq level (context-coloring-scope-get-level scope)))
326 (setq scope-stack (cdr scope-stack)))))
330 (defun context-coloring-make-backtick (end enabled)
335 (defun context-coloring-backtick-get-end (backtick)
336 (plist-get backtick :end))
338 (defun context-coloring-backtick-get-enabled (backtick)
339 (plist-get backtick :enabled))
341 (defun context-coloring-backtick-enabled-p (backtick-stack)
342 (context-coloring-backtick-get-enabled (car backtick-stack)))
344 (defun context-coloring-make-let-value (end)
348 (defun context-coloring-let-value-get-end (let-value)
349 (plist-get let-value :end))
351 (defun context-coloring-emacs-lisp-identifier-syntax-p (syntax-code)
352 (or (= 2 syntax-code)
355 (defun context-coloring-forward-sws ()
356 "Move forward through whitespace and comments."
357 (while (forward-comment 1)))
359 (defun context-coloring-at-open-parenthesis ()
360 (= 4 (logand #xFFFF (car (syntax-after (point))))))
362 (defun context-coloring-emacs-lisp-colorize ()
363 "Color the current buffer by parsing emacs lisp sexps."
364 (with-silent-modifications
366 ;; TODO: Can probably make this lazy to the nearest defun
367 (goto-char (point-min))
368 (let* ((inhibit-point-motion-hooks t)
370 (last-ppss-pos (point))
372 (scope-stack `(,(context-coloring-make-scope -1 0))) ; -1 never matches a depth
373 (backtick-stack `(,(context-coloring-make-backtick -1 nil)))
374 (let-value-stack `(,(context-coloring-make-let-value -1)))
401 (while (> end (progn (skip-syntax-forward "^()w_'" end)
403 (setq token-pos (point))
404 (setq token-syntax (syntax-after token-pos))
405 (setq ppss (parse-partial-sexp last-ppss-pos token-pos nil nil ppss))
406 (setq last-ppss-pos token-pos)
407 ;; `skip-syntax-forward' leaves the point at the delimiter, move past
409 (setq token-syntax-code (logand #xFFFF (car token-syntax)))
410 (setq token-char (string-to-char (buffer-substring-no-properties
415 ;; Resolve invalid state
419 (skip-syntax-forward "^\"" end)
424 (skip-syntax-forward "^>" end) ; comment ender
428 ;; Has to come first in case of commas
429 ((= 6 token-syntax-code)
432 ;; Just outright skip top-level symbols
433 ((not (or (cadr backtick-stack)
434 (= token-char 96))) ; 96 = '`'
435 (goto-char (scan-sexps (point) 1)))
436 ((or (= token-char 96) ; 96 = '`'
437 (= token-char 44)) ; 44 = ','
438 ;; Have to manage backticks
439 (setq backtick-stack (cons (context-coloring-make-backtick
440 (scan-sexps (point) 1) ; End of the backtick
441 (= token-char 96)) ; 96 = '`'
445 ((and (cadr backtick-stack)
446 (>= (point) (context-coloring-backtick-get-end (car backtick-stack))))
447 (setq backtick-stack (cdr backtick-stack)))
449 ;; Restricted by backtick
450 ((and (cadr backtick-stack)
451 (context-coloring-backtick-enabled-p backtick-stack))
455 ((= 4 token-syntax-code)
457 ;; Lookahead for scopes / function calls
458 (context-coloring-forward-sws)
459 (setq child-0-pos (point))
460 (setq child-0-syntax (syntax-after child-0-pos))
461 (setq child-0-syntax-code (logand #xFFFF (car child-0-syntax)))
464 ((context-coloring-emacs-lisp-identifier-syntax-p child-0-syntax-code)
465 (setq one-word-found-p t)
466 (setq child-0-end (scan-sexps child-0-pos 1))
467 (setq child-0-string (buffer-substring-no-properties child-0-pos child-0-end))
469 ((string-match-p "\\`defun\\'\\|\\`defmacro\\'" child-0-string)
471 ((string-match-p "\\`lambda\\'" child-0-string)
472 (setq in-lambda-p t))
473 ((string-match-p "\\`let\\*\\'" child-0-string)
474 (setq in-let*-p t)))))
478 (setq scope-stack (cons (context-coloring-make-scope
480 (1+ (context-coloring-scope-get-level
483 ;; TODO: Probably redundant and wasteful
484 (context-coloring-colorize-region token-pos
485 (scan-sexps token-pos 1)
486 (context-coloring-scope-get-level
491 (goto-char child-0-end)
493 ;; Lookahead for defun name
494 (context-coloring-forward-sws)
495 (setq child-1-pos (point))
496 (setq child-1-syntax (syntax-after child-1-pos))
497 (setq child-1-syntax-code (logand #xFFFF (car child-1-syntax)))
500 ((context-coloring-emacs-lisp-identifier-syntax-p child-1-syntax-code)
501 (setq child-1-end (scan-sexps child-1-pos 1))
502 ;; defuns are global so use level 0
503 (context-coloring-colorize-region child-1-pos child-1-end 0)
504 (goto-char child-1-end))))
505 ;; Lookahead for parameters
506 (context-coloring-forward-sws)
507 (when (context-coloring-at-open-parenthesis)
508 ;; Actually it should be `child-1-end' for `lambda'.
509 (setq child-2-end (scan-sexps (point) 1))
510 (setq defun-arglist (read (buffer-substring-no-properties
514 (setq defun-arg (car defun-arglist))
515 (when (and (symbolp defun-arg)
516 (string-match-p "\\`[^&:]" (symbol-name defun-arg)))
517 (context-coloring-scope-add-variable
520 (setq defun-arglist (cdr defun-arglist)))
521 (goto-char child-2-end))
523 (setq in-defun-p nil)
524 (setq in-lambda-p nil))
526 (goto-char child-0-end)
527 ;; Lookahead for bindings
528 (context-coloring-forward-sws)
529 (setq child-1-pos (point))
530 (setq child-1-syntax (syntax-after child-1-pos))
531 (setq child-1-syntax-code (logand #xFFFF (car child-1-syntax)))
532 (when (= 4 child-1-syntax-code)
533 (setq child-1-end (scan-sexps (point) 1))
534 (setq let-varlist (read (buffer-substring-no-properties
538 (setq let-var (car let-varlist))
541 (context-coloring-scope-add-variable
545 (context-coloring-scope-add-variable
548 ;; TODO: Recurse or use stack to eval var value
550 (setq let-varlist (cdr let-varlist)))
551 (goto-char child-1-end))
553 (setq in-let*-p nil))
556 ;; If there was a word, continue parsing after it.
562 (setq one-word-found-p nil))
565 ((context-coloring-emacs-lisp-identifier-syntax-p token-syntax-code)
566 (setq variable-end (scan-sexps (point) 1))
567 (setq variable-string (buffer-substring-no-properties
571 ;; Ignore constants such as numbers, keywords, t, nil. These can't
572 ;; be rebound, so they should be treated like syntax.
573 ((string-match-p "\\`[-+]?[0-9]\\|\\`t\\'\\|\\`nil\\'" variable-string))
574 ((keywordp (read variable-string)))
576 (setq variable (intern variable-string))
577 (setq variable-scope-level
578 (context-coloring-get-variable-level scope-stack variable))
579 (when (/= variable-scope-level (context-coloring-scope-get-level
581 (context-coloring-colorize-region
584 variable-scope-level))))
585 (goto-char variable-end))
588 ((= 5 token-syntax-code)
591 (setq ppss (parse-partial-sexp last-ppss-pos (point) nil nil ppss))
592 (setq last-ppss-pos (point))
593 (when (= (nth 0 ppss) (context-coloring-scope-get-depth (car scope-stack)))
594 (setq scope-stack (cdr scope-stack))))
597 (context-coloring-maybe-colorize-comments-and-strings)))
600 ;;; Shell command scopification / colorization
602 (defun context-coloring-apply-tokens (tokens)
603 "Process a vector of TOKENS to apply context-based coloring to
604 the current buffer. Tokens are 3 integers: start, end, level.
605 The vector is flat, with a new token occurring after every 3rd
607 (with-silent-modifications
609 (len (length tokens)))
611 (context-coloring-colorize-region
614 (elt tokens (+ i 2)))
616 (context-coloring-maybe-colorize-comments-and-strings)))
618 (defun context-coloring-parse-array (array)
619 "Parse ARRAY as a flat JSON array of numbers."
620 (let ((braceless (substring (context-coloring-trim array) 1 -1)))
622 ((> (length braceless) 0)
624 (mapcar 'string-to-number (split-string braceless ","))))
628 (defvar-local context-coloring-scopifier-process nil
629 "The single scopifier process that can be running.")
631 (defun context-coloring-kill-scopifier ()
632 "Kill the currently-running scopifier process."
633 (when (not (null context-coloring-scopifier-process))
634 (delete-process context-coloring-scopifier-process)
635 (setq context-coloring-scopifier-process nil)))
637 (defun context-coloring-scopify-shell-command (command callback)
638 "Invoke a scopifier via COMMAND, read its response
639 asynchronously and invoke CALLBACK with its output."
641 ;; Prior running tokenization is implicitly obsolete if this function is
643 (context-coloring-kill-scopifier)
645 ;; Start the process.
646 (setq context-coloring-scopifier-process
647 (start-process-shell-command "scopifier" nil command))
651 ;; The process may produce output in multiple chunks. This filter
652 ;; accumulates the chunks into a message.
654 context-coloring-scopifier-process
655 (lambda (_process chunk)
656 (setq output (concat output chunk))))
658 ;; When the process's message is complete, this sentinel parses it as JSON
659 ;; and applies the tokens to the buffer.
660 (set-process-sentinel
661 context-coloring-scopifier-process
662 (lambda (_process event)
663 (when (equal "finished\n" event)
664 (funcall callback output))))))
666 (defun context-coloring-send-buffer-to-scopifier ()
667 "Give the scopifier process its input so it can begin
670 context-coloring-scopifier-process
671 (point-min) (point-max))
673 context-coloring-scopifier-process))
675 (defun context-coloring-scopify-and-colorize (command &optional callback)
676 "Invoke a scopifier via COMMAND with the current buffer's contents,
677 read the scopifier's response asynchronously and apply a parsed
678 list of tokens to `context-coloring-apply-tokens'.
680 Invoke CALLBACK when complete."
681 (let ((buffer context-coloring-buffer))
682 (context-coloring-scopify-shell-command
685 (let ((tokens (context-coloring-parse-array output)))
686 (with-current-buffer buffer
687 (context-coloring-apply-tokens tokens))
688 (setq context-coloring-scopifier-process nil)
689 (when callback (funcall callback))))))
690 (context-coloring-send-buffer-to-scopifier))
695 (defvar context-coloring-dispatch-hash-table (make-hash-table :test 'eq)
696 "Map dispatch strategy names to their corresponding property
697 lists, which contain details about the strategies.")
699 (defvar context-coloring-mode-hash-table (make-hash-table :test 'eq)
700 "Map major mode names to dispatch property lists.")
702 (defun context-coloring-define-dispatch (symbol &rest properties)
703 "Define a new dispatch named SYMBOL with PROPERTIES.
705 A \"dispatch\" is a property list describing a strategy for
706 coloring a buffer. There are three possible strategies: Parse
707 and color in a single function (`:colorizer'), parse in a
708 function that returns scope data (`:scopifier'), or parse with a
709 shell command that returns scope data (`:command'). In the
710 latter two cases, the scope data will be used to automatically
713 PROPERTIES must include `:modes' and one of `:colorizer',
714 `:scopifier' or `:command'.
716 `:modes' - List of major modes this dispatch is valid for.
718 `:colorizer' - Symbol referring to a function that parses and
721 `:scopifier' - Symbol referring to a function that parses the
722 buffer a returns a flat vector of start, end and level data.
724 `:executable' - Optional name of an executable required by
727 `:command' - Shell command to execute with the current buffer
728 sent via stdin, and with a flat JSON array of start, end and
729 level data returned via stdout.
731 `:version' - Minimum required version that should be printed when
732 executing `:command' with a \"--version\" flag. The version
733 should be numeric, e.g. \"2\", \"19700101\", \"1.2.3\",
736 `:setup' - Arbitrary code to set up this dispatch when
737 `context-coloring-mode' is enabled.
739 `:teardown' - Arbitrary code to tear down this dispatch when
740 `context-coloring-mode' is disabled."
741 (let ((modes (plist-get properties :modes))
742 (colorizer (plist-get properties :colorizer))
743 (scopifier (plist-get properties :scopifier))
744 (command (plist-get properties :command)))
746 (error "No mode defined for dispatch"))
747 (when (not (or colorizer
750 (error "No colorizer, scopifier or command defined for dispatch"))
751 (puthash symbol properties context-coloring-dispatch-hash-table)
753 (when (null (gethash mode context-coloring-mode-hash-table))
754 (puthash mode properties context-coloring-mode-hash-table)))))
756 (context-coloring-define-dispatch
758 :modes '(js-mode js3-mode)
759 :executable "scopifier"
763 (context-coloring-define-dispatch
766 :colorizer 'context-coloring-js2-colorize
769 (add-hook 'js2-post-parse-callbacks 'context-coloring-colorize nil t))
772 (remove-hook 'js2-post-parse-callbacks 'context-coloring-colorize t)))
774 (context-coloring-define-dispatch
776 :modes '(emacs-lisp-mode)
777 :colorizer 'context-coloring-emacs-lisp-colorize)
779 (defun context-coloring-dispatch (&optional callback)
780 "Determine the optimal track for scopification / coloring of
781 the current buffer, then execute it.
783 Invoke CALLBACK when complete. It is invoked synchronously for
784 elisp tracks, and asynchronously for shell command tracks."
785 (let ((dispatch (gethash major-mode context-coloring-mode-hash-table))
790 ((setq colorizer (plist-get dispatch :colorizer))
792 (when callback (funcall callback)))
793 ((setq scopifier (plist-get dispatch :scopifier))
794 (context-coloring-apply-tokens (funcall scopifier))
795 (when callback (funcall callback)))
796 ((setq command (plist-get dispatch :command))
797 (context-coloring-scopify-and-colorize command callback)))))
802 (defvar context-coloring-colorize-hook nil
803 "Hooks to run after coloring a buffer.")
805 (defun context-coloring-colorize (&optional callback)
806 "Color the current buffer by function context.
808 Invoke CALLBACK when complete; see `context-coloring-dispatch'."
810 (context-coloring-dispatch
812 (when callback (funcall callback))
813 (run-hooks 'context-coloring-colorize-hook))))
815 (defvar-local context-coloring-changed nil
816 "Indication that the buffer has changed recently, which implies
817 that it should be colored again by
818 `context-coloring-colorize-idle-timer' if that timer is being
821 (defun context-coloring-change-function (_start _end _length)
822 "Register a change so that a buffer can be colorized soon."
823 ;; Tokenization is obsolete if there was a change.
824 (context-coloring-kill-scopifier)
825 (setq context-coloring-changed t))
827 (defun context-coloring-maybe-colorize ()
828 "Colorize the current buffer if it has changed."
829 (when (and (eq context-coloring-buffer (window-buffer (selected-window)))
830 context-coloring-changed)
831 (setq context-coloring-changed nil)
832 (context-coloring-colorize)))
837 (defun context-coloring-parse-version (string)
838 "Extract segments of a version STRING into a list. \"v1.0.0\"
839 produces (1 0 0), \"19700101\" produces (19700101), etc."
841 (while (string-match "[0-9]+" string)
842 (setq version (append version
843 (list (string-to-number (match-string 0 string)))))
844 (setq string (substring string (match-end 0))))
847 (defun context-coloring-check-version (expected actual)
848 "Check that version EXPECTED is less than or equal to ACTUAL."
849 (let ((expected (context-coloring-parse-version expected))
850 (actual (context-coloring-parse-version actual))
853 (while (and continue expected)
854 (let ((an-expected (car expected))
855 (an-actual (car actual)))
857 ((> an-actual an-expected)
860 ((< an-actual an-expected)
861 (setq acceptable nil)
862 (setq continue nil))))
863 (setq expected (cdr expected))
864 (setq actual (cdr actual)))
867 (defvar context-coloring-check-scopifier-version-hook nil
868 "Hooks to run after checking the scopifier version.")
870 (defun context-coloring-check-scopifier-version (&optional callback)
871 "Asynchronously invoke CALLBACK with a predicate indicating
872 whether the current scopifier version satisfies the minimum
873 version number required for the current major mode."
874 (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
876 (let ((version (plist-get dispatch :version))
877 (command (plist-get dispatch :command)))
878 (context-coloring-scopify-shell-command
879 (context-coloring-join (list command "--version") " ")
881 (if (context-coloring-check-version version output)
883 (when callback (funcall callback t)))
884 (when callback (funcall callback nil)))
885 (run-hooks 'context-coloring-check-scopifier-version-hook)))))))
890 (defvar context-coloring-theme-hash-table (make-hash-table :test 'eq)
891 "Map theme names to theme properties.")
893 (defun context-coloring-theme-p (theme)
894 "Return t if THEME is defined, nil otherwise."
895 (and (gethash theme context-coloring-theme-hash-table)))
897 (defconst context-coloring-level-face-regexp
898 "context-coloring-level-\\([[:digit:]]+\\)-face"
899 "Extract a level from a face.")
901 (defvar context-coloring-originally-set-theme-hash-table
902 (make-hash-table :test 'eq)
903 "Cache custom themes who originally set their own
904 `context-coloring-level-N-face' faces.")
906 (defun context-coloring-theme-originally-set-p (theme)
907 "Return t if there is a `context-coloring-level-N-face'
908 originally set for THEME, nil otherwise."
909 (let (originally-set)
911 ;; `setq' might return a non-nil value for the sake of this `cond'.
916 context-coloring-originally-set-theme-hash-table))
917 (eq originally-set 'yes))
919 (let* ((settings (get theme 'theme-settings))
922 (while (and tail (not found))
923 (and (eq (nth 0 (car tail)) 'theme-face)
925 context-coloring-level-face-regexp
926 (symbol-name (nth 1 (car tail))))
928 (setq tail (cdr tail)))
931 (defun context-coloring-cache-originally-set (theme originally-set)
932 "Remember if THEME had colors originally set for it. If
933 ORIGINALLY-SET is non-nil, it did, otherwise it didn't."
934 ;; Caching whether a theme was originally set is kind of dirty, but we have to
935 ;; do it to remember the past state of the theme. There are probably some
936 ;; edge cases where caching will be an issue, but they are probably rare.
939 (if originally-set 'yes 'no)
940 context-coloring-originally-set-theme-hash-table))
942 (defun context-coloring-warn-theme-originally-set (theme)
943 "Warn the user that the colors for THEME are already originally
945 (warn "Context coloring colors for theme `%s' are already defined" theme))
947 (defun context-coloring-theme-highest-level (theme)
948 "Return the highest level N of a face like
949 `context-coloring-level-N-face' set for THEME, or `-1' if there
951 (let* ((settings (get theme 'theme-settings))
957 (and (eq (nth 0 (car tail)) 'theme-face)
958 (setq face-string (symbol-name (nth 1 (car tail))))
960 context-coloring-level-face-regexp
962 (setq number (string-to-number
963 (substring face-string
968 (setq tail (cdr tail)))
971 (defun context-coloring-apply-theme (theme)
972 "Apply THEME's properties to its respective custom theme,
973 which must already exist and which *should* already be enabled."
974 (let* ((properties (gethash theme context-coloring-theme-hash-table))
975 (colors (plist-get properties :colors))
977 ;; Only clobber when we have to.
978 (when (custom-theme-enabled-p theme)
979 (setq context-coloring-maximum-face (- (length colors) 1)))
981 'custom-theme-set-faces
985 (setq level (+ level 1))
986 `(,(context-coloring-level-face level) ((t (:foreground ,color)))))
989 (defun context-coloring-define-theme (theme &rest properties)
990 "Define a context theme named THEME for coloring scope levels.
992 PROPERTIES is a property list specifiying the following details:
994 `:aliases': List of symbols of other custom themes that these
995 colors are applicable to.
997 `:colors': List of colors that this context theme uses.
999 `:override': If non-nil, this context theme is intentionally
1000 overriding colors set by a custom theme. Don't set this non-nil
1001 unless there is a custom theme you want to use which sets
1002 `context-coloring-level-N-face' faces that you want to replace.
1004 `:recede': If non-nil, this context theme should not apply its
1005 colors if a custom theme already sets
1006 `context-coloring-level-N-face' faces. This option is
1007 optimistic; set this non-nil if you would rather confer the duty
1008 of picking colors to a custom theme author (if / when he ever
1011 By default, context themes will always override custom themes,
1012 even if those custom themes set `context-coloring-level-N-face'
1013 faces. If a context theme does override a custom theme, a
1014 warning will be raised, at which point you may want to enable the
1015 `:override' option, or just delete your context theme and opt to
1016 use your custom theme's author's colors instead.
1018 Context themes only work for the custom theme with the highest
1019 precedence, i.e. the car of `custom-enabled-themes'."
1020 (let ((aliases (plist-get properties :aliases))
1021 (override (plist-get properties :override))
1022 (recede (plist-get properties :recede)))
1023 (dolist (name (append `(,theme) aliases))
1024 (puthash name properties context-coloring-theme-hash-table)
1025 (when (custom-theme-p name)
1026 (let ((originally-set (context-coloring-theme-originally-set-p name)))
1027 (context-coloring-cache-originally-set name originally-set)
1028 ;; In the particular case when you innocently define colors that a
1029 ;; custom theme originally set, warn. Arguably this only has to be
1030 ;; done at enable time, but it is probably more useful to do it at
1031 ;; definition time for prompter feedback.
1032 (when (and originally-set
1035 (context-coloring-warn-theme-originally-set name))
1036 ;; Set (or overwrite) colors.
1037 (when (not (and originally-set
1039 (context-coloring-apply-theme name)))))))
1041 (defun context-coloring-enable-theme (theme)
1042 "Apply THEME if its colors are not already set, else just set
1043 `context-coloring-maximum-face' to the correct value for THEME."
1044 (let* ((properties (gethash theme context-coloring-theme-hash-table))
1045 (recede (plist-get properties :recede))
1046 (override (plist-get properties :override)))
1049 (let ((highest-level (context-coloring-theme-highest-level theme)))
1051 ;; This can be true whether originally set by a custom theme or by a
1053 ((> highest-level -1)
1054 (setq context-coloring-maximum-face highest-level))
1055 ;; It is possible that the corresponding custom theme did not exist at
1056 ;; the time of defining this context theme, and in that case the above
1057 ;; condition proves the custom theme did not originally set any faces,
1058 ;; so we have license to apply the context theme for the first time
1061 (context-coloring-apply-theme theme)))))
1063 (let ((originally-set (context-coloring-theme-originally-set-p theme)))
1064 ;; Cache now in case the context theme was defined after the custom
1066 (context-coloring-cache-originally-set theme originally-set)
1067 (when (and originally-set
1069 (context-coloring-warn-theme-originally-set theme))
1070 (context-coloring-apply-theme theme))))))
1072 (defadvice enable-theme (after context-coloring-enable-theme (theme) activate)
1073 "Enable colors for context themes just-in-time."
1074 (when (and (not (eq theme 'user)) ; Called internally by `enable-theme'.
1075 (custom-theme-p theme) ; Guard against non-existent themes.
1076 (context-coloring-theme-p theme))
1077 (when (= (length custom-enabled-themes) 1)
1078 ;; Cache because we can't reliably figure it out in reverse.
1079 (setq context-coloring-original-maximum-face
1080 context-coloring-maximum-face))
1081 (context-coloring-enable-theme theme)))
1083 (defadvice disable-theme (after context-coloring-disable-theme (theme) activate)
1084 "Update `context-coloring-maximum-face'."
1085 (when (custom-theme-p theme) ; Guard against non-existent themes.
1086 (let ((enabled-theme (car custom-enabled-themes)))
1087 (if (context-coloring-theme-p enabled-theme)
1089 (context-coloring-enable-theme enabled-theme))
1090 ;; Assume we are back to no theme; act as if nothing ever happened.
1091 ;; This is still prone to intervention, but rather extraordinarily.
1092 (setq context-coloring-maximum-face
1093 context-coloring-original-maximum-face)))))
1095 (context-coloring-define-theme
1107 (context-coloring-define-theme
1122 (context-coloring-define-theme
1134 (context-coloring-define-theme
1147 (context-coloring-define-theme
1160 (context-coloring-define-theme
1163 :aliases '(solarized-light
1165 sanityinc-solarized-light
1166 sanityinc-solarized-dark)
1185 (context-coloring-define-theme
1197 (context-coloring-define-theme
1214 (context-coloring-define-theme
1232 (defvar-local context-coloring-colorize-idle-timer nil
1233 "The currently-running idle timer.")
1235 (defcustom context-coloring-delay 0.25
1236 "Delay between a buffer update and colorization.
1238 Increase this if your machine is high-performing. Decrease it if
1241 Supported modes: `js-mode', `js3-mode'"
1242 :group 'context-coloring)
1244 (defun context-coloring-setup-idle-change-detection ()
1245 "Setup idle change detection."
1247 'after-change-functions 'context-coloring-change-function nil t)
1248 (setq context-coloring-colorize-idle-timer
1249 (run-with-idle-timer
1250 context-coloring-delay
1252 'context-coloring-maybe-colorize)))
1255 (define-minor-mode context-coloring-mode
1256 "Context-based code coloring, inspired by Douglas Crockford."
1258 (if (not context-coloring-mode)
1260 (context-coloring-kill-scopifier)
1261 (when context-coloring-colorize-idle-timer
1262 (cancel-timer context-coloring-colorize-idle-timer))
1263 (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
1265 (let ((command (plist-get dispatch :command))
1266 (teardown (plist-get dispatch :teardown)))
1269 'after-change-functions 'context-coloring-change-function t))
1271 (funcall teardown)))))
1275 ;; Remember this buffer. This value should not be dynamically-bound.
1276 (setq context-coloring-buffer (current-buffer))
1278 ;; Font lock is incompatible with this mode; the converse is also true.
1282 ;; Safely change the valye of this function as necessary.
1283 (make-local-variable 'font-lock-syntactic-face-function)
1285 (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
1288 (let ((command (plist-get dispatch :command))
1289 (version (plist-get dispatch :version))
1290 (executable (plist-get dispatch :executable))
1291 (setup (plist-get dispatch :setup))
1292 (colorize-initially-p t))
1294 ;; Shell commands recolor on change, idly.
1297 (null (executable-find executable)))
1298 (message "Executable \"%s\" not found" executable)
1299 (setq colorize-initially-p nil))
1301 (context-coloring-check-scopifier-version
1302 (lambda (sufficient-p)
1305 (context-coloring-setup-idle-change-detection)
1306 (context-coloring-colorize))
1307 (message "Update to the minimum version of \"%s\" (%s)"
1308 executable version))))
1309 (setq colorize-initially-p nil))
1311 (context-coloring-setup-idle-change-detection))))
1314 ;; Colorize once initially.
1315 (when colorize-initially-p
1316 (context-coloring-colorize))))
1317 (when (null dispatch)
1318 (message "Context coloring is not available for this major mode"))))))
1320 (provide 'context-coloring)
1322 ;;; context-coloring.el ends here