]> code.delx.au - gnu-emacs-elpa/blob - context-coloring.el
Version 6.5.0.
[gnu-emacs-elpa] / context-coloring.el
1 ;;; context-coloring.el --- Highlight by scope -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
4
5 ;; Author: Jackson Ray Hamilton <jackson@jacksonrayhamilton.com>
6 ;; Version: 6.5.0
7 ;; Keywords: convenience faces tools
8 ;; Package-Requires: ((emacs "24.3") (js2-mode "20150126"))
9 ;; URL: https://github.com/jacksonrayhamilton/context-coloring
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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/>.
25
26 ;;; Commentary:
27
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.
32
33 ;; By default, comments and strings are still highlighted syntactically.
34
35 ;;; Code:
36
37 (require 'js2-mode)
38
39
40 ;;; Utilities
41
42 (defun context-coloring-join (strings delimiter)
43 "Join a list of STRINGS with the string DELIMITER."
44 (mapconcat #'identity strings delimiter))
45
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)
50 string))
51
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)
56 string))
57
58 (defsubst context-coloring-trim (string)
59 "Remove leading and trailing whitespace from STRING."
60 (context-coloring-trim-left (context-coloring-trim-right string)))
61
62
63 ;;; Faces
64
65 (defun context-coloring-defface (level tty light dark)
66 "Define a face for LEVEL with colors for TTY, LIGHT and DARK
67 backgrounds."
68 (let ((face (intern (format "context-coloring-level-%s-face" level)))
69 (doc (format "Context coloring face, level %s." level)))
70 (custom-declare-face
71 face
72 `((((type tty)) (:foreground ,tty))
73 (((background light)) (:foreground ,light))
74 (((background dark)) (:foreground ,dark)))
75 doc
76 :group 'context-coloring)))
77
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"))
81
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)
90
91 (defvar context-coloring-maximum-face nil
92 "Index of the highest face available for coloring.")
93
94 (defvar context-coloring-original-maximum-face nil
95 "Fallback value for `context-coloring-maximum-face' when all
96 themes have been disabled.")
97
98 (setq context-coloring-maximum-face 7)
99
100 (setq context-coloring-original-maximum-face
101 context-coloring-maximum-face)
102
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.
105 (dotimes (number 18)
106 (context-coloring-defface-neutral (+ number context-coloring-maximum-face 1)))
107
108
109 ;;; Face functions
110
111 (defsubst context-coloring-level-face (level)
112 "Return the symbol for a face with LEVEL."
113 ;; `concat' is faster than `format' here.
114 (intern-soft
115 (concat "context-coloring-level-" (number-to-string level) "-face")))
116
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)))
121
122
123 ;;; Change detection
124
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-maybe-colorize-idle-timer' if that timer is
129 being used.")
130
131 (defvar-local context-coloring-changed-start nil
132 "Beginning of last text that changed.")
133
134 (defvar-local context-coloring-changed-end nil
135 "End of last text that changed.")
136
137 (defvar-local context-coloring-changed-length nil
138 "Length of last text that changed.")
139
140 (defun context-coloring-change-function (start end length)
141 "Register a change so that a buffer can be colorized soon.
142
143 START, END and LENGTH are recorded for later use."
144 ;; Tokenization is obsolete if there was a change.
145 (context-coloring-cancel-scopification)
146 (setq context-coloring-changed-start start)
147 (setq context-coloring-changed-end end)
148 (setq context-coloring-changed-length length)
149 (setq context-coloring-changed-p t))
150
151 (defun context-coloring-maybe-colorize-with-buffer (buffer)
152 "Color BUFFER and if it has changed."
153 (when (and (eq buffer (current-buffer))
154 context-coloring-changed-p)
155 (context-coloring-colorize-with-buffer buffer)
156 (setq context-coloring-changed-p nil)
157 (setq context-coloring-changed-start nil)
158 (setq context-coloring-changed-end nil)
159 (setq context-coloring-changed-length nil)))
160
161 (defvar-local context-coloring-maybe-colorize-idle-timer nil
162 "The currently-running idle timer for conditional coloring.")
163
164 (defvar-local context-coloring-colorize-idle-timer nil
165 "The currently-running idle timer for unconditional coloring.")
166
167 (defcustom context-coloring-default-delay 0.25
168 "Default (sometimes overridden) delay between a buffer update
169 and colorization.
170
171 Increase this if your machine is high-performing. Decrease it if
172 it ain't.
173
174 Supported modes: `js-mode', `js3-mode'"
175 :group 'context-coloring)
176
177 (make-obsolete-variable
178 'context-coloring-delay
179 'context-coloring-default-delay
180 "6.4.0")
181
182 (defun context-coloring-cancel-timer (timer)
183 "Cancel TIMER."
184 (when timer
185 (cancel-timer timer)))
186
187 (defun context-coloring-schedule-coloring (time)
188 "Schedule coloring to occur once after Emacs is idle for TIME."
189 (context-coloring-cancel-timer context-coloring-colorize-idle-timer)
190 (setq context-coloring-colorize-idle-timer
191 (run-with-idle-timer
192 time
193 nil
194 #'context-coloring-colorize-with-buffer
195 (current-buffer))))
196
197 (defun context-coloring-setup-idle-change-detection ()
198 "Setup idle change detection."
199 (let ((dispatch (context-coloring-get-current-dispatch)))
200 (add-hook
201 'after-change-functions #'context-coloring-change-function nil t)
202 (add-hook
203 'kill-buffer-hook #'context-coloring-teardown-idle-change-detection nil t)
204 (setq context-coloring-maybe-colorize-idle-timer
205 (run-with-idle-timer
206 (or (plist-get dispatch :delay) context-coloring-default-delay)
207 t
208 #'context-coloring-maybe-colorize-with-buffer
209 (current-buffer)))))
210
211 (defun context-coloring-teardown-idle-change-detection ()
212 "Teardown idle change detection."
213 (context-coloring-cancel-scopification)
214 (dolist (timer (list context-coloring-colorize-idle-timer
215 context-coloring-maybe-colorize-idle-timer))
216 (context-coloring-cancel-timer timer))
217 (remove-hook
218 'kill-buffer-hook #'context-coloring-teardown-idle-change-detection t)
219 (remove-hook
220 'after-change-functions #'context-coloring-change-function t))
221
222
223 ;;; Colorization utilities
224
225 (defsubst context-coloring-colorize-region (start end level)
226 "Color characters from the 1-indexed START point (inclusive) to
227 the END point (exclusive) with the face corresponding to LEVEL."
228 (add-text-properties
229 start
230 end
231 `(face ,(context-coloring-bounded-level-face level))))
232
233 (make-obsolete-variable
234 'context-coloring-comments-and-strings
235 "use `context-coloring-syntactic-comments' and
236 `context-coloring-syntactic-strings' instead."
237 "6.1.0")
238
239 (defcustom context-coloring-syntactic-comments t
240 "If non-nil, also color comments using `font-lock'."
241 :group 'context-coloring)
242
243 (defcustom context-coloring-syntactic-strings t
244 "If non-nil, also color strings using `font-lock'."
245 :group 'context-coloring)
246
247 (defun context-coloring-font-lock-syntactic-comment-function (state)
248 "Tell `font-lock' to color a comment but not a string according
249 to STATE."
250 (if (nth 3 state) nil font-lock-comment-face))
251
252 (defun context-coloring-font-lock-syntactic-string-function (state)
253 "Tell `font-lock' to color a string but not a comment according
254 to STATE."
255 (if (nth 3 state) font-lock-string-face nil))
256
257 (defsubst context-coloring-colorize-comments-and-strings (&optional min max)
258 "Color the current buffer's comments or strings if
259 `context-coloring-syntactic-comments' or
260 `context-coloring-syntactic-strings' are non-nil. MIN defaults
261 to the beginning of the buffer and MAX defaults to the end."
262 (when (or context-coloring-syntactic-comments
263 context-coloring-syntactic-strings)
264 (let ((min (or min (point-min)))
265 (max (or max (point-max)))
266 (font-lock-syntactic-face-function
267 (cond
268 ((and context-coloring-syntactic-comments
269 (not context-coloring-syntactic-strings))
270 #'context-coloring-font-lock-syntactic-comment-function)
271 ((and context-coloring-syntactic-strings
272 (not context-coloring-syntactic-comments))
273 #'context-coloring-font-lock-syntactic-string-function)
274 (t
275 font-lock-syntactic-face-function))))
276 (save-excursion
277 (font-lock-fontify-syntactically-region min max)
278 ;; TODO: Make configurable at the dispatch level.
279 (when (eq major-mode 'emacs-lisp-mode)
280 (font-lock-fontify-keywords-region min max))))))
281
282
283 ;;; js2-mode colorization
284
285 (defvar-local context-coloring-js2-scope-level-hash-table nil
286 "Associate `js2-scope' structures and with their scope
287 levels.")
288
289 (defcustom context-coloring-js-block-scopes nil
290 "If non-nil, also color block scopes in the scope hierarchy in JavaScript.
291
292 The block-scoped `let' and `const' are introduced in ES6. Enable
293 this for ES6 code; disable it elsewhere.
294
295 Supported modes: `js2-mode'"
296 :group 'context-coloring)
297
298 (defsubst context-coloring-js2-scope-level (scope)
299 "Return the level of SCOPE."
300 (cond ((gethash scope context-coloring-js2-scope-level-hash-table))
301 (t
302 (let ((level 0)
303 (current-scope scope)
304 enclosing-scope)
305 (while (and current-scope
306 (js2-node-parent current-scope)
307 (setq enclosing-scope
308 (js2-node-get-enclosing-scope current-scope)))
309 (when (or context-coloring-js-block-scopes
310 (let ((type (js2-scope-type current-scope)))
311 (or (= type js2-SCRIPT)
312 (= type js2-FUNCTION)
313 (= type js2-CATCH))))
314 (setq level (+ level 1)))
315 (setq current-scope enclosing-scope))
316 (puthash scope level context-coloring-js2-scope-level-hash-table)))))
317
318 (defsubst context-coloring-js2-local-name-node-p (node)
319 "Determine if NODE is a `js2-name-node' representing a local
320 variable."
321 (and (js2-name-node-p node)
322 (let ((parent (js2-node-parent node)))
323 (not (or (and (js2-object-prop-node-p parent)
324 (eq node (js2-object-prop-node-left parent)))
325 (and (js2-prop-get-node-p parent)
326 ;; For nested property lookup, the node on the left is a
327 ;; `js2-prop-get-node', so this always works.
328 (eq node (js2-prop-get-node-right parent))))))))
329
330 (defvar-local context-coloring-point-max nil
331 "Cached value of `point-max'.")
332
333 (defsubst context-coloring-js2-colorize-node (node level)
334 "Color NODE with the color for LEVEL."
335 (let ((start (js2-node-abs-pos node)))
336 (context-coloring-colorize-region
337 start
338 (min
339 ;; End
340 (+ start (js2-node-len node))
341 ;; Somes nodes (like the ast when there is an unterminated multiline
342 ;; comment) will stretch to the value of `point-max'.
343 context-coloring-point-max)
344 level)))
345
346 (defun context-coloring-js2-colorize ()
347 "Color the current buffer using the abstract syntax tree
348 generated by `js2-mode'."
349 ;; Reset the hash table; the old one could be obsolete.
350 (setq context-coloring-js2-scope-level-hash-table (make-hash-table :test #'eq))
351 (setq context-coloring-point-max (point-max))
352 (with-silent-modifications
353 (js2-visit-ast
354 js2-mode-ast
355 (lambda (node end-p)
356 (when (null end-p)
357 (cond
358 ((js2-scope-p node)
359 (context-coloring-js2-colorize-node
360 node
361 (context-coloring-js2-scope-level node)))
362 ((context-coloring-js2-local-name-node-p node)
363 (let* ((enclosing-scope (js2-node-get-enclosing-scope node))
364 (defining-scope (js2-get-defining-scope
365 enclosing-scope
366 (js2-name-node-name node))))
367 ;; The tree seems to be walked lexically, so an entire scope will
368 ;; be colored, including its name nodes, before they are reached.
369 ;; Coloring the nodes defined in that scope would be redundant, so
370 ;; don't do it.
371 (when (not (eq defining-scope enclosing-scope))
372 (context-coloring-js2-colorize-node
373 node
374 (context-coloring-js2-scope-level defining-scope))))))
375 ;; The `t' indicates to search children.
376 t)))
377 (context-coloring-colorize-comments-and-strings)))
378
379
380 ;;; Emacs Lisp colorization
381
382 (defsubst context-coloring-forward-sws ()
383 "Move forward through whitespace and comments."
384 (while (forward-comment 1)))
385
386 (defsubst context-coloring-elisp-forward-sws ()
387 "Move forward through whitespace and comments, colorizing
388 comments along the way."
389 (let ((start (point)))
390 (context-coloring-forward-sws)
391 (context-coloring-colorize-comments-and-strings start (point))))
392
393 (defsubst context-coloring-elisp-forward-sexp ()
394 "Like `forward-sexp', but colorize comments and strings along
395 the way."
396 (let ((start (point)))
397 (forward-sexp)
398 (context-coloring-elisp-colorize-comments-and-strings-in-region
399 start (point))))
400
401 (defsubst context-coloring-get-syntax-code ()
402 "Get the syntax code at point."
403 (syntax-class
404 ;; Faster version of `syntax-after':
405 (aref (syntax-table) (char-after (point)))))
406
407 (defsubst context-coloring-exact-regexp (word)
408 "Create a regexp matching exactly WORD."
409 (concat "\\`" (regexp-quote word) "\\'"))
410
411 (defsubst context-coloring-exact-or-regexp (words)
412 "Create a regexp matching any exact word in WORDS."
413 (context-coloring-join
414 (mapcar #'context-coloring-exact-regexp words) "\\|"))
415
416 (defconst context-coloring-elisp-ignored-word-regexp
417 (context-coloring-join (list "\\`[-+]?[0-9]"
418 "\\`[&:].+"
419 (context-coloring-exact-or-regexp
420 '("t" "nil" "." "?")))
421 "\\|")
422 "Match words that might be considered symbols but can't be
423 bound as variables.")
424
425 (defconst context-coloring-WORD-CODE 2)
426 (defconst context-coloring-SYMBOL-CODE 3)
427 (defconst context-coloring-OPEN-PARENTHESIS-CODE 4)
428 (defconst context-coloring-CLOSE-PARENTHESIS-CODE 5)
429 (defconst context-coloring-EXPRESSION-PREFIX-CODE 6)
430 (defconst context-coloring-STRING-QUOTE-CODE 7)
431 (defconst context-coloring-ESCAPE-CODE 9)
432 (defconst context-coloring-COMMENT-START-CODE 11)
433 (defconst context-coloring-COMMENT-END-CODE 12)
434
435 (defconst context-coloring-OCTOTHORPE-CHAR (string-to-char "#"))
436 (defconst context-coloring-APOSTROPHE-CHAR (string-to-char "'"))
437 (defconst context-coloring-OPEN-PARENTHESIS-CHAR (string-to-char "("))
438 (defconst context-coloring-COMMA-CHAR (string-to-char ","))
439 (defconst context-coloring-AT-CHAR (string-to-char "@"))
440 (defconst context-coloring-BACKTICK-CHAR (string-to-char "`"))
441
442 (defsubst context-coloring-elisp-identifier-p (syntax-code)
443 "Check if SYNTAX-CODE is an elisp identifier constituent."
444 (or (= syntax-code context-coloring-WORD-CODE)
445 (= syntax-code context-coloring-SYMBOL-CODE)))
446
447 (defvar context-coloring-parse-interruptable-p t
448 "Set this to nil to force parse to continue until finished.")
449
450 (defconst context-coloring-elisp-sexps-per-pause 350
451 "Pause after this many iterations to check for user input.
452 If user input is pending, stop the parse. This makes for a
453 smoother user experience for large files.
454
455 This number should trigger pausing at about 60 frames per
456 second.")
457
458 (defvar context-coloring-elisp-sexp-count 0
459 "Current number of sexps leading up to the next pause.")
460
461 (defsubst context-coloring-elisp-increment-sexp-count ()
462 "Maybe check if the current parse should be interrupted as a
463 result of pending user input."
464 (setq context-coloring-elisp-sexp-count
465 (1+ context-coloring-elisp-sexp-count))
466 (when (and (zerop (% context-coloring-elisp-sexp-count
467 context-coloring-elisp-sexps-per-pause))
468 context-coloring-parse-interruptable-p
469 (input-pending-p))
470 (throw 'interrupted t)))
471
472 (defvar context-coloring-elisp-scope-stack '()
473 "List of scopes in the current parse.")
474
475 (defsubst context-coloring-elisp-make-scope (level)
476 "Make a scope object for LEVEL."
477 (list
478 :level level
479 :variables '()))
480
481 (defsubst context-coloring-elisp-scope-get-level (scope)
482 "Get the level of SCOPE object."
483 (plist-get scope :level))
484
485 (defsubst context-coloring-elisp-scope-add-variable (scope variable)
486 "Add to SCOPE a VARIABLE."
487 (plist-put scope :variables (cons variable (plist-get scope :variables))))
488
489 (defsubst context-coloring-elisp-scope-has-variable (scope variable)
490 "Check if SCOPE has VARIABLE."
491 (member variable (plist-get scope :variables)))
492
493 (defsubst context-coloring-elisp-get-variable-level (variable)
494 "Search up the scope chain for the first instance of VARIABLE
495 and return its level, or 0 (global) if it isn't found."
496 (let* ((scope-stack context-coloring-elisp-scope-stack)
497 scope
498 level)
499 (while (and scope-stack (not level))
500 (setq scope (car scope-stack))
501 (cond
502 ((context-coloring-elisp-scope-has-variable scope variable)
503 (setq level (context-coloring-elisp-scope-get-level scope)))
504 (t
505 (setq scope-stack (cdr scope-stack)))))
506 ;; Assume a global variable.
507 (or level 0)))
508
509 (defsubst context-coloring-elisp-get-current-scope-level ()
510 "Get the nesting level of the current scope."
511 (cond
512 ((car context-coloring-elisp-scope-stack)
513 (context-coloring-elisp-scope-get-level (car context-coloring-elisp-scope-stack)))
514 (t
515 0)))
516
517 (defsubst context-coloring-elisp-push-scope ()
518 "Add a new scope to the bottom of the scope chain."
519 (push (context-coloring-elisp-make-scope
520 (1+ (context-coloring-elisp-get-current-scope-level)))
521 context-coloring-elisp-scope-stack))
522
523 (defsubst context-coloring-elisp-pop-scope ()
524 "Remove the scope on the bottom of the scope chain."
525 (pop context-coloring-elisp-scope-stack))
526
527 (defsubst context-coloring-elisp-add-variable (variable)
528 "Add VARIABLE to the current scope."
529 (context-coloring-elisp-scope-add-variable
530 (car context-coloring-elisp-scope-stack)
531 variable))
532
533 (defsubst context-coloring-elisp-parse-bindable (callback)
534 "Parse the symbol at point, and if the symbol can be bound,
535 invoke CALLBACK with it."
536 (let* ((arg-string (buffer-substring-no-properties
537 (point)
538 (progn (context-coloring-elisp-forward-sexp)
539 (point)))))
540 (when (not (string-match-p
541 context-coloring-elisp-ignored-word-regexp
542 arg-string))
543 (funcall callback arg-string))))
544
545 (defun context-coloring-elisp-parse-let-varlist (type)
546 "Parse the list of variable initializers at point. If TYPE is
547 `let', all the variables are bound after all their initializers
548 are parsed; if TYPE is `let*', each variable is bound immediately
549 after its own initializer is parsed."
550 (let ((varlist '())
551 syntax-code)
552 ;; Enter.
553 (forward-char)
554 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
555 context-coloring-CLOSE-PARENTHESIS-CODE)
556 (cond
557 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
558 (forward-char)
559 (context-coloring-elisp-forward-sws)
560 (setq syntax-code (context-coloring-get-syntax-code))
561 (when (context-coloring-elisp-identifier-p syntax-code)
562 (context-coloring-elisp-parse-bindable
563 (lambda (var)
564 (push var varlist)))
565 (context-coloring-elisp-forward-sws)
566 (setq syntax-code (context-coloring-get-syntax-code))
567 (when (/= syntax-code context-coloring-CLOSE-PARENTHESIS-CODE)
568 (context-coloring-elisp-colorize-sexp)))
569 (context-coloring-elisp-forward-sws)
570 ;; Skip past the closing parenthesis.
571 (forward-char))
572 ((context-coloring-elisp-identifier-p syntax-code)
573 (context-coloring-elisp-parse-bindable
574 (lambda (var)
575 (push var varlist))))
576 (t
577 ;; Ignore artifacts.
578 (context-coloring-elisp-forward-sexp)))
579 (when (eq type 'let*)
580 (context-coloring-elisp-add-variable (pop varlist)))
581 (context-coloring-elisp-forward-sws))
582 (when (eq type 'let)
583 (while varlist
584 (context-coloring-elisp-add-variable (pop varlist))))
585 ;; Exit.
586 (forward-char)))
587
588 (defun context-coloring-elisp-parse-arglist ()
589 "Parse the list of function arguments at point."
590 (let (syntax-code)
591 ;; Enter.
592 (forward-char)
593 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
594 context-coloring-CLOSE-PARENTHESIS-CODE)
595 (cond
596 ((context-coloring-elisp-identifier-p syntax-code)
597 (context-coloring-elisp-parse-bindable
598 (lambda (arg)
599 (context-coloring-elisp-add-variable arg))))
600 (t
601 ;; Ignore artifacts.
602 (context-coloring-elisp-forward-sexp)))
603 (context-coloring-elisp-forward-sws))
604 ;; Exit.
605 (forward-char)))
606
607 (defun context-coloring-elisp-skip-callee-name ()
608 "Skip past the opening parenthesis and name of a function."
609 ;; Enter.
610 (forward-char)
611 (context-coloring-elisp-forward-sws)
612 ;; Skip past the function name.
613 (forward-sexp)
614 (context-coloring-elisp-forward-sws))
615
616 (defun context-coloring-elisp-colorize-scope (callback)
617 "Color the whole scope at point with its one color. Handle a
618 header in CALLBACK."
619 (let ((start (point))
620 (end (progn (forward-sexp)
621 (point))))
622 (context-coloring-elisp-push-scope)
623 ;; Splash the whole thing in one color.
624 (context-coloring-colorize-region
625 start
626 end
627 (context-coloring-elisp-get-current-scope-level))
628 ;; Even if the parse is interrupted, this region should still be colored
629 ;; syntactically.
630 (context-coloring-elisp-colorize-comments-and-strings-in-region
631 start
632 end)
633 (goto-char start)
634 (context-coloring-elisp-skip-callee-name)
635 (funcall callback)
636 (context-coloring-elisp-colorize-region (point) (1- end))
637 ;; Exit.
638 (forward-char)
639 (context-coloring-elisp-pop-scope)))
640
641 (defun context-coloring-elisp-parse-header (callback)
642 "Parse a function header at point with CALLBACK."
643 (when (= (context-coloring-get-syntax-code) context-coloring-OPEN-PARENTHESIS-CODE)
644 (funcall callback)))
645
646 (defun context-coloring-elisp-colorize-defun-like (callback)
647 "Color the defun-like function at point, parsing the header
648 with CALLBACK."
649 (context-coloring-elisp-colorize-scope
650 (lambda ()
651 (when (context-coloring-elisp-identifier-p (context-coloring-get-syntax-code))
652 ;; Color the defun's name with the top-level color.
653 (context-coloring-colorize-region
654 (point)
655 (progn (forward-sexp)
656 (point))
657 0)
658 (context-coloring-elisp-forward-sws)
659 (context-coloring-elisp-parse-header callback)))))
660
661 (defun context-coloring-elisp-colorize-defun ()
662 "Color the `defun' at point."
663 (context-coloring-elisp-colorize-defun-like
664 'context-coloring-elisp-parse-arglist))
665
666 (defun context-coloring-elisp-colorize-defadvice ()
667 "Color the `defadvice' at point."
668 (context-coloring-elisp-colorize-defun-like
669 (lambda ()
670 (let (syntax-code)
671 ;; Enter.
672 (forward-char)
673 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
674 context-coloring-CLOSE-PARENTHESIS-CODE)
675 (cond
676 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
677 (context-coloring-elisp-parse-arglist))
678 (t
679 ;; Ignore artifacts.
680 (context-coloring-elisp-forward-sexp)))
681 (context-coloring-elisp-forward-sws))))))
682
683 (defun context-coloring-elisp-colorize-lambda-like (callback)
684 "Color the lambda-like function at point, parsing the header
685 with CALLBACK."
686 (context-coloring-elisp-colorize-scope
687 (lambda ()
688 (context-coloring-elisp-parse-header callback))))
689
690 (defun context-coloring-elisp-colorize-lambda ()
691 "Color the `lambda' at point."
692 (context-coloring-elisp-colorize-lambda-like
693 'context-coloring-elisp-parse-arglist))
694
695 (defun context-coloring-elisp-colorize-let ()
696 "Color the `let' at point."
697 (context-coloring-elisp-colorize-lambda-like
698 (lambda ()
699 (context-coloring-elisp-parse-let-varlist 'let))))
700
701 (defun context-coloring-elisp-colorize-let* ()
702 "Color the `let*' at point."
703 (context-coloring-elisp-colorize-lambda-like
704 (lambda ()
705 (context-coloring-elisp-parse-let-varlist 'let*))))
706
707 (defun context-coloring-elisp-colorize-cond ()
708 "Color the `cond' at point."
709 (let (syntax-code)
710 (context-coloring-elisp-skip-callee-name)
711 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
712 context-coloring-CLOSE-PARENTHESIS-CODE)
713 (cond
714 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
715 ;; Colorize inside the parens.
716 (let ((start (point)))
717 (forward-sexp)
718 (context-coloring-elisp-colorize-region
719 (1+ start) (1- (point)))
720 ;; Exit.
721 (forward-char)))
722 (t
723 ;; Ignore artifacts.
724 (context-coloring-elisp-forward-sexp)))
725 (context-coloring-elisp-forward-sws))
726 ;; Exit.
727 (forward-char)))
728
729 (defun context-coloring-elisp-colorize-condition-case ()
730 "Color the `condition-case' at point."
731 (let (syntax-code
732 variable
733 case-pos
734 case-end)
735 (context-coloring-elisp-colorize-scope
736 (lambda ()
737 (setq syntax-code (context-coloring-get-syntax-code))
738 ;; Gracefully ignore missing variables.
739 (when (context-coloring-elisp-identifier-p syntax-code)
740 (context-coloring-elisp-parse-bindable
741 (lambda (parsed-variable)
742 (setq variable parsed-variable)))
743 (context-coloring-elisp-forward-sws))
744 (context-coloring-elisp-colorize-sexp)
745 (context-coloring-elisp-forward-sws)
746 ;; Parse the handlers with the error variable in scope.
747 (when variable
748 (context-coloring-elisp-add-variable variable))
749 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
750 context-coloring-CLOSE-PARENTHESIS-CODE)
751 (cond
752 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
753 (setq case-pos (point))
754 (context-coloring-elisp-forward-sexp)
755 (setq case-end (point))
756 (goto-char case-pos)
757 ;; Enter.
758 (forward-char)
759 (context-coloring-elisp-forward-sws)
760 (setq syntax-code (context-coloring-get-syntax-code))
761 (when (/= syntax-code context-coloring-CLOSE-PARENTHESIS-CODE)
762 ;; Skip the condition name(s).
763 (context-coloring-elisp-forward-sexp)
764 ;; Color the remaining portion of the handler.
765 (context-coloring-elisp-colorize-region
766 (point)
767 (1- case-end)))
768 ;; Exit.
769 (forward-char))
770 (t
771 ;; Ignore artifacts.
772 (context-coloring-elisp-forward-sexp)))
773 (context-coloring-elisp-forward-sws))))))
774
775 (defun context-coloring-elisp-colorize-dolist ()
776 "Color the `dolist' at point."
777 (let (syntax-code
778 (index 0))
779 (context-coloring-elisp-colorize-scope
780 (lambda ()
781 (setq syntax-code (context-coloring-get-syntax-code))
782 (when (= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
783 (forward-char)
784 (context-coloring-elisp-forward-sws)
785 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
786 context-coloring-CLOSE-PARENTHESIS-CODE)
787 (cond
788 ((and
789 (or (= index 0) (= index 2))
790 (context-coloring-elisp-identifier-p syntax-code))
791 ;; Add the first or third name to the scope.
792 (context-coloring-elisp-parse-bindable
793 (lambda (variable)
794 (context-coloring-elisp-add-variable variable))))
795 (t
796 ;; Color artifacts.
797 (context-coloring-elisp-colorize-sexp)))
798 (context-coloring-elisp-forward-sws)
799 (setq index (1+ index)))
800 ;; Exit.
801 (forward-char))))))
802
803 (defun context-coloring-elisp-colorize-quote ()
804 "Color the `quote' at point."
805 (let* ((start (point))
806 (end (progn (forward-sexp)
807 (point))))
808 (context-coloring-colorize-region
809 start
810 end
811 (context-coloring-elisp-get-current-scope-level))
812 (context-coloring-elisp-colorize-comments-and-strings-in-region start end)))
813
814 (defvar context-coloring-elisp-callee-dispatch-hash-table
815 (let ((table (make-hash-table :test 'equal)))
816 (dolist (callee '("defun" "defun*" "defsubst" "defmacro" "cl-defun" "cl-defsubst" "cl-defmacro"))
817 (puthash callee #'context-coloring-elisp-colorize-defun table))
818 (dolist (callee '("condition-case" "condition-case-unless-debug"))
819 (puthash callee #'context-coloring-elisp-colorize-condition-case table))
820 (dolist (callee '("dolist" "dotimes"))
821 (puthash callee #'context-coloring-elisp-colorize-dolist table))
822 (puthash "let" #'context-coloring-elisp-colorize-let table)
823 (puthash "let*" #'context-coloring-elisp-colorize-let* table)
824 (puthash "lambda" #'context-coloring-elisp-colorize-lambda table)
825 (puthash "cond" #'context-coloring-elisp-colorize-cond table)
826 (puthash "defadvice" #'context-coloring-elisp-colorize-defadvice table)
827 (puthash "quote" #'context-coloring-elisp-colorize-quote table)
828 (puthash "backquote" #'context-coloring-elisp-colorize-backquote table)
829 table)
830 "Map function names to their coloring functions.")
831
832 (defun context-coloring-elisp-colorize-parenthesized-sexp ()
833 "Color the sexp enclosed by parenthesis at point."
834 (context-coloring-elisp-increment-sexp-count)
835 (let* ((start (point))
836 (end (progn (forward-sexp)
837 (point)))
838 (syntax-code (progn (goto-char start)
839 (forward-char)
840 ;; Coloring is unnecessary here, it'll happen
841 ;; presently.
842 (context-coloring-forward-sws)
843 (context-coloring-get-syntax-code)))
844 dispatch-function)
845 ;; Figure out if the sexp is a special form.
846 (cond
847 ((and (context-coloring-elisp-identifier-p syntax-code)
848 (setq dispatch-function (gethash
849 (buffer-substring-no-properties
850 (point)
851 (progn (forward-sexp)
852 (point)))
853 context-coloring-elisp-callee-dispatch-hash-table)))
854 (goto-char start)
855 (funcall dispatch-function))
856 ;; Not a special form; just colorize the remaining region.
857 (t
858 (context-coloring-colorize-region
859 start
860 end
861 (context-coloring-elisp-get-current-scope-level))
862 (context-coloring-elisp-colorize-region (point) (1- end))
863 (forward-char)))))
864
865 (defun context-coloring-elisp-colorize-symbol ()
866 "Color the symbol at point."
867 (context-coloring-elisp-increment-sexp-count)
868 (let* ((symbol-pos (point))
869 (symbol-end (progn (forward-sexp)
870 (point)))
871 (symbol-string (buffer-substring-no-properties
872 symbol-pos
873 symbol-end)))
874 (cond
875 ((string-match-p context-coloring-elisp-ignored-word-regexp symbol-string))
876 (t
877 (context-coloring-colorize-region
878 symbol-pos
879 symbol-end
880 (context-coloring-elisp-get-variable-level
881 symbol-string))))))
882
883 (defun context-coloring-elisp-colorize-backquote-form ()
884 "Color the backquote form at point."
885 (let ((start (point))
886 (end (progn (forward-sexp)
887 (point)))
888 char)
889 (goto-char start)
890 (while (> end (progn (forward-char)
891 (point)))
892 (setq char (char-after))
893 (when (= char context-coloring-COMMA-CHAR)
894 (forward-char)
895 (when (= (char-after) context-coloring-AT-CHAR)
896 ;; If we don't do this "@" could be interpreted as a symbol.
897 (forward-char))
898 (context-coloring-elisp-forward-sws)
899 (context-coloring-elisp-colorize-sexp)))
900 ;; We could probably do this as part of the above loop but it'd be
901 ;; repetitive.
902 (context-coloring-elisp-colorize-comments-and-strings-in-region
903 start end)))
904
905 (defun context-coloring-elisp-colorize-backquote ()
906 "Color the `backquote' at point."
907 (context-coloring-elisp-skip-callee-name)
908 (context-coloring-elisp-colorize-backquote-form)
909 ;; Exit.
910 (forward-char))
911
912 (defun context-coloring-elisp-colorize-expression-prefix ()
913 "Color the expression prefix and the following expression at
914 point. It could be a quoted or backquoted expression."
915 (context-coloring-elisp-increment-sexp-count)
916 (cond
917 ((/= (char-after) context-coloring-BACKTICK-CHAR)
918 (context-coloring-elisp-forward-sexp))
919 (t
920 (context-coloring-elisp-colorize-backquote-form))))
921
922 (defun context-coloring-elisp-colorize-comment ()
923 "Color the comment at point."
924 (context-coloring-elisp-increment-sexp-count)
925 (context-coloring-elisp-forward-sws))
926
927 (defun context-coloring-elisp-colorize-string ()
928 "Color the string at point."
929 (context-coloring-elisp-increment-sexp-count)
930 (let ((start (point)))
931 (forward-sexp)
932 (context-coloring-colorize-comments-and-strings start (point))))
933
934 ;; Elisp has whitespace, words, symbols, open/close parenthesis, expression
935 ;; prefix, string quote, comment starters/enders and escape syntax classes only.
936
937 (defun context-coloring-elisp-colorize-sexp ()
938 "Color the sexp at point."
939 (let ((syntax-code (context-coloring-get-syntax-code)))
940 (cond
941 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
942 (context-coloring-elisp-colorize-parenthesized-sexp))
943 ((context-coloring-elisp-identifier-p syntax-code)
944 (context-coloring-elisp-colorize-symbol))
945 ((= syntax-code context-coloring-EXPRESSION-PREFIX-CODE)
946 (context-coloring-elisp-colorize-expression-prefix))
947 ((= syntax-code context-coloring-STRING-QUOTE-CODE)
948 (context-coloring-elisp-colorize-string))
949 ((= syntax-code context-coloring-ESCAPE-CODE)
950 (forward-char 2)))))
951
952 (defun context-coloring-elisp-colorize-comments-and-strings-in-region (start end)
953 "Color comments and strings between START and END."
954 (let (syntax-code)
955 (goto-char start)
956 (while (> end (progn (skip-syntax-forward "^\"<\\" end)
957 (point)))
958 (setq syntax-code (context-coloring-get-syntax-code))
959 (cond
960 ((= syntax-code context-coloring-STRING-QUOTE-CODE)
961 (context-coloring-elisp-colorize-string))
962 ((= syntax-code context-coloring-COMMENT-START-CODE)
963 (context-coloring-elisp-colorize-comment))
964 ((= syntax-code context-coloring-ESCAPE-CODE)
965 (forward-char 2))))))
966
967 (defun context-coloring-elisp-colorize-region (start end)
968 "Color everything between START and END."
969 (let (syntax-code)
970 (goto-char start)
971 (while (> end (progn (skip-syntax-forward "^w_('\"<\\" end)
972 (point)))
973 (setq syntax-code (context-coloring-get-syntax-code))
974 (cond
975 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
976 (context-coloring-elisp-colorize-parenthesized-sexp))
977 ((context-coloring-elisp-identifier-p syntax-code)
978 (context-coloring-elisp-colorize-symbol))
979 ((= syntax-code context-coloring-EXPRESSION-PREFIX-CODE)
980 (context-coloring-elisp-colorize-expression-prefix))
981 ((= syntax-code context-coloring-STRING-QUOTE-CODE)
982 (context-coloring-elisp-colorize-string))
983 ((= syntax-code context-coloring-COMMENT-START-CODE)
984 (context-coloring-elisp-colorize-comment))
985 ((= syntax-code context-coloring-ESCAPE-CODE)
986 (forward-char 2))))))
987
988 (defun context-coloring-elisp-colorize-region-initially (start end)
989 "Begin coloring everything between START and END."
990 (setq context-coloring-elisp-sexp-count 0)
991 (setq context-coloring-elisp-scope-stack '())
992 (let ((inhibit-point-motion-hooks t)
993 (case-fold-search nil)
994 ;; This is a recursive-descent parser, so give it a big stack.
995 (max-lisp-eval-depth (max max-lisp-eval-depth 3000))
996 (max-specpdl-size (max max-specpdl-size 3000)))
997 (context-coloring-elisp-colorize-region start end)))
998
999 (defun context-coloring-elisp-colorize-guard (callback)
1000 "Silently color in CALLBACK."
1001 (with-silent-modifications
1002 (save-excursion
1003 (condition-case nil
1004 (funcall callback)
1005 ;; Scan errors can happen virtually anywhere if parenthesis are
1006 ;; unbalanced. Just swallow them. (`progn' for test coverage.)
1007 (scan-error (progn))))))
1008
1009 (defun context-coloring-elisp-colorize ()
1010 "Color the current buffer, parsing elisp to determine its
1011 scopes and variables."
1012 (interactive)
1013 (context-coloring-elisp-colorize-guard
1014 (lambda ()
1015 (cond
1016 ;; Just colorize the changed region.
1017 (context-coloring-changed-p
1018 (let* ( ;; Prevent `beginning-of-defun' from making poor assumptions.
1019 (open-paren-in-column-0-is-defun-start nil)
1020 ;; Seek the beginning and end of the previous and next
1021 ;; offscreen defuns, so just enough is colored.
1022 (start (progn (goto-char context-coloring-changed-start)
1023 (while (and (< (point-min) (point))
1024 (pos-visible-in-window-p))
1025 (end-of-line 0))
1026 (beginning-of-defun)
1027 (point)))
1028 (end (progn (goto-char context-coloring-changed-end)
1029 (while (and (> (point-max) (point))
1030 (pos-visible-in-window-p))
1031 (forward-line 1))
1032 (end-of-defun)
1033 (point))))
1034 (context-coloring-elisp-colorize-region-initially start end)
1035 ;; Fast coloring is nice, but if the code is not well-formed
1036 ;; (e.g. an unclosed string literal is parsed at any time) then
1037 ;; there could be leftover incorrectly-colored code offscreen. So
1038 ;; do a clean sweep as soon as appropriate.
1039 (context-coloring-schedule-coloring context-coloring-default-delay)))
1040 (t
1041 (context-coloring-elisp-colorize-region-initially (point-min) (point-max)))))))
1042
1043 (defun context-coloring-eval-expression-colorize ()
1044 "Color the `eval-expression' minibuffer prompt as elisp."
1045 (interactive)
1046 (context-coloring-elisp-colorize-guard
1047 (lambda ()
1048 (context-coloring-elisp-colorize-region-initially
1049 (progn
1050 (string-match "\\`Eval: " (buffer-string))
1051 (1+ (match-end 0)))
1052 (point-max)))))
1053
1054
1055 ;;; Shell command scopification / colorization
1056
1057 (defun context-coloring-apply-tokens (tokens)
1058 "Process a string of TOKENS to apply context-based coloring to
1059 the current buffer. Tokens are 3 integers: start, end, level. A
1060 new token occurrs after every 3rd element, and the elements are
1061 separated by commas."
1062 (let* ((tokens (mapcar #'string-to-number (split-string tokens ","))))
1063 (while tokens
1064 (context-coloring-colorize-region
1065 (pop tokens)
1066 (pop tokens)
1067 (pop tokens))))
1068 (context-coloring-colorize-comments-and-strings))
1069
1070 (defun context-coloring-parse-array (array)
1071 "Parse ARRAY as a flat JSON array of numbers and use the tokens
1072 to colorize the buffer."
1073 (let* ((braceless (substring-no-properties (context-coloring-trim array) 1 -1)))
1074 (when (> (length braceless) 0)
1075 (with-silent-modifications
1076 (context-coloring-apply-tokens braceless)))))
1077
1078 (defvar-local context-coloring-scopifier-cancel-function nil
1079 "Kills the current scopification process.")
1080
1081 (defvar-local context-coloring-scopifier-process nil
1082 "The single scopifier process that can be running.")
1083
1084 (defun context-coloring-cancel-scopification ()
1085 "Stop the currently-running scopifier from scopifying."
1086 (when context-coloring-scopifier-cancel-function
1087 (funcall context-coloring-scopifier-cancel-function)
1088 (setq context-coloring-scopifier-cancel-function nil))
1089 (when (not (null context-coloring-scopifier-process))
1090 (delete-process context-coloring-scopifier-process)
1091 (setq context-coloring-scopifier-process nil)))
1092
1093 (defun context-coloring-shell-command (command callback)
1094 "Invoke COMMAND, read its response asynchronously and invoke
1095 CALLBACK with its output. Return the command process."
1096 (let ((process (start-process-shell-command "context-coloring-process" nil command))
1097 (output ""))
1098 ;; The process may produce output in multiple chunks. This filter
1099 ;; accumulates the chunks into a message.
1100 (set-process-filter
1101 process
1102 (lambda (_process chunk)
1103 (setq output (concat output chunk))))
1104 ;; When the process's message is complete, this sentinel parses it as JSON
1105 ;; and applies the tokens to the buffer.
1106 (set-process-sentinel
1107 process
1108 (lambda (_process event)
1109 (when (equal "finished\n" event)
1110 (funcall callback output))))
1111 process))
1112
1113 (defun context-coloring-scopify-shell-command (command callback)
1114 "Invoke a scopifier via COMMAND, read its response
1115 asynchronously and invoke CALLBACK with its output."
1116 ;; Prior running tokenization is implicitly obsolete if this function is
1117 ;; called.
1118 (context-coloring-cancel-scopification)
1119 ;; Start the process.
1120 (setq context-coloring-scopifier-process
1121 (context-coloring-shell-command command callback)))
1122
1123 (defun context-coloring-send-buffer-to-scopifier ()
1124 "Give the scopifier process its input so it can begin
1125 scopifying."
1126 (process-send-region
1127 context-coloring-scopifier-process
1128 (point-min) (point-max))
1129 (process-send-eof
1130 context-coloring-scopifier-process))
1131
1132 (defun context-coloring-start-scopifier-server (command host port callback)
1133 "Connect to or start a scopifier server with COMMAND, HOST and PORT.
1134 Invoke CALLBACK with a network stream when the server is ready
1135 for connections."
1136 (let* ((connect
1137 (lambda ()
1138 (let ((stream (open-network-stream "context-coloring-stream" nil host port)))
1139 (funcall callback stream)))))
1140 ;; Try to connect in case a server is running, otherwise start one.
1141 (condition-case nil
1142 (progn
1143 (funcall connect))
1144 (error
1145 (let ((server (start-process-shell-command
1146 "context-coloring-scopifier-server" nil
1147 (context-coloring-join
1148 (list command
1149 "--server"
1150 "--host" host
1151 "--port" (number-to-string port))
1152 " ")))
1153 (output ""))
1154 ;; Connect as soon as the "listening" message is printed.
1155 (set-process-filter
1156 server
1157 (lambda (_process chunk)
1158 (setq output (concat output chunk))
1159 (when (string-match-p (format "^Scopifier listening at %s:%s$" host port) output)
1160 (funcall connect)))))))))
1161
1162 (defun context-coloring-send-buffer-to-scopifier-server (command host port callback)
1163 "Send the current buffer to the scopifier server running with
1164 COMMAND, HOST and PORT. Invoke CALLBACK with the server's
1165 response (a stringified JSON array)."
1166 (context-coloring-start-scopifier-server
1167 command host port
1168 (lambda (process)
1169 (let* ((body (buffer-substring-no-properties (point-min) (point-max)))
1170 (header (concat "POST / HTTP/1.0\r\n"
1171 "Host: localhost\r\n"
1172 "Content-Type: application/x-www-form-urlencoded"
1173 "; charset=UTF8\r\n"
1174 (format "Content-Length: %d\r\n" (length body))
1175 "\r\n"))
1176 (output "")
1177 (active t))
1178 (set-process-filter
1179 process
1180 (lambda (_process chunk)
1181 (setq output (concat output chunk))))
1182 (set-process-sentinel
1183 process
1184 (lambda (_process event)
1185 (when (and (equal "connection broken by remote peer\n" event)
1186 active)
1187 ;; Strip the response headers.
1188 (string-match "\r\n\r\n" output)
1189 (setq output (substring-no-properties output (match-end 0)))
1190 (funcall callback output))))
1191 (process-send-string process (concat header body "\r\n"))
1192 (setq context-coloring-scopifier-cancel-function
1193 (lambda ()
1194 "Cancel this scopification."
1195 (setq active nil)))))))
1196
1197 (defun context-coloring-scopify-and-colorize-server (command host port &optional callback)
1198 "Color the current buffer via the server started with COMMAND,
1199 HOST and PORT. Invoke CALLBACK when complete."
1200 (let ((buffer (current-buffer)))
1201 (context-coloring-send-buffer-to-scopifier-server
1202 command host port
1203 (lambda (output)
1204 (with-current-buffer buffer
1205 (context-coloring-parse-array output))
1206 (when callback (funcall callback))))))
1207
1208 (defun context-coloring-scopify-and-colorize (command &optional callback)
1209 "Color the current buffer via COMMAND. Invoke CALLBACK when
1210 complete."
1211 (let ((buffer (current-buffer)))
1212 (context-coloring-scopify-shell-command
1213 command
1214 (lambda (output)
1215 (with-current-buffer buffer
1216 (context-coloring-parse-array output))
1217 (setq context-coloring-scopifier-process nil)
1218 (when callback (funcall callback)))))
1219 (context-coloring-send-buffer-to-scopifier))
1220
1221
1222 ;;; Dispatch
1223
1224 (defvar context-coloring-dispatch-hash-table (make-hash-table :test #'eq)
1225 "Map dispatch strategy names to their corresponding property
1226 lists, which contain details about the strategies.")
1227
1228 (defvar context-coloring-mode-hash-table (make-hash-table :test #'eq)
1229 "Map major mode names to dispatch property lists.")
1230
1231 (defvar context-coloring-dispatch-predicates '()
1232 "Functions which may return a dispatch.")
1233
1234 (defun context-coloring-get-current-dispatch ()
1235 "Return the first dispatch appropriate for the current state."
1236 (let ((predicates context-coloring-dispatch-predicates)
1237 (parent major-mode)
1238 dispatch)
1239 ;; Maybe a predicate will be satisfied and return a dispatch.
1240 (while (and predicates
1241 (not (setq dispatch (funcall (pop predicates))))))
1242 ;; If not, maybe a major mode (or a derivative) will define a dispatch.
1243 (when (not dispatch)
1244 (while (and parent
1245 (not (setq dispatch (gethash parent context-coloring-mode-hash-table)))
1246 (setq parent (get parent 'derived-mode-parent)))))
1247 dispatch))
1248
1249 (defun context-coloring-define-dispatch (symbol &rest properties)
1250 "Define a new dispatch named SYMBOL with PROPERTIES.
1251
1252 A \"dispatch\" is a property list describing a strategy for
1253 coloring a buffer. There are three possible strategies: Parse
1254 and color in a single function (`:colorizer'), parse with a shell
1255 command that returns scope data (`:command'), or parse with a
1256 server that returns scope data (`:command', `:host' and `:port').
1257 In the latter two cases, the scope data will be used to
1258 automatically color the buffer.
1259
1260 PROPERTIES must include one of `:modes' or `:predicate', and one
1261 of `:colorizer' or `:command'.
1262
1263 `:modes' - List of major modes this dispatch is valid for.
1264
1265 `:predicate' - Function that determines if the dispatch is valid
1266 for any given state.
1267
1268 `:colorizer' - Function that parses and colors the buffer.
1269
1270 `:executable' - Optional name of an executable required by
1271 `:command'.
1272
1273 `:command' - Shell command to execute with the current buffer
1274 sent via stdin, and with a flat JSON array of start, end and
1275 level data returned via stdout.
1276
1277 `:host' - Hostname of the scopifier server, e.g. \"localhost\".
1278
1279 `:port' - Port number of the scopifier server, e.g. 80, 1337.
1280
1281 `:delay' - Delay between buffer update and colorization, to
1282 override `context-coloring-default-delay'.
1283
1284 `:version' - Minimum required version that should be printed when
1285 executing `:command' with a \"--version\" flag. The version
1286 should be numeric, e.g. \"2\", \"19700101\", \"1.2.3\",
1287 \"v1.2.3\" etc.
1288
1289 `:setup' - Arbitrary code to set up this dispatch when
1290 `context-coloring-mode' is enabled.
1291
1292 `:teardown' - Arbitrary code to tear down this dispatch when
1293 `context-coloring-mode' is disabled."
1294 (let ((modes (plist-get properties :modes))
1295 (predicate (plist-get properties :predicate))
1296 (colorizer (plist-get properties :colorizer))
1297 (command (plist-get properties :command)))
1298 (when (null (or modes
1299 predicate))
1300 (error "No mode or predicate defined for dispatch"))
1301 (when (not (or colorizer
1302 command))
1303 (error "No colorizer or command defined for dispatch"))
1304 (puthash symbol properties context-coloring-dispatch-hash-table)
1305 (dolist (mode modes)
1306 (puthash mode properties context-coloring-mode-hash-table))
1307 (when predicate
1308 (push (lambda ()
1309 (when (funcall predicate)
1310 properties)) context-coloring-dispatch-predicates))))
1311
1312
1313 ;;; Colorization
1314
1315 (defvar context-coloring-colorize-hook nil
1316 "Hooks to run after coloring a buffer.")
1317
1318 (defun context-coloring-colorize (&optional callback)
1319 "Color the current buffer by function context.
1320
1321 Invoke CALLBACK when complete; see `context-coloring-dispatch'."
1322 (interactive)
1323 (context-coloring-dispatch
1324 (lambda ()
1325 (when callback (funcall callback))
1326 (run-hooks 'context-coloring-colorize-hook))))
1327
1328 (defun context-coloring-colorize-with-buffer (buffer)
1329 "Color BUFFER."
1330 ;; Don't select deleted buffers.
1331 (when (get-buffer buffer)
1332 (with-current-buffer buffer
1333 (context-coloring-colorize))))
1334
1335
1336 ;;; Versioning
1337
1338 (defun context-coloring-parse-version (string)
1339 "Extract segments of a version STRING into a list. \"v1.0.0\"
1340 produces (1 0 0), \"19700101\" produces (19700101), etc."
1341 (let (version)
1342 (while (string-match "[0-9]+" string)
1343 (setq version (append version
1344 (list (string-to-number (match-string 0 string)))))
1345 (setq string (substring string (match-end 0))))
1346 version))
1347
1348 (defun context-coloring-check-version (expected actual)
1349 "Check that version EXPECTED is less than or equal to ACTUAL."
1350 (let ((expected (context-coloring-parse-version expected))
1351 (actual (context-coloring-parse-version actual))
1352 (continue t)
1353 (acceptable t))
1354 (while (and continue expected)
1355 (let ((an-expected (car expected))
1356 (an-actual (car actual)))
1357 (cond
1358 ((> an-actual an-expected)
1359 (setq acceptable t)
1360 (setq continue nil))
1361 ((< an-actual an-expected)
1362 (setq acceptable nil)
1363 (setq continue nil))))
1364 (setq expected (cdr expected))
1365 (setq actual (cdr actual)))
1366 acceptable))
1367
1368 (defvar context-coloring-check-scopifier-version-hook nil
1369 "Hooks to run after checking the scopifier version.")
1370
1371 (defun context-coloring-check-scopifier-version (&optional callback)
1372 "Asynchronously invoke CALLBACK with a predicate indicating
1373 whether the current scopifier version satisfies the minimum
1374 version number required for the current major mode."
1375 (let ((dispatch (context-coloring-get-current-dispatch)))
1376 (when dispatch
1377 (let ((version (plist-get dispatch :version))
1378 (command (plist-get dispatch :command)))
1379 (context-coloring-shell-command
1380 (context-coloring-join (list command "--version") " ")
1381 (lambda (output)
1382 (cond
1383 ((context-coloring-check-version version output)
1384 (when callback (funcall callback t)))
1385 (t
1386 (when callback (funcall callback nil))))
1387 (run-hooks 'context-coloring-check-scopifier-version-hook)))))))
1388
1389
1390 ;;; Themes
1391
1392 (defvar context-coloring-theme-hash-table (make-hash-table :test #'eq)
1393 "Map theme names to theme properties.")
1394
1395 (defun context-coloring-theme-p (theme)
1396 "Return t if THEME is defined, nil otherwise."
1397 (and (gethash theme context-coloring-theme-hash-table)))
1398
1399 (defconst context-coloring-level-face-regexp
1400 "context-coloring-level-\\([[:digit:]]+\\)-face"
1401 "Extract a level from a face.")
1402
1403 (defvar context-coloring-originally-set-theme-hash-table
1404 (make-hash-table :test #'eq)
1405 "Cache custom themes who originally set their own
1406 `context-coloring-level-N-face' faces.")
1407
1408 (defun context-coloring-theme-originally-set-p (theme)
1409 "Return t if there is a `context-coloring-level-N-face'
1410 originally set for THEME, nil otherwise."
1411 (let (originally-set)
1412 (cond
1413 ;; `setq' might return a non-nil value for the sake of this `cond'.
1414 ((setq
1415 originally-set
1416 (gethash
1417 theme
1418 context-coloring-originally-set-theme-hash-table))
1419 (eq originally-set 'yes))
1420 (t
1421 (let* ((settings (get theme 'theme-settings))
1422 (tail settings)
1423 found)
1424 (while (and tail (not found))
1425 (and (eq (nth 0 (car tail)) 'theme-face)
1426 (string-match
1427 context-coloring-level-face-regexp
1428 (symbol-name (nth 1 (car tail))))
1429 (setq found t))
1430 (setq tail (cdr tail)))
1431 found)))))
1432
1433 (defun context-coloring-cache-originally-set (theme originally-set)
1434 "Remember if THEME had colors originally set for it. If
1435 ORIGINALLY-SET is non-nil, it did, otherwise it didn't."
1436 ;; Caching whether a theme was originally set is kind of dirty, but we have to
1437 ;; do it to remember the past state of the theme. There are probably some
1438 ;; edge cases where caching will be an issue, but they are probably rare.
1439 (puthash
1440 theme
1441 (if originally-set 'yes 'no)
1442 context-coloring-originally-set-theme-hash-table))
1443
1444 (defun context-coloring-warn-theme-originally-set (theme)
1445 "Warn the user that the colors for THEME are already originally
1446 set."
1447 (warn "Context coloring colors for theme `%s' are already defined" theme))
1448
1449 (defun context-coloring-theme-highest-level (theme)
1450 "Return the highest level N of a face like
1451 `context-coloring-level-N-face' set for THEME, or `-1' if there
1452 is none."
1453 (let* ((settings (get theme 'theme-settings))
1454 (tail settings)
1455 face-string
1456 number
1457 (found -1))
1458 (while tail
1459 (and (eq (nth 0 (car tail)) 'theme-face)
1460 (setq face-string (symbol-name (nth 1 (car tail))))
1461 (string-match
1462 context-coloring-level-face-regexp
1463 face-string)
1464 (setq number (string-to-number
1465 (substring face-string
1466 (match-beginning 1)
1467 (match-end 1))))
1468 (> number found)
1469 (setq found number))
1470 (setq tail (cdr tail)))
1471 found))
1472
1473 (defun context-coloring-apply-theme (theme)
1474 "Apply THEME's properties to its respective custom theme,
1475 which must already exist and which *should* already be enabled."
1476 (let* ((properties (gethash theme context-coloring-theme-hash-table))
1477 (colors (plist-get properties :colors))
1478 (level -1))
1479 ;; Only clobber when we have to.
1480 (when (custom-theme-enabled-p theme)
1481 (setq context-coloring-maximum-face (- (length colors) 1)))
1482 (apply
1483 #'custom-theme-set-faces
1484 theme
1485 (mapcar
1486 (lambda (color)
1487 (setq level (+ level 1))
1488 `(,(context-coloring-level-face level) ((t (:foreground ,color)))))
1489 colors))))
1490
1491 (defun context-coloring-define-theme (theme &rest properties)
1492 "Define a context theme named THEME for coloring scope levels.
1493
1494 PROPERTIES is a property list specifiying the following details:
1495
1496 `:aliases': List of symbols of other custom themes that these
1497 colors are applicable to.
1498
1499 `:colors': List of colors that this context theme uses.
1500
1501 `:override': If non-nil, this context theme is intentionally
1502 overriding colors set by a custom theme. Don't set this non-nil
1503 unless there is a custom theme you want to use which sets
1504 `context-coloring-level-N-face' faces that you want to replace.
1505
1506 `:recede': If non-nil, this context theme should not apply its
1507 colors if a custom theme already sets
1508 `context-coloring-level-N-face' faces. This option is
1509 optimistic; set this non-nil if you would rather confer the duty
1510 of picking colors to a custom theme author (if / when he ever
1511 gets around to it).
1512
1513 By default, context themes will always override custom themes,
1514 even if those custom themes set `context-coloring-level-N-face'
1515 faces. If a context theme does override a custom theme, a
1516 warning will be raised, at which point you may want to enable the
1517 `:override' option, or just delete your context theme and opt to
1518 use your custom theme's author's colors instead.
1519
1520 Context themes only work for the custom theme with the highest
1521 precedence, i.e. the car of `custom-enabled-themes'."
1522 (let ((aliases (plist-get properties :aliases))
1523 (override (plist-get properties :override))
1524 (recede (plist-get properties :recede)))
1525 (dolist (name (append `(,theme) aliases))
1526 (puthash name properties context-coloring-theme-hash-table)
1527 (when (custom-theme-p name)
1528 (let ((originally-set (context-coloring-theme-originally-set-p name)))
1529 (context-coloring-cache-originally-set name originally-set)
1530 ;; In the particular case when you innocently define colors that a
1531 ;; custom theme originally set, warn. Arguably this only has to be
1532 ;; done at enable time, but it is probably more useful to do it at
1533 ;; definition time for prompter feedback.
1534 (when (and originally-set
1535 (not recede)
1536 (not override))
1537 (context-coloring-warn-theme-originally-set name))
1538 ;; Set (or overwrite) colors.
1539 (when (not (and originally-set
1540 recede))
1541 (context-coloring-apply-theme name)))))))
1542
1543 (defun context-coloring-enable-theme (theme)
1544 "Apply THEME if its colors are not already set, else just set
1545 `context-coloring-maximum-face' to the correct value for THEME."
1546 (let* ((properties (gethash theme context-coloring-theme-hash-table))
1547 (recede (plist-get properties :recede))
1548 (override (plist-get properties :override)))
1549 (cond
1550 (recede
1551 (let ((highest-level (context-coloring-theme-highest-level theme)))
1552 (cond
1553 ;; This can be true whether originally set by a custom theme or by a
1554 ;; context theme.
1555 ((> highest-level -1)
1556 (setq context-coloring-maximum-face highest-level))
1557 ;; It is possible that the corresponding custom theme did not exist at
1558 ;; the time of defining this context theme, and in that case the above
1559 ;; condition proves the custom theme did not originally set any faces,
1560 ;; so we have license to apply the context theme for the first time
1561 ;; here.
1562 (t
1563 (context-coloring-apply-theme theme)))))
1564 (t
1565 (let ((originally-set (context-coloring-theme-originally-set-p theme)))
1566 ;; Cache now in case the context theme was defined after the custom
1567 ;; theme.
1568 (context-coloring-cache-originally-set theme originally-set)
1569 (when (and originally-set
1570 (not override))
1571 (context-coloring-warn-theme-originally-set theme))
1572 (context-coloring-apply-theme theme))))))
1573
1574 (defadvice enable-theme (after context-coloring-enable-theme (theme) activate)
1575 "Enable colors for context themes just-in-time."
1576 (when (and (not (eq theme 'user)) ; Called internally by `enable-theme'.
1577 (custom-theme-p theme) ; Guard against non-existent themes.
1578 (context-coloring-theme-p theme))
1579 (when (= (length custom-enabled-themes) 1)
1580 ;; Cache because we can't reliably figure it out in reverse.
1581 (setq context-coloring-original-maximum-face
1582 context-coloring-maximum-face))
1583 (context-coloring-enable-theme theme)))
1584
1585 (defadvice disable-theme (after context-coloring-disable-theme (theme) activate)
1586 "Update `context-coloring-maximum-face'."
1587 (when (custom-theme-p theme) ; Guard against non-existent themes.
1588 (let ((enabled-theme (car custom-enabled-themes)))
1589 (cond
1590 ((context-coloring-theme-p enabled-theme)
1591 (context-coloring-enable-theme enabled-theme))
1592 (t
1593 ;; Assume we are back to no theme; act as if nothing ever happened.
1594 ;; This is still prone to intervention, but rather extraordinarily.
1595 (setq context-coloring-maximum-face
1596 context-coloring-original-maximum-face))))))
1597
1598 (context-coloring-define-theme
1599 'ample
1600 :recede t
1601 :colors '("#bdbdb3"
1602 "#baba36"
1603 "#6aaf50"
1604 "#5180b3"
1605 "#ab75c3"
1606 "#cd7542"
1607 "#df9522"
1608 "#454545"))
1609
1610 (context-coloring-define-theme
1611 'anti-zenburn
1612 :recede t
1613 :colors '("#232333"
1614 "#6c1f1c"
1615 "#401440"
1616 "#0f2050"
1617 "#205070"
1618 "#336c6c"
1619 "#23733c"
1620 "#6b400c"
1621 "#603a60"
1622 "#2f4070"
1623 "#235c5c"))
1624
1625 (context-coloring-define-theme
1626 'grandshell
1627 :recede t
1628 :colors '("#bebebe"
1629 "#5af2ee"
1630 "#b2baf6"
1631 "#f09fff"
1632 "#efc334"
1633 "#f6df92"
1634 "#acfb5a"
1635 "#888888"))
1636
1637 (context-coloring-define-theme
1638 'leuven
1639 :recede t
1640 :colors '("#333333"
1641 "#0000ff"
1642 "#6434a3"
1643 "#ba36a5"
1644 "#d0372d"
1645 "#036a07"
1646 "#006699"
1647 "#006fe0"
1648 "#808080"))
1649
1650 (context-coloring-define-theme
1651 'monokai
1652 :recede t
1653 :colors '("#f8f8f2"
1654 "#66d9ef"
1655 "#a1efe4"
1656 "#a6e22e"
1657 "#e6db74"
1658 "#fd971f"
1659 "#f92672"
1660 "#fd5ff0"
1661 "#ae81ff"))
1662
1663 (context-coloring-define-theme
1664 'solarized
1665 :recede t
1666 :aliases '(solarized-light
1667 solarized-dark
1668 sanityinc-solarized-light
1669 sanityinc-solarized-dark)
1670 :colors '("#839496"
1671 "#268bd2"
1672 "#2aa198"
1673 "#859900"
1674 "#b58900"
1675 "#cb4b16"
1676 "#dc322f"
1677 "#d33682"
1678 "#6c71c4"
1679 "#69b7f0"
1680 "#69cabf"
1681 "#b4c342"
1682 "#deb542"
1683 "#f2804f"
1684 "#ff6e64"
1685 "#f771ac"
1686 "#9ea0e5"))
1687
1688 (context-coloring-define-theme
1689 'spacegray
1690 :recede t
1691 :colors '("#ffffff"
1692 "#89aaeb"
1693 "#c189eb"
1694 "#bf616a"
1695 "#dca432"
1696 "#ebcb8b"
1697 "#b4eb89"
1698 "#89ebca"))
1699
1700 (context-coloring-define-theme
1701 'tango
1702 :recede t
1703 :colors '("#2e3436"
1704 "#346604"
1705 "#204a87"
1706 "#5c3566"
1707 "#a40000"
1708 "#b35000"
1709 "#c4a000"
1710 "#8ae234"
1711 "#8cc4ff"
1712 "#ad7fa8"
1713 "#ef2929"
1714 "#fcaf3e"
1715 "#fce94f"))
1716
1717 (context-coloring-define-theme
1718 'zenburn
1719 :recede t
1720 :colors '("#dcdccc"
1721 "#93e0e3"
1722 "#bfebbf"
1723 "#f0dfaf"
1724 "#dfaf8f"
1725 "#cc9393"
1726 "#dc8cc3"
1727 "#94bff3"
1728 "#9fc59f"
1729 "#d0bf8f"
1730 "#dca3a3"))
1731
1732
1733 ;;; Built-in dispatches
1734
1735 (context-coloring-define-dispatch
1736 'javascript-node
1737 :modes '(js-mode js3-mode)
1738 :executable "scopifier"
1739 :command "scopifier"
1740 :version "v1.2.1"
1741 :host "localhost"
1742 :port 6969)
1743
1744 (context-coloring-define-dispatch
1745 'javascript-js2
1746 :modes '(js2-mode)
1747 :colorizer #'context-coloring-js2-colorize
1748 :setup
1749 (lambda ()
1750 (add-hook 'js2-post-parse-callbacks #'context-coloring-colorize nil t))
1751 :teardown
1752 (lambda ()
1753 (remove-hook 'js2-post-parse-callbacks #'context-coloring-colorize t)))
1754
1755 (context-coloring-define-dispatch
1756 'emacs-lisp
1757 :modes '(emacs-lisp-mode)
1758 :colorizer #'context-coloring-elisp-colorize
1759 :delay 0.016 ;; Thanks to lazy colorization this can be 60 frames per second.
1760 :setup #'context-coloring-setup-idle-change-detection
1761 :teardown #'context-coloring-teardown-idle-change-detection)
1762
1763 ;; `eval-expression-minibuffer-setup-hook' is not available in Emacs 24.3, so
1764 ;; the backwards-compatible recommendation is to use `minibuffer-setup-hook' and
1765 ;; rely on this predicate instead.
1766 (defun context-coloring-eval-expression-predicate ()
1767 "Non-nil if the minibuffer is for `eval-expression'."
1768 (eq this-command 'eval-expression))
1769
1770 (context-coloring-define-dispatch
1771 'eval-expression
1772 :predicate #'context-coloring-eval-expression-predicate
1773 :colorizer #'context-coloring-eval-expression-colorize
1774 :delay 0.016
1775 :setup #'context-coloring-setup-idle-change-detection
1776 :teardown #'context-coloring-teardown-idle-change-detection)
1777
1778 (defun context-coloring-dispatch (&optional callback)
1779 "Determine the optimal track for scopification / coloring of
1780 the current buffer, then execute it.
1781
1782 Invoke CALLBACK when complete. It is invoked synchronously for
1783 elisp tracks, and asynchronously for shell command tracks."
1784 (let* ((dispatch (context-coloring-get-current-dispatch))
1785 (colorizer (plist-get dispatch :colorizer))
1786 (command (plist-get dispatch :command))
1787 (host (plist-get dispatch :host))
1788 (port (plist-get dispatch :port))
1789 interrupted-p)
1790 (cond
1791 (colorizer
1792 (setq interrupted-p
1793 (catch 'interrupted
1794 (funcall colorizer)))
1795 (when (and (not interrupted-p)
1796 callback)
1797 (funcall callback)))
1798 (command
1799 (cond
1800 ((and host port)
1801 (context-coloring-scopify-and-colorize-server command host port callback))
1802 (t
1803 (context-coloring-scopify-and-colorize command callback)))))))
1804
1805
1806 ;;; Minor mode
1807
1808 ;;;###autoload
1809 (define-minor-mode context-coloring-mode
1810 "Toggle contextual code coloring.
1811 With a prefix argument ARG, enable Context Coloring mode if ARG
1812 is positive, and disable it otherwise. If called from Lisp,
1813 enable the mode if ARG is omitted or nil.
1814
1815 Context Coloring mode is a buffer-local minor mode. When
1816 enabled, code is colored by scope. Scopes are colored
1817 hierarchically. Variables referenced from nested scopes retain
1818 the color of their defining scopes. Certain syntax, like
1819 comments and strings, is still colored with `font-lock'.
1820
1821 The entire buffer is colored initially. Changes to the buffer
1822 trigger recoloring.
1823
1824 Certain custom themes have predefined colors from their palettes
1825 to use for coloring. See `context-coloring-theme-hash-table' for
1826 the supported themes. If the currently-enabled custom theme is
1827 not among these, you can define colors for it with
1828 `context-coloring-define-theme', which see.
1829
1830 New language / major mode support can be added with
1831 `context-coloring-define-dispatch', which see.
1832
1833 Feature inspired by Douglas Crockford."
1834 nil " Context" nil
1835 (cond
1836 (context-coloring-mode
1837 ;; Font lock is incompatible with this mode; the converse is also true.
1838 (font-lock-mode 0)
1839 (jit-lock-mode nil)
1840 ;; ...but we do use font-lock functions here.
1841 (font-lock-set-defaults)
1842 ;; Safely change the value of this function as necessary.
1843 (make-local-variable 'font-lock-syntactic-face-function)
1844 (let ((dispatch (context-coloring-get-current-dispatch)))
1845 (cond
1846 (dispatch
1847 (let ((command (plist-get dispatch :command))
1848 (version (plist-get dispatch :version))
1849 (executable (plist-get dispatch :executable))
1850 (setup (plist-get dispatch :setup))
1851 (colorize-initially-p t))
1852 (when command
1853 ;; Shell commands recolor on change, idly.
1854 (cond
1855 ((and executable
1856 (null (executable-find executable)))
1857 (message "Executable \"%s\" not found" executable)
1858 (setq colorize-initially-p nil))
1859 (version
1860 (context-coloring-check-scopifier-version
1861 (lambda (sufficient-p)
1862 (cond
1863 (sufficient-p
1864 (context-coloring-setup-idle-change-detection)
1865 (context-coloring-colorize))
1866 (t
1867 (message "Update to the minimum version of \"%s\" (%s)"
1868 executable version)))))
1869 (setq colorize-initially-p nil))
1870 (t
1871 (context-coloring-setup-idle-change-detection))))
1872 (when setup
1873 (funcall setup))
1874 ;; Colorize once initially.
1875 (when colorize-initially-p
1876 (let ((context-coloring-parse-interruptable-p nil))
1877 (context-coloring-colorize)))))
1878 (t
1879 (message "Context coloring is not available for this major mode")))))
1880 (t
1881 (let ((dispatch (context-coloring-get-current-dispatch)))
1882 (when dispatch
1883 (let ((command (plist-get dispatch :command))
1884 (teardown (plist-get dispatch :teardown)))
1885 (when command
1886 (context-coloring-teardown-idle-change-detection))
1887 (when teardown
1888 (funcall teardown)))))
1889 (font-lock-mode)
1890 (jit-lock-mode t))))
1891
1892 (provide 'context-coloring)
1893
1894 ;;; context-coloring.el ends here