1 ;;; context-coloring.el --- Syntax highlighting, except not for syntax. -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
5 ;; Author: Jackson Ray Hamilton <jackson@jacksonrayhamilton.com>
6 ;; URL: https://github.com/jacksonrayhamilton/context-coloring
7 ;; Keywords: context coloring syntax highlighting
9 ;; Package-Requires: ((emacs "24") (js2-mode "20150126"))
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 according to function context.
30 ;; - Code in the global scope is one color. Code in functions within the global
31 ;; scope is a different color, and code within such functions is another
33 ;; - Identifiers retain the color of the scope in which they are declared.
35 ;; Lexical scope information at-a-glance can assist a programmer in
36 ;; understanding the overall structure of a program. It can help to curb nasty
37 ;; bugs like name shadowing. A rainbow can indicate excessive complexity.
38 ;; State change within a closure is easily monitored.
40 ;; By default, Context Coloring still highlights comments and strings
41 ;; syntactically. It is still easy to differentiate code from non-code, and
42 ;; strings cannot be confused for variables.
44 ;; To use, add the following to your ~/.emacs:
46 ;; (require 'context-coloring)
47 ;; (add-hook 'js2-mode-hook 'context-coloring-mode)
49 ;; js-mode or js3-mode support requires Node.js 0.10+ and the scopifier
52 ;; $ npm install -g scopifier
61 (defvar-local context-coloring-buffer nil
62 "Reference to this buffer (for timers).")
67 (defun context-coloring-defface (level tty light dark)
68 "Define a face for LEVEL with colors for TTY, LIGHT and DARK
70 (let ((face (intern (format "context-coloring-level-%s-face" level)))
71 (doc (format "Context coloring face, level %s." level)))
74 `((((type tty)) (:foreground ,tty))
75 (((background light)) (:foreground ,light))
76 (((background dark)) (:foreground ,dark)))
78 :group 'context-coloring)))
80 (defun context-coloring-defface-neutral (level)
81 "Define a face for LEVEL with the default neutral colors."
82 (context-coloring-defface level nil "#3f3f3f" "#cdcdcd"))
84 (context-coloring-defface 0 nil "#000000" "#ffffff")
85 (context-coloring-defface 1 "yellow" "#007f80" "#ffff80")
86 (context-coloring-defface 2 "green" "#001580" "#cdfacd")
87 (context-coloring-defface 3 "cyan" "#550080" "#d8d8ff")
88 (context-coloring-defface 4 "blue" "#802b00" "#e7c7ff")
89 (context-coloring-defface 5 "magenta" "#6a8000" "#ffcdcd")
90 (context-coloring-defface 6 "red" "#008000" "#ffe390")
91 (context-coloring-defface-neutral 7)
93 (defvar context-coloring-maximum-face nil
94 "Index of the highest face available for coloring.")
96 (defvar context-coloring-original-maximum-face nil
97 "Fallback value for `context-coloring-maximum-face' when all
98 themes have been disabled.")
100 (setq context-coloring-maximum-face 7)
102 (setq context-coloring-original-maximum-face
103 context-coloring-maximum-face)
105 ;; Theme authors can have up to 26 levels: 1 (0th) for globals, 24 (1st-24th)
106 ;; for nested levels, and 1 (25th) for infinity.
108 (context-coloring-defface-neutral (+ number context-coloring-maximum-face 1)))
113 (defsubst context-coloring-level-face (level)
114 "Return the symbol for a face with LEVEL."
115 ;; `concat' is faster than `format' here.
117 (concat "context-coloring-level-" (number-to-string level) "-face")))
119 (defsubst context-coloring-bounded-level-face (level)
120 "Return the symbol for a face with LEVEL, bounded by
121 `context-coloring-maximum-face'."
122 (context-coloring-level-face (min level context-coloring-maximum-face)))
125 ;;; Colorization utilities
127 (defsubst context-coloring-colorize-region (start end level)
128 "Color characters from the 1-indexed START point (inclusive) to
129 the END point (exclusive) with the face corresponding to LEVEL."
133 `(face ,(context-coloring-bounded-level-face level))))
135 (defcustom context-coloring-comments-and-strings t
136 "If non-nil, also color comments and strings using `font-lock'."
137 :group 'context-coloring)
139 (defsubst context-coloring-maybe-colorize-comments-and-strings ()
140 "Color the current buffer's comments and strings if
141 `context-coloring-comments-and-strings' is non-nil."
142 (when context-coloring-comments-and-strings
144 (font-lock-fontify-syntactically-region (point-min) (point-max)))))
147 ;;; js2-mode colorization
149 (defvar-local context-coloring-js2-scope-level-hash-table nil
150 "Associate `js2-scope' structures and with their scope
153 (defcustom context-coloring-js-block-scopes nil
154 "If non-nil, also color block scopes in the scope hierarchy in JavaScript.
156 The block-scoped `let' and `const' are introduced in ES6. Enable
157 this for ES6 code; disable it elsewhere.
159 Supported modes: `js2-mode'"
160 :group 'context-coloring)
162 (defsubst context-coloring-js2-scope-level (scope)
163 "Return the level of SCOPE."
164 (cond ((gethash scope context-coloring-js2-scope-level-hash-table))
167 (current-scope scope)
169 (while (and current-scope
170 (js2-node-parent current-scope)
171 (setq enclosing-scope
172 (js2-node-get-enclosing-scope current-scope)))
173 (when (or context-coloring-js-block-scopes
174 (let ((type (js2-scope-type current-scope)))
175 (or (= type js2-SCRIPT)
176 (= type js2-FUNCTION)
177 (= type js2-CATCH))))
178 (setq level (+ level 1)))
179 (setq current-scope enclosing-scope))
180 (puthash scope level context-coloring-js2-scope-level-hash-table)))))
182 (defsubst context-coloring-js2-local-name-node-p (node)
183 "Determine if NODE is a `js2-name-node' representing a local
185 (and (js2-name-node-p node)
186 (let ((parent (js2-node-parent node)))
187 (not (or (and (js2-object-prop-node-p parent)
188 (eq node (js2-object-prop-node-left parent)))
189 (and (js2-prop-get-node-p parent)
190 ;; For nested property lookup, the node on the left is a
191 ;; `js2-prop-get-node', so this always works.
192 (eq node (js2-prop-get-node-right parent))))))))
194 (defsubst context-coloring-js2-colorize-node (node level)
195 "Color NODE with the color for LEVEL."
196 (let ((start (js2-node-abs-pos node)))
197 (context-coloring-colorize-region
199 (+ start (js2-node-len node)) ; End
202 (defun context-coloring-js2-colorize ()
203 "Color the current buffer using the abstract syntax tree
204 generated by `js2-mode'."
205 ;; Reset the hash table; the old one could be obsolete.
206 (setq context-coloring-js2-scope-level-hash-table (make-hash-table :test 'eq))
207 (with-silent-modifications
214 (context-coloring-js2-colorize-node
216 (context-coloring-js2-scope-level node)))
217 ((context-coloring-js2-local-name-node-p node)
218 (let* ((enclosing-scope (js2-node-get-enclosing-scope node))
219 (defining-scope (js2-get-defining-scope
221 (js2-name-node-name node))))
222 ;; The tree seems to be walked lexically, so an entire scope will
223 ;; be colored, including its name nodes, before they are reached.
224 ;; Coloring the nodes defined in that scope would be redundant, so
226 (when (not (eq defining-scope enclosing-scope))
227 (context-coloring-js2-colorize-node
229 (context-coloring-js2-scope-level defining-scope))))))
230 ;; The `t' indicates to search children.
232 (context-coloring-maybe-colorize-comments-and-strings)))
235 ;;; Shell command scopification / colorization
237 (defun context-coloring-apply-tokens (tokens)
238 "Process a vector of TOKENS to apply context-based coloring to
239 the current buffer. Tokens are 3 integers: start, end, level.
240 The vector is flat, with a new token occurring after every 3rd
242 (with-silent-modifications
244 (len (length tokens)))
246 (context-coloring-colorize-region
249 (elt tokens (+ i 2)))
251 (context-coloring-maybe-colorize-comments-and-strings)))
253 (defun context-coloring-parse-array (array)
254 "Parse ARRAY as a flat JSON array of numbers."
256 (mapcar 'string-to-number (split-string (substring array 1 -1) ","))))
258 (defvar-local context-coloring-scopifier-process nil
259 "The single scopifier process that can be running.")
261 (defun context-coloring-kill-scopifier ()
262 "Kill the currently-running scopifier process."
263 (when (not (null context-coloring-scopifier-process))
264 (delete-process context-coloring-scopifier-process)
265 (setq context-coloring-scopifier-process nil)))
267 (defun context-coloring-scopify-shell-command (command &optional callback)
268 "Invoke a scopifier via COMMAND with the current buffer's contents,
269 read the scopifier's response asynchronously and apply a parsed
270 list of tokens to `context-coloring-apply-tokens'.
272 Invoke CALLBACK when complete."
274 ;; Prior running tokenization is implicitly obsolete if this function is
276 (context-coloring-kill-scopifier)
278 ;; Start the process.
279 (setq context-coloring-scopifier-process
280 (start-process-shell-command "scopifier" nil command))
283 (buffer context-coloring-buffer))
285 ;; The process may produce output in multiple chunks. This filter
286 ;; accumulates the chunks into a message.
288 context-coloring-scopifier-process
289 (lambda (_process chunk)
290 (setq output (concat output chunk))))
292 ;; When the process's message is complete, this sentinel parses it as JSON
293 ;; and applies the tokens to the buffer.
294 (set-process-sentinel
295 context-coloring-scopifier-process
296 (lambda (_process event)
297 (when (equal "finished\n" event)
298 (let ((tokens (context-coloring-parse-array output)))
299 (with-current-buffer buffer
300 (context-coloring-apply-tokens tokens))
301 (setq context-coloring-scopifier-process nil)
302 (when callback (funcall callback)))))))
304 ;; Give the process its input so it can begin.
306 context-coloring-scopifier-process
307 (point-min) (point-max))
309 context-coloring-scopifier-process))
314 (defvar context-coloring-dispatch-hash-table (make-hash-table :test 'eq)
315 "Map dispatch strategy names to their corresponding property
316 lists, which contain details about the strategies.")
318 (defvar context-coloring-mode-hash-table (make-hash-table :test 'eq)
319 "Map major mode names to dispatch property lists.")
321 (defun context-coloring-select-dispatch (mode dispatch)
322 "Use DISPATCH for MODE."
327 context-coloring-dispatch-hash-table)
328 context-coloring-mode-hash-table))
330 (defun context-coloring-define-dispatch (symbol &rest properties)
331 "Define a new dispatch named SYMBOL with PROPERTIES.
333 A \"dispatch\" is a property list describing a strategy for
334 coloring a buffer. There are three possible strategies: Parse
335 and color in a single function (`:colorizer'), parse in a
336 function that returns scope data (`:scopifier'), or parse with a
337 shell command that returns scope data (`:command'). In the
338 latter two cases, the scope data will be used to automatically
341 PROPERTIES must include `:modes' and one of `:colorizer',
342 `:scopifier' or `:command'.
344 `:modes' - List of major modes this dispatch is valid for.
346 `:colorizer' - Symbol referring to a function that parses and
349 `:scopifier' - Symbol referring to a function that parses the
350 buffer a returns a flat vector of start, end and level data.
352 `:executable' - Optional name of an executable required by
355 `:command' - Shell command to execute with the current buffer
356 sent via stdin, and with a flat JSON array of start, end and
357 level data returned via stdout.
359 `:setup' - Arbitrary code to set up this dispatch when
360 `context-coloring-mode' is enabled.
362 `:teardown' - Arbitrary code to tear down this dispatch when
363 `context-coloring-mode' is disabled."
364 (let ((modes (plist-get properties :modes))
365 (colorizer (plist-get properties :colorizer))
366 (scopifier (plist-get properties :scopifier))
367 (command (plist-get properties :command)))
369 (error "No mode defined for dispatch"))
370 (when (not (or colorizer
373 (error "No colorizer, scopifier or command defined for dispatch"))
374 (puthash symbol properties context-coloring-dispatch-hash-table)
376 (when (null (gethash mode context-coloring-mode-hash-table))
377 (puthash mode properties context-coloring-mode-hash-table)))))
379 (context-coloring-define-dispatch
381 :modes '(js-mode js3-mode)
382 :executable "scopifier"
383 :command "scopifier")
385 (context-coloring-define-dispatch
388 :colorizer 'context-coloring-js2-colorize
391 (add-hook 'js2-post-parse-callbacks 'context-coloring-colorize nil t))
394 (remove-hook 'js2-post-parse-callbacks 'context-coloring-colorize t)))
396 (defun context-coloring-dispatch (&optional callback)
397 "Determine the optimal track for scopification / coloring of
398 the current buffer, then execute it.
400 Invoke CALLBACK when complete. It is invoked synchronously for
401 elisp tracks, and asynchronously for shell command tracks."
402 (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
403 (when (null dispatch)
404 (message "%s" "Context coloring is not available for this major mode"))
410 ((setq colorizer (plist-get dispatch :colorizer))
412 (when callback (funcall callback)))
413 ((setq scopifier (plist-get dispatch :scopifier))
414 (context-coloring-apply-tokens (funcall scopifier))
415 (when callback (funcall callback)))
416 ((setq command (plist-get dispatch :command))
417 (setq executable (plist-get dispatch :executable))
419 (null (executable-find executable)))
421 (message "Executable \"%s\" not found" executable))
422 (context-coloring-scopify-shell-command command callback)))))))
427 (defun context-coloring-colorize (&optional callback)
428 "Color the current buffer by function context.
430 Invoke CALLBACK when complete; see `context-coloring-dispatch'."
432 (context-coloring-dispatch
434 (when callback (funcall callback)))))
436 (defvar-local context-coloring-changed nil
437 "Indication that the buffer has changed recently, which implies
438 that it should be colored again by
439 `context-coloring-colorize-idle-timer' if that timer is being
442 (defun context-coloring-change-function (_start _end _length)
443 "Register a change so that a buffer can be colorized soon."
444 ;; Tokenization is obsolete if there was a change.
445 (context-coloring-kill-scopifier)
446 (setq context-coloring-changed t))
448 (defun context-coloring-maybe-colorize ()
449 "Colorize the current buffer if it has changed."
450 (when (and (eq context-coloring-buffer (window-buffer (selected-window)))
451 context-coloring-changed)
452 (setq context-coloring-changed nil)
453 (context-coloring-colorize)))
458 (defvar context-coloring-theme-hash-table (make-hash-table :test 'eq)
459 "Map theme names to theme properties.")
461 (defun context-coloring-theme-p (theme)
462 "Return t if THEME is defined, nil otherwise."
463 (and (gethash theme context-coloring-theme-hash-table)))
465 (defconst context-coloring-level-face-regexp
466 "context-coloring-level-\\([[:digit:]]+\\)-face"
467 "Extract a level from a face.")
469 (defvar context-coloring-originally-set-theme-hash-table
470 (make-hash-table :test 'eq)
471 "Cache custom themes who originally set their own
472 `context-coloring-level-N-face' faces.")
474 (defun context-coloring-theme-originally-set-p (theme)
475 "Return t if there is a `context-coloring-level-N-face'
476 originally set for THEME, nil otherwise."
477 (let (originally-set)
479 ;; `setq' might return a non-nil value for the sake of this `cond'.
484 context-coloring-originally-set-theme-hash-table))
485 (eq originally-set 'yes))
487 (let* ((settings (get theme 'theme-settings))
490 (while (and tail (not found))
491 (and (eq (nth 0 (car tail)) 'theme-face)
493 context-coloring-level-face-regexp
494 (symbol-name (nth 1 (car tail))))
496 (setq tail (cdr tail)))
499 (defun context-coloring-cache-originally-set (theme originally-set)
500 "Remember if THEME had colors originally set for it. If
501 ORIGINALLY-SET is non-nil, it did, otherwise it didn't."
502 ;; Caching whether a theme was originally set is kind of dirty, but we have to
503 ;; do it to remember the past state of the theme. There are probably some
504 ;; edge cases where caching will be an issue, but they are probably rare.
507 (if originally-set 'yes 'no)
508 context-coloring-originally-set-theme-hash-table))
510 (defun context-coloring-warn-theme-originally-set (theme)
511 "Warn the user that the colors for THEME are already originally
513 (warn "Context coloring colors for theme `%s' are already defined" theme))
515 (defun context-coloring-theme-highest-level (theme)
516 "Return the highest level N of a face like
517 `context-coloring-level-N-face' set for THEME, or `-1' if there
519 (let* ((settings (get theme 'theme-settings))
525 (and (eq (nth 0 (car tail)) 'theme-face)
526 (setq face-string (symbol-name (nth 1 (car tail))))
528 context-coloring-level-face-regexp
530 (setq number (string-to-number
531 (substring face-string
536 (setq tail (cdr tail)))
539 (defun context-coloring-apply-theme (theme)
540 "Apply THEME's properties to its respective custom theme,
541 which must already exist and which *should* already be enabled."
542 (let* ((properties (gethash theme context-coloring-theme-hash-table))
543 (colors (plist-get properties :colors))
545 (setq context-coloring-maximum-face (- (length colors) 1))
547 'custom-theme-set-faces
551 (setq level (+ level 1))
552 `(,(context-coloring-level-face level) ((t (:foreground ,color)))))
555 (defun context-coloring-define-theme (theme &rest properties)
556 "Define a context theme named THEME for coloring scope levels.
558 PROPERTIES is a property list specifiying the following details:
560 `:aliases': List of symbols of other custom themes that these
561 colors are applicable to.
563 `:colors': List of colors that this context theme uses.
565 `:override': If non-nil, this context theme is intentionally
566 overriding colors set by a custom theme. Don't set this non-nil
567 unless there is a custom theme you want to use which sets
568 `context-coloring-level-N-face' faces that you want to replace.
570 `:recede': If non-nil, this context theme should not apply its
571 colors if a custom theme already sets
572 `context-coloring-level-N-face' faces. This option is
573 optimistic; set this non-nil if you would rather confer the duty
574 of picking colors to a custom theme author (if / when he ever
577 By default, context themes will always override custom themes,
578 even if those custom themes set `context-coloring-level-N-face'
579 faces. If a context theme does override a custom theme, a
580 warning will be raised, at which point you may want to enable the
581 `:override' option, or just delete your context theme and opt to
582 use your custom theme's author's colors instead.
584 Context themes only work for the custom theme with the highest
585 precedence, i.e. the car of `custom-enabled-themes'."
586 (let ((aliases (plist-get properties :aliases))
587 (override (plist-get properties :override))
588 (recede (plist-get properties :recede)))
589 (dolist (name (append `(,theme) aliases))
590 (puthash name properties context-coloring-theme-hash-table)
591 (when (custom-theme-p name)
592 (let ((originally-set (context-coloring-theme-originally-set-p name)))
593 (context-coloring-cache-originally-set name originally-set)
594 ;; In the particular case when you innocently define colors that a
595 ;; custom theme originally set, warn. Arguably this only has to be
596 ;; done at enable time, but it is probably more useful to do it at
597 ;; definition time for prompter feedback.
598 (when (and originally-set
601 (context-coloring-warn-theme-originally-set name))
602 ;; Set (or overwrite) colors.
603 (when (not (and originally-set
605 (context-coloring-apply-theme name)))))))
607 (defun context-coloring-enable-theme (theme)
608 "Apply THEME if its colors are not already set, else just set
609 `context-coloring-maximum-face' to the correct value for THEME."
610 (let* ((properties (gethash theme context-coloring-theme-hash-table))
611 (recede (plist-get properties :recede))
612 (override (plist-get properties :override)))
615 (let ((highest-level (context-coloring-theme-highest-level theme)))
617 ;; This can be true whether originally set by a custom theme or by a
619 ((> highest-level -1)
620 (setq context-coloring-maximum-face highest-level))
621 ;; It is possible that the corresponding custom theme did not exist at
622 ;; the time of defining this context theme, and in that case the above
623 ;; condition proves the custom theme did not originally set any faces,
624 ;; so we have license to apply the context theme for the first time
627 (context-coloring-apply-theme theme)))))
629 (let ((originally-set (context-coloring-theme-originally-set-p theme)))
630 ;; Cache now in case the context theme was defined after the custom
632 (context-coloring-cache-originally-set theme originally-set)
633 (when (and originally-set
635 (context-coloring-warn-theme-originally-set theme))
636 (context-coloring-apply-theme theme))))))
638 (defadvice enable-theme (after context-coloring-enable-theme (theme) activate)
639 "Enable colors for context themes just-in-time."
640 (when (and (not (eq theme 'user)) ; Called internally by `enable-theme'.
641 (custom-theme-p theme) ; Guard against non-existent themes.
642 (context-coloring-theme-p theme))
643 (when (= (length custom-enabled-themes) 0)
644 ;; Cache because we can't reliably figure it out in reverse.
645 (setq context-coloring-original-maximum-face
646 context-coloring-maximum-face))
647 (context-coloring-enable-theme theme)))
649 (defadvice disable-theme (after context-coloring-disable-theme (theme) activate)
650 "Update `context-coloring-maximum-face'."
651 (when (custom-theme-p theme) ; Guard against non-existent themes.
652 (let ((enabled-theme (car custom-enabled-themes)))
653 (if (context-coloring-theme-p enabled-theme)
655 (context-coloring-enable-theme enabled-theme))
656 ;; Assume we are back to no theme; act as if nothing ever happened.
657 ;; This is still prone to intervention, but rather extraordinarily.
658 (setq context-coloring-maximum-face
659 context-coloring-original-maximum-face)))))
661 (context-coloring-define-theme
673 (context-coloring-define-theme
688 (context-coloring-define-theme
700 (context-coloring-define-theme
713 (context-coloring-define-theme
726 (context-coloring-define-theme
729 :aliases '(solarized-light
731 sanityinc-solarized-light
732 sanityinc-solarized-dark)
751 (context-coloring-define-theme
763 (context-coloring-define-theme
780 (context-coloring-define-theme
798 (defvar-local context-coloring-colorize-idle-timer nil
799 "The currently-running idle timer.")
801 (defcustom context-coloring-delay 0.25
802 "Delay between a buffer update and colorization.
804 Increase this if your machine is high-performing. Decrease it if
807 Supported modes: `js-mode', `js3-mode'"
808 :group 'context-coloring)
811 (define-minor-mode context-coloring-mode
812 "Context-based code coloring, inspired by Douglas Crockford."
814 (if (not context-coloring-mode)
816 (context-coloring-kill-scopifier)
817 (when context-coloring-colorize-idle-timer
818 (cancel-timer context-coloring-colorize-idle-timer))
819 (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
821 (let ((command (plist-get dispatch :command))
822 (teardown (plist-get dispatch :teardown)))
825 'after-change-functions 'context-coloring-change-function t))
827 (funcall teardown)))))
831 ;; Remember this buffer. This value should not be dynamically-bound.
832 (setq context-coloring-buffer (current-buffer))
834 ;; Font lock is incompatible with this mode; the converse is also true.
838 (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
840 (let ((command (plist-get dispatch :command))
841 (setup (plist-get dispatch :setup)))
843 ;; Shell commands recolor on change, idly.
845 'after-change-functions 'context-coloring-change-function nil t)
846 (setq context-coloring-colorize-idle-timer
848 context-coloring-delay
850 'context-coloring-maybe-colorize)))
854 ;; Colorize once initially.
855 (context-coloring-colorize)))
857 (provide 'context-coloring)
859 ;;; context-coloring.el ends here