]> code.delx.au - gnu-emacs-elpa/blobdiff - context-coloring.el
Add lazy coloring.
[gnu-emacs-elpa] / context-coloring.el
index 010e0afc8552b3a24878cd59440ab5fcf902c8df..ec7ab7adb04cf2b2ce025d7ae002b5509c1d27bb 100644 (file)
@@ -1,12 +1,12 @@
-;;; context-coloring.el --- Syntax highlighting, except not for syntax. -*- lexical-binding: t; -*-
+;;; context-coloring.el --- Highlight by scope  -*- lexical-binding: t; -*-
 
 ;; Copyright (C) 2014-2015  Free Software Foundation, Inc.
 
 ;; Author: Jackson Ray Hamilton <jackson@jacksonrayhamilton.com>
-;; URL: https://github.com/jacksonrayhamilton/context-coloring
+;; Version: 6.3.0
 ;; Keywords: convenience faces tools
-;; Version: 6.2.0
 ;; Package-Requires: ((emacs "24") (js2-mode "20150126"))
+;; URL: https://github.com/jacksonrayhamilton/context-coloring
 
 ;; This file is part of GNU Emacs.
 
 
 ;;; Commentary:
 
-;; Highlights code according to function context.
-
-;; - Code in the global scope is one color.  Code in functions within the global
-;;   scope is a different color, and code within such functions is another
-;;   color, and so on.
-;; - Identifiers retain the color of the scope in which they are declared.
-
-;; Lexical scope information at-a-glance can assist a programmer in
-;; understanding the overall structure of a program.  It can help to curb nasty
-;; bugs like name shadowing.  A rainbow can indicate excessive complexity.
-;; State change within a closure is easily monitored.
-
-;; By default, Context Coloring still highlights comments and strings
-;; syntactically.  It is still easy to differentiate code from non-code, and
-;; strings cannot be confused for variables.
-
-;; To use, add the following to your ~/.emacs:
-
-;; (require 'context-coloring)
-;; (add-hook 'js2-mode-hook 'context-coloring-mode)
+;; Highlights code by scope.  Top-level scopes are one color, second-level
+;; scopes are another color, and so on.  Variables retain the color of the scope
+;; in which they are defined.  A variable defined in an outer scope referenced
+;; by an inner scope is colored the same as the outer scope.
 
-;; js-mode or js3-mode support requires Node.js 0.10+ and the scopifier
-;; executable.
-
-;; $ npm install -g scopifier
+;; By default, comments and strings are still highlighted syntactically.
 
 ;;; Code:
 
 (require 'js2-mode)
 
 
-;;; Local variables
-
-(defvar-local context-coloring-buffer nil
-  "Reference to this buffer (for timers).")
-
-
 ;;; Utilities
 
 (defun context-coloring-join (strings delimiter)
   "Join a list of STRINGS with the string DELIMITER."
   (mapconcat 'identity strings delimiter))
 
-(defun context-coloring-trim (string)
-  "Remove leading and trailing whitespace from STRING."
-  ;; Trim right.
-  (when (string-match "[ \t\n\r]+\\'" string)
-    (setq string (replace-match "" t t string)))
-  ;; Trim left.
+(defsubst context-coloring-trim-right (string)
+  "Remove leading whitespace from STRING."
+  (if (string-match "[ \t\n\r]+\\'" string)
+      (replace-match "" t t string)
+    string))
+
+(defsubst context-coloring-trim-left (string)
+  "Remove trailing whitespace from STRING."
   (if (string-match "\\`[ \t\n\r]+" string)
       (replace-match "" t t string)
     string))
 
+(defsubst context-coloring-trim (string)
+  "Remove leading and trailing whitespace from STRING."
+  (context-coloring-trim-left (context-coloring-trim-right string)))
+
 
 ;;; Faces
 
@@ -139,6 +120,78 @@ backgrounds."
   (context-coloring-level-face (min level context-coloring-maximum-face)))
 
 
+;;; Change detection
+
+(defvar-local context-coloring-changed-p nil
+  "Indication that the buffer has changed recently, which implies
+that it should be colored again by
+`context-coloring-colorize-idle-timer' if that timer is being
+used.")
+
+(defvar-local context-coloring-changed-start nil
+  "Beginning of last text that changed.")
+
+(defvar-local context-coloring-changed-end nil
+  "End of last text that changed.")
+
+(defvar-local context-coloring-changed-length nil
+  "Length of last text that changed.")
+
+(defun context-coloring-change-function (start end length)
+  "Register a change so that a buffer can be colorized soon."
+  ;; Tokenization is obsolete if there was a change.
+  (context-coloring-cancel-scopification)
+  (setq context-coloring-changed-start start)
+  (setq context-coloring-changed-end end)
+  (setq context-coloring-changed-length length)
+  (setq context-coloring-changed-p t))
+
+(defun context-coloring-maybe-colorize (buffer)
+  "Colorize the current buffer if it has changed."
+  (when (and (eq buffer (current-buffer))
+             context-coloring-changed-p)
+    (context-coloring-colorize)
+    (setq context-coloring-changed-p nil)
+    (setq context-coloring-changed-start nil)
+    (setq context-coloring-changed-end nil)
+    (setq context-coloring-changed-length nil)))
+
+(defvar-local context-coloring-colorize-idle-timer nil
+  "The currently-running idle timer.")
+
+(defcustom context-coloring-delay 0.25
+  "Delay between a buffer update and colorization.
+
+Increase this if your machine is high-performing.  Decrease it if
+it ain't.
+
+Supported modes: `js-mode', `js3-mode', `emacs-lisp-mode'"
+  :group 'context-coloring)
+
+(defun context-coloring-setup-idle-change-detection ()
+  "Setup idle change detection."
+  (add-hook
+   'after-change-functions 'context-coloring-change-function nil t)
+  (add-hook
+   'kill-buffer-hook 'context-coloring-teardown-idle-change-detection nil t)
+  (setq context-coloring-colorize-idle-timer
+        (run-with-idle-timer
+         context-coloring-delay
+         t
+         'context-coloring-maybe-colorize
+         (current-buffer))))
+
+(defun context-coloring-teardown-idle-change-detection ()
+  "Teardown idle change detection."
+  (context-coloring-cancel-scopification)
+  (when context-coloring-colorize-idle-timer
+    (cancel-timer context-coloring-colorize-idle-timer))
+  (remove-hook
+   'kill-buffer-hook 'context-coloring-teardown-idle-change-detection t)
+  (remove-hook
+   'after-change-functions 'context-coloring-change-function t))
+
+
 ;;; Colorization utilities
 
 (defsubst context-coloring-colorize-region (start end level)
@@ -149,10 +202,6 @@ the END point (exclusive) with the face corresponding to LEVEL."
    end
    `(face ,(context-coloring-bounded-level-face level))))
 
-(defcustom context-coloring-comments-and-strings nil
-  "If non-nil, also color comments and strings using `font-lock'."
-  :group 'context-coloring)
-
 (make-obsolete-variable
  'context-coloring-comments-and-strings
  "use `context-coloring-syntactic-comments' and
@@ -175,29 +224,29 @@ the END point (exclusive) with the face corresponding to LEVEL."
   "Tell `font-lock' to color a string but not a comment."
   (if (nth 3 state) font-lock-string-face nil))
 
-(defsubst context-coloring-maybe-colorize-comments-and-strings ()
-  "Color the current buffer's comments and strings if
-`context-coloring-comments-and-strings' is non-nil."
-  (when (or context-coloring-comments-and-strings
-            context-coloring-syntactic-comments
+(defsubst context-coloring-maybe-colorize-comments-and-strings (&optional min max)
+  "Color the current buffer's comments or strings if
+`context-coloring-syntactic-comments' or
+`context-coloring-syntactic-strings' are non-nil."
+  (when (or context-coloring-syntactic-comments
             context-coloring-syntactic-strings)
-    (let ((old-function font-lock-syntactic-face-function)
-          saved-function-p)
-      (cond
-       ((and context-coloring-syntactic-comments
-             (not context-coloring-syntactic-strings))
-        (setq font-lock-syntactic-face-function
-              'context-coloring-font-lock-syntactic-comment-function)
-        (setq saved-function-p t))
-       ((and context-coloring-syntactic-strings
-             (not context-coloring-syntactic-comments))
-        (setq font-lock-syntactic-face-function
-              'context-coloring-font-lock-syntactic-string-function)
-        (setq saved-function-p t)))
+    (let ((min (or min (point-min)))
+          (max (or max (point-max)))
+          (font-lock-syntactic-face-function
+           (cond
+            ((and context-coloring-syntactic-comments
+                  (not context-coloring-syntactic-strings))
+             'context-coloring-font-lock-syntactic-comment-function)
+            ((and context-coloring-syntactic-strings
+                  (not context-coloring-syntactic-comments))
+             'context-coloring-font-lock-syntactic-string-function)
+            (t
+             font-lock-syntactic-face-function))))
       (save-excursion
-        (font-lock-fontify-syntactically-region (point-min) (point-max)))
-      (when saved-function-p
-        (setq font-lock-syntactic-face-function old-function)))))
+        (font-lock-fontify-syntactically-region min max)
+        ;; TODO: Make configurable at the dispatch level.
+        (when (eq major-mode 'emacs-lisp-mode)
+          (font-lock-fontify-keywords-region min max))))))
 
 
 ;;; js2-mode colorization
@@ -247,114 +296,689 @@ variable."
                        ;; `js2-prop-get-node', so this always works.
                        (eq node (js2-prop-get-node-right parent))))))))
 
+(defvar-local context-coloring-point-max nil
+  "Cached value of `point-max'.")
+
 (defsubst context-coloring-js2-colorize-node (node level)
   "Color NODE with the color for LEVEL."
   (let ((start (js2-node-abs-pos node)))
     (context-coloring-colorize-region
      start
-     (+ start (js2-node-len node)) ; End
+     (min
+      ;; End
+      (+ start (js2-node-len node))
+      ;; Somes nodes (like the ast when there is an unterminated multiline
+      ;; comment) will stretch to the value of `point-max'.
+      context-coloring-point-max)
      level)))
 
 (defun context-coloring-js2-colorize ()
   "Color the current buffer using the abstract syntax tree
 generated by `js2-mode'."
-  ;; Don't bother trying to color a mangled tree.
-  (when (= 0 (length js2-parsed-errors))
-    ;; Reset the hash table; the old one could be obsolete.
-    (setq context-coloring-js2-scope-level-hash-table (make-hash-table :test 'eq))
-    (with-silent-modifications
-      (js2-visit-ast
-       js2-mode-ast
-       (lambda (node end-p)
-         (when (null end-p)
-           (cond
-            ((js2-scope-p node)
-             (context-coloring-js2-colorize-node
-              node
-              (context-coloring-js2-scope-level node)))
-            ((context-coloring-js2-local-name-node-p node)
-             (let* ((enclosing-scope (js2-node-get-enclosing-scope node))
-                    (defining-scope (js2-get-defining-scope
-                                     enclosing-scope
-                                     (js2-name-node-name node))))
-               ;; The tree seems to be walked lexically, so an entire scope will
-               ;; be colored, including its name nodes, before they are reached.
-               ;; Coloring the nodes defined in that scope would be redundant, so
-               ;; don't do it.
-               (when (not (eq defining-scope enclosing-scope))
-                 (context-coloring-js2-colorize-node
-                  node
-                  (context-coloring-js2-scope-level defining-scope))))))
-           ;; The `t' indicates to search children.
-           t)))
-      (context-coloring-maybe-colorize-comments-and-strings))))
+  ;; Reset the hash table; the old one could be obsolete.
+  (setq context-coloring-js2-scope-level-hash-table (make-hash-table :test 'eq))
+  (setq context-coloring-point-max (point-max))
+  (with-silent-modifications
+    (js2-visit-ast
+     js2-mode-ast
+     (lambda (node end-p)
+       (when (null end-p)
+         (cond
+          ((js2-scope-p node)
+           (context-coloring-js2-colorize-node
+            node
+            (context-coloring-js2-scope-level node)))
+          ((context-coloring-js2-local-name-node-p node)
+           (let* ((enclosing-scope (js2-node-get-enclosing-scope node))
+                  (defining-scope (js2-get-defining-scope
+                                   enclosing-scope
+                                   (js2-name-node-name node))))
+             ;; The tree seems to be walked lexically, so an entire scope will
+             ;; be colored, including its name nodes, before they are reached.
+             ;; Coloring the nodes defined in that scope would be redundant, so
+             ;; don't do it.
+             (when (not (eq defining-scope enclosing-scope))
+               (context-coloring-js2-colorize-node
+                node
+                (context-coloring-js2-scope-level defining-scope))))))
+         ;; The `t' indicates to search children.
+         t)))
+    (context-coloring-maybe-colorize-comments-and-strings)))
+
+
+;;; Emacs Lisp colorization
+
+(defsubst context-coloring-forward-sws ()
+  "Move forward through whitespace and comments."
+  (while (forward-comment 1)))
+
+(defsubst context-coloring-elisp-forward-sws ()
+  "Move forward through whitespace and comments, colorizing
+them along the way."
+  (let ((start (point)))
+    (context-coloring-forward-sws)
+    (context-coloring-maybe-colorize-comments-and-strings start (point))))
+
+(defsubst context-coloring-elisp-forward-sexp ()
+  "Like `forward-sexp', but colorize comments and strings along
+the way."
+  (let ((start (point)))
+    (forward-sexp)
+    (context-coloring-elisp-colorize-comments-and-strings-in-region
+     start (point))))
+
+(defsubst context-coloring-get-syntax-code ()
+  (syntax-class
+   ;; Faster version of `syntax-after':
+   (aref (syntax-table) (char-after (point)))))
+
+(defsubst context-coloring-exact-regexp (word)
+  "Create a regexp that matches exactly WORD."
+  (concat "\\`" (regexp-quote word) "\\'"))
+
+(defsubst context-coloring-exact-or-regexp (words)
+  "Create a regexp that matches any exact word in WORDS."
+  (context-coloring-join
+   (mapcar 'context-coloring-exact-regexp words) "\\|"))
+
+(defconst context-coloring-elisp-defun-regexp
+  (context-coloring-exact-or-regexp
+   '("defun" "defun*" "defsubst" "defmacro"
+     "cl-defun" "cl-defsubst" "cl-defmacro")))
+
+(defconst context-coloring-elisp-condition-case-regexp
+  (context-coloring-exact-or-regexp
+   '("condition-case"
+     "condition-case-unless-debug")))
+
+(defconst context-coloring-ignored-word-regexp
+  (context-coloring-join (list "\\`[-+]?[0-9]"
+                               "\\`[&:].+"
+                               (context-coloring-exact-or-regexp
+                                '("t" "nil" "." "?")))
+                         "\\|"))
+
+(defconst context-coloring-WORD-CODE 2)
+(defconst context-coloring-SYMBOL-CODE 3)
+(defconst context-coloring-OPEN-PARENTHESIS-CODE 4)
+(defconst context-coloring-CLOSE-PARENTHESIS-CODE 5)
+(defconst context-coloring-EXPRESSION-PREFIX-CODE 6)
+(defconst context-coloring-STRING-QUOTE-CODE 7)
+(defconst context-coloring-ESCAPE-CODE 9)
+(defconst context-coloring-COMMENT-START-CODE 11)
+(defconst context-coloring-COMMENT-END-CODE 12)
+
+(defconst context-coloring-OCTOTHORPE-CHAR (string-to-char "#"))
+(defconst context-coloring-APOSTROPHE-CHAR (string-to-char "'"))
+(defconst context-coloring-OPEN-PARENTHESIS-CHAR (string-to-char "("))
+(defconst context-coloring-COMMA-CHAR (string-to-char ","))
+(defconst context-coloring-AT-CHAR (string-to-char "@"))
+(defconst context-coloring-BACKTICK-CHAR (string-to-char "`"))
+
+(defvar context-coloring-parse-interruptable-p t
+  "Set this to nil to force parse to continue until finished.")
+
+(defconst context-coloring-elisp-sexps-per-pause 1000
+  "Pause after this many iterations to check for user input.
+If user input is pending, stop the parse.  This makes for a
+smoother user experience for large files.")
+
+(defvar context-coloring-elisp-sexp-count 0)
+
+(defsubst context-coloring-elisp-increment-sexp-count ()
+  (setq context-coloring-elisp-sexp-count
+        (1+ context-coloring-elisp-sexp-count))
+  (when (and (zerop (% context-coloring-elisp-sexp-count
+                       context-coloring-elisp-sexps-per-pause))
+             context-coloring-parse-interruptable-p
+             (input-pending-p))
+    (throw 'interrupted t)))
+
+(defvar context-coloring-elisp-scope-stack '())
+
+(defsubst context-coloring-elisp-make-scope (level)
+  (list
+   :level level
+   :variables '()))
+
+(defsubst context-coloring-elisp-scope-get-level (scope)
+  (plist-get scope :level))
+
+(defsubst context-coloring-elisp-scope-add-variable (scope variable)
+  (plist-put scope :variables (cons variable (plist-get scope :variables))))
+
+(defsubst context-coloring-elisp-scope-has-variable (scope variable)
+  (member variable (plist-get scope :variables)))
+
+(defsubst context-coloring-elisp-get-variable-level (variable)
+  (let* ((scope-stack context-coloring-elisp-scope-stack)
+         scope
+         level)
+    (while (and scope-stack (not level))
+      (setq scope (car scope-stack))
+      (cond
+       ((context-coloring-elisp-scope-has-variable scope variable)
+        (setq level (context-coloring-elisp-scope-get-level scope)))
+       (t
+        (setq scope-stack (cdr scope-stack)))))
+    ;; Assume a global variable.
+    (or level 0)))
+
+(defsubst context-coloring-elisp-current-scope-level ()
+  (cond
+   ((car context-coloring-elisp-scope-stack)
+    (context-coloring-elisp-scope-get-level (car context-coloring-elisp-scope-stack)))
+   (t
+    0)))
+
+(defsubst context-coloring-elisp-push-scope ()
+  (push (context-coloring-elisp-make-scope
+         (1+ (context-coloring-elisp-current-scope-level)))
+        context-coloring-elisp-scope-stack))
+
+(defsubst context-coloring-elisp-pop-scope ()
+  (pop context-coloring-elisp-scope-stack))
+
+(defsubst context-coloring-elisp-add-variable (variable)
+  (context-coloring-elisp-scope-add-variable
+   (car context-coloring-elisp-scope-stack)
+   variable))
+
+(defsubst context-coloring-elisp-parse-arg (callback)
+  (let* ((arg-string (buffer-substring-no-properties
+                      (point)
+                      (progn (context-coloring-elisp-forward-sexp)
+                             (point)))))
+    (when (not (string-match-p
+                context-coloring-ignored-word-regexp
+                arg-string))
+      (funcall callback arg-string))))
+
+(defun context-coloring-elisp-parse-let-varlist (type)
+  (let ((varlist '())
+        syntax-code)
+    ;; Enter.
+    (forward-char)
+    (while (/= (setq syntax-code (context-coloring-get-syntax-code))
+               context-coloring-CLOSE-PARENTHESIS-CODE)
+      (cond
+       ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
+        (forward-char)
+        (context-coloring-elisp-forward-sws)
+        (setq syntax-code (context-coloring-get-syntax-code))
+        (when (or (= syntax-code context-coloring-WORD-CODE)
+                  (= syntax-code context-coloring-SYMBOL-CODE))
+          (context-coloring-elisp-parse-arg
+           (lambda (var)
+             (push var varlist)))
+          (context-coloring-elisp-forward-sws)
+          (setq syntax-code (context-coloring-get-syntax-code))
+          (when (/= syntax-code context-coloring-CLOSE-PARENTHESIS-CODE)
+            (context-coloring-elisp-colorize-sexp)))
+        (context-coloring-elisp-forward-sws)
+        ;; Skip past the closing parenthesis.
+        (forward-char))
+       ((or (= syntax-code context-coloring-WORD-CODE)
+            (= syntax-code context-coloring-SYMBOL-CODE))
+        (context-coloring-elisp-parse-arg
+         (lambda (var)
+           (push var varlist)))))
+      (when (eq type 'let*)
+        (context-coloring-elisp-add-variable (pop varlist)))
+      (context-coloring-elisp-forward-sws))
+    (when (eq type 'let)
+      (while varlist
+        (context-coloring-elisp-add-variable (pop varlist))))
+    ;; Exit.
+    (forward-char)))
+
+(defun context-coloring-elisp-parse-arglist ()
+  (let (syntax-code)
+    ;; Enter.
+    (forward-char)
+    (while (/= (setq syntax-code (context-coloring-get-syntax-code))
+               context-coloring-CLOSE-PARENTHESIS-CODE)
+      (cond
+       ((or (= syntax-code context-coloring-WORD-CODE)
+            (= syntax-code context-coloring-SYMBOL-CODE))
+        (context-coloring-elisp-parse-arg
+         (lambda (arg)
+           (context-coloring-elisp-add-variable arg))))
+       (t
+        (context-coloring-elisp-forward-sexp)))
+      (context-coloring-elisp-forward-sws))
+    ;; Exit.
+    (forward-char)))
+
+(defun context-coloring-elisp-colorize-defun-like (&optional anonymous-p
+                                                             let-type)
+  (let ((start (point))
+        end
+        stop
+        syntax-code
+        defun-name-pos
+        defun-name-end)
+    (context-coloring-elisp-push-scope)
+    ;; Color the whole sexp.
+    (forward-sexp)
+    (setq end (point))
+    (context-coloring-colorize-region
+     start
+     end
+     (context-coloring-elisp-current-scope-level))
+    (goto-char start)
+    ;; Enter.
+    (forward-char)
+    (context-coloring-elisp-forward-sws)
+    ;; Skip past the "defun".
+    (forward-sexp)
+    (context-coloring-elisp-forward-sws)
+    (setq stop nil)
+    (unless anonymous-p
+      ;; Check for the defun's name.
+      (setq syntax-code (context-coloring-get-syntax-code))
+      (cond
+       ((or (= syntax-code context-coloring-WORD-CODE)
+            (= syntax-code context-coloring-SYMBOL-CODE))
+        ;; Color the defun's name with the top-level color.
+        (setq defun-name-pos (point))
+        (forward-sexp)
+        (setq defun-name-end (point))
+        (context-coloring-colorize-region defun-name-pos defun-name-end 0)
+        (context-coloring-elisp-forward-sws))
+       (t
+        (setq stop t))))
+    (cond
+     (stop
+      ;; Skip it.
+      (goto-char start)
+      (context-coloring-elisp-forward-sexp))
+     (t
+      (setq syntax-code (context-coloring-get-syntax-code))
+      (cond
+       ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
+        (cond
+         (let-type
+          (context-coloring-elisp-parse-let-varlist let-type))
+         (t
+          (context-coloring-elisp-parse-arglist)))
+        ;; Colorize the rest of the function.
+        (context-coloring-elisp-colorize-region (point) (1- end))
+        ;; Exit the defun.
+        (forward-char))
+       (t
+        ;; Skip it.
+        (goto-char start)
+        (context-coloring-elisp-forward-sexp)))))
+    (context-coloring-elisp-pop-scope)))
+
+(defun context-coloring-elisp-colorize-defun ()
+  (context-coloring-elisp-colorize-defun-like))
+
+(defun context-coloring-elisp-colorize-lambda ()
+  (context-coloring-elisp-colorize-defun-like t))
+
+(defun context-coloring-elisp-colorize-let ()
+  (context-coloring-elisp-colorize-defun-like t 'let))
+
+(defun context-coloring-elisp-colorize-let* ()
+  (context-coloring-elisp-colorize-defun-like t 'let*))
+
+(defun context-coloring-elisp-colorize-cond ()
+  (let (syntax-code)
+    ;; Enter.
+    (forward-char)
+    (context-coloring-elisp-forward-sws)
+    ;; Skip past the "cond".
+    (forward-sexp)
+    (context-coloring-elisp-forward-sws)
+    (while (/= (setq syntax-code (context-coloring-get-syntax-code))
+               context-coloring-CLOSE-PARENTHESIS-CODE)
+      (cond
+       ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
+        ;; Colorize inside the parens.
+        (let ((start (point)))
+          (forward-sexp)
+          (context-coloring-elisp-colorize-region
+           (1+ start) (1- (point)))
+          ;; Exit.
+          (forward-char)))
+       (t
+        (context-coloring-elisp-forward-sexp)))
+      (context-coloring-elisp-forward-sws))
+    ;; Exit.
+    (forward-char)))
+
+(defun context-coloring-elisp-colorize-condition-case ()
+  (let ((start (point))
+        end
+        syntax-code
+        variable
+        case-pos
+        case-end)
+    (context-coloring-elisp-push-scope)
+    ;; Color the whole sexp.
+    (forward-sexp)
+    (setq end (point))
+    (context-coloring-colorize-region
+     start
+     end
+     (context-coloring-elisp-current-scope-level))
+    (goto-char start)
+    ;; Enter.
+    (forward-char)
+    (context-coloring-elisp-forward-sws)
+    ;; Skip past the "condition-case".
+    (forward-sexp)
+    (context-coloring-elisp-forward-sws)
+    (setq syntax-code (context-coloring-get-syntax-code))
+    ;; Gracefully ignore missing variables.
+    (when (or (= syntax-code context-coloring-WORD-CODE)
+              (= syntax-code context-coloring-SYMBOL-CODE))
+      (context-coloring-elisp-parse-arg
+       (lambda (parsed-variable)
+         (setq variable parsed-variable)))
+      (context-coloring-elisp-forward-sws))
+    (context-coloring-elisp-colorize-sexp)
+    (context-coloring-elisp-forward-sws)
+    ;; Parse the handlers with the error variable in scope.
+    (when variable
+      (context-coloring-elisp-add-variable variable))
+    (while (/= (setq syntax-code (context-coloring-get-syntax-code))
+               context-coloring-CLOSE-PARENTHESIS-CODE)
+      (cond
+       ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
+        (setq case-pos (point))
+        (context-coloring-elisp-forward-sexp)
+        (setq case-end (point))
+        (goto-char case-pos)
+        ;; Enter.
+        (forward-char)
+        (context-coloring-elisp-forward-sws)
+        (setq syntax-code (context-coloring-get-syntax-code))
+        (when (/= syntax-code context-coloring-CLOSE-PARENTHESIS-CODE)
+          ;; Skip the condition name(s).
+          (context-coloring-elisp-forward-sexp)
+          ;; Color the remaining portion of the handler.
+          (context-coloring-elisp-colorize-region
+           (point)
+           (1- case-end)))
+        ;; Exit.
+        (forward-char))
+       (t
+        ;; Ignore artifacts.
+        (context-coloring-elisp-forward-sexp)))
+      (context-coloring-elisp-forward-sws))
+    ;; Exit.
+    (forward-char)
+    (context-coloring-elisp-pop-scope)))
+
+(defun context-coloring-elisp-colorize-parenthesized-sexp ()
+  (context-coloring-elisp-increment-sexp-count)
+  (let* ((start (point))
+         (end (progn (forward-sexp)
+                     (point)))
+         (syntax-code (progn (goto-char start)
+                             (forward-char)
+                             ;; Coloring is unnecessary here, it'll happen
+                             ;; presently.
+                             (context-coloring-forward-sws)
+                             (context-coloring-get-syntax-code))))
+    ;; Figure out if the sexp is a special form.
+    (cond
+     ((when (or (= syntax-code context-coloring-WORD-CODE)
+                (= syntax-code context-coloring-SYMBOL-CODE))
+        (let ((name-string (buffer-substring-no-properties
+                            (point)
+                            (progn (forward-sexp)
+                                   (point)))))
+          (cond
+           ((string-match-p context-coloring-elisp-defun-regexp name-string)
+            (goto-char start)
+            (context-coloring-elisp-colorize-defun)
+            t)
+           ((string-equal "let" name-string)
+            (goto-char start)
+            (context-coloring-elisp-colorize-let)
+            t)
+           ((string-equal "let*" name-string)
+            (goto-char start)
+            (context-coloring-elisp-colorize-let*)
+            t)
+           ((string-equal "lambda" name-string)
+            (goto-char start)
+            (context-coloring-elisp-colorize-lambda)
+            t)
+           ((string-equal "cond" name-string)
+            (goto-char start)
+            (context-coloring-elisp-colorize-cond)
+            t)
+           ((string-match-p context-coloring-elisp-condition-case-regexp name-string)
+            (goto-char start)
+            (context-coloring-elisp-colorize-condition-case)
+            t)
+           (t
+            nil)))))
+     ;; Not a special form; just colorize the remaining region.
+     (t
+      (context-coloring-colorize-region
+       start
+       end
+       (context-coloring-elisp-current-scope-level))
+      (context-coloring-elisp-colorize-region (point) (1- end))
+      (forward-char)))))
+
+(defun context-coloring-elisp-colorize-symbol ()
+  (context-coloring-elisp-increment-sexp-count)
+  (let* ((symbol-pos (point))
+         (symbol-end (progn (forward-sexp)
+                            (point)))
+         (symbol-string (buffer-substring-no-properties
+                         symbol-pos
+                         symbol-end)))
+    (cond
+     ((string-match-p context-coloring-ignored-word-regexp symbol-string))
+     (t
+      (context-coloring-colorize-region
+       symbol-pos
+       symbol-end
+       (context-coloring-elisp-get-variable-level
+        symbol-string))))))
+
+(defun context-coloring-elisp-colorize-expression-prefix ()
+  (context-coloring-elisp-increment-sexp-count)
+  (let ((char (char-after))
+        start
+        end)
+    (cond
+     ((or (= char context-coloring-APOSTROPHE-CHAR)
+          (= char context-coloring-OCTOTHORPE-CHAR))
+      (context-coloring-elisp-forward-sexp))
+     ((= char context-coloring-BACKTICK-CHAR)
+      (setq start (point))
+      (setq end (progn (forward-sexp)
+                       (point)))
+      (goto-char start)
+      (while (> end (progn (forward-char)
+                           (point)))
+        (setq char (char-after))
+        (when (= char context-coloring-COMMA-CHAR)
+          (forward-char)
+          (when (= (char-after) context-coloring-AT-CHAR)
+            ;; If we don't do this "@" could be interpreted as a symbol.
+            (forward-char))
+          (context-coloring-elisp-forward-sws)
+          (context-coloring-elisp-colorize-sexp)))
+      ;; We could probably do this as part of the above loop but it'd be
+      ;; repetitive.
+      (context-coloring-elisp-colorize-comments-and-strings-in-region
+       start end)))))
+
+(defun context-coloring-elisp-colorize-comment ()
+  (context-coloring-elisp-increment-sexp-count)
+  (context-coloring-elisp-forward-sws))
+
+(defun context-coloring-elisp-colorize-string ()
+  (context-coloring-elisp-increment-sexp-count)
+  (let ((start (point)))
+    (forward-sexp)
+    (context-coloring-maybe-colorize-comments-and-strings
+     start
+     (point))))
+
+(defun context-coloring-elisp-colorize-sexp ()
+  (let ((syntax-code (context-coloring-get-syntax-code)))
+    (cond
+     ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
+      (context-coloring-elisp-colorize-parenthesized-sexp))
+     ((or (= syntax-code context-coloring-WORD-CODE)
+          (= syntax-code context-coloring-SYMBOL-CODE))
+      (context-coloring-elisp-colorize-symbol))
+     ((= syntax-code context-coloring-EXPRESSION-PREFIX-CODE)
+      (context-coloring-elisp-colorize-expression-prefix))
+     ((= syntax-code context-coloring-STRING-QUOTE-CODE)
+      (context-coloring-elisp-colorize-string))
+     ((= syntax-code context-coloring-ESCAPE-CODE)
+      (forward-char 2))
+     (t
+      (forward-char)))))
+
+(defun context-coloring-elisp-colorize-comments-and-strings-in-region (start end)
+  (let (syntax-code)
+    (goto-char start)
+    (while (> end (progn (skip-syntax-forward "^<\"\\" end)
+                         (point)))
+      (setq syntax-code (context-coloring-get-syntax-code))
+      (cond
+       ((= syntax-code context-coloring-STRING-QUOTE-CODE)
+        (context-coloring-elisp-colorize-string))
+       ((= syntax-code context-coloring-COMMENT-START-CODE)
+        (context-coloring-elisp-colorize-comment))
+       ((= syntax-code context-coloring-ESCAPE-CODE)
+        (forward-char 2))
+       (t
+        (forward-char))))))
+
+(defun context-coloring-elisp-colorize-region (start end)
+  (let (syntax-code)
+    (goto-char start)
+    (while (> end (progn (skip-syntax-forward "^()w_'<\"\\" end)
+                         (point)))
+      (setq syntax-code (context-coloring-get-syntax-code))
+      (cond
+       ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
+        (context-coloring-elisp-colorize-parenthesized-sexp))
+       ((or (= syntax-code context-coloring-WORD-CODE)
+            (= syntax-code context-coloring-SYMBOL-CODE))
+        (context-coloring-elisp-colorize-symbol))
+       ((= syntax-code context-coloring-EXPRESSION-PREFIX-CODE)
+        (context-coloring-elisp-colorize-expression-prefix))
+       ((= syntax-code context-coloring-STRING-QUOTE-CODE)
+        (context-coloring-elisp-colorize-string))
+       ((= syntax-code context-coloring-COMMENT-START-CODE)
+        (context-coloring-elisp-colorize-comment))
+       ((= syntax-code context-coloring-ESCAPE-CODE)
+        (forward-char 2))
+       (t
+        (forward-char))))))
+
+(defun context-coloring-elisp-colorize-region-initially (start end)
+  (setq context-coloring-elisp-sexp-count 0)
+  (setq context-coloring-elisp-scope-stack '())
+  (let ((inhibit-point-motion-hooks t)
+        (case-fold-search nil)
+        ;; This is a recursive-descent parser, so give it a big stack.
+        (max-lisp-eval-depth (max max-lisp-eval-depth 3000))
+        (max-specpdl-size (max max-specpdl-size 3000)))
+    (context-coloring-elisp-colorize-region start end)))
+
+(defun context-coloring-elisp-colorize ()
+  "Color the current buffer, parsing elisp to determine its
+scopes and variables."
+  (interactive)
+  (with-silent-modifications
+    (save-excursion
+      (cond
+       ;; Just colorize the changed region.
+       (context-coloring-changed-p
+        (let ((start (progn (goto-char context-coloring-changed-start)
+                            (beginning-of-defun)
+                            (point)))
+              (end (progn (goto-char context-coloring-changed-end)
+                          (end-of-defun)
+                          (point))))
+          (context-coloring-elisp-colorize-region-initially start end)))
+       (t
+        (context-coloring-elisp-colorize-region-initially (point-min) (point-max)))))))
 
 
 ;;; Shell command scopification / colorization
 
 (defun context-coloring-apply-tokens (tokens)
-  "Process a vector of TOKENS to apply context-based coloring to
-the current buffer.  Tokens are 3 integers: start, end, level.
-The vector is flat, with a new token occurring after every 3rd
-element."
-  (with-silent-modifications
-    (let ((i 0)
-          (len (length tokens)))
-      (while (< i len)
-        (context-coloring-colorize-region
-         (elt tokens i)
-         (elt tokens (+ i 1))
-         (elt tokens (+ i 2)))
-        (setq i (+ i 3))))
-    (context-coloring-maybe-colorize-comments-and-strings)))
+  "Process a string of TOKENS to apply context-based coloring to
+the current buffer.  Tokens are 3 integers: start, end, level.  A
+new token occurrs after every 3rd element, and the elements are
+separated by commas."
+  (let* ((tokens (mapcar 'string-to-number (split-string tokens ","))))
+    (while tokens
+      (context-coloring-colorize-region
+       (pop tokens)
+       (pop tokens)
+       (pop tokens))))
+  (context-coloring-maybe-colorize-comments-and-strings))
 
 (defun context-coloring-parse-array (array)
-  "Parse ARRAY as a flat JSON array of numbers."
-  (let ((braceless (substring (context-coloring-trim array) 1 -1)))
-    (cond
-     ((> (length braceless) 0)
-      (vconcat
-       (mapcar 'string-to-number (split-string braceless ","))))
-     (t
-      (vector)))))
+  "Parse ARRAY as a flat JSON array of numbers and use the tokens
+to colorize the buffer."
+  (let* ((braceless (substring-no-properties (context-coloring-trim array) 1 -1)))
+    (when (> (length braceless) 0)
+      (with-silent-modifications
+        (context-coloring-apply-tokens braceless)))))
+
+(defvar-local context-coloring-scopifier-cancel-function nil
+  "Kills the current scopification process.")
 
 (defvar-local context-coloring-scopifier-process nil
   "The single scopifier process that can be running.")
 
-(defun context-coloring-kill-scopifier ()
-  "Kill the currently-running scopifier process."
+(defun context-coloring-cancel-scopification ()
+  "Stop the currently-running scopifier from scopifying."
+  (when context-coloring-scopifier-cancel-function
+    (funcall context-coloring-scopifier-cancel-function)
+    (setq context-coloring-scopifier-cancel-function nil))
   (when (not (null context-coloring-scopifier-process))
     (delete-process context-coloring-scopifier-process)
     (setq context-coloring-scopifier-process nil)))
 
-(defun context-coloring-scopify-shell-command (command callback)
-  "Invoke a scopifier via COMMAND, read its response
-asynchronously and invoke CALLBACK with its output."
-
-  ;; Prior running tokenization is implicitly obsolete if this function is
-  ;; called.
-  (context-coloring-kill-scopifier)
-
-  ;; Start the process.
-  (setq context-coloring-scopifier-process
-        (start-process-shell-command "scopifier" nil command))
-
-  (let ((output ""))
-
+(defun context-coloring-shell-command (command callback)
+  "Invoke COMMAND, read its response asynchronously and invoke
+CALLBACK with its output.  Return the command process."
+  (let ((process (start-process-shell-command "context-coloring-process" nil command))
+        (output ""))
     ;; The process may produce output in multiple chunks.  This filter
     ;; accumulates the chunks into a message.
     (set-process-filter
-     context-coloring-scopifier-process
+     process
      (lambda (_process chunk)
        (setq output (concat output chunk))))
-
     ;; When the process's message is complete, this sentinel parses it as JSON
     ;; and applies the tokens to the buffer.
     (set-process-sentinel
-     context-coloring-scopifier-process
+     process
      (lambda (_process event)
        (when (equal "finished\n" event)
-         (funcall callback output))))))
+         (funcall callback output))))
+    process))
+
+(defun context-coloring-scopify-shell-command (command callback)
+  "Invoke a scopifier via COMMAND, read its response
+asynchronously and invoke CALLBACK with its output."
+  ;; Prior running tokenization is implicitly obsolete if this function is
+  ;; called.
+  (context-coloring-cancel-scopification)
+  ;; Start the process.
+  (setq context-coloring-scopifier-process
+        (context-coloring-shell-command command callback)))
 
 (defun context-coloring-send-buffer-to-scopifier ()
   "Give the scopifier process its input so it can begin
@@ -365,21 +989,94 @@ scopifying."
   (process-send-eof
    context-coloring-scopifier-process))
 
+(defun context-coloring-start-scopifier-server (command host port callback)
+  (let* ((connect
+          (lambda ()
+            (let ((stream (open-network-stream "context-coloring-stream" nil host port)))
+              (funcall callback stream)))))
+    ;; Try to connect in case a server is running, otherwise start one.
+    (condition-case nil
+        (progn
+          (funcall connect))
+      (error
+       (let ((server (start-process-shell-command
+                      "context-coloring-scopifier-server" nil
+                      (context-coloring-join
+                       (list command
+                             "--server"
+                             "--host" host
+                             "--port" (number-to-string port))
+                       " ")))
+             (output ""))
+         ;; Connect as soon as the "listening" message is printed.
+         (set-process-filter
+          server
+          (lambda (_process chunk)
+            (setq output (concat output chunk))
+            (when (string-match-p (format "^Scopifier listening at %s:%s$" host port) output)
+              (funcall connect)))))))))
+
+(defun context-coloring-send-buffer-to-scopifier-server (command host port callback)
+  (context-coloring-start-scopifier-server
+   command host port
+   (lambda (process)
+     (let* ((body (buffer-substring-no-properties (point-min) (point-max)))
+            (header (concat "POST / HTTP/1.0\r\n"
+                            "Host: localhost\r\n"
+                            "Content-Type: application/x-www-form-urlencoded"
+                            "; charset=UTF8\r\n"
+                            (format "Content-Length: %d\r\n" (length body))
+                            "\r\n"))
+            (output "")
+            (active t))
+       (set-process-filter
+        process
+        (lambda (_process chunk)
+          (setq output (concat output chunk))))
+       (set-process-sentinel
+        process
+        (lambda (_process event)
+          (when (and (equal "connection broken by remote peer\n" event)
+                     active)
+            ;; Strip the response headers.
+            (string-match "\r\n\r\n" output)
+            (setq output (substring-no-properties output (match-end 0)))
+            (funcall callback output))))
+       (process-send-string process (concat header body "\r\n"))
+       (setq context-coloring-scopifier-cancel-function
+             (lambda ()
+               "Cancel this scopification."
+               (setq active nil)))))))
+
+(defun context-coloring-scopify-and-colorize-server (command host port &optional callback)
+  "Contact or start a scopifier server via COMMAND at HOST and
+PORT with the current buffer's contents, read the scopifier's
+response asynchronously and apply a parsed list of tokens to
+`context-coloring-apply-tokens'.
+
+Invoke CALLBACK when complete."
+  (let ((buffer (current-buffer)))
+    (context-coloring-send-buffer-to-scopifier-server
+     command host port
+     (lambda (output)
+       (with-current-buffer buffer
+         (context-coloring-parse-array output))
+       (when callback (funcall callback))))))
+
 (defun context-coloring-scopify-and-colorize (command &optional callback)
   "Invoke a scopifier via COMMAND with the current buffer's contents,
 read the scopifier's response asynchronously and apply a parsed
 list of tokens to `context-coloring-apply-tokens'.
 
 Invoke CALLBACK when complete."
-  (let ((buffer context-coloring-buffer))
+  (let ((buffer (current-buffer)))
     (context-coloring-scopify-shell-command
      command
      (lambda (output)
-       (let ((tokens (context-coloring-parse-array output)))
-         (with-current-buffer buffer
-           (context-coloring-apply-tokens tokens))
-         (setq context-coloring-scopifier-process nil)
-         (when callback (funcall callback))))))
+       (with-current-buffer buffer
+         (context-coloring-parse-array output))
+       (setq context-coloring-scopifier-process nil)
+       (when callback (funcall callback)))))
   (context-coloring-send-buffer-to-scopifier))
 
 
@@ -392,16 +1089,25 @@ Invoke CALLBACK when complete."
 (defvar context-coloring-mode-hash-table (make-hash-table :test 'eq)
   "Map major mode names to dispatch property lists.")
 
+(defun context-coloring-get-dispatch-for-mode (mode)
+  "Return the dispatch for MODE (or a derivative mode)."
+  (let ((parent mode)
+        dispatch)
+    (while (and parent
+                (not (setq dispatch (gethash parent context-coloring-mode-hash-table)))
+                (setq parent (get parent 'derived-mode-parent))))
+    dispatch))
+
 (defun context-coloring-define-dispatch (symbol &rest properties)
   "Define a new dispatch named SYMBOL with PROPERTIES.
 
 A \"dispatch\" is a property list describing a strategy for
 coloring a buffer.  There are three possible strategies: Parse
-and color in a single function (`:colorizer'), parse in a
-function that returns scope data (`:scopifier'), or parse with a
-shell command that returns scope data (`:command').  In the
-latter two cases, the scope data will be used to automatically
-color the buffer.
+and color in a single function (`:colorizer'), parse with a shell
+command that returns scope data (`:command'), or parse with a
+server that returns scope data (`:command', `:host' and `:port').
+In the latter two cases, the scope data will be used to
+automatically color the buffer.
 
 PROPERTIES must include `:modes' and one of `:colorizer',
 `:scopifier' or `:command'.
@@ -411,9 +1117,6 @@ PROPERTIES must include `:modes' and one of `:colorizer',
 `:colorizer' - Symbol referring to a function that parses and
 colors the buffer.
 
-`:scopifier' - Symbol referring to a function that parses the
-buffer a returns a flat vector of start, end and level data.
-
 `:executable' - Optional name of an executable required by
 `:command'.
 
@@ -421,6 +1124,10 @@ buffer a returns a flat vector of start, end and level data.
 sent via stdin, and with a flat JSON array of start, end and
 level data returned via stdout.
 
+`:host' - Hostname of the scopifier server, e.g. \"localhost\".
+
+`:port' - Port number of the scopifier server, e.g. 80, 1337.
+
 `:version' - Minimum required version that should be printed when
 executing `:command' with a \"--version\" flag.  The version
 should be numeric, e.g. \"2\", \"19700101\", \"1.2.3\",
@@ -433,56 +1140,15 @@ should be numeric, e.g. \"2\", \"19700101\", \"1.2.3\",
 `context-coloring-mode' is disabled."
   (let ((modes (plist-get properties :modes))
         (colorizer (plist-get properties :colorizer))
-        (scopifier (plist-get properties :scopifier))
         (command (plist-get properties :command)))
     (when (null modes)
       (error "No mode defined for dispatch"))
     (when (not (or colorizer
-                   scopifier
                    command))
-      (error "No colorizer, scopifier or command defined for dispatch"))
+      (error "No colorizer or command defined for dispatch"))
     (puthash symbol properties context-coloring-dispatch-hash-table)
     (dolist (mode modes)
-      (when (null (gethash mode context-coloring-mode-hash-table))
-        (puthash mode properties context-coloring-mode-hash-table)))))
-
-(context-coloring-define-dispatch
- 'javascript-node
- :modes '(js-mode js3-mode)
- :executable "scopifier"
- :command "scopifier"
- :version "v1.1.1")
-
-(context-coloring-define-dispatch
- 'javascript-js2
- :modes '(js2-mode)
- :colorizer 'context-coloring-js2-colorize
- :setup
- (lambda ()
-   (add-hook 'js2-post-parse-callbacks 'context-coloring-colorize nil t))
- :teardown
- (lambda ()
-   (remove-hook 'js2-post-parse-callbacks 'context-coloring-colorize t)))
-
-(defun context-coloring-dispatch (&optional callback)
-  "Determine the optimal track for scopification / coloring of
-the current buffer, then execute it.
-
-Invoke CALLBACK when complete.  It is invoked synchronously for
-elisp tracks, and asynchronously for shell command tracks."
-  (let ((dispatch (gethash major-mode context-coloring-mode-hash-table))
-        colorizer
-        scopifier
-        command)
-    (cond
-     ((setq colorizer (plist-get dispatch :colorizer))
-      (funcall colorizer)
-      (when callback (funcall callback)))
-     ((setq scopifier (plist-get dispatch :scopifier))
-      (context-coloring-apply-tokens (funcall scopifier))
-      (when callback (funcall callback)))
-     ((setq command (plist-get dispatch :command))
-      (context-coloring-scopify-and-colorize command callback)))))
+      (puthash mode properties context-coloring-mode-hash-table))))
 
 
 ;;; Colorization
@@ -500,25 +1166,6 @@ Invoke CALLBACK when complete; see `context-coloring-dispatch'."
      (when callback (funcall callback))
      (run-hooks 'context-coloring-colorize-hook))))
 
-(defvar-local context-coloring-changed nil
-  "Indication that the buffer has changed recently, which implies
-that it should be colored again by
-`context-coloring-colorize-idle-timer' if that timer is being
-used.")
-
-(defun context-coloring-change-function (_start _end _length)
-  "Register a change so that a buffer can be colorized soon."
-  ;; Tokenization is obsolete if there was a change.
-  (context-coloring-kill-scopifier)
-  (setq context-coloring-changed t))
-
-(defun context-coloring-maybe-colorize ()
-  "Colorize the current buffer if it has changed."
-  (when (and (eq context-coloring-buffer (window-buffer (selected-window)))
-             context-coloring-changed)
-    (setq context-coloring-changed nil)
-    (context-coloring-colorize)))
-
 
 ;;; Versioning
 
@@ -559,11 +1206,11 @@ produces (1 0 0), \"19700101\" produces (19700101), etc."
   "Asynchronously invoke CALLBACK with a predicate indicating
 whether the current scopifier version satisfies the minimum
 version number required for the current major mode."
-  (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
+  (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
     (when dispatch
       (let ((version (plist-get dispatch :version))
             (command (plist-get dispatch :command)))
-        (context-coloring-scopify-shell-command
+        (context-coloring-shell-command
          (context-coloring-join (list command "--version") " ")
          (lambda (output)
            (if (context-coloring-check-version version output)
@@ -789,7 +1436,7 @@ precedence, i.e. the car of `custom-enabled-themes'."
            "#5180b3"
            "#ab75c3"
            "#cd7542"
-           "#dF9522"
+           "#df9522"
            "#454545"))
 
 (context-coloring-define-theme
@@ -823,27 +1470,27 @@ precedence, i.e. the car of `custom-enabled-themes'."
  'leuven
  :recede t
  :colors '("#333333"
-           "#0000FF"
-           "#6434A3"
-           "#BA36A5"
-           "#D0372D"
-           "#036A07"
+           "#0000ff"
+           "#6434a3"
+           "#ba36a5"
+           "#d0372d"
+           "#036a07"
            "#006699"
-           "#006FE0"
+           "#006fe0"
            "#808080"))
 
 (context-coloring-define-theme
  'monokai
  :recede t
- :colors '("#F8F8F2"
-           "#66D9EF"
-           "#A1EFE4"
-           "#A6E22E"
-           "#E6DB74"
-           "#FD971F"
-           "#F92672"
-           "#FD5FF0"
-           "#AE81FF"))
+ :colors '("#f8f8f2"
+           "#66d9ef"
+           "#a1efe4"
+           "#a6e22e"
+           "#e6db74"
+           "#fd971f"
+           "#f92672"
+           "#fd5ff0"
+           "#ae81ff"))
 
 (context-coloring-define-theme
  'solarized
@@ -861,26 +1508,26 @@ precedence, i.e. the car of `custom-enabled-themes'."
            "#dc322f"
            "#d33682"
            "#6c71c4"
-           "#69B7F0"
-           "#69CABF"
-           "#B4C342"
-           "#DEB542"
-           "#F2804F"
-           "#FF6E64"
-           "#F771AC"
-           "#9EA0E5"))
+           "#69b7f0"
+           "#69cabf"
+           "#b4c342"
+           "#deb542"
+           "#f2804f"
+           "#ff6e64"
+           "#f771ac"
+           "#9ea0e5"))
 
 (context-coloring-define-theme
  'spacegray
  :recede t
  :colors '("#ffffff"
-           "#89AAEB"
-           "#C189EB"
+           "#89aaeb"
+           "#c189eb"
            "#bf616a"
-           "#DCA432"
+           "#dca432"
            "#ebcb8b"
-           "#B4EB89"
-           "#89EBCA"))
+           "#b4eb89"
+           "#89ebca"))
 
 (context-coloring-define-theme
  'tango
@@ -902,42 +1549,77 @@ precedence, i.e. the car of `custom-enabled-themes'."
 (context-coloring-define-theme
  'zenburn
  :recede t
- :colors '("#DCDCCC"
-           "#93E0E3"
-           "#BFEBBF"
-           "#F0DFAF"
-           "#DFAF8F"
-           "#CC9393"
-           "#DC8CC3"
-           "#94BFF3"
-           "#9FC59F"
-           "#D0BF8F"
-           "#DCA3A3"))
+ :colors '("#dcdccc"
+           "#93e0e3"
+           "#bfebbf"
+           "#f0dfaf"
+           "#dfaf8f"
+           "#cc9393"
+           "#dc8cc3"
+           "#94bff3"
+           "#9fc59f"
+           "#d0bf8f"
+           "#dca3a3"))
 
 
-;;; Minor mode
+;;; Built-in dispatches
 
-(defvar-local context-coloring-colorize-idle-timer nil
-  "The currently-running idle timer.")
+(context-coloring-define-dispatch
+ 'javascript-node
+ :modes '(js-mode js3-mode)
+ :executable "scopifier"
+ :command "scopifier"
+ :version "v1.2.1"
+ :host "localhost"
+ :port 6969)
 
-(defcustom context-coloring-delay 0.25
-  "Delay between a buffer update and colorization.
+(context-coloring-define-dispatch
+ 'javascript-js2
+ :modes '(js2-mode)
+ :colorizer 'context-coloring-js2-colorize
+ :setup
+ (lambda ()
+   (add-hook 'js2-post-parse-callbacks 'context-coloring-colorize nil t))
+ :teardown
+ (lambda ()
+   (remove-hook 'js2-post-parse-callbacks 'context-coloring-colorize t)))
 
-Increase this if your machine is high-performing.  Decrease it if
-it ain't.
+(context-coloring-define-dispatch
+ 'emacs-lisp
+ :modes '(emacs-lisp-mode)
+ :colorizer 'context-coloring-elisp-colorize
+ :setup 'context-coloring-setup-idle-change-detection
+ :teardown 'context-coloring-teardown-idle-change-detection)
 
-Supported modes: `js-mode', `js3-mode'"
-  :group 'context-coloring)
+(defun context-coloring-dispatch (&optional callback)
+  "Determine the optimal track for scopification / coloring of
+the current buffer, then execute it.
 
-(defun context-coloring-setup-idle-change-detection ()
-  "Setup idle change detection."
-  (add-hook
-   'after-change-functions 'context-coloring-change-function nil t)
-  (setq context-coloring-colorize-idle-timer
-        (run-with-idle-timer
-         context-coloring-delay
-         t
-         'context-coloring-maybe-colorize)))
+Invoke CALLBACK when complete.  It is invoked synchronously for
+elisp tracks, and asynchronously for shell command tracks."
+  (let* ((dispatch (context-coloring-get-dispatch-for-mode major-mode))
+         (colorizer (plist-get dispatch :colorizer))
+         (command (plist-get dispatch :command))
+         (host (plist-get dispatch :host))
+         (port (plist-get dispatch :port))
+         interrupted-p)
+    (cond
+     (colorizer
+      (setq interrupted-p
+            (catch 'interrupted
+              (funcall colorizer)))
+      (when (and (not interrupted-p)
+                 callback)
+        (funcall callback)))
+     (command
+      (cond
+       ((and host port)
+        (context-coloring-scopify-and-colorize-server command host port callback))
+       (t
+        (context-coloring-scopify-and-colorize command callback)))))))
+
+
+;;; Minor mode
 
 ;;;###autoload
 (define-minor-mode context-coloring-mode
@@ -945,32 +1627,28 @@ Supported modes: `js-mode', `js3-mode'"
   nil " Context" nil
   (if (not context-coloring-mode)
       (progn
-        (context-coloring-kill-scopifier)
-        (when context-coloring-colorize-idle-timer
-          (cancel-timer context-coloring-colorize-idle-timer))
-        (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
+        (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
           (when dispatch
             (let ((command (plist-get dispatch :command))
                   (teardown (plist-get dispatch :teardown)))
               (when command
-                (remove-hook
-                 'after-change-functions 'context-coloring-change-function t))
+                (context-coloring-teardown-idle-change-detection))
               (when teardown
                 (funcall teardown)))))
         (font-lock-mode)
         (jit-lock-mode t))
 
-    ;; Remember this buffer.  This value should not be dynamically-bound.
-    (setq context-coloring-buffer (current-buffer))
-
     ;; Font lock is incompatible with this mode; the converse is also true.
     (font-lock-mode 0)
     (jit-lock-mode nil)
 
+    ;; ...but we do use font-lock functions here.
+    (font-lock-set-defaults)
+
     ;; Safely change the valye of this function as necessary.
     (make-local-variable 'font-lock-syntactic-face-function)
 
-    (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
+    (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
       (if dispatch
           (progn
             (let ((command (plist-get dispatch :command))
@@ -1001,7 +1679,8 @@ Supported modes: `js-mode', `js3-mode'"
                 (funcall setup))
               ;; Colorize once initially.
               (when colorize-initially-p
-                (context-coloring-colorize))))
+                (let ((context-coloring-parse-interruptable-p nil))
+                  (context-coloring-colorize)))))
         (when (null dispatch)
           (message "Context coloring is not available for this major mode"))))))