]> code.delx.au - gnu-emacs-elpa/blob - context-coloring.el
Checkdoc compliance.
[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
47 ;;; Faces
48
49 (defun context-coloring-defface (level light dark tty)
50 "Define a face for LEVEL with LIGHT, DARK and TTY colors."
51 (let ((face (intern (format "context-coloring-level-%s-face" level)))
52 (doc (format "Context coloring face, level %s." level)))
53 (custom-declare-face
54 face
55 `((((type tty)) (:foreground ,tty))
56 (((background light)) (:foreground ,light))
57 (((background dark)) (:foreground ,dark)))
58 doc
59 :group 'context-coloring)))
60
61 ;; Provide some default colors based off Emacs's defaults.
62 (context-coloring-defface 0 "#000000" "#ffffff" nil)
63 (context-coloring-defface 1 "#008b8b" "#00ffff" "yellow")
64 (context-coloring-defface 2 "#0000ff" "#87cefa" "green")
65 (context-coloring-defface 3 "#483d8b" "#b0c4de" "cyan")
66 (context-coloring-defface 4 "#a020f0" "#eedd82" "blue")
67 (context-coloring-defface 5 "#a0522d" "#98fb98" "magenta")
68 (context-coloring-defface 6 "#228b22" "#7fffd4" "red")
69 (context-coloring-defface 7 "#3f3f3f" "#cdcdcd" nil)
70
71 (defconst context-coloring-default-maximum-face 7
72 "Maximum face when there are no custom faces.")
73
74 ;; Create placeholder faces for users and theme authors.
75 (dotimes (level 18)
76 (let* ((level (+ level 8))
77 (face (intern (format "context-coloring-level-%s-face" level)))
78 (doc (format "Context coloring face, level %s." level)))
79 (custom-declare-face face nil doc :group 'context-coloring)))
80
81 (defvar-local context-coloring-maximum-face nil
82 "Dynamic index of the highest face available for coloring.")
83
84 (defsubst context-coloring-level-face (level)
85 "Return symbol for face with LEVEL."
86 ;; `concat' is faster than `format' here.
87 (intern-soft
88 (concat "context-coloring-level-" (number-to-string level) "-face")))
89
90 (defsubst context-coloring-bounded-level-face (level)
91 "Return symbol for face with LEVEL, bounded by the maximum."
92 (context-coloring-level-face (min level context-coloring-maximum-face)))
93
94 (defconst context-coloring-level-face-regexp
95 "context-coloring-level-\\([[:digit:]]+\\)-face"
96 "Extract a level from a face.")
97
98 (defun context-coloring-theme-highest-level (theme)
99 "Return the highest coloring level for THEME, or -1."
100 (let* ((settings (get theme 'theme-settings))
101 (tail settings)
102 face-string
103 number
104 (found -1))
105 (while tail
106 (and (eq (nth 0 (car tail)) 'theme-face)
107 (setq face-string (symbol-name (nth 1 (car tail))))
108 (string-match
109 context-coloring-level-face-regexp
110 face-string)
111 (setq number (string-to-number
112 (substring face-string
113 (match-beginning 1)
114 (match-end 1))))
115 (> number found)
116 (setq found number))
117 (setq tail (cdr tail)))
118 found))
119
120 (defun context-coloring-update-maximum-face ()
121 "Save the highest possible face for the current theme."
122 (let ((themes (append custom-enabled-themes '(user)))
123 (continue t)
124 theme
125 highest-level)
126 (while continue
127 (setq theme (car themes))
128 (setq themes (cdr themes))
129 (setq highest-level (context-coloring-theme-highest-level theme))
130 (setq continue (and themes (= highest-level -1))))
131 (setq context-coloring-maximum-face
132 (cond
133 ((= highest-level -1)
134 context-coloring-default-maximum-face)
135 (t
136 highest-level)))))
137
138
139 ;;; Change detection
140
141 (defvar-local context-coloring-changed-p nil
142 "Indication that the buffer has changed recently, which implies
143 that it should be colored again by
144 `context-coloring-maybe-colorize-idle-timer' if that timer is
145 being used.")
146
147 (defvar-local context-coloring-changed-start nil
148 "Beginning of last text that changed.")
149
150 (defvar-local context-coloring-changed-end nil
151 "End of last text that changed.")
152
153 (defvar-local context-coloring-changed-length nil
154 "Length of last text that changed.")
155
156 (defun context-coloring-change-function (start end length)
157 "Register a change so that a buffer can be colorized soon.
158
159 START, END and LENGTH are recorded for later use."
160 ;; Tokenization is obsolete if there was a change.
161 (setq context-coloring-changed-start start)
162 (setq context-coloring-changed-end end)
163 (setq context-coloring-changed-length length)
164 (setq context-coloring-changed-p t))
165
166 (defun context-coloring-maybe-colorize-with-buffer (buffer)
167 "Color BUFFER and if it has changed."
168 (when (and (eq buffer (current-buffer))
169 context-coloring-changed-p)
170 (context-coloring-colorize-with-buffer buffer)
171 (setq context-coloring-changed-p nil)
172 (setq context-coloring-changed-start nil)
173 (setq context-coloring-changed-end nil)
174 (setq context-coloring-changed-length nil)))
175
176 (defvar-local context-coloring-maybe-colorize-idle-timer nil
177 "The currently-running idle timer for conditional coloring.")
178
179 (defvar-local context-coloring-colorize-idle-timer nil
180 "The currently-running idle timer for unconditional coloring.")
181
182 (defcustom context-coloring-default-delay 0.25
183 "Default delay between a buffer update and colorization.
184
185 Increase this if your machine is high-performing. Decrease it if
186 it ain't."
187 :group 'context-coloring)
188
189 (make-obsolete-variable
190 'context-coloring-delay
191 'context-coloring-default-delay
192 "6.4.0")
193
194 (defun context-coloring-cancel-timer (timer)
195 "Cancel TIMER."
196 (when timer
197 (cancel-timer timer)))
198
199 (defun context-coloring-schedule-coloring (time)
200 "Schedule coloring to occur once after Emacs is idle for TIME."
201 (context-coloring-cancel-timer context-coloring-colorize-idle-timer)
202 (setq context-coloring-colorize-idle-timer
203 (run-with-idle-timer
204 time
205 nil
206 #'context-coloring-colorize-with-buffer
207 (current-buffer))))
208
209 (defun context-coloring-setup-idle-change-detection ()
210 "Setup idle change detection."
211 (let ((dispatch (context-coloring-get-current-dispatch)))
212 (add-hook
213 'after-change-functions #'context-coloring-change-function nil t)
214 (add-hook
215 'kill-buffer-hook #'context-coloring-teardown-idle-change-detection nil t)
216 (setq context-coloring-maybe-colorize-idle-timer
217 (run-with-idle-timer
218 (or (plist-get dispatch :delay) context-coloring-default-delay)
219 t
220 #'context-coloring-maybe-colorize-with-buffer
221 (current-buffer)))))
222
223 (defun context-coloring-teardown-idle-change-detection ()
224 "Teardown idle change detection."
225 (dolist (timer (list context-coloring-colorize-idle-timer
226 context-coloring-maybe-colorize-idle-timer))
227 (context-coloring-cancel-timer timer))
228 (remove-hook
229 'kill-buffer-hook #'context-coloring-teardown-idle-change-detection t)
230 (remove-hook
231 'after-change-functions #'context-coloring-change-function t))
232
233
234 ;;; Colorization utilities
235
236 (defsubst context-coloring-colorize-region (start end level)
237 "Color from START (inclusive) to END (exclusive) with LEVEL."
238 (add-text-properties
239 start
240 end
241 `(face ,(context-coloring-bounded-level-face level))))
242
243 (make-obsolete-variable
244 'context-coloring-comments-and-strings
245 "use `context-coloring-syntactic-comments' and
246 `context-coloring-syntactic-strings' instead."
247 "6.1.0")
248
249 (defcustom context-coloring-syntactic-comments t
250 "If non-nil, also color comments using `font-lock'."
251 :group 'context-coloring)
252
253 (defcustom context-coloring-syntactic-strings t
254 "If non-nil, also color strings using `font-lock'."
255 :group 'context-coloring)
256
257 (defun context-coloring-font-lock-syntactic-comment-function (state)
258 "Color a comment according to STATE."
259 (if (nth 3 state) nil font-lock-comment-face))
260
261 (defun context-coloring-font-lock-syntactic-string-function (state)
262 "Color a string according to STATE."
263 (if (nth 3 state) font-lock-string-face nil))
264
265 (defsubst context-coloring-colorize-comments-and-strings (&optional min max)
266 "Maybe color comments and strings in buffer from MIN to MAX.
267 MIN defaults to beginning of buffer. MAX defaults to end."
268 (when (or context-coloring-syntactic-comments
269 context-coloring-syntactic-strings)
270 (let ((min (or min (point-min)))
271 (max (or max (point-max)))
272 (font-lock-syntactic-face-function
273 (cond
274 ((and context-coloring-syntactic-comments
275 (not context-coloring-syntactic-strings))
276 #'context-coloring-font-lock-syntactic-comment-function)
277 ((and context-coloring-syntactic-strings
278 (not context-coloring-syntactic-comments))
279 #'context-coloring-font-lock-syntactic-string-function)
280 (t
281 font-lock-syntactic-face-function))))
282 (save-excursion
283 (font-lock-fontify-syntactically-region min max)
284 ;; TODO: Make configurable at the dispatch level.
285 (when (eq major-mode 'emacs-lisp-mode)
286 (font-lock-fontify-keywords-region min max))))))
287
288
289 ;;; js2-mode colorization
290
291 (defvar-local context-coloring-js2-scope-level-hash-table nil
292 "Associate `js2-scope' structures and with their scope
293 levels.")
294
295 (defcustom context-coloring-javascript-block-scopes nil
296 "If non-nil, also color block scopes in the scope hierarchy in JavaScript.
297
298 The block-scoped `let' and `const' are introduced in ES6. Enable
299 this for ES6 code; disable it elsewhere."
300 :group 'context-coloring)
301
302 (make-obsolete-variable
303 'context-coloring-js-block-scopes
304 'context-coloring-javascript-block-scopes
305 "7.0.0")
306
307 (defsubst context-coloring-js2-scope-level (scope)
308 "Return the level of SCOPE."
309 (cond ((gethash scope context-coloring-js2-scope-level-hash-table))
310 (t
311 (let ((level 0)
312 (current-scope scope)
313 enclosing-scope)
314 (while (and current-scope
315 (js2-node-parent current-scope)
316 (setq enclosing-scope
317 (js2-node-get-enclosing-scope current-scope)))
318 (when (or context-coloring-javascript-block-scopes
319 (let ((type (js2-scope-type current-scope)))
320 (or (= type js2-SCRIPT)
321 (= type js2-FUNCTION)
322 (= type js2-CATCH))))
323 (setq level (+ level 1)))
324 (setq current-scope enclosing-scope))
325 (puthash scope level context-coloring-js2-scope-level-hash-table)))))
326
327 (defsubst context-coloring-js2-local-name-node-p (node)
328 "Determine if NODE represents a local variable."
329 (and (js2-name-node-p node)
330 (let ((parent (js2-node-parent node)))
331 (not (or (and (js2-object-prop-node-p parent)
332 (eq node (js2-object-prop-node-left parent)))
333 (and (js2-prop-get-node-p parent)
334 ;; For nested property lookup, the node on the left is a
335 ;; `js2-prop-get-node', so this always works.
336 (eq node (js2-prop-get-node-right parent))))))))
337
338 (defvar-local context-coloring-point-max nil
339 "Cached value of `point-max'.")
340
341 (defsubst context-coloring-js2-colorize-node (node level)
342 "Color NODE with the color for LEVEL."
343 (let ((start (js2-node-abs-pos node)))
344 (context-coloring-colorize-region
345 start
346 (min
347 ;; End
348 (+ start (js2-node-len node))
349 ;; Somes nodes (like the ast when there is an unterminated multiline
350 ;; comment) will stretch to the value of `point-max'.
351 context-coloring-point-max)
352 level)))
353
354 (defun context-coloring-js2-colorize ()
355 "Color the buffer using the `js2-mode' abstract syntax tree."
356 ;; Reset the hash table; the old one could be obsolete.
357 (setq context-coloring-js2-scope-level-hash-table (make-hash-table :test #'eq))
358 (setq context-coloring-point-max (point-max))
359 (with-silent-modifications
360 (js2-visit-ast
361 js2-mode-ast
362 (lambda (node end-p)
363 (when (null end-p)
364 (cond
365 ((js2-scope-p node)
366 (context-coloring-js2-colorize-node
367 node
368 (context-coloring-js2-scope-level node)))
369 ((context-coloring-js2-local-name-node-p node)
370 (let* ((enclosing-scope (js2-node-get-enclosing-scope node))
371 (defining-scope (js2-get-defining-scope
372 enclosing-scope
373 (js2-name-node-name node))))
374 ;; The tree seems to be walked lexically, so an entire scope will
375 ;; be colored, including its name nodes, before they are reached.
376 ;; Coloring the nodes defined in that scope would be redundant, so
377 ;; don't do it.
378 (when (not (eq defining-scope enclosing-scope))
379 (context-coloring-js2-colorize-node
380 node
381 (context-coloring-js2-scope-level defining-scope))))))
382 ;; The `t' indicates to search children.
383 t)))
384 (context-coloring-colorize-comments-and-strings)))
385
386
387 ;;; Emacs Lisp colorization
388
389 (defsubst context-coloring-forward-sws ()
390 "Move forward through whitespace and comments."
391 (while (forward-comment 1)))
392
393 (defsubst context-coloring-elisp-forward-sws ()
394 "Move through whitespace and comments, coloring comments."
395 (let ((start (point)))
396 (context-coloring-forward-sws)
397 (context-coloring-colorize-comments-and-strings start (point))))
398
399 (defsubst context-coloring-elisp-forward-sexp ()
400 "Like `forward-sexp', coloring skipped comments and strings."
401 (let ((start (point)))
402 (forward-sexp)
403 (context-coloring-elisp-colorize-comments-and-strings-in-region
404 start (point))))
405
406 (defsubst context-coloring-get-syntax-code ()
407 "Get the syntax code at point."
408 (syntax-class
409 ;; Faster version of `syntax-after':
410 (aref (syntax-table) (char-after (point)))))
411
412 (defsubst context-coloring-exact-regexp (word)
413 "Create a regexp matching exactly WORD."
414 (concat "\\`" (regexp-quote word) "\\'"))
415
416 (defsubst context-coloring-exact-or-regexp (words)
417 "Create a regexp matching any exact word in WORDS."
418 (context-coloring-join
419 (mapcar #'context-coloring-exact-regexp words) "\\|"))
420
421 (defconst context-coloring-elisp-ignored-word-regexp
422 (context-coloring-join (list "\\`[-+]?[0-9]"
423 "\\`[&:].+"
424 (context-coloring-exact-or-regexp
425 '("t" "nil" "." "?")))
426 "\\|")
427 "Match symbols that can't be bound as variables.")
428
429 (defconst context-coloring-WORD-CODE 2)
430 (defconst context-coloring-SYMBOL-CODE 3)
431 (defconst context-coloring-OPEN-PARENTHESIS-CODE 4)
432 (defconst context-coloring-CLOSE-PARENTHESIS-CODE 5)
433 (defconst context-coloring-EXPRESSION-PREFIX-CODE 6)
434 (defconst context-coloring-STRING-QUOTE-CODE 7)
435 (defconst context-coloring-ESCAPE-CODE 9)
436 (defconst context-coloring-COMMENT-START-CODE 11)
437 (defconst context-coloring-COMMENT-END-CODE 12)
438
439 (defconst context-coloring-OCTOTHORPE-CHAR (string-to-char "#"))
440 (defconst context-coloring-APOSTROPHE-CHAR (string-to-char "'"))
441 (defconst context-coloring-OPEN-PARENTHESIS-CHAR (string-to-char "("))
442 (defconst context-coloring-COMMA-CHAR (string-to-char ","))
443 (defconst context-coloring-AT-CHAR (string-to-char "@"))
444 (defconst context-coloring-BACKTICK-CHAR (string-to-char "`"))
445
446 (defsubst context-coloring-elisp-identifier-p (syntax-code)
447 "Check if SYNTAX-CODE is an elisp identifier constituent."
448 (or (= syntax-code context-coloring-WORD-CODE)
449 (= syntax-code context-coloring-SYMBOL-CODE)))
450
451 (defvar context-coloring-parse-interruptable-p t
452 "Set this to nil to force parse to continue until finished.")
453
454 (defconst context-coloring-elisp-sexps-per-pause 350
455 "Pause after this many iterations to check for user input.
456 If user input is pending, stop the parse. This makes for a
457 smoother user experience for large files.
458
459 This number should trigger pausing at about 60 frames per
460 second.")
461
462 (defvar context-coloring-elisp-sexp-count 0
463 "Current number of sexps leading up to the next pause.")
464
465 (defsubst context-coloring-elisp-increment-sexp-count ()
466 "Maybe check if the user interrupted the current parse."
467 (setq context-coloring-elisp-sexp-count
468 (1+ context-coloring-elisp-sexp-count))
469 (when (and (zerop (% context-coloring-elisp-sexp-count
470 context-coloring-elisp-sexps-per-pause))
471 context-coloring-parse-interruptable-p
472 (input-pending-p))
473 (throw 'interrupted t)))
474
475 (defvar context-coloring-elisp-scope-stack '()
476 "List of scopes in the current parse.")
477
478 (defsubst context-coloring-elisp-make-scope (level)
479 "Make a scope object for LEVEL."
480 (list
481 :level level
482 :variables '()))
483
484 (defsubst context-coloring-elisp-scope-get-level (scope)
485 "Get the level of SCOPE object."
486 (plist-get scope :level))
487
488 (defsubst context-coloring-elisp-scope-add-variable (scope variable)
489 "Add to SCOPE a VARIABLE."
490 (plist-put scope :variables (cons variable (plist-get scope :variables))))
491
492 (defsubst context-coloring-elisp-scope-has-variable (scope variable)
493 "Check if SCOPE has VARIABLE."
494 (member variable (plist-get scope :variables)))
495
496 (defsubst context-coloring-elisp-get-variable-level (variable)
497 "Return the level of VARIABLE, or 0 if it isn't found."
498 (let* ((scope-stack context-coloring-elisp-scope-stack)
499 scope
500 level)
501 (while (and scope-stack (not level))
502 (setq scope (car scope-stack))
503 (cond
504 ((context-coloring-elisp-scope-has-variable scope variable)
505 (setq level (context-coloring-elisp-scope-get-level scope)))
506 (t
507 (setq scope-stack (cdr scope-stack)))))
508 ;; Assume a global variable.
509 (or level 0)))
510
511 (defsubst context-coloring-elisp-get-current-scope-level ()
512 "Get the nesting level of the current scope."
513 (cond
514 ((car context-coloring-elisp-scope-stack)
515 (context-coloring-elisp-scope-get-level (car context-coloring-elisp-scope-stack)))
516 (t
517 0)))
518
519 (defsubst context-coloring-elisp-push-scope ()
520 "Add a new scope to the bottom of the scope chain."
521 (push (context-coloring-elisp-make-scope
522 (1+ (context-coloring-elisp-get-current-scope-level)))
523 context-coloring-elisp-scope-stack))
524
525 (defsubst context-coloring-elisp-pop-scope ()
526 "Remove the scope on the bottom of the scope chain."
527 (pop context-coloring-elisp-scope-stack))
528
529 (defsubst context-coloring-elisp-add-variable (variable)
530 "Add VARIABLE to the current scope."
531 (context-coloring-elisp-scope-add-variable
532 (car context-coloring-elisp-scope-stack)
533 variable))
534
535 (defsubst context-coloring-elisp-parse-bindable (callback)
536 "Parse the symbol at point.
537 If the symbol can be bound, invoke CALLBACK with it."
538 (let* ((arg-string (buffer-substring-no-properties
539 (point)
540 (progn (context-coloring-elisp-forward-sexp)
541 (point)))))
542 (when (not (string-match-p
543 context-coloring-elisp-ignored-word-regexp
544 arg-string))
545 (funcall callback arg-string))))
546
547 (defun context-coloring-elisp-parse-let-varlist (type)
548 "Parse the list of variable initializers at point.
549 If TYPE is `let', all the variables are bound after all their
550 initializers are parsed; if TYPE is `let*', each variable is
551 bound immediately after its own initializer is parsed."
552 (let ((varlist '())
553 syntax-code)
554 ;; Enter.
555 (forward-char)
556 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
557 context-coloring-CLOSE-PARENTHESIS-CODE)
558 (cond
559 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
560 (forward-char)
561 (context-coloring-elisp-forward-sws)
562 (setq syntax-code (context-coloring-get-syntax-code))
563 (when (context-coloring-elisp-identifier-p syntax-code)
564 (context-coloring-elisp-parse-bindable
565 (lambda (var)
566 (push var varlist)))
567 (context-coloring-elisp-forward-sws)
568 (setq syntax-code (context-coloring-get-syntax-code))
569 (when (/= syntax-code context-coloring-CLOSE-PARENTHESIS-CODE)
570 (context-coloring-elisp-colorize-sexp)))
571 (context-coloring-elisp-forward-sws)
572 ;; Skip past the closing parenthesis.
573 (forward-char))
574 ((context-coloring-elisp-identifier-p syntax-code)
575 (context-coloring-elisp-parse-bindable
576 (lambda (var)
577 (push var varlist))))
578 (t
579 ;; Ignore artifacts.
580 (context-coloring-elisp-forward-sexp)))
581 (when (eq type 'let*)
582 (context-coloring-elisp-add-variable (pop varlist)))
583 (context-coloring-elisp-forward-sws))
584 (when (eq type 'let)
585 (while varlist
586 (context-coloring-elisp-add-variable (pop varlist))))
587 ;; Exit.
588 (forward-char)))
589
590 (defun context-coloring-elisp-parse-arglist ()
591 "Parse the list of function arguments at point."
592 (let (syntax-code)
593 ;; Enter.
594 (forward-char)
595 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
596 context-coloring-CLOSE-PARENTHESIS-CODE)
597 (cond
598 ((context-coloring-elisp-identifier-p syntax-code)
599 (context-coloring-elisp-parse-bindable
600 (lambda (arg)
601 (context-coloring-elisp-add-variable arg))))
602 (t
603 ;; Ignore artifacts.
604 (context-coloring-elisp-forward-sexp)))
605 (context-coloring-elisp-forward-sws))
606 ;; Exit.
607 (forward-char)))
608
609 (defun context-coloring-elisp-skip-callee-name ()
610 "Skip past the opening parenthesis and name of a function."
611 ;; Enter.
612 (forward-char)
613 (context-coloring-elisp-forward-sws)
614 ;; Skip past the function name.
615 (forward-sexp)
616 (context-coloring-elisp-forward-sws))
617
618 (defun context-coloring-elisp-colorize-scope (callback)
619 "Color the whole scope at point with its one color.
620 Handle a header in CALLBACK."
621 (let ((start (point))
622 (end (progn (forward-sexp)
623 (point))))
624 (context-coloring-elisp-push-scope)
625 ;; Splash the whole thing in one color.
626 (context-coloring-colorize-region
627 start
628 end
629 (context-coloring-elisp-get-current-scope-level))
630 ;; Even if the parse is interrupted, this region should still be colored
631 ;; syntactically.
632 (context-coloring-elisp-colorize-comments-and-strings-in-region
633 start
634 end)
635 (goto-char start)
636 (context-coloring-elisp-skip-callee-name)
637 (funcall callback)
638 (context-coloring-elisp-colorize-region (point) (1- end))
639 ;; Exit.
640 (forward-char)
641 (context-coloring-elisp-pop-scope)))
642
643 (defun context-coloring-elisp-parse-header (callback)
644 "Parse a function header at point with CALLBACK."
645 (when (= (context-coloring-get-syntax-code) context-coloring-OPEN-PARENTHESIS-CODE)
646 (funcall callback)))
647
648 (defun context-coloring-elisp-colorize-defun-like (callback)
649 "Color the defun-like function at point.
650 Parse the header with CALLBACK."
651 (context-coloring-elisp-colorize-scope
652 (lambda ()
653 (when (context-coloring-elisp-identifier-p (context-coloring-get-syntax-code))
654 ;; Color the defun's name with the top-level color.
655 (context-coloring-colorize-region
656 (point)
657 (progn (forward-sexp)
658 (point))
659 0)
660 (context-coloring-elisp-forward-sws)
661 (context-coloring-elisp-parse-header callback)))))
662
663 (defun context-coloring-elisp-colorize-defun ()
664 "Color the `defun' at point."
665 (context-coloring-elisp-colorize-defun-like
666 'context-coloring-elisp-parse-arglist))
667
668 (defun context-coloring-elisp-colorize-defadvice ()
669 "Color the `defadvice' at point."
670 (context-coloring-elisp-colorize-defun-like
671 (lambda ()
672 (let (syntax-code)
673 ;; Enter.
674 (forward-char)
675 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
676 context-coloring-CLOSE-PARENTHESIS-CODE)
677 (cond
678 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
679 (context-coloring-elisp-parse-arglist))
680 (t
681 ;; Ignore artifacts.
682 (context-coloring-elisp-forward-sexp)))
683 (context-coloring-elisp-forward-sws))))))
684
685 (defun context-coloring-elisp-colorize-lambda-like (callback)
686 "Color the lambda-like function at point.
687 Parsing the header with CALLBACK."
688 (context-coloring-elisp-colorize-scope
689 (lambda ()
690 (context-coloring-elisp-parse-header callback))))
691
692 (defun context-coloring-elisp-colorize-lambda ()
693 "Color the `lambda' at point."
694 (context-coloring-elisp-colorize-lambda-like
695 'context-coloring-elisp-parse-arglist))
696
697 (defun context-coloring-elisp-colorize-let ()
698 "Color the `let' at point."
699 (context-coloring-elisp-colorize-lambda-like
700 (lambda ()
701 (context-coloring-elisp-parse-let-varlist 'let))))
702
703 (defun context-coloring-elisp-colorize-let* ()
704 "Color the `let*' at point."
705 (context-coloring-elisp-colorize-lambda-like
706 (lambda ()
707 (context-coloring-elisp-parse-let-varlist 'let*))))
708
709 (defun context-coloring-elisp-colorize-cond ()
710 "Color the `cond' at point."
711 (let (syntax-code)
712 (context-coloring-elisp-skip-callee-name)
713 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
714 context-coloring-CLOSE-PARENTHESIS-CODE)
715 (cond
716 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
717 ;; Colorize inside the parens.
718 (let ((start (point)))
719 (forward-sexp)
720 (context-coloring-elisp-colorize-region
721 (1+ start) (1- (point)))
722 ;; Exit.
723 (forward-char)))
724 (t
725 ;; Ignore artifacts.
726 (context-coloring-elisp-forward-sexp)))
727 (context-coloring-elisp-forward-sws))
728 ;; Exit.
729 (forward-char)))
730
731 (defun context-coloring-elisp-colorize-condition-case ()
732 "Color the `condition-case' at point."
733 (let (syntax-code
734 variable
735 case-pos
736 case-end)
737 (context-coloring-elisp-colorize-scope
738 (lambda ()
739 (setq syntax-code (context-coloring-get-syntax-code))
740 ;; Gracefully ignore missing variables.
741 (when (context-coloring-elisp-identifier-p syntax-code)
742 (context-coloring-elisp-parse-bindable
743 (lambda (parsed-variable)
744 (setq variable parsed-variable)))
745 (context-coloring-elisp-forward-sws))
746 (context-coloring-elisp-colorize-sexp)
747 (context-coloring-elisp-forward-sws)
748 ;; Parse the handlers with the error variable in scope.
749 (when variable
750 (context-coloring-elisp-add-variable variable))
751 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
752 context-coloring-CLOSE-PARENTHESIS-CODE)
753 (cond
754 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
755 (setq case-pos (point))
756 (context-coloring-elisp-forward-sexp)
757 (setq case-end (point))
758 (goto-char case-pos)
759 ;; Enter.
760 (forward-char)
761 (context-coloring-elisp-forward-sws)
762 (setq syntax-code (context-coloring-get-syntax-code))
763 (when (/= syntax-code context-coloring-CLOSE-PARENTHESIS-CODE)
764 ;; Skip the condition name(s).
765 (context-coloring-elisp-forward-sexp)
766 ;; Color the remaining portion of the handler.
767 (context-coloring-elisp-colorize-region
768 (point)
769 (1- case-end)))
770 ;; Exit.
771 (forward-char))
772 (t
773 ;; Ignore artifacts.
774 (context-coloring-elisp-forward-sexp)))
775 (context-coloring-elisp-forward-sws))))))
776
777 (defun context-coloring-elisp-colorize-dolist ()
778 "Color the `dolist' at point."
779 (let (syntax-code
780 (index 0))
781 (context-coloring-elisp-colorize-scope
782 (lambda ()
783 (setq syntax-code (context-coloring-get-syntax-code))
784 (when (= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
785 (forward-char)
786 (context-coloring-elisp-forward-sws)
787 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
788 context-coloring-CLOSE-PARENTHESIS-CODE)
789 (cond
790 ((and
791 (or (= index 0) (= index 2))
792 (context-coloring-elisp-identifier-p syntax-code))
793 ;; Add the first or third name to the scope.
794 (context-coloring-elisp-parse-bindable
795 (lambda (variable)
796 (context-coloring-elisp-add-variable variable))))
797 (t
798 ;; Color artifacts.
799 (context-coloring-elisp-colorize-sexp)))
800 (context-coloring-elisp-forward-sws)
801 (setq index (1+ index)))
802 ;; Exit.
803 (forward-char))))))
804
805 (defun context-coloring-elisp-colorize-quote ()
806 "Color the `quote' at point."
807 (let* ((start (point))
808 (end (progn (forward-sexp)
809 (point))))
810 (context-coloring-colorize-region
811 start
812 end
813 (context-coloring-elisp-get-current-scope-level))
814 (context-coloring-elisp-colorize-comments-and-strings-in-region start end)))
815
816 (defvar context-coloring-elisp-callee-dispatch-hash-table
817 (let ((table (make-hash-table :test 'equal)))
818 (dolist (callee '("defun" "defun*" "defsubst" "defmacro" "cl-defun" "cl-defsubst" "cl-defmacro"))
819 (puthash callee #'context-coloring-elisp-colorize-defun table))
820 (dolist (callee '("condition-case" "condition-case-unless-debug"))
821 (puthash callee #'context-coloring-elisp-colorize-condition-case table))
822 (dolist (callee '("dolist" "dotimes"))
823 (puthash callee #'context-coloring-elisp-colorize-dolist table))
824 (puthash "let" #'context-coloring-elisp-colorize-let table)
825 (puthash "let*" #'context-coloring-elisp-colorize-let* table)
826 (puthash "lambda" #'context-coloring-elisp-colorize-lambda table)
827 (puthash "cond" #'context-coloring-elisp-colorize-cond table)
828 (puthash "defadvice" #'context-coloring-elisp-colorize-defadvice table)
829 (puthash "quote" #'context-coloring-elisp-colorize-quote table)
830 (puthash "backquote" #'context-coloring-elisp-colorize-backquote table)
831 table)
832 "Map function names to their coloring functions.")
833
834 (defun context-coloring-elisp-colorize-parenthesized-sexp ()
835 "Color the sexp enclosed by parenthesis at point."
836 (context-coloring-elisp-increment-sexp-count)
837 (let* ((start (point))
838 (end (progn (forward-sexp)
839 (point)))
840 (syntax-code (progn (goto-char start)
841 (forward-char)
842 ;; Coloring is unnecessary here, it'll happen
843 ;; presently.
844 (context-coloring-forward-sws)
845 (context-coloring-get-syntax-code)))
846 dispatch-function)
847 ;; Figure out if the sexp is a special form.
848 (cond
849 ((and (context-coloring-elisp-identifier-p syntax-code)
850 (setq dispatch-function (gethash
851 (buffer-substring-no-properties
852 (point)
853 (progn (forward-sexp)
854 (point)))
855 context-coloring-elisp-callee-dispatch-hash-table)))
856 (goto-char start)
857 (funcall dispatch-function))
858 ;; Not a special form; just colorize the remaining region.
859 (t
860 (context-coloring-colorize-region
861 start
862 end
863 (context-coloring-elisp-get-current-scope-level))
864 (context-coloring-elisp-colorize-region (point) (1- end))
865 (forward-char)))))
866
867 (defun context-coloring-elisp-colorize-symbol ()
868 "Color the symbol at point."
869 (context-coloring-elisp-increment-sexp-count)
870 (let* ((symbol-pos (point))
871 (symbol-end (progn (forward-sexp)
872 (point)))
873 (symbol-string (buffer-substring-no-properties
874 symbol-pos
875 symbol-end)))
876 (cond
877 ((string-match-p context-coloring-elisp-ignored-word-regexp symbol-string))
878 (t
879 (context-coloring-colorize-region
880 symbol-pos
881 symbol-end
882 (context-coloring-elisp-get-variable-level
883 symbol-string))))))
884
885 (defun context-coloring-elisp-colorize-backquote-form ()
886 "Color the backquote form at point."
887 (let ((start (point))
888 (end (progn (forward-sexp)
889 (point)))
890 char)
891 (goto-char start)
892 (while (> end (progn (forward-char)
893 (point)))
894 (setq char (char-after))
895 (when (= char context-coloring-COMMA-CHAR)
896 (forward-char)
897 (when (= (char-after) context-coloring-AT-CHAR)
898 ;; If we don't do this "@" could be interpreted as a symbol.
899 (forward-char))
900 (context-coloring-elisp-forward-sws)
901 (context-coloring-elisp-colorize-sexp)))
902 ;; We could probably do this as part of the above loop but it'd be
903 ;; repetitive.
904 (context-coloring-elisp-colorize-comments-and-strings-in-region
905 start end)))
906
907 (defun context-coloring-elisp-colorize-backquote ()
908 "Color the `backquote' at point."
909 (context-coloring-elisp-skip-callee-name)
910 (context-coloring-elisp-colorize-backquote-form)
911 ;; Exit.
912 (forward-char))
913
914 (defun context-coloring-elisp-colorize-expression-prefix ()
915 "Color the expression prefix and expression at point.
916 It could be a quoted or backquoted expression."
917 (context-coloring-elisp-increment-sexp-count)
918 (cond
919 ((/= (char-after) context-coloring-BACKTICK-CHAR)
920 (context-coloring-elisp-forward-sexp))
921 (t
922 (context-coloring-elisp-colorize-backquote-form))))
923
924 (defun context-coloring-elisp-colorize-comment ()
925 "Color the comment at point."
926 (context-coloring-elisp-increment-sexp-count)
927 (context-coloring-elisp-forward-sws))
928
929 (defun context-coloring-elisp-colorize-string ()
930 "Color the string at point."
931 (context-coloring-elisp-increment-sexp-count)
932 (let ((start (point)))
933 (forward-sexp)
934 (context-coloring-colorize-comments-and-strings start (point))))
935
936 ;; Elisp has whitespace, words, symbols, open/close parenthesis, expression
937 ;; prefix, string quote, comment starters/enders and escape syntax classes only.
938
939 (defun context-coloring-elisp-colorize-sexp ()
940 "Color the sexp at point."
941 (let ((syntax-code (context-coloring-get-syntax-code)))
942 (cond
943 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
944 (context-coloring-elisp-colorize-parenthesized-sexp))
945 ((context-coloring-elisp-identifier-p syntax-code)
946 (context-coloring-elisp-colorize-symbol))
947 ((= syntax-code context-coloring-EXPRESSION-PREFIX-CODE)
948 (context-coloring-elisp-colorize-expression-prefix))
949 ((= syntax-code context-coloring-STRING-QUOTE-CODE)
950 (context-coloring-elisp-colorize-string))
951 ((= syntax-code context-coloring-ESCAPE-CODE)
952 (forward-char 2)))))
953
954 (defun context-coloring-elisp-colorize-comments-and-strings-in-region (start end)
955 "Color comments and strings between START and END."
956 (let (syntax-code)
957 (goto-char start)
958 (while (> end (progn (skip-syntax-forward "^\"<\\" end)
959 (point)))
960 (setq syntax-code (context-coloring-get-syntax-code))
961 (cond
962 ((= syntax-code context-coloring-STRING-QUOTE-CODE)
963 (context-coloring-elisp-colorize-string))
964 ((= syntax-code context-coloring-COMMENT-START-CODE)
965 (context-coloring-elisp-colorize-comment))
966 ((= syntax-code context-coloring-ESCAPE-CODE)
967 (forward-char 2))))))
968
969 (defun context-coloring-elisp-colorize-region (start end)
970 "Color everything between START and END."
971 (let (syntax-code)
972 (goto-char start)
973 (while (> end (progn (skip-syntax-forward "^w_('\"<\\" end)
974 (point)))
975 (setq syntax-code (context-coloring-get-syntax-code))
976 (cond
977 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
978 (context-coloring-elisp-colorize-parenthesized-sexp))
979 ((context-coloring-elisp-identifier-p syntax-code)
980 (context-coloring-elisp-colorize-symbol))
981 ((= syntax-code context-coloring-EXPRESSION-PREFIX-CODE)
982 (context-coloring-elisp-colorize-expression-prefix))
983 ((= syntax-code context-coloring-STRING-QUOTE-CODE)
984 (context-coloring-elisp-colorize-string))
985 ((= syntax-code context-coloring-COMMENT-START-CODE)
986 (context-coloring-elisp-colorize-comment))
987 ((= syntax-code context-coloring-ESCAPE-CODE)
988 (forward-char 2))))))
989
990 (defun context-coloring-elisp-colorize-region-initially (start end)
991 "Begin coloring everything between START and END."
992 (setq context-coloring-elisp-sexp-count 0)
993 (setq context-coloring-elisp-scope-stack '())
994 (let ((inhibit-point-motion-hooks t)
995 (case-fold-search nil)
996 ;; This is a recursive-descent parser, so give it a big stack.
997 (max-lisp-eval-depth (max max-lisp-eval-depth 3000))
998 (max-specpdl-size (max max-specpdl-size 3000)))
999 (context-coloring-elisp-colorize-region start end)))
1000
1001 (defun context-coloring-elisp-colorize-guard (callback)
1002 "Silently color in CALLBACK."
1003 (with-silent-modifications
1004 (save-excursion
1005 (condition-case nil
1006 (funcall callback)
1007 ;; Scan errors can happen virtually anywhere if parenthesis are
1008 ;; unbalanced. Just swallow them. (`progn' for test coverage.)
1009 (scan-error (progn))))))
1010
1011 (defun context-coloring-elisp-colorize ()
1012 "Color the current Emacs Lisp buffer."
1013 (interactive)
1014 (context-coloring-elisp-colorize-guard
1015 (lambda ()
1016 (cond
1017 ;; Just colorize the changed region.
1018 (context-coloring-changed-p
1019 (let* ( ;; Prevent `beginning-of-defun' from making poor assumptions.
1020 (open-paren-in-column-0-is-defun-start nil)
1021 ;; Seek the beginning and end of the previous and next
1022 ;; offscreen defuns, so just enough is colored.
1023 (start (progn (goto-char context-coloring-changed-start)
1024 (while (and (< (point-min) (point))
1025 (pos-visible-in-window-p))
1026 (end-of-line 0))
1027 (beginning-of-defun)
1028 (point)))
1029 (end (progn (goto-char context-coloring-changed-end)
1030 (while (and (> (point-max) (point))
1031 (pos-visible-in-window-p))
1032 (forward-line 1))
1033 (end-of-defun)
1034 (point))))
1035 (context-coloring-elisp-colorize-region-initially start end)
1036 ;; Fast coloring is nice, but if the code is not well-formed
1037 ;; (e.g. an unclosed string literal is parsed at any time) then
1038 ;; there could be leftover incorrectly-colored code offscreen. So
1039 ;; do a clean sweep as soon as appropriate.
1040 (context-coloring-schedule-coloring context-coloring-default-delay)))
1041 (t
1042 (context-coloring-elisp-colorize-region-initially (point-min) (point-max)))))))
1043
1044
1045 ;;; eval-expression colorization
1046
1047 (defun context-coloring-eval-expression-colorize ()
1048 "Color the `eval-expression' minibuffer prompt as elisp."
1049 (interactive)
1050 (context-coloring-elisp-colorize-guard
1051 (lambda ()
1052 (context-coloring-elisp-colorize-region-initially
1053 (progn
1054 (string-match "\\`Eval: " (buffer-string))
1055 (1+ (match-end 0)))
1056 (point-max)))))
1057
1058
1059 ;;; Dispatch
1060
1061 (defvar context-coloring-dispatch-hash-table (make-hash-table :test #'eq)
1062 "Map dispatch strategy names to their property lists.")
1063
1064 (defvar context-coloring-mode-hash-table (make-hash-table :test #'eq)
1065 "Map major mode names to dispatch property lists.")
1066
1067 (defvar context-coloring-dispatch-predicates '()
1068 "Functions which may return a dispatch.")
1069
1070 (defun context-coloring-get-current-dispatch ()
1071 "Return the first dispatch appropriate for the current state."
1072 (let ((predicates context-coloring-dispatch-predicates)
1073 (parent major-mode)
1074 dispatch)
1075 ;; Maybe a predicate will be satisfied and return a dispatch.
1076 (while (and predicates
1077 (not (setq dispatch (funcall (pop predicates))))))
1078 ;; If not, maybe a major mode (or a derivative) will define a dispatch.
1079 (when (not dispatch)
1080 (while (and parent
1081 (not (setq dispatch (gethash parent context-coloring-mode-hash-table)))
1082 (setq parent (get parent 'derived-mode-parent)))))
1083 dispatch))
1084
1085 (defun context-coloring-define-dispatch (symbol &rest properties)
1086 "Define a new dispatch named SYMBOL with PROPERTIES.
1087
1088 A \"dispatch\" is a property list describing a strategy for
1089 coloring a buffer.
1090
1091 PROPERTIES must include one of `:modes' or `:predicate', and a
1092 `:colorizer'.
1093
1094 `:modes' - List of major modes this dispatch is valid for.
1095
1096 `:predicate' - Function that determines if the dispatch is valid
1097 for any given state.
1098
1099 `:colorizer' - Function that parses and colors the buffer.
1100
1101 `:delay' - Delay between buffer update and colorization, to
1102 override `context-coloring-default-delay'.
1103
1104 `:setup' - Arbitrary code to set up this dispatch when
1105 `context-coloring-mode' is enabled.
1106
1107 `:teardown' - Arbitrary code to tear down this dispatch when
1108 `context-coloring-mode' is disabled."
1109 (let ((modes (plist-get properties :modes))
1110 (predicate (plist-get properties :predicate))
1111 (colorizer (plist-get properties :colorizer)))
1112 (when (null (or modes predicate))
1113 (error "No mode or predicate defined for dispatch"))
1114 (when (not colorizer)
1115 (error "No colorizer defined for dispatch"))
1116 (puthash symbol properties context-coloring-dispatch-hash-table)
1117 (dolist (mode modes)
1118 (puthash mode properties context-coloring-mode-hash-table))
1119 (when predicate
1120 (push (lambda ()
1121 (when (funcall predicate)
1122 properties)) context-coloring-dispatch-predicates))))
1123
1124 (defun context-coloring-dispatch ()
1125 "Determine how to color the current buffer, and color it."
1126 (let* ((dispatch (context-coloring-get-current-dispatch))
1127 (colorizer (plist-get dispatch :colorizer)))
1128 (catch 'interrupted
1129 (funcall colorizer))))
1130
1131
1132 ;;; Colorization
1133
1134 (defun context-coloring-colorize ()
1135 "Color the current buffer by function context."
1136 (interactive)
1137 (context-coloring-update-maximum-face)
1138 (context-coloring-dispatch))
1139
1140 (defun context-coloring-colorize-with-buffer (buffer)
1141 "Color BUFFER."
1142 ;; Don't select deleted buffers.
1143 (when (get-buffer buffer)
1144 (with-current-buffer buffer
1145 (context-coloring-colorize))))
1146
1147
1148 ;;; Built-in dispatches
1149
1150 (context-coloring-define-dispatch
1151 'javascript
1152 :modes '(js2-mode)
1153 :colorizer #'context-coloring-js2-colorize
1154 :setup
1155 (lambda ()
1156 (add-hook 'js2-post-parse-callbacks #'context-coloring-colorize nil t))
1157 :teardown
1158 (lambda ()
1159 (remove-hook 'js2-post-parse-callbacks #'context-coloring-colorize t)))
1160
1161 (context-coloring-define-dispatch
1162 'emacs-lisp
1163 :modes '(emacs-lisp-mode)
1164 :colorizer #'context-coloring-elisp-colorize
1165 :delay 0.016 ;; Thanks to lazy colorization this can be 60 frames per second.
1166 :setup #'context-coloring-setup-idle-change-detection
1167 :teardown #'context-coloring-teardown-idle-change-detection)
1168
1169 ;; `eval-expression-minibuffer-setup-hook' is not available in Emacs 24.3, so
1170 ;; the backwards-compatible recommendation is to use `minibuffer-setup-hook' and
1171 ;; rely on this predicate instead.
1172 (defun context-coloring-eval-expression-predicate ()
1173 "Non-nil if the minibuffer is for `eval-expression'."
1174 (eq this-command 'eval-expression))
1175
1176 (context-coloring-define-dispatch
1177 'eval-expression
1178 :predicate #'context-coloring-eval-expression-predicate
1179 :colorizer #'context-coloring-eval-expression-colorize
1180 :delay 0.016
1181 :setup #'context-coloring-setup-idle-change-detection
1182 :teardown #'context-coloring-teardown-idle-change-detection)
1183
1184
1185 ;;; Minor mode
1186
1187 ;;;###autoload
1188 (define-minor-mode context-coloring-mode
1189 "Toggle contextual code coloring.
1190 With a prefix argument ARG, enable Context Coloring mode if ARG
1191 is positive, and disable it otherwise. If called from Lisp,
1192 enable the mode if ARG is omitted or nil.
1193
1194 Context Coloring mode is a buffer-local minor mode. When
1195 enabled, code is colored by scope. Scopes are colored
1196 hierarchically. Variables referenced from nested scopes retain
1197 the color of their defining scopes. Certain syntax, like
1198 comments and strings, is still colored with `font-lock'.
1199
1200 The entire buffer is colored initially. Changes to the buffer
1201 trigger recoloring.
1202
1203 Define your own colors by customizing faces like
1204 `context-coloring-level-N-face', where N is a number starting
1205 from 0. If no face is found on a custom theme nor the `user'
1206 theme, the defaults are used.
1207
1208 New language / major mode support can be added with
1209 `context-coloring-define-dispatch', which see.
1210
1211 Feature inspired by Douglas Crockford."
1212 nil " Context" nil
1213 (cond
1214 (context-coloring-mode
1215 ;; Font lock is incompatible with this mode; the converse is also true.
1216 (font-lock-mode 0)
1217 (jit-lock-mode nil)
1218 ;; ...but we do use font-lock functions here.
1219 (font-lock-set-defaults)
1220 ;; Safely change the value of this function as necessary.
1221 (make-local-variable 'font-lock-syntactic-face-function)
1222 (let ((dispatch (context-coloring-get-current-dispatch)))
1223 (cond
1224 (dispatch
1225 (let ((setup (plist-get dispatch :setup)))
1226 (when setup
1227 (funcall setup))
1228 ;; Colorize once initially.
1229 (let ((context-coloring-parse-interruptable-p nil))
1230 (context-coloring-colorize))))
1231 (t
1232 (message "Context coloring is not available for this major mode")))))
1233 (t
1234 (let ((dispatch (context-coloring-get-current-dispatch)))
1235 (when dispatch
1236 (let ((teardown (plist-get dispatch :teardown)))
1237 (when teardown
1238 (funcall teardown)))))
1239 (font-lock-mode)
1240 (jit-lock-mode t))))
1241
1242 (provide 'context-coloring)
1243
1244 ;;; context-coloring.el ends here