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.
42 (defun context-coloring-join (strings delimiter)
43 "Join a list of STRINGS with the string DELIMITER."
44 (mapconcat #'identity strings delimiter))
46 (defsubst context-coloring-trim-right (string)
47 "Remove leading whitespace from STRING."
48 (if (string-match "[ \t\n\r]+\\'" string)
49 (replace-match "" t t string)
52 (defsubst context-coloring-trim-left (string)
53 "Remove trailing whitespace from STRING."
54 (if (string-match "\\`[ \t\n\r]+" string)
55 (replace-match "" t t string)
58 (defsubst context-coloring-trim (string)
59 "Remove leading and trailing whitespace from STRING."
60 (context-coloring-trim-left (context-coloring-trim-right string)))
65 (defun context-coloring-defface (level tty light dark)
66 "Define a face for LEVEL with colors for TTY, LIGHT and DARK
68 (let ((face (intern (format "context-coloring-level-%s-face" level)))
69 (doc (format "Context coloring face, level %s." level)))
72 `((((type tty)) (:foreground ,tty))
73 (((background light)) (:foreground ,light))
74 (((background dark)) (:foreground ,dark)))
76 :group 'context-coloring)))
78 (defun context-coloring-defface-neutral (level)
79 "Define a face for LEVEL with the default neutral colors."
80 (context-coloring-defface level nil "#3f3f3f" "#cdcdcd"))
82 (context-coloring-defface 0 nil "#000000" "#ffffff")
83 (context-coloring-defface 1 "yellow" "#008b8b" "#00ffff")
84 (context-coloring-defface 2 "green" "#0000ff" "#87cefa")
85 (context-coloring-defface 3 "cyan" "#483d8b" "#b0c4de")
86 (context-coloring-defface 4 "blue" "#a020f0" "#eedd82")
87 (context-coloring-defface 5 "magenta" "#a0522d" "#98fb98")
88 (context-coloring-defface 6 "red" "#228b22" "#7fffd4")
89 (context-coloring-defface-neutral 7)
91 (defvar context-coloring-maximum-face nil
92 "Index of the highest face available for coloring.")
94 (defvar context-coloring-original-maximum-face nil
95 "Fallback value for `context-coloring-maximum-face' when all
96 themes have been disabled.")
98 (setq context-coloring-maximum-face 7)
100 (setq context-coloring-original-maximum-face
101 context-coloring-maximum-face)
103 ;; Theme authors can have up to 26 levels: 1 (0th) for globals, 24 (1st-24th)
104 ;; for nested levels, and 1 (25th) for infinity.
106 (context-coloring-defface-neutral (+ number context-coloring-maximum-face 1)))
111 (defsubst context-coloring-level-face (level)
112 "Return the symbol for a face with LEVEL."
113 ;; `concat' is faster than `format' here.
115 (concat "context-coloring-level-" (number-to-string level) "-face")))
117 (defsubst context-coloring-bounded-level-face (level)
118 "Return the symbol for a face with LEVEL, bounded by
119 `context-coloring-maximum-face'."
120 (context-coloring-level-face (min level context-coloring-maximum-face)))
125 (defvar-local context-coloring-changed-p nil
126 "Indication that the buffer has changed recently, which implies
127 that it should be colored again by
128 `context-coloring-colorize-idle-timer' if that timer is being
131 (defvar-local context-coloring-changed-start nil
132 "Beginning of last text that changed.")
134 (defvar-local context-coloring-changed-end nil
135 "End of last text that changed.")
137 (defvar-local context-coloring-changed-length nil
138 "Length of last text that changed.")
140 (defun context-coloring-change-function (start end length)
141 "Register a change so that a buffer can be colorized soon."
142 ;; Tokenization is obsolete if there was a change.
143 (context-coloring-cancel-scopification)
144 (setq context-coloring-changed-start start)
145 (setq context-coloring-changed-end end)
146 (setq context-coloring-changed-length length)
147 (setq context-coloring-changed-p t))
149 (defun context-coloring-maybe-colorize (buffer)
150 "Colorize the current buffer if it has changed."
151 (when (and (eq buffer (current-buffer))
152 context-coloring-changed-p)
153 (context-coloring-colorize)
154 (setq context-coloring-changed-p nil)
155 (setq context-coloring-changed-start nil)
156 (setq context-coloring-changed-end nil)
157 (setq context-coloring-changed-length nil)))
159 (defvar-local context-coloring-colorize-idle-timer nil
160 "The currently-running idle timer.")
162 (defcustom context-coloring-default-delay 0.25
163 "Default (sometimes overridden) delay between a buffer update
166 Increase this if your machine is high-performing. Decrease it if
169 Supported modes: `js-mode', `js3-mode'"
170 :group 'context-coloring)
172 (make-obsolete-variable
173 'context-coloring-delay
174 'context-coloring-default-delay
177 (defun context-coloring-setup-idle-change-detection ()
178 "Setup idle change detection."
179 (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
181 'after-change-functions #'context-coloring-change-function nil t)
183 'kill-buffer-hook #'context-coloring-teardown-idle-change-detection nil t)
184 (setq context-coloring-colorize-idle-timer
186 (or (plist-get dispatch :delay) context-coloring-default-delay)
188 #'context-coloring-maybe-colorize
191 (defun context-coloring-teardown-idle-change-detection ()
192 "Teardown idle change detection."
193 (context-coloring-cancel-scopification)
194 (when context-coloring-colorize-idle-timer
195 (cancel-timer context-coloring-colorize-idle-timer))
197 'kill-buffer-hook #'context-coloring-teardown-idle-change-detection t)
199 'after-change-functions #'context-coloring-change-function t))
202 ;;; Colorization utilities
204 (defsubst context-coloring-colorize-region (start end level)
205 "Color characters from the 1-indexed START point (inclusive) to
206 the END point (exclusive) with the face corresponding to LEVEL."
210 `(face ,(context-coloring-bounded-level-face level))))
212 (make-obsolete-variable
213 'context-coloring-comments-and-strings
214 "use `context-coloring-syntactic-comments' and
215 `context-coloring-syntactic-strings' instead."
218 (defcustom context-coloring-syntactic-comments t
219 "If non-nil, also color comments using `font-lock'."
220 :group 'context-coloring)
222 (defcustom context-coloring-syntactic-strings t
223 "If non-nil, also color strings using `font-lock'."
224 :group 'context-coloring)
226 (defun context-coloring-font-lock-syntactic-comment-function (state)
227 "Tell `font-lock' to color a comment but not a string."
228 (if (nth 3 state) nil font-lock-comment-face))
230 (defun context-coloring-font-lock-syntactic-string-function (state)
231 "Tell `font-lock' to color a string but not a comment."
232 (if (nth 3 state) font-lock-string-face nil))
234 (defsubst context-coloring-maybe-colorize-comments-and-strings (&optional min max)
235 "Color the current buffer's comments or strings if
236 `context-coloring-syntactic-comments' or
237 `context-coloring-syntactic-strings' are non-nil."
238 (when (or context-coloring-syntactic-comments
239 context-coloring-syntactic-strings)
240 (let ((min (or min (point-min)))
241 (max (or max (point-max)))
242 (font-lock-syntactic-face-function
244 ((and context-coloring-syntactic-comments
245 (not context-coloring-syntactic-strings))
246 #'context-coloring-font-lock-syntactic-comment-function)
247 ((and context-coloring-syntactic-strings
248 (not context-coloring-syntactic-comments))
249 #'context-coloring-font-lock-syntactic-string-function)
251 font-lock-syntactic-face-function))))
253 (font-lock-fontify-syntactically-region min max)
254 ;; TODO: Make configurable at the dispatch level.
255 (when (eq major-mode 'emacs-lisp-mode)
256 (font-lock-fontify-keywords-region min max))))))
259 ;;; js2-mode colorization
261 (defvar-local context-coloring-js2-scope-level-hash-table nil
262 "Associate `js2-scope' structures and with their scope
265 (defcustom context-coloring-js-block-scopes nil
266 "If non-nil, also color block scopes in the scope hierarchy in JavaScript.
268 The block-scoped `let' and `const' are introduced in ES6. Enable
269 this for ES6 code; disable it elsewhere.
271 Supported modes: `js2-mode'"
272 :group 'context-coloring)
274 (defsubst context-coloring-js2-scope-level (scope)
275 "Return the level of SCOPE."
276 (cond ((gethash scope context-coloring-js2-scope-level-hash-table))
279 (current-scope scope)
281 (while (and current-scope
282 (js2-node-parent current-scope)
283 (setq enclosing-scope
284 (js2-node-get-enclosing-scope current-scope)))
285 (when (or context-coloring-js-block-scopes
286 (let ((type (js2-scope-type current-scope)))
287 (or (= type js2-SCRIPT)
288 (= type js2-FUNCTION)
289 (= type js2-CATCH))))
290 (setq level (+ level 1)))
291 (setq current-scope enclosing-scope))
292 (puthash scope level context-coloring-js2-scope-level-hash-table)))))
294 (defsubst context-coloring-js2-local-name-node-p (node)
295 "Determine if NODE is a `js2-name-node' representing a local
297 (and (js2-name-node-p node)
298 (let ((parent (js2-node-parent node)))
299 (not (or (and (js2-object-prop-node-p parent)
300 (eq node (js2-object-prop-node-left parent)))
301 (and (js2-prop-get-node-p parent)
302 ;; For nested property lookup, the node on the left is a
303 ;; `js2-prop-get-node', so this always works.
304 (eq node (js2-prop-get-node-right parent))))))))
306 (defvar-local context-coloring-point-max nil
307 "Cached value of `point-max'.")
309 (defsubst context-coloring-js2-colorize-node (node level)
310 "Color NODE with the color for LEVEL."
311 (let ((start (js2-node-abs-pos node)))
312 (context-coloring-colorize-region
316 (+ start (js2-node-len node))
317 ;; Somes nodes (like the ast when there is an unterminated multiline
318 ;; comment) will stretch to the value of `point-max'.
319 context-coloring-point-max)
322 (defun context-coloring-js2-colorize ()
323 "Color the current buffer using the abstract syntax tree
324 generated by `js2-mode'."
325 ;; Reset the hash table; the old one could be obsolete.
326 (setq context-coloring-js2-scope-level-hash-table (make-hash-table :test #'eq))
327 (setq context-coloring-point-max (point-max))
328 (with-silent-modifications
335 (context-coloring-js2-colorize-node
337 (context-coloring-js2-scope-level node)))
338 ((context-coloring-js2-local-name-node-p node)
339 (let* ((enclosing-scope (js2-node-get-enclosing-scope node))
340 (defining-scope (js2-get-defining-scope
342 (js2-name-node-name node))))
343 ;; The tree seems to be walked lexically, so an entire scope will
344 ;; be colored, including its name nodes, before they are reached.
345 ;; Coloring the nodes defined in that scope would be redundant, so
347 (when (not (eq defining-scope enclosing-scope))
348 (context-coloring-js2-colorize-node
350 (context-coloring-js2-scope-level defining-scope))))))
351 ;; The `t' indicates to search children.
353 (context-coloring-maybe-colorize-comments-and-strings)))
356 ;;; Emacs Lisp colorization
358 (defsubst context-coloring-forward-sws ()
359 "Move forward through whitespace and comments."
360 (while (forward-comment 1)))
362 (defsubst context-coloring-elisp-forward-sws ()
363 "Move forward through whitespace and comments, colorizing
365 (let ((start (point)))
366 (context-coloring-forward-sws)
367 (context-coloring-maybe-colorize-comments-and-strings start (point))))
369 (defsubst context-coloring-elisp-forward-sexp ()
370 "Like `forward-sexp', but colorize comments and strings along
372 (let ((start (point)))
374 (context-coloring-elisp-colorize-comments-and-strings-in-region
377 (defsubst context-coloring-get-syntax-code ()
379 ;; Faster version of `syntax-after':
380 (aref (syntax-table) (char-after (point)))))
382 (defsubst context-coloring-exact-regexp (word)
383 "Create a regexp that matches exactly WORD."
384 (concat "\\`" (regexp-quote word) "\\'"))
386 (defsubst context-coloring-exact-or-regexp (words)
387 "Create a regexp that matches any exact word in WORDS."
388 (context-coloring-join
389 (mapcar #'context-coloring-exact-regexp words) "\\|"))
391 (defconst context-coloring-elisp-defun-regexp
392 (context-coloring-exact-or-regexp
393 '("defun" "defun*" "defsubst" "defmacro"
394 "cl-defun" "cl-defsubst" "cl-defmacro")))
396 (defconst context-coloring-elisp-condition-case-regexp
397 (context-coloring-exact-or-regexp
399 "condition-case-unless-debug")))
401 (defconst context-coloring-ignored-word-regexp
402 (context-coloring-join (list "\\`[-+]?[0-9]"
404 (context-coloring-exact-or-regexp
405 '("t" "nil" "." "?")))
408 (defconst context-coloring-WORD-CODE 2)
409 (defconst context-coloring-SYMBOL-CODE 3)
410 (defconst context-coloring-OPEN-PARENTHESIS-CODE 4)
411 (defconst context-coloring-CLOSE-PARENTHESIS-CODE 5)
412 (defconst context-coloring-EXPRESSION-PREFIX-CODE 6)
413 (defconst context-coloring-STRING-QUOTE-CODE 7)
414 (defconst context-coloring-ESCAPE-CODE 9)
415 (defconst context-coloring-COMMENT-START-CODE 11)
416 (defconst context-coloring-COMMENT-END-CODE 12)
418 (defconst context-coloring-OCTOTHORPE-CHAR (string-to-char "#"))
419 (defconst context-coloring-APOSTROPHE-CHAR (string-to-char "'"))
420 (defconst context-coloring-OPEN-PARENTHESIS-CHAR (string-to-char "("))
421 (defconst context-coloring-COMMA-CHAR (string-to-char ","))
422 (defconst context-coloring-AT-CHAR (string-to-char "@"))
423 (defconst context-coloring-BACKTICK-CHAR (string-to-char "`"))
425 (defvar context-coloring-parse-interruptable-p t
426 "Set this to nil to force parse to continue until finished.")
428 (defconst context-coloring-elisp-sexps-per-pause 1000
429 "Pause after this many iterations to check for user input.
430 If user input is pending, stop the parse. This makes for a
431 smoother user experience for large files.")
433 (defvar context-coloring-elisp-sexp-count 0)
435 (defsubst context-coloring-elisp-increment-sexp-count ()
436 (setq context-coloring-elisp-sexp-count
437 (1+ context-coloring-elisp-sexp-count))
438 (when (and (zerop (% context-coloring-elisp-sexp-count
439 context-coloring-elisp-sexps-per-pause))
440 context-coloring-parse-interruptable-p
442 (throw 'interrupted t)))
444 (defvar context-coloring-elisp-scope-stack '())
446 (defsubst context-coloring-elisp-make-scope (level)
451 (defsubst context-coloring-elisp-scope-get-level (scope)
452 (plist-get scope :level))
454 (defsubst context-coloring-elisp-scope-add-variable (scope variable)
455 (plist-put scope :variables (cons variable (plist-get scope :variables))))
457 (defsubst context-coloring-elisp-scope-has-variable (scope variable)
458 (member variable (plist-get scope :variables)))
460 (defsubst context-coloring-elisp-get-variable-level (variable)
461 (let* ((scope-stack context-coloring-elisp-scope-stack)
464 (while (and scope-stack (not level))
465 (setq scope (car scope-stack))
467 ((context-coloring-elisp-scope-has-variable scope variable)
468 (setq level (context-coloring-elisp-scope-get-level scope)))
470 (setq scope-stack (cdr scope-stack)))))
471 ;; Assume a global variable.
474 (defsubst context-coloring-elisp-current-scope-level ()
476 ((car context-coloring-elisp-scope-stack)
477 (context-coloring-elisp-scope-get-level (car context-coloring-elisp-scope-stack)))
481 (defsubst context-coloring-elisp-push-scope ()
482 (push (context-coloring-elisp-make-scope
483 (1+ (context-coloring-elisp-current-scope-level)))
484 context-coloring-elisp-scope-stack))
486 (defsubst context-coloring-elisp-pop-scope ()
487 (pop context-coloring-elisp-scope-stack))
489 (defsubst context-coloring-elisp-add-variable (variable)
490 (context-coloring-elisp-scope-add-variable
491 (car context-coloring-elisp-scope-stack)
494 (defsubst context-coloring-elisp-parse-arg (callback)
495 (let* ((arg-string (buffer-substring-no-properties
497 (progn (context-coloring-elisp-forward-sexp)
499 (when (not (string-match-p
500 context-coloring-ignored-word-regexp
502 (funcall callback arg-string))))
504 (defun context-coloring-elisp-parse-let-varlist (type)
509 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
510 context-coloring-CLOSE-PARENTHESIS-CODE)
512 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
514 (context-coloring-elisp-forward-sws)
515 (setq syntax-code (context-coloring-get-syntax-code))
516 (when (or (= syntax-code context-coloring-WORD-CODE)
517 (= syntax-code context-coloring-SYMBOL-CODE))
518 (context-coloring-elisp-parse-arg
521 (context-coloring-elisp-forward-sws)
522 (setq syntax-code (context-coloring-get-syntax-code))
523 (when (/= syntax-code context-coloring-CLOSE-PARENTHESIS-CODE)
524 (context-coloring-elisp-colorize-sexp)))
525 (context-coloring-elisp-forward-sws)
526 ;; Skip past the closing parenthesis.
528 ((or (= syntax-code context-coloring-WORD-CODE)
529 (= syntax-code context-coloring-SYMBOL-CODE))
530 (context-coloring-elisp-parse-arg
532 (push var varlist)))))
533 (when (eq type 'let*)
534 (context-coloring-elisp-add-variable (pop varlist)))
535 (context-coloring-elisp-forward-sws))
538 (context-coloring-elisp-add-variable (pop varlist))))
542 (defun context-coloring-elisp-parse-arglist ()
546 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
547 context-coloring-CLOSE-PARENTHESIS-CODE)
549 ((or (= syntax-code context-coloring-WORD-CODE)
550 (= syntax-code context-coloring-SYMBOL-CODE))
551 (context-coloring-elisp-parse-arg
553 (context-coloring-elisp-add-variable arg))))
555 (context-coloring-elisp-forward-sexp)))
556 (context-coloring-elisp-forward-sws))
560 (defun context-coloring-elisp-colorize-defun-like (&optional anonymous-p
562 (let ((start (point))
568 (context-coloring-elisp-push-scope)
569 ;; Color the whole sexp.
572 (context-coloring-colorize-region
575 (context-coloring-elisp-current-scope-level))
579 (context-coloring-elisp-forward-sws)
580 ;; Skip past the "defun".
582 (context-coloring-elisp-forward-sws)
585 ;; Check for the defun's name.
586 (setq syntax-code (context-coloring-get-syntax-code))
588 ((or (= syntax-code context-coloring-WORD-CODE)
589 (= syntax-code context-coloring-SYMBOL-CODE))
590 ;; Color the defun's name with the top-level color.
591 (setq defun-name-pos (point))
593 (setq defun-name-end (point))
594 (context-coloring-colorize-region defun-name-pos defun-name-end 0)
595 (context-coloring-elisp-forward-sws))
602 (context-coloring-elisp-forward-sexp))
604 (setq syntax-code (context-coloring-get-syntax-code))
606 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
609 (context-coloring-elisp-parse-let-varlist let-type))
611 (context-coloring-elisp-parse-arglist)))
612 ;; Colorize the rest of the function.
613 (context-coloring-elisp-colorize-region (point) (1- end))
619 (context-coloring-elisp-forward-sexp)))))
620 (context-coloring-elisp-pop-scope)))
622 (defun context-coloring-elisp-colorize-defun ()
623 (context-coloring-elisp-colorize-defun-like))
625 (defun context-coloring-elisp-colorize-lambda ()
626 (context-coloring-elisp-colorize-defun-like t))
628 (defun context-coloring-elisp-colorize-let ()
629 (context-coloring-elisp-colorize-defun-like t 'let))
631 (defun context-coloring-elisp-colorize-let* ()
632 (context-coloring-elisp-colorize-defun-like t 'let*))
634 (defun context-coloring-elisp-colorize-cond ()
638 (context-coloring-elisp-forward-sws)
639 ;; Skip past the "cond".
641 (context-coloring-elisp-forward-sws)
642 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
643 context-coloring-CLOSE-PARENTHESIS-CODE)
645 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
646 ;; Colorize inside the parens.
647 (let ((start (point)))
649 (context-coloring-elisp-colorize-region
650 (1+ start) (1- (point)))
654 (context-coloring-elisp-forward-sexp)))
655 (context-coloring-elisp-forward-sws))
659 (defun context-coloring-elisp-colorize-condition-case ()
660 (let ((start (point))
666 (context-coloring-elisp-push-scope)
667 ;; Color the whole sexp.
670 (context-coloring-colorize-region
673 (context-coloring-elisp-current-scope-level))
677 (context-coloring-elisp-forward-sws)
678 ;; Skip past the "condition-case".
680 (context-coloring-elisp-forward-sws)
681 (setq syntax-code (context-coloring-get-syntax-code))
682 ;; Gracefully ignore missing variables.
683 (when (or (= syntax-code context-coloring-WORD-CODE)
684 (= syntax-code context-coloring-SYMBOL-CODE))
685 (context-coloring-elisp-parse-arg
686 (lambda (parsed-variable)
687 (setq variable parsed-variable)))
688 (context-coloring-elisp-forward-sws))
689 (context-coloring-elisp-colorize-sexp)
690 (context-coloring-elisp-forward-sws)
691 ;; Parse the handlers with the error variable in scope.
693 (context-coloring-elisp-add-variable variable))
694 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
695 context-coloring-CLOSE-PARENTHESIS-CODE)
697 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
698 (setq case-pos (point))
699 (context-coloring-elisp-forward-sexp)
700 (setq case-end (point))
704 (context-coloring-elisp-forward-sws)
705 (setq syntax-code (context-coloring-get-syntax-code))
706 (when (/= syntax-code context-coloring-CLOSE-PARENTHESIS-CODE)
707 ;; Skip the condition name(s).
708 (context-coloring-elisp-forward-sexp)
709 ;; Color the remaining portion of the handler.
710 (context-coloring-elisp-colorize-region
717 (context-coloring-elisp-forward-sexp)))
718 (context-coloring-elisp-forward-sws))
721 (context-coloring-elisp-pop-scope)))
723 (defun context-coloring-elisp-colorize-parenthesized-sexp ()
724 (context-coloring-elisp-increment-sexp-count)
725 (let* ((start (point))
726 (end (progn (forward-sexp)
728 (syntax-code (progn (goto-char start)
730 ;; Coloring is unnecessary here, it'll happen
732 (context-coloring-forward-sws)
733 (context-coloring-get-syntax-code))))
734 ;; Figure out if the sexp is a special form.
736 ((when (or (= syntax-code context-coloring-WORD-CODE)
737 (= syntax-code context-coloring-SYMBOL-CODE))
738 (let ((name-string (buffer-substring-no-properties
740 (progn (forward-sexp)
743 ((string-match-p context-coloring-elisp-defun-regexp name-string)
745 (context-coloring-elisp-colorize-defun)
747 ((string-equal "let" name-string)
749 (context-coloring-elisp-colorize-let)
751 ((string-equal "let*" name-string)
753 (context-coloring-elisp-colorize-let*)
755 ((string-equal "lambda" name-string)
757 (context-coloring-elisp-colorize-lambda)
759 ((string-equal "cond" name-string)
761 (context-coloring-elisp-colorize-cond)
763 ((string-match-p context-coloring-elisp-condition-case-regexp name-string)
765 (context-coloring-elisp-colorize-condition-case)
769 ;; Not a special form; just colorize the remaining region.
771 (context-coloring-colorize-region
774 (context-coloring-elisp-current-scope-level))
775 (context-coloring-elisp-colorize-region (point) (1- end))
778 (defun context-coloring-elisp-colorize-symbol ()
779 (context-coloring-elisp-increment-sexp-count)
780 (let* ((symbol-pos (point))
781 (symbol-end (progn (forward-sexp)
783 (symbol-string (buffer-substring-no-properties
787 ((string-match-p context-coloring-ignored-word-regexp symbol-string))
789 (context-coloring-colorize-region
792 (context-coloring-elisp-get-variable-level
795 (defun context-coloring-elisp-colorize-expression-prefix ()
796 (context-coloring-elisp-increment-sexp-count)
797 (let ((char (char-after))
801 ((or (= char context-coloring-APOSTROPHE-CHAR)
802 (= char context-coloring-OCTOTHORPE-CHAR))
803 (context-coloring-elisp-forward-sexp))
804 ((= char context-coloring-BACKTICK-CHAR)
806 (setq end (progn (forward-sexp)
809 (while (> end (progn (forward-char)
811 (setq char (char-after))
812 (when (= char context-coloring-COMMA-CHAR)
814 (when (= (char-after) context-coloring-AT-CHAR)
815 ;; If we don't do this "@" could be interpreted as a symbol.
817 (context-coloring-elisp-forward-sws)
818 (context-coloring-elisp-colorize-sexp)))
819 ;; We could probably do this as part of the above loop but it'd be
821 (context-coloring-elisp-colorize-comments-and-strings-in-region
824 (defun context-coloring-elisp-colorize-comment ()
825 (context-coloring-elisp-increment-sexp-count)
826 (context-coloring-elisp-forward-sws))
828 (defun context-coloring-elisp-colorize-string ()
829 (context-coloring-elisp-increment-sexp-count)
830 (let ((start (point)))
832 (context-coloring-maybe-colorize-comments-and-strings
836 (defun context-coloring-elisp-colorize-sexp ()
837 (let ((syntax-code (context-coloring-get-syntax-code)))
839 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
840 (context-coloring-elisp-colorize-parenthesized-sexp))
841 ((or (= syntax-code context-coloring-WORD-CODE)
842 (= syntax-code context-coloring-SYMBOL-CODE))
843 (context-coloring-elisp-colorize-symbol))
844 ((= syntax-code context-coloring-EXPRESSION-PREFIX-CODE)
845 (context-coloring-elisp-colorize-expression-prefix))
846 ((= syntax-code context-coloring-STRING-QUOTE-CODE)
847 (context-coloring-elisp-colorize-string))
848 ((= syntax-code context-coloring-ESCAPE-CODE)
853 (defun context-coloring-elisp-colorize-comments-and-strings-in-region (start end)
856 (while (> end (progn (skip-syntax-forward "^<\"\\" end)
858 (setq syntax-code (context-coloring-get-syntax-code))
860 ((= syntax-code context-coloring-STRING-QUOTE-CODE)
861 (context-coloring-elisp-colorize-string))
862 ((= syntax-code context-coloring-COMMENT-START-CODE)
863 (context-coloring-elisp-colorize-comment))
864 ((= syntax-code context-coloring-ESCAPE-CODE)
869 (defun context-coloring-elisp-colorize-region (start end)
872 (while (> end (progn (skip-syntax-forward "^()w_'<\"\\" end)
874 (setq syntax-code (context-coloring-get-syntax-code))
876 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
877 (context-coloring-elisp-colorize-parenthesized-sexp))
878 ((or (= syntax-code context-coloring-WORD-CODE)
879 (= syntax-code context-coloring-SYMBOL-CODE))
880 (context-coloring-elisp-colorize-symbol))
881 ((= syntax-code context-coloring-EXPRESSION-PREFIX-CODE)
882 (context-coloring-elisp-colorize-expression-prefix))
883 ((= syntax-code context-coloring-STRING-QUOTE-CODE)
884 (context-coloring-elisp-colorize-string))
885 ((= syntax-code context-coloring-COMMENT-START-CODE)
886 (context-coloring-elisp-colorize-comment))
887 ((= syntax-code context-coloring-ESCAPE-CODE)
892 (defun context-coloring-elisp-colorize-region-initially (start end)
893 (setq context-coloring-elisp-sexp-count 0)
894 (setq context-coloring-elisp-scope-stack '())
895 (let ((inhibit-point-motion-hooks t)
896 (case-fold-search nil)
897 ;; This is a recursive-descent parser, so give it a big stack.
898 (max-lisp-eval-depth (max max-lisp-eval-depth 3000))
899 (max-specpdl-size (max max-specpdl-size 3000)))
900 (context-coloring-elisp-colorize-region start end)))
902 (defun context-coloring-elisp-colorize ()
903 "Color the current buffer, parsing elisp to determine its
904 scopes and variables."
906 (with-silent-modifications
909 ;; Just colorize the changed region.
910 (context-coloring-changed-p
911 (let ((start (progn (goto-char context-coloring-changed-start)
914 (end (progn (goto-char context-coloring-changed-end)
917 (context-coloring-elisp-colorize-region-initially start end)))
919 (context-coloring-elisp-colorize-region-initially (point-min) (point-max)))))))
922 ;;; Shell command scopification / colorization
924 (defun context-coloring-apply-tokens (tokens)
925 "Process a string of TOKENS to apply context-based coloring to
926 the current buffer. Tokens are 3 integers: start, end, level. A
927 new token occurrs after every 3rd element, and the elements are
928 separated by commas."
929 (let* ((tokens (mapcar #'string-to-number (split-string tokens ","))))
931 (context-coloring-colorize-region
935 (context-coloring-maybe-colorize-comments-and-strings))
937 (defun context-coloring-parse-array (array)
938 "Parse ARRAY as a flat JSON array of numbers and use the tokens
939 to colorize the buffer."
940 (let* ((braceless (substring-no-properties (context-coloring-trim array) 1 -1)))
941 (when (> (length braceless) 0)
942 (with-silent-modifications
943 (context-coloring-apply-tokens braceless)))))
945 (defvar-local context-coloring-scopifier-cancel-function nil
946 "Kills the current scopification process.")
948 (defvar-local context-coloring-scopifier-process nil
949 "The single scopifier process that can be running.")
951 (defun context-coloring-cancel-scopification ()
952 "Stop the currently-running scopifier from scopifying."
953 (when context-coloring-scopifier-cancel-function
954 (funcall context-coloring-scopifier-cancel-function)
955 (setq context-coloring-scopifier-cancel-function nil))
956 (when (not (null context-coloring-scopifier-process))
957 (delete-process context-coloring-scopifier-process)
958 (setq context-coloring-scopifier-process nil)))
960 (defun context-coloring-shell-command (command callback)
961 "Invoke COMMAND, read its response asynchronously and invoke
962 CALLBACK with its output. Return the command process."
963 (let ((process (start-process-shell-command "context-coloring-process" nil command))
965 ;; The process may produce output in multiple chunks. This filter
966 ;; accumulates the chunks into a message.
969 (lambda (_process chunk)
970 (setq output (concat output chunk))))
971 ;; When the process's message is complete, this sentinel parses it as JSON
972 ;; and applies the tokens to the buffer.
973 (set-process-sentinel
975 (lambda (_process event)
976 (when (equal "finished\n" event)
977 (funcall callback output))))
980 (defun context-coloring-scopify-shell-command (command callback)
981 "Invoke a scopifier via COMMAND, read its response
982 asynchronously and invoke CALLBACK with its output."
983 ;; Prior running tokenization is implicitly obsolete if this function is
985 (context-coloring-cancel-scopification)
986 ;; Start the process.
987 (setq context-coloring-scopifier-process
988 (context-coloring-shell-command command callback)))
990 (defun context-coloring-send-buffer-to-scopifier ()
991 "Give the scopifier process its input so it can begin
994 context-coloring-scopifier-process
995 (point-min) (point-max))
997 context-coloring-scopifier-process))
999 (defun context-coloring-start-scopifier-server (command host port callback)
1002 (let ((stream (open-network-stream "context-coloring-stream" nil host port)))
1003 (funcall callback stream)))))
1004 ;; Try to connect in case a server is running, otherwise start one.
1009 (let ((server (start-process-shell-command
1010 "context-coloring-scopifier-server" nil
1011 (context-coloring-join
1015 "--port" (number-to-string port))
1018 ;; Connect as soon as the "listening" message is printed.
1021 (lambda (_process chunk)
1022 (setq output (concat output chunk))
1023 (when (string-match-p (format "^Scopifier listening at %s:%s$" host port) output)
1024 (funcall connect)))))))))
1026 (defun context-coloring-send-buffer-to-scopifier-server (command host port callback)
1027 (context-coloring-start-scopifier-server
1030 (let* ((body (buffer-substring-no-properties (point-min) (point-max)))
1031 (header (concat "POST / HTTP/1.0\r\n"
1032 "Host: localhost\r\n"
1033 "Content-Type: application/x-www-form-urlencoded"
1034 "; charset=UTF8\r\n"
1035 (format "Content-Length: %d\r\n" (length body))
1041 (lambda (_process chunk)
1042 (setq output (concat output chunk))))
1043 (set-process-sentinel
1045 (lambda (_process event)
1046 (when (and (equal "connection broken by remote peer\n" event)
1048 ;; Strip the response headers.
1049 (string-match "\r\n\r\n" output)
1050 (setq output (substring-no-properties output (match-end 0)))
1051 (funcall callback output))))
1052 (process-send-string process (concat header body "\r\n"))
1053 (setq context-coloring-scopifier-cancel-function
1055 "Cancel this scopification."
1056 (setq active nil)))))))
1058 (defun context-coloring-scopify-and-colorize-server (command host port &optional callback)
1059 "Contact or start a scopifier server via COMMAND at HOST and
1060 PORT with the current buffer's contents, read the scopifier's
1061 response asynchronously and apply a parsed list of tokens to
1062 `context-coloring-apply-tokens'.
1064 Invoke CALLBACK when complete."
1065 (let ((buffer (current-buffer)))
1066 (context-coloring-send-buffer-to-scopifier-server
1069 (with-current-buffer buffer
1070 (context-coloring-parse-array output))
1071 (when callback (funcall callback))))))
1073 (defun context-coloring-scopify-and-colorize (command &optional callback)
1074 "Invoke a scopifier via COMMAND with the current buffer's contents,
1075 read the scopifier's response asynchronously and apply a parsed
1076 list of tokens to `context-coloring-apply-tokens'.
1078 Invoke CALLBACK when complete."
1079 (let ((buffer (current-buffer)))
1080 (context-coloring-scopify-shell-command
1083 (with-current-buffer buffer
1084 (context-coloring-parse-array output))
1085 (setq context-coloring-scopifier-process nil)
1086 (when callback (funcall callback)))))
1087 (context-coloring-send-buffer-to-scopifier))
1092 (defvar context-coloring-dispatch-hash-table (make-hash-table :test #'eq)
1093 "Map dispatch strategy names to their corresponding property
1094 lists, which contain details about the strategies.")
1096 (defvar context-coloring-mode-hash-table (make-hash-table :test #'eq)
1097 "Map major mode names to dispatch property lists.")
1099 (defun context-coloring-get-dispatch-for-mode (mode)
1100 "Return the dispatch for MODE (or a derivative mode)."
1104 (not (setq dispatch (gethash parent context-coloring-mode-hash-table)))
1105 (setq parent (get parent 'derived-mode-parent))))
1108 (defun context-coloring-define-dispatch (symbol &rest properties)
1109 "Define a new dispatch named SYMBOL with PROPERTIES.
1111 A \"dispatch\" is a property list describing a strategy for
1112 coloring a buffer. There are three possible strategies: Parse
1113 and color in a single function (`:colorizer'), parse with a shell
1114 command that returns scope data (`:command'), or parse with a
1115 server that returns scope data (`:command', `:host' and `:port').
1116 In the latter two cases, the scope data will be used to
1117 automatically color the buffer.
1119 PROPERTIES must include `:modes' and one of `:colorizer',
1120 `:scopifier' or `:command'.
1122 `:modes' - List of major modes this dispatch is valid for.
1124 `:colorizer' - Symbol referring to a function that parses and
1127 `:executable' - Optional name of an executable required by
1130 `:command' - Shell command to execute with the current buffer
1131 sent via stdin, and with a flat JSON array of start, end and
1132 level data returned via stdout.
1134 `:host' - Hostname of the scopifier server, e.g. \"localhost\".
1136 `:port' - Port number of the scopifier server, e.g. 80, 1337.
1138 `:delay' - Delay between buffer update and colorization, to
1139 override `context-coloring-default-delay'.
1141 `:version' - Minimum required version that should be printed when
1142 executing `:command' with a \"--version\" flag. The version
1143 should be numeric, e.g. \"2\", \"19700101\", \"1.2.3\",
1146 `:setup' - Arbitrary code to set up this dispatch when
1147 `context-coloring-mode' is enabled.
1149 `:teardown' - Arbitrary code to tear down this dispatch when
1150 `context-coloring-mode' is disabled."
1151 (let ((modes (plist-get properties :modes))
1152 (colorizer (plist-get properties :colorizer))
1153 (command (plist-get properties :command)))
1155 (error "No mode defined for dispatch"))
1156 (when (not (or colorizer
1158 (error "No colorizer or command defined for dispatch"))
1159 (puthash symbol properties context-coloring-dispatch-hash-table)
1160 (dolist (mode modes)
1161 (puthash mode properties context-coloring-mode-hash-table))))
1166 (defvar context-coloring-colorize-hook nil
1167 "Hooks to run after coloring a buffer.")
1169 (defun context-coloring-colorize (&optional callback)
1170 "Color the current buffer by function context.
1172 Invoke CALLBACK when complete; see `context-coloring-dispatch'."
1174 (context-coloring-dispatch
1176 (when callback (funcall callback))
1177 (run-hooks 'context-coloring-colorize-hook))))
1182 (defun context-coloring-parse-version (string)
1183 "Extract segments of a version STRING into a list. \"v1.0.0\"
1184 produces (1 0 0), \"19700101\" produces (19700101), etc."
1186 (while (string-match "[0-9]+" string)
1187 (setq version (append version
1188 (list (string-to-number (match-string 0 string)))))
1189 (setq string (substring string (match-end 0))))
1192 (defun context-coloring-check-version (expected actual)
1193 "Check that version EXPECTED is less than or equal to ACTUAL."
1194 (let ((expected (context-coloring-parse-version expected))
1195 (actual (context-coloring-parse-version actual))
1198 (while (and continue expected)
1199 (let ((an-expected (car expected))
1200 (an-actual (car actual)))
1202 ((> an-actual an-expected)
1204 (setq continue nil))
1205 ((< an-actual an-expected)
1206 (setq acceptable nil)
1207 (setq continue nil))))
1208 (setq expected (cdr expected))
1209 (setq actual (cdr actual)))
1212 (defvar context-coloring-check-scopifier-version-hook nil
1213 "Hooks to run after checking the scopifier version.")
1215 (defun context-coloring-check-scopifier-version (&optional callback)
1216 "Asynchronously invoke CALLBACK with a predicate indicating
1217 whether the current scopifier version satisfies the minimum
1218 version number required for the current major mode."
1219 (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
1221 (let ((version (plist-get dispatch :version))
1222 (command (plist-get dispatch :command)))
1223 (context-coloring-shell-command
1224 (context-coloring-join (list command "--version") " ")
1226 (if (context-coloring-check-version version output)
1228 (when callback (funcall callback t)))
1229 (when callback (funcall callback nil)))
1230 (run-hooks 'context-coloring-check-scopifier-version-hook)))))))
1235 (defvar context-coloring-theme-hash-table (make-hash-table :test #'eq)
1236 "Map theme names to theme properties.")
1238 (defun context-coloring-theme-p (theme)
1239 "Return t if THEME is defined, nil otherwise."
1240 (and (gethash theme context-coloring-theme-hash-table)))
1242 (defconst context-coloring-level-face-regexp
1243 "context-coloring-level-\\([[:digit:]]+\\)-face"
1244 "Extract a level from a face.")
1246 (defvar context-coloring-originally-set-theme-hash-table
1247 (make-hash-table :test #'eq)
1248 "Cache custom themes who originally set their own
1249 `context-coloring-level-N-face' faces.")
1251 (defun context-coloring-theme-originally-set-p (theme)
1252 "Return t if there is a `context-coloring-level-N-face'
1253 originally set for THEME, nil otherwise."
1254 (let (originally-set)
1256 ;; `setq' might return a non-nil value for the sake of this `cond'.
1261 context-coloring-originally-set-theme-hash-table))
1262 (eq originally-set 'yes))
1264 (let* ((settings (get theme 'theme-settings))
1267 (while (and tail (not found))
1268 (and (eq (nth 0 (car tail)) 'theme-face)
1270 context-coloring-level-face-regexp
1271 (symbol-name (nth 1 (car tail))))
1273 (setq tail (cdr tail)))
1276 (defun context-coloring-cache-originally-set (theme originally-set)
1277 "Remember if THEME had colors originally set for it. If
1278 ORIGINALLY-SET is non-nil, it did, otherwise it didn't."
1279 ;; Caching whether a theme was originally set is kind of dirty, but we have to
1280 ;; do it to remember the past state of the theme. There are probably some
1281 ;; edge cases where caching will be an issue, but they are probably rare.
1284 (if originally-set 'yes 'no)
1285 context-coloring-originally-set-theme-hash-table))
1287 (defun context-coloring-warn-theme-originally-set (theme)
1288 "Warn the user that the colors for THEME are already originally
1290 (warn "Context coloring colors for theme `%s' are already defined" theme))
1292 (defun context-coloring-theme-highest-level (theme)
1293 "Return the highest level N of a face like
1294 `context-coloring-level-N-face' set for THEME, or `-1' if there
1296 (let* ((settings (get theme 'theme-settings))
1302 (and (eq (nth 0 (car tail)) 'theme-face)
1303 (setq face-string (symbol-name (nth 1 (car tail))))
1305 context-coloring-level-face-regexp
1307 (setq number (string-to-number
1308 (substring face-string
1312 (setq found number))
1313 (setq tail (cdr tail)))
1316 (defun context-coloring-apply-theme (theme)
1317 "Apply THEME's properties to its respective custom theme,
1318 which must already exist and which *should* already be enabled."
1319 (let* ((properties (gethash theme context-coloring-theme-hash-table))
1320 (colors (plist-get properties :colors))
1322 ;; Only clobber when we have to.
1323 (when (custom-theme-enabled-p theme)
1324 (setq context-coloring-maximum-face (- (length colors) 1)))
1326 #'custom-theme-set-faces
1330 (setq level (+ level 1))
1331 `(,(context-coloring-level-face level) ((t (:foreground ,color)))))
1334 (defun context-coloring-define-theme (theme &rest properties)
1335 "Define a context theme named THEME for coloring scope levels.
1337 PROPERTIES is a property list specifiying the following details:
1339 `:aliases': List of symbols of other custom themes that these
1340 colors are applicable to.
1342 `:colors': List of colors that this context theme uses.
1344 `:override': If non-nil, this context theme is intentionally
1345 overriding colors set by a custom theme. Don't set this non-nil
1346 unless there is a custom theme you want to use which sets
1347 `context-coloring-level-N-face' faces that you want to replace.
1349 `:recede': If non-nil, this context theme should not apply its
1350 colors if a custom theme already sets
1351 `context-coloring-level-N-face' faces. This option is
1352 optimistic; set this non-nil if you would rather confer the duty
1353 of picking colors to a custom theme author (if / when he ever
1356 By default, context themes will always override custom themes,
1357 even if those custom themes set `context-coloring-level-N-face'
1358 faces. If a context theme does override a custom theme, a
1359 warning will be raised, at which point you may want to enable the
1360 `:override' option, or just delete your context theme and opt to
1361 use your custom theme's author's colors instead.
1363 Context themes only work for the custom theme with the highest
1364 precedence, i.e. the car of `custom-enabled-themes'."
1365 (let ((aliases (plist-get properties :aliases))
1366 (override (plist-get properties :override))
1367 (recede (plist-get properties :recede)))
1368 (dolist (name (append `(,theme) aliases))
1369 (puthash name properties context-coloring-theme-hash-table)
1370 (when (custom-theme-p name)
1371 (let ((originally-set (context-coloring-theme-originally-set-p name)))
1372 (context-coloring-cache-originally-set name originally-set)
1373 ;; In the particular case when you innocently define colors that a
1374 ;; custom theme originally set, warn. Arguably this only has to be
1375 ;; done at enable time, but it is probably more useful to do it at
1376 ;; definition time for prompter feedback.
1377 (when (and originally-set
1380 (context-coloring-warn-theme-originally-set name))
1381 ;; Set (or overwrite) colors.
1382 (when (not (and originally-set
1384 (context-coloring-apply-theme name)))))))
1386 (defun context-coloring-enable-theme (theme)
1387 "Apply THEME if its colors are not already set, else just set
1388 `context-coloring-maximum-face' to the correct value for THEME."
1389 (let* ((properties (gethash theme context-coloring-theme-hash-table))
1390 (recede (plist-get properties :recede))
1391 (override (plist-get properties :override)))
1394 (let ((highest-level (context-coloring-theme-highest-level theme)))
1396 ;; This can be true whether originally set by a custom theme or by a
1398 ((> highest-level -1)
1399 (setq context-coloring-maximum-face highest-level))
1400 ;; It is possible that the corresponding custom theme did not exist at
1401 ;; the time of defining this context theme, and in that case the above
1402 ;; condition proves the custom theme did not originally set any faces,
1403 ;; so we have license to apply the context theme for the first time
1406 (context-coloring-apply-theme theme)))))
1408 (let ((originally-set (context-coloring-theme-originally-set-p theme)))
1409 ;; Cache now in case the context theme was defined after the custom
1411 (context-coloring-cache-originally-set theme originally-set)
1412 (when (and originally-set
1414 (context-coloring-warn-theme-originally-set theme))
1415 (context-coloring-apply-theme theme))))))
1417 (defadvice enable-theme (after context-coloring-enable-theme (theme) activate)
1418 "Enable colors for context themes just-in-time."
1419 (when (and (not (eq theme 'user)) ; Called internally by `enable-theme'.
1420 (custom-theme-p theme) ; Guard against non-existent themes.
1421 (context-coloring-theme-p theme))
1422 (when (= (length custom-enabled-themes) 1)
1423 ;; Cache because we can't reliably figure it out in reverse.
1424 (setq context-coloring-original-maximum-face
1425 context-coloring-maximum-face))
1426 (context-coloring-enable-theme theme)))
1428 (defadvice disable-theme (after context-coloring-disable-theme (theme) activate)
1429 "Update `context-coloring-maximum-face'."
1430 (when (custom-theme-p theme) ; Guard against non-existent themes.
1431 (let ((enabled-theme (car custom-enabled-themes)))
1432 (if (context-coloring-theme-p enabled-theme)
1434 (context-coloring-enable-theme enabled-theme))
1435 ;; Assume we are back to no theme; act as if nothing ever happened.
1436 ;; This is still prone to intervention, but rather extraordinarily.
1437 (setq context-coloring-maximum-face
1438 context-coloring-original-maximum-face)))))
1440 (context-coloring-define-theme
1452 (context-coloring-define-theme
1467 (context-coloring-define-theme
1479 (context-coloring-define-theme
1492 (context-coloring-define-theme
1505 (context-coloring-define-theme
1508 :aliases '(solarized-light
1510 sanityinc-solarized-light
1511 sanityinc-solarized-dark)
1530 (context-coloring-define-theme
1542 (context-coloring-define-theme
1559 (context-coloring-define-theme
1575 ;;; Built-in dispatches
1577 (context-coloring-define-dispatch
1579 :modes '(js-mode js3-mode)
1580 :executable "scopifier"
1581 :command "scopifier"
1586 (context-coloring-define-dispatch
1589 :colorizer #'context-coloring-js2-colorize
1592 (add-hook 'js2-post-parse-callbacks #'context-coloring-colorize nil t))
1595 (remove-hook 'js2-post-parse-callbacks #'context-coloring-colorize t)))
1597 (context-coloring-define-dispatch
1599 :modes '(emacs-lisp-mode)
1600 :colorizer #'context-coloring-elisp-colorize
1601 :delay 0.016 ;; Thanks to lazy colorization this can be 60 frames per second.
1602 :setup #'context-coloring-setup-idle-change-detection
1603 :teardown #'context-coloring-teardown-idle-change-detection)
1605 (defun context-coloring-dispatch (&optional callback)
1606 "Determine the optimal track for scopification / coloring of
1607 the current buffer, then execute it.
1609 Invoke CALLBACK when complete. It is invoked synchronously for
1610 elisp tracks, and asynchronously for shell command tracks."
1611 (let* ((dispatch (context-coloring-get-dispatch-for-mode major-mode))
1612 (colorizer (plist-get dispatch :colorizer))
1613 (command (plist-get dispatch :command))
1614 (host (plist-get dispatch :host))
1615 (port (plist-get dispatch :port))
1621 (funcall colorizer)))
1622 (when (and (not interrupted-p)
1624 (funcall callback)))
1628 (context-coloring-scopify-and-colorize-server command host port callback))
1630 (context-coloring-scopify-and-colorize command callback)))))))
1636 (define-minor-mode context-coloring-mode
1637 "Context-based code coloring, inspired by Douglas Crockford."
1639 (if (not context-coloring-mode)
1641 (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
1643 (let ((command (plist-get dispatch :command))
1644 (teardown (plist-get dispatch :teardown)))
1646 (context-coloring-teardown-idle-change-detection))
1648 (funcall teardown)))))
1652 ;; Font lock is incompatible with this mode; the converse is also true.
1656 ;; ...but we do use font-lock functions here.
1657 (font-lock-set-defaults)
1659 ;; Safely change the value of this function as necessary.
1660 (make-local-variable 'font-lock-syntactic-face-function)
1662 (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
1665 (let ((command (plist-get dispatch :command))
1666 (version (plist-get dispatch :version))
1667 (executable (plist-get dispatch :executable))
1668 (setup (plist-get dispatch :setup))
1669 (colorize-initially-p t))
1671 ;; Shell commands recolor on change, idly.
1674 (null (executable-find executable)))
1675 (message "Executable \"%s\" not found" executable)
1676 (setq colorize-initially-p nil))
1678 (context-coloring-check-scopifier-version
1679 (lambda (sufficient-p)
1682 (context-coloring-setup-idle-change-detection)
1683 (context-coloring-colorize))
1684 (message "Update to the minimum version of \"%s\" (%s)"
1685 executable version))))
1686 (setq colorize-initially-p nil))
1688 (context-coloring-setup-idle-change-detection))))
1691 ;; Colorize once initially.
1692 (when colorize-initially-p
1693 (let ((context-coloring-parse-interruptable-p nil))
1694 (context-coloring-colorize)))))
1695 (when (null dispatch)
1696 (message "Context coloring is not available for this major mode"))))))
1698 (provide 'context-coloring)
1700 ;;; context-coloring.el ends here