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