]> code.delx.au - gnu-emacs-elpa/blob - context-coloring.el
Pass let and let* tests with recursive colorizer.
[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.3.0
7 ;; Keywords: convenience faces tools
8 ;; Package-Requires: ((emacs "24") (js2-mode "20150126"))
9 ;; URL: https://github.com/jacksonrayhamilton/context-coloring
10
11 ;; This file is part of GNU Emacs.
12
13 ;; This program is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; Highlights code by scope. Top-level scopes are one color, second-level
29 ;; scopes are another color, and so on. Variables retain the color of the scope
30 ;; in which they are defined. A variable defined in an outer scope referenced
31 ;; by an inner scope is colored the same as the outer scope.
32
33 ;; By default, comments and strings are still highlighted syntactically.
34
35 ;;; Code:
36
37 (require 'js2-mode)
38
39
40 ;;; Utilities
41
42 (defun context-coloring-join (strings delimiter)
43 "Join a list of STRINGS with the string DELIMITER."
44 (mapconcat 'identity strings delimiter))
45
46 (defsubst context-coloring-trim-right (string)
47 "Remove leading whitespace from STRING."
48 (if (string-match "[ \t\n\r]+\\'" string)
49 (replace-match "" t t string)
50 string))
51
52 (defsubst context-coloring-trim-left (string)
53 "Remove trailing whitespace from STRING."
54 (if (string-match "\\`[ \t\n\r]+" string)
55 (replace-match "" t t string)
56 string))
57
58 (defsubst context-coloring-trim (string)
59 "Remove leading and trailing whitespace from STRING."
60 (context-coloring-trim-left (context-coloring-trim-right string)))
61
62
63 ;;; Faces
64
65 (defun context-coloring-defface (level tty light dark)
66 "Define a face for LEVEL with colors for TTY, LIGHT and DARK
67 backgrounds."
68 (let ((face (intern (format "context-coloring-level-%s-face" level)))
69 (doc (format "Context coloring face, level %s." level)))
70 (custom-declare-face
71 face
72 `((((type tty)) (:foreground ,tty))
73 (((background light)) (:foreground ,light))
74 (((background dark)) (:foreground ,dark)))
75 doc
76 :group 'context-coloring)))
77
78 (defun context-coloring-defface-neutral (level)
79 "Define a face for LEVEL with the default neutral colors."
80 (context-coloring-defface level nil "#3f3f3f" "#cdcdcd"))
81
82 (context-coloring-defface 0 nil "#000000" "#ffffff")
83 (context-coloring-defface 1 "yellow" "#008b8b" "#00ffff")
84 (context-coloring-defface 2 "green" "#0000ff" "#87cefa")
85 (context-coloring-defface 3 "cyan" "#483d8b" "#b0c4de")
86 (context-coloring-defface 4 "blue" "#a020f0" "#eedd82")
87 (context-coloring-defface 5 "magenta" "#a0522d" "#98fb98")
88 (context-coloring-defface 6 "red" "#228b22" "#7fffd4")
89 (context-coloring-defface-neutral 7)
90
91 (defvar context-coloring-maximum-face nil
92 "Index of the highest face available for coloring.")
93
94 (defvar context-coloring-original-maximum-face nil
95 "Fallback value for `context-coloring-maximum-face' when all
96 themes have been disabled.")
97
98 (setq context-coloring-maximum-face 7)
99
100 (setq context-coloring-original-maximum-face
101 context-coloring-maximum-face)
102
103 ;; Theme authors can have up to 26 levels: 1 (0th) for globals, 24 (1st-24th)
104 ;; for nested levels, and 1 (25th) for infinity.
105 (dotimes (number 18)
106 (context-coloring-defface-neutral (+ number context-coloring-maximum-face 1)))
107
108
109 ;;; Face functions
110
111 (defsubst context-coloring-level-face (level)
112 "Return the symbol for a face with LEVEL."
113 ;; `concat' is faster than `format' here.
114 (intern-soft
115 (concat "context-coloring-level-" (number-to-string level) "-face")))
116
117 (defsubst context-coloring-bounded-level-face (level)
118 "Return the symbol for a face with LEVEL, bounded by
119 `context-coloring-maximum-face'."
120 (context-coloring-level-face (min level context-coloring-maximum-face)))
121
122
123 ;;; Colorization utilities
124
125 (defsubst context-coloring-colorize-region (start end level)
126 "Color characters from the 1-indexed START point (inclusive) to
127 the END point (exclusive) with the face corresponding to LEVEL."
128 (add-text-properties
129 start
130 end
131 `(face ,(context-coloring-bounded-level-face level))))
132
133 (make-obsolete-variable
134 'context-coloring-comments-and-strings
135 "use `context-coloring-syntactic-comments' and
136 `context-coloring-syntactic-strings' instead."
137 "6.1.0")
138
139 (defcustom context-coloring-syntactic-comments t
140 "If non-nil, also color comments using `font-lock'."
141 :group 'context-coloring)
142
143 (defcustom context-coloring-syntactic-strings t
144 "If non-nil, also color strings using `font-lock'."
145 :group 'context-coloring)
146
147 (defun context-coloring-font-lock-syntactic-comment-function (state)
148 "Tell `font-lock' to color a comment but not a string."
149 (if (nth 3 state) nil font-lock-comment-face))
150
151 (defun context-coloring-font-lock-syntactic-string-function (state)
152 "Tell `font-lock' to color a string but not a comment."
153 (if (nth 3 state) font-lock-string-face nil))
154
155 (defsubst context-coloring-maybe-colorize-comments-and-strings (&optional min max)
156 "Color the current buffer's comments or strings if
157 `context-coloring-syntactic-comments' or
158 `context-coloring-syntactic-strings' are non-nil."
159 (when (or context-coloring-syntactic-comments
160 context-coloring-syntactic-strings)
161 (let ((min (or min (point-min)))
162 (max (or max (point-max)))
163 (font-lock-syntactic-face-function
164 (cond
165 ((and context-coloring-syntactic-comments
166 (not context-coloring-syntactic-strings))
167 'context-coloring-font-lock-syntactic-comment-function)
168 ((and context-coloring-syntactic-strings
169 (not context-coloring-syntactic-comments))
170 'context-coloring-font-lock-syntactic-string-function)
171 (t
172 font-lock-syntactic-face-function))))
173 (save-excursion
174 (font-lock-fontify-syntactically-region min max)
175 ;; TODO: Make configurable at the dispatch level.
176 (when (eq major-mode 'emacs-lisp-mode)
177 (font-lock-fontify-keywords-region min max))))))
178
179
180 ;;; js2-mode colorization
181
182 (defvar-local context-coloring-js2-scope-level-hash-table nil
183 "Associate `js2-scope' structures and with their scope
184 levels.")
185
186 (defcustom context-coloring-js-block-scopes nil
187 "If non-nil, also color block scopes in the scope hierarchy in JavaScript.
188
189 The block-scoped `let' and `const' are introduced in ES6. Enable
190 this for ES6 code; disable it elsewhere.
191
192 Supported modes: `js2-mode'"
193 :group 'context-coloring)
194
195 (defsubst context-coloring-js2-scope-level (scope)
196 "Return the level of SCOPE."
197 (cond ((gethash scope context-coloring-js2-scope-level-hash-table))
198 (t
199 (let ((level 0)
200 (current-scope scope)
201 enclosing-scope)
202 (while (and current-scope
203 (js2-node-parent current-scope)
204 (setq enclosing-scope
205 (js2-node-get-enclosing-scope current-scope)))
206 (when (or context-coloring-js-block-scopes
207 (let ((type (js2-scope-type current-scope)))
208 (or (= type js2-SCRIPT)
209 (= type js2-FUNCTION)
210 (= type js2-CATCH))))
211 (setq level (+ level 1)))
212 (setq current-scope enclosing-scope))
213 (puthash scope level context-coloring-js2-scope-level-hash-table)))))
214
215 (defsubst context-coloring-js2-local-name-node-p (node)
216 "Determine if NODE is a `js2-name-node' representing a local
217 variable."
218 (and (js2-name-node-p node)
219 (let ((parent (js2-node-parent node)))
220 (not (or (and (js2-object-prop-node-p parent)
221 (eq node (js2-object-prop-node-left parent)))
222 (and (js2-prop-get-node-p parent)
223 ;; For nested property lookup, the node on the left is a
224 ;; `js2-prop-get-node', so this always works.
225 (eq node (js2-prop-get-node-right parent))))))))
226
227 (defvar-local context-coloring-point-max nil
228 "Cached value of `point-max'.")
229
230 (defsubst context-coloring-js2-colorize-node (node level)
231 "Color NODE with the color for LEVEL."
232 (let ((start (js2-node-abs-pos node)))
233 (context-coloring-colorize-region
234 start
235 (min
236 ;; End
237 (+ start (js2-node-len node))
238 ;; Somes nodes (like the ast when there is an unterminated multiline
239 ;; comment) will stretch to the value of `point-max'.
240 context-coloring-point-max)
241 level)))
242
243 (defun context-coloring-js2-colorize ()
244 "Color the current buffer using the abstract syntax tree
245 generated by `js2-mode'."
246 ;; Reset the hash table; the old one could be obsolete.
247 (setq context-coloring-js2-scope-level-hash-table (make-hash-table :test 'eq))
248 (setq context-coloring-point-max (point-max))
249 (with-silent-modifications
250 (js2-visit-ast
251 js2-mode-ast
252 (lambda (node end-p)
253 (when (null end-p)
254 (cond
255 ((js2-scope-p node)
256 (context-coloring-js2-colorize-node
257 node
258 (context-coloring-js2-scope-level node)))
259 ((context-coloring-js2-local-name-node-p node)
260 (let* ((enclosing-scope (js2-node-get-enclosing-scope node))
261 (defining-scope (js2-get-defining-scope
262 enclosing-scope
263 (js2-name-node-name node))))
264 ;; The tree seems to be walked lexically, so an entire scope will
265 ;; be colored, including its name nodes, before they are reached.
266 ;; Coloring the nodes defined in that scope would be redundant, so
267 ;; don't do it.
268 (when (not (eq defining-scope enclosing-scope))
269 (context-coloring-js2-colorize-node
270 node
271 (context-coloring-js2-scope-level defining-scope))))))
272 ;; The `t' indicates to search children.
273 t)))
274 (context-coloring-maybe-colorize-comments-and-strings)))
275
276
277 ;;; Emacs Lisp colorization
278
279 (defsubst context-coloring-forward-sws ()
280 "Move forward through whitespace and comments."
281 (while (forward-comment 1)))
282
283 (defsubst context-coloring-forward-sexp-position ()
284 "Like vanilla `forward-sexp', but just return the position."
285 (scan-sexps (point) 1))
286
287 (defsubst context-coloring-elisp-identifier-syntax-p (syntax-code)
288 (or (= 2 syntax-code)
289 (= 3 syntax-code)))
290
291 (defsubst context-coloring-open-parenthesis-p (syntax-code)
292 (= 4 syntax-code))
293
294 (defsubst context-coloring-close-parenthesis-p (syntax-code)
295 (= 5 syntax-code))
296
297 (defsubst context-coloring-expression-prefix-p (syntax-code)
298 (= 6 syntax-code))
299
300 (defsubst context-coloring-at-open-parenthesis-p ()
301 (= 4 (logand #xFFFF (car (syntax-after (point))))))
302
303 (defsubst context-coloring-ppss-depth (ppss)
304 ;; Same as (nth 0 ppss).
305 (car ppss))
306
307 (defsubst context-coloring-at-stack-depth-p (stack depth)
308 (= (plist-get (car stack) :depth) depth))
309
310 (defsubst context-coloring-exact-regexp (word)
311 "Create a regexp that matches exactly WORD."
312 (concat "\\`" (regexp-quote word) "\\'"))
313
314 (defsubst context-coloring-exact-or-regexp (words)
315 "Create a regexp that matches any exact word in WORDS."
316 (context-coloring-join
317 (mapcar 'context-coloring-exact-regexp words) "\\|"))
318
319 (defconst context-coloring-elisp-defun-regexp
320 (context-coloring-exact-or-regexp
321 '("defun" "defun*" "defsubst" "defmacro"
322 "cl-defun" "cl-defsubst" "cl-defmacro")))
323
324 (defconst context-coloring-elisp-lambda-regexp
325 (context-coloring-exact-regexp "lambda"))
326
327 (defconst context-coloring-elisp-let-regexp
328 (context-coloring-exact-regexp "let"))
329
330 (defconst context-coloring-elisp-let*-regexp
331 (context-coloring-exact-regexp "let*"))
332
333 (defconst context-coloring-elisp-arglist-arg-regexp
334 "\\`[^&:]")
335
336 (defconst context-coloring-ignored-word-regexp
337 (context-coloring-join (list "\\`[-+]?[0-9]"
338 "\\`[&:].+"
339 (context-coloring-exact-or-regexp
340 '("t" "nil" "." "?")))
341 "\\|"))
342
343 (defconst context-coloring-WORD-CODE 2)
344 (defconst context-coloring-SYMBOL-CODE 3)
345 (defconst context-coloring-OPEN-PARENTHESIS-CODE 4)
346 (defconst context-coloring-CLOSE-PARENTHESIS-CODE 5)
347 (defconst context-coloring-EXPRESSION-PREFIX-CODE 6)
348
349 (defconst context-coloring-APOSTROPHE-CHAR (string-to-char "'"))
350 (defconst context-coloring-OPEN-PARENTHESIS-CHAR (string-to-char "("))
351 (defconst context-coloring-COMMA-CHAR (string-to-char ","))
352 (defconst context-coloring-BACKTICK-CHAR (string-to-char "`"))
353
354 (defvar context-coloring-parse-interruptable-p t
355 "Set this to nil to force parse to continue until finished.")
356
357 (defconst context-coloring-elisp-iterations-per-pause 1000
358 "Pause after this many iterations to check for user input.
359 If user input is pending, stop the parse. This makes for a
360 smoother user experience for large files.
361
362 As of this writing, emacs lisp colorization seems to run at about
363 60,000 iterations per second. A default value of 1000 should
364 provide visually \"instant\" updates at 60 frames per second.")
365
366 (defun context-coloring-get-syntax-code ()
367 (syntax-class (syntax-after (point))))
368
369 (defvar context-coloring-elisp-scope-stack '())
370
371 (defsubst context-coloring-elisp-make-scope (level)
372 (list
373 :level level
374 :variables (make-hash-table :test 'equal)))
375
376 (defsubst context-coloring-elisp-scope-get-level (scope)
377 (plist-get scope :level))
378
379 (defsubst context-coloring-elisp-scope-add-variable (scope variable)
380 (puthash variable t (plist-get scope :variables)))
381
382 (defsubst context-coloring-elisp-scope-get-variable (scope variable)
383 (gethash variable (plist-get scope :variables)))
384
385 (defsubst context-coloring-elisp-get-variable-level (variable)
386 (let* ((scope-stack context-coloring-elisp-scope-stack)
387 scope
388 level)
389 (while (and scope-stack (not level))
390 (setq scope (car scope-stack))
391 (cond
392 ((context-coloring-elisp-scope-get-variable scope variable)
393 (setq level (context-coloring-elisp-scope-get-level scope)))
394 (t
395 (setq scope-stack (cdr scope-stack)))))
396 ;; Assume a global variable.
397 (or level 0)))
398
399 (defun context-coloring-elisp-push-scope ()
400 (push (context-coloring-elisp-make-scope
401 (1+ (context-coloring-elisp-current-scope-level)))
402 context-coloring-elisp-scope-stack))
403
404 (defun context-coloring-elisp-pop-scope ()
405 (pop context-coloring-elisp-scope-stack))
406
407 (defun context-coloring-elisp-add-variable (variable)
408 (let ((current-scope (car context-coloring-elisp-scope-stack)))
409 (context-coloring-elisp-scope-add-variable current-scope variable)))
410
411 (defun context-coloring-elisp-current-scope-level ()
412 (let ((current-scope (car context-coloring-elisp-scope-stack)))
413 (cond
414 (current-scope
415 (context-coloring-elisp-scope-get-level current-scope))
416 (t
417 0))))
418
419 (defsubst context-coloring-elisp-make-let-varlist (type)
420 (list
421 :type type
422 :vars '()))
423
424 (defsubst context-coloring-elisp-let-varlist-get-type (let-varlist)
425 (plist-get let-varlist :type))
426
427 (defsubst context-coloring-elisp-let-varlist-get-vars (let-varlist)
428 (plist-get let-varlist :vars))
429
430 (defsubst context-coloring-elisp-let-varlist-set-vars (let-varlist vars)
431 (plist-put let-varlist :vars vars))
432
433 (defsubst context-coloring-elisp-let-varlist-add-var (let-varlist var)
434 (plist-put let-varlist :vars (cons var (plist-get let-varlist :vars))))
435
436 (defsubst context-coloring-elisp-let-varlist-pop-vars (let-varlist)
437 (let* ((type (context-coloring-elisp-let-varlist-get-type let-varlist))
438 (vars (context-coloring-elisp-let-varlist-get-vars let-varlist))
439 (popped (cond
440 ;; `let' binds all at once at the end.
441 ((eq type 'let)
442 (prog1
443 vars
444 (context-coloring-elisp-let-varlist-set-vars
445 let-varlist '())))
446 ;; `let*' binds incrementally.
447 ((eq type 'let*)
448 (prog1
449 (list (car vars))
450 (context-coloring-elisp-let-varlist-set-vars
451 let-varlist (cdr vars)))))))
452 (while popped
453 (context-coloring-elisp-add-variable (car popped))
454 (setq popped (cdr popped)))))
455
456 (defun context-coloring-elisp-parse-arg (callback)
457 (let (arg-pos
458 arg-end
459 arg-string)
460 (setq arg-pos (point))
461 (forward-sexp)
462 (setq arg-end (point))
463 (setq arg-string (buffer-substring-no-properties
464 arg-pos
465 arg-end))
466 (when (string-match-p
467 context-coloring-elisp-arglist-arg-regexp
468 arg-string)
469 (funcall callback arg-string))))
470
471 (defun context-coloring-elisp-parse-let-varlist (type)
472 (let ((let-varlist (context-coloring-elisp-make-let-varlist type))
473 syntax-code)
474 ;; Enter.
475 (forward-char)
476 (while (/= (progn
477 (setq syntax-code (context-coloring-get-syntax-code))
478 syntax-code)
479 context-coloring-CLOSE-PARENTHESIS-CODE)
480 (cond
481 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
482 (forward-char)
483 (context-coloring-forward-sws)
484 (setq syntax-code (context-coloring-get-syntax-code))
485 (when (or (= syntax-code context-coloring-WORD-CODE)
486 (= syntax-code context-coloring-SYMBOL-CODE))
487 (context-coloring-elisp-parse-arg
488 (lambda (var)
489 (context-coloring-elisp-let-varlist-add-var let-varlist var)))
490 (context-coloring-forward-sws)
491 (setq syntax-code (context-coloring-get-syntax-code))
492 (when (/= syntax-code context-coloring-CLOSE-PARENTHESIS-CODE)
493 (context-coloring-elisp-colorize-sexp)))
494 (context-coloring-forward-sws)
495 ;; Skip past the closing parenthesis.
496 (forward-char))
497 ((or (= syntax-code context-coloring-WORD-CODE)
498 (= syntax-code context-coloring-SYMBOL-CODE))
499 (context-coloring-elisp-parse-arg
500 (lambda (var)
501 (context-coloring-elisp-let-varlist-add-var let-varlist var)))))
502 (when (eq type 'let*)
503 (context-coloring-elisp-let-varlist-pop-vars let-varlist))
504 (context-coloring-forward-sws))
505 (when (eq type 'let)
506 (context-coloring-elisp-let-varlist-pop-vars let-varlist))
507 ;; Exit.
508 (forward-char)))
509
510 (defun context-coloring-elisp-parse-arglist ()
511 (let (syntax-code)
512 ;; Enter.
513 (forward-char)
514 (while (/= (progn
515 (setq syntax-code (context-coloring-get-syntax-code))
516 syntax-code)
517 context-coloring-CLOSE-PARENTHESIS-CODE)
518 (cond
519 ((or (= syntax-code context-coloring-WORD-CODE)
520 (= syntax-code context-coloring-SYMBOL-CODE))
521 (context-coloring-elisp-parse-arg
522 (lambda (arg)
523 (context-coloring-elisp-add-variable arg))))
524 (t
525 (forward-sexp)))
526 (context-coloring-forward-sws))
527 ;; Exit.
528 (forward-char)))
529
530 (defun context-coloring-elisp-colorize-defun (&optional anonymous-p
531 let-type)
532 (let ((start (point))
533 end
534 stop
535 syntax-code
536 defun-name-pos
537 defun-name-end)
538 (context-coloring-elisp-push-scope)
539 ;; Color the whole sexp.
540 (forward-sexp)
541 (setq end (point))
542 (context-coloring-colorize-region
543 start
544 end
545 (context-coloring-elisp-current-scope-level))
546 (goto-char start)
547 ;; Skip past the "defun".
548 (skip-syntax-forward "^w_")
549 (forward-sexp)
550 (context-coloring-forward-sws)
551 (setq stop nil)
552 (unless anonymous-p
553 ;; Check for the defun's name.
554 (setq syntax-code (context-coloring-get-syntax-code))
555 (cond
556 ((or (= syntax-code context-coloring-WORD-CODE)
557 (= syntax-code context-coloring-SYMBOL-CODE))
558 ;; Color the defun's name with the top-level color.
559 (setq defun-name-pos (point))
560 (forward-sexp)
561 (setq defun-name-end (point))
562 (context-coloring-colorize-region defun-name-pos defun-name-end 0)
563 (context-coloring-forward-sws))
564 (t
565 (setq stop t))))
566 (cond
567 (stop
568 ;; Skip it.
569 (goto-char start)
570 (forward-sexp))
571 (t
572 (setq syntax-code (context-coloring-get-syntax-code))
573 (cond
574 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
575 (cond
576 (let-type
577 (context-coloring-elisp-parse-let-varlist let-type))
578 (t
579 (context-coloring-elisp-parse-arglist)))
580 ;; Colorize the rest of the function.
581 (context-coloring-elisp-colorize-region (point) (1- end))
582 ;; Exit the defun.
583 (forward-char))
584 (t
585 ;; Skip it.
586 (goto-char start)
587 (forward-sexp)))))
588 (context-coloring-elisp-pop-scope)))
589
590 (defun context-coloring-elisp-colorize-lambda ()
591 (context-coloring-elisp-colorize-defun t))
592
593 (defun context-coloring-elisp-colorize-let ()
594 (context-coloring-elisp-colorize-defun t 'let))
595
596 (defun context-coloring-elisp-colorize-let* ()
597 (context-coloring-elisp-colorize-defun t 'let*))
598
599 (defun context-coloring-elisp-colorize-parenthesized-sexp ()
600 (let ((start (point))
601 end
602 syntax-code
603 child-0-pos
604 child-0-end
605 child-0-string)
606 (forward-sexp)
607 (setq end (point))
608 (goto-char start)
609 (forward-char)
610 (context-coloring-forward-sws)
611 (setq syntax-code (context-coloring-get-syntax-code))
612 ;; Figure out if the sexp is a special form.
613 (cond
614 ((or (= syntax-code context-coloring-WORD-CODE)
615 (= syntax-code context-coloring-SYMBOL-CODE))
616 (setq child-0-pos (point))
617 (forward-sexp)
618 (setq child-0-end (point))
619 (setq child-0-string (buffer-substring-no-properties
620 child-0-pos
621 child-0-end))
622 (cond
623 ((string-match-p context-coloring-elisp-defun-regexp child-0-string)
624 (goto-char start)
625 (context-coloring-elisp-colorize-defun))
626 ((string-match-p context-coloring-elisp-lambda-regexp child-0-string)
627 (goto-char start)
628 (context-coloring-elisp-colorize-lambda))
629 ((string-match-p context-coloring-elisp-let-regexp child-0-string)
630 (goto-char start)
631 (context-coloring-elisp-colorize-let))
632 ((string-match-p context-coloring-elisp-let*-regexp child-0-string)
633 (goto-char start)
634 (context-coloring-elisp-colorize-let*))
635 ;; Not a special form; just colorize the remaining region.
636 (t
637 (context-coloring-colorize-region
638 start
639 end
640 (context-coloring-elisp-current-scope-level))
641 (context-coloring-elisp-colorize-region (point) (1- end))
642 (forward-char))))
643 (t
644 ;; Skip it.
645 (goto-char start)
646 (forward-sexp)))))
647
648 (defun context-coloring-elisp-colorize-symbol ()
649 (let (symbol-pos
650 symbol-end
651 symbol-string)
652 (setq symbol-pos (point))
653 (forward-sexp)
654 (setq symbol-end (point))
655 (setq symbol-string (buffer-substring-no-properties
656 symbol-pos
657 symbol-end))
658 (cond
659 ((string-match-p context-coloring-ignored-word-regexp symbol-string))
660 (t
661 (context-coloring-colorize-region
662 symbol-pos
663 symbol-end
664 (context-coloring-elisp-get-variable-level
665 symbol-string))))))
666
667 (defun context-coloring-elisp-colorize-expression-prefix ()
668 (let (start
669 end
670 char)
671 (setq char (char-after))
672 (cond
673 ((= char context-coloring-APOSTROPHE-CHAR)
674 (forward-sexp))
675 ((= char context-coloring-BACKTICK-CHAR)
676 (setq start (point))
677 (forward-sexp)
678 (setq end (point))
679 (goto-char start)
680 (while (> end (progn (forward-char)
681 (point)))
682 (setq char (char-after))
683 (when (= char context-coloring-COMMA-CHAR)
684 (forward-char)
685 (context-coloring-forward-sws)
686 (context-coloring-elisp-colorize-sexp)))))))
687
688 (defun context-coloring-elisp-colorize-sexp ()
689 (let (syntax-code)
690 (setq syntax-code (context-coloring-get-syntax-code))
691 (cond
692 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
693 (context-coloring-elisp-colorize-parenthesized-sexp))
694 ((or (= syntax-code context-coloring-WORD-CODE)
695 (= syntax-code context-coloring-SYMBOL-CODE))
696 (context-coloring-elisp-colorize-symbol))
697 ((= syntax-code context-coloring-EXPRESSION-PREFIX-CODE)
698 (context-coloring-elisp-colorize-expression-prefix))
699 (t
700 (forward-char)))))
701
702 (defun context-coloring-elisp-colorize-region (start end)
703 (let (syntax-code)
704 (goto-char start)
705 (while (> end (progn (skip-syntax-forward "^()w_'" end)
706 (point)))
707 (setq syntax-code (context-coloring-get-syntax-code))
708 (cond
709 ((or (= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
710 (= syntax-code context-coloring-WORD-CODE)
711 (= syntax-code context-coloring-SYMBOL-CODE)
712 (= syntax-code context-coloring-EXPRESSION-PREFIX-CODE))
713 (context-coloring-elisp-colorize-sexp))
714 (t
715 (forward-char))))))
716
717 (defun context-coloring-elisp-colorize-changed-region (start end)
718 (with-silent-modifications
719 (save-excursion
720 (let ((start (progn (goto-char start)
721 (beginning-of-defun)
722 (point)))
723 (end (progn (goto-char end)
724 (end-of-defun)
725 (point))))
726 (setq context-coloring-elisp-scope-stack '())
727 (context-coloring-elisp-colorize-region start end)))))
728
729 (defun context-coloring-elisp-colorize-buffer ()
730 (interactive)
731 (with-silent-modifications
732 (save-excursion
733 (setq context-coloring-elisp-scope-stack '())
734 (context-coloring-elisp-colorize-region (point-min) (point-max)))))
735
736 (defalias 'ccecb 'context-coloring-elisp-colorize-buffer)
737
738
739 ;;; Shell command scopification / colorization
740
741 (defun context-coloring-apply-tokens (tokens)
742 "Process a vector of TOKENS to apply context-based coloring to
743 the current buffer. Tokens are 3 integers: start, end, level.
744 The vector is flat, with a new token occurring after every 3rd
745 element."
746 (with-silent-modifications
747 (let ((i 0)
748 (len (length tokens)))
749 (while (< i len)
750 (context-coloring-colorize-region
751 (elt tokens i)
752 (elt tokens (+ i 1))
753 (elt tokens (+ i 2)))
754 (setq i (+ i 3))))
755 (context-coloring-maybe-colorize-comments-and-strings)))
756
757 (defun context-coloring-parse-array (array)
758 "Parse ARRAY as a flat JSON array of numbers."
759 (let ((braceless (substring (context-coloring-trim array) 1 -1)))
760 (cond
761 ((> (length braceless) 0)
762 (vconcat
763 (mapcar 'string-to-number (split-string braceless ","))))
764 (t
765 (vector)))))
766
767 (defvar-local context-coloring-scopifier-process nil
768 "The single scopifier process that can be running.")
769
770 (defun context-coloring-kill-scopifier ()
771 "Kill the currently-running scopifier process."
772 (when (not (null context-coloring-scopifier-process))
773 (delete-process context-coloring-scopifier-process)
774 (setq context-coloring-scopifier-process nil)))
775
776 (defun context-coloring-scopify-shell-command (command callback)
777 "Invoke a scopifier via COMMAND, read its response
778 asynchronously and invoke CALLBACK with its output."
779
780 ;; Prior running tokenization is implicitly obsolete if this function is
781 ;; called.
782 (context-coloring-kill-scopifier)
783
784 ;; Start the process.
785 (setq context-coloring-scopifier-process
786 (start-process-shell-command "scopifier" nil command))
787
788 (let ((output ""))
789
790 ;; The process may produce output in multiple chunks. This filter
791 ;; accumulates the chunks into a message.
792 (set-process-filter
793 context-coloring-scopifier-process
794 (lambda (_process chunk)
795 (setq output (concat output chunk))))
796
797 ;; When the process's message is complete, this sentinel parses it as JSON
798 ;; and applies the tokens to the buffer.
799 (set-process-sentinel
800 context-coloring-scopifier-process
801 (lambda (_process event)
802 (when (equal "finished\n" event)
803 (funcall callback output))))))
804
805 (defun context-coloring-send-buffer-to-scopifier ()
806 "Give the scopifier process its input so it can begin
807 scopifying."
808 (process-send-region
809 context-coloring-scopifier-process
810 (point-min) (point-max))
811 (process-send-eof
812 context-coloring-scopifier-process))
813
814 (defun context-coloring-scopify-and-colorize (command &optional callback)
815 "Invoke a scopifier via COMMAND with the current buffer's contents,
816 read the scopifier's response asynchronously and apply a parsed
817 list of tokens to `context-coloring-apply-tokens'.
818
819 Invoke CALLBACK when complete."
820 (let ((buffer (current-buffer)))
821 (context-coloring-scopify-shell-command
822 command
823 (lambda (output)
824 (let ((tokens (context-coloring-parse-array output)))
825 (with-current-buffer buffer
826 (context-coloring-apply-tokens tokens))
827 (setq context-coloring-scopifier-process nil)
828 (when callback (funcall callback))))))
829 (context-coloring-send-buffer-to-scopifier))
830
831
832 ;;; Dispatch
833
834 (defvar context-coloring-dispatch-hash-table (make-hash-table :test 'eq)
835 "Map dispatch strategy names to their corresponding property
836 lists, which contain details about the strategies.")
837
838 (defvar context-coloring-mode-hash-table (make-hash-table :test 'eq)
839 "Map major mode names to dispatch property lists.")
840
841 (defun context-coloring-get-dispatch-for-mode (mode)
842 "Return the dispatch for MODE (or a derivative mode)."
843 (let ((parent mode)
844 dispatch)
845 (while (and parent
846 (not (setq dispatch (gethash parent context-coloring-mode-hash-table)))
847 (setq parent (get parent 'derived-mode-parent))))
848 dispatch))
849
850 (defun context-coloring-define-dispatch (symbol &rest properties)
851 "Define a new dispatch named SYMBOL with PROPERTIES.
852
853 A \"dispatch\" is a property list describing a strategy for
854 coloring a buffer. There are three possible strategies: Parse
855 and color in a single function (`:colorizer'), parse in a
856 function that returns scope data (`:scopifier'), or parse with a
857 shell command that returns scope data (`:command'). In the
858 latter two cases, the scope data will be used to automatically
859 color the buffer.
860
861 PROPERTIES must include `:modes' and one of `:colorizer',
862 `:scopifier' or `:command'.
863
864 `:modes' - List of major modes this dispatch is valid for.
865
866 `:colorizer' - Symbol referring to a function that parses and
867 colors the buffer.
868
869 `:scopifier' - Symbol referring to a function that parses the
870 buffer a returns a flat vector of start, end and level data.
871
872 `:executable' - Optional name of an executable required by
873 `:command'.
874
875 `:command' - Shell command to execute with the current buffer
876 sent via stdin, and with a flat JSON array of start, end and
877 level data returned via stdout.
878
879 `:version' - Minimum required version that should be printed when
880 executing `:command' with a \"--version\" flag. The version
881 should be numeric, e.g. \"2\", \"19700101\", \"1.2.3\",
882 \"v1.2.3\" etc.
883
884 `:setup' - Arbitrary code to set up this dispatch when
885 `context-coloring-mode' is enabled.
886
887 `:teardown' - Arbitrary code to tear down this dispatch when
888 `context-coloring-mode' is disabled."
889 (let ((modes (plist-get properties :modes))
890 (colorizer (plist-get properties :colorizer))
891 (scopifier (plist-get properties :scopifier))
892 (command (plist-get properties :command)))
893 (when (null modes)
894 (error "No mode defined for dispatch"))
895 (when (not (or colorizer
896 scopifier
897 command))
898 (error "No colorizer, scopifier or command defined for dispatch"))
899 (puthash symbol properties context-coloring-dispatch-hash-table)
900 (dolist (mode modes)
901 (puthash mode properties context-coloring-mode-hash-table))))
902
903
904 ;;; Colorization
905
906 (defvar context-coloring-colorize-hook nil
907 "Hooks to run after coloring a buffer.")
908
909 (defun context-coloring-colorize (&optional callback)
910 "Color the current buffer by function context.
911
912 Invoke CALLBACK when complete; see `context-coloring-dispatch'."
913 (interactive)
914 (context-coloring-dispatch
915 (lambda ()
916 (when callback (funcall callback))
917 (run-hooks 'context-coloring-colorize-hook))))
918
919 (defvar-local context-coloring-changed nil
920 "Indication that the buffer has changed recently, which implies
921 that it should be colored again by
922 `context-coloring-colorize-idle-timer' if that timer is being
923 used.")
924
925 (defun context-coloring-change-function (_start _end _length)
926 "Register a change so that a buffer can be colorized soon."
927 ;; Tokenization is obsolete if there was a change.
928 (context-coloring-kill-scopifier)
929 (setq context-coloring-changed t))
930
931 (defun context-coloring-maybe-colorize (buffer)
932 "Colorize the current buffer if it has changed."
933 (when (and (eq buffer (current-buffer))
934 context-coloring-changed)
935 (setq context-coloring-changed nil)
936 (context-coloring-colorize)))
937
938
939 ;;; Versioning
940
941 (defun context-coloring-parse-version (string)
942 "Extract segments of a version STRING into a list. \"v1.0.0\"
943 produces (1 0 0), \"19700101\" produces (19700101), etc."
944 (let (version)
945 (while (string-match "[0-9]+" string)
946 (setq version (append version
947 (list (string-to-number (match-string 0 string)))))
948 (setq string (substring string (match-end 0))))
949 version))
950
951 (defun context-coloring-check-version (expected actual)
952 "Check that version EXPECTED is less than or equal to ACTUAL."
953 (let ((expected (context-coloring-parse-version expected))
954 (actual (context-coloring-parse-version actual))
955 (continue t)
956 (acceptable t))
957 (while (and continue expected)
958 (let ((an-expected (car expected))
959 (an-actual (car actual)))
960 (cond
961 ((> an-actual an-expected)
962 (setq acceptable t)
963 (setq continue nil))
964 ((< an-actual an-expected)
965 (setq acceptable nil)
966 (setq continue nil))))
967 (setq expected (cdr expected))
968 (setq actual (cdr actual)))
969 acceptable))
970
971 (defvar context-coloring-check-scopifier-version-hook nil
972 "Hooks to run after checking the scopifier version.")
973
974 (defun context-coloring-check-scopifier-version (&optional callback)
975 "Asynchronously invoke CALLBACK with a predicate indicating
976 whether the current scopifier version satisfies the minimum
977 version number required for the current major mode."
978 (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
979 (when dispatch
980 (let ((version (plist-get dispatch :version))
981 (command (plist-get dispatch :command)))
982 (context-coloring-scopify-shell-command
983 (context-coloring-join (list command "--version") " ")
984 (lambda (output)
985 (if (context-coloring-check-version version output)
986 (progn
987 (when callback (funcall callback t)))
988 (when callback (funcall callback nil)))
989 (run-hooks 'context-coloring-check-scopifier-version-hook)))))))
990
991
992 ;;; Themes
993
994 (defvar context-coloring-theme-hash-table (make-hash-table :test 'eq)
995 "Map theme names to theme properties.")
996
997 (defun context-coloring-theme-p (theme)
998 "Return t if THEME is defined, nil otherwise."
999 (and (gethash theme context-coloring-theme-hash-table)))
1000
1001 (defconst context-coloring-level-face-regexp
1002 "context-coloring-level-\\([[:digit:]]+\\)-face"
1003 "Extract a level from a face.")
1004
1005 (defvar context-coloring-originally-set-theme-hash-table
1006 (make-hash-table :test 'eq)
1007 "Cache custom themes who originally set their own
1008 `context-coloring-level-N-face' faces.")
1009
1010 (defun context-coloring-theme-originally-set-p (theme)
1011 "Return t if there is a `context-coloring-level-N-face'
1012 originally set for THEME, nil otherwise."
1013 (let (originally-set)
1014 (cond
1015 ;; `setq' might return a non-nil value for the sake of this `cond'.
1016 ((setq
1017 originally-set
1018 (gethash
1019 theme
1020 context-coloring-originally-set-theme-hash-table))
1021 (eq originally-set 'yes))
1022 (t
1023 (let* ((settings (get theme 'theme-settings))
1024 (tail settings)
1025 found)
1026 (while (and tail (not found))
1027 (and (eq (nth 0 (car tail)) 'theme-face)
1028 (string-match
1029 context-coloring-level-face-regexp
1030 (symbol-name (nth 1 (car tail))))
1031 (setq found t))
1032 (setq tail (cdr tail)))
1033 found)))))
1034
1035 (defun context-coloring-cache-originally-set (theme originally-set)
1036 "Remember if THEME had colors originally set for it. If
1037 ORIGINALLY-SET is non-nil, it did, otherwise it didn't."
1038 ;; Caching whether a theme was originally set is kind of dirty, but we have to
1039 ;; do it to remember the past state of the theme. There are probably some
1040 ;; edge cases where caching will be an issue, but they are probably rare.
1041 (puthash
1042 theme
1043 (if originally-set 'yes 'no)
1044 context-coloring-originally-set-theme-hash-table))
1045
1046 (defun context-coloring-warn-theme-originally-set (theme)
1047 "Warn the user that the colors for THEME are already originally
1048 set."
1049 (warn "Context coloring colors for theme `%s' are already defined" theme))
1050
1051 (defun context-coloring-theme-highest-level (theme)
1052 "Return the highest level N of a face like
1053 `context-coloring-level-N-face' set for THEME, or `-1' if there
1054 is none."
1055 (let* ((settings (get theme 'theme-settings))
1056 (tail settings)
1057 face-string
1058 number
1059 (found -1))
1060 (while tail
1061 (and (eq (nth 0 (car tail)) 'theme-face)
1062 (setq face-string (symbol-name (nth 1 (car tail))))
1063 (string-match
1064 context-coloring-level-face-regexp
1065 face-string)
1066 (setq number (string-to-number
1067 (substring face-string
1068 (match-beginning 1)
1069 (match-end 1))))
1070 (> number found)
1071 (setq found number))
1072 (setq tail (cdr tail)))
1073 found))
1074
1075 (defun context-coloring-apply-theme (theme)
1076 "Apply THEME's properties to its respective custom theme,
1077 which must already exist and which *should* already be enabled."
1078 (let* ((properties (gethash theme context-coloring-theme-hash-table))
1079 (colors (plist-get properties :colors))
1080 (level -1))
1081 ;; Only clobber when we have to.
1082 (when (custom-theme-enabled-p theme)
1083 (setq context-coloring-maximum-face (- (length colors) 1)))
1084 (apply
1085 'custom-theme-set-faces
1086 theme
1087 (mapcar
1088 (lambda (color)
1089 (setq level (+ level 1))
1090 `(,(context-coloring-level-face level) ((t (:foreground ,color)))))
1091 colors))))
1092
1093 (defun context-coloring-define-theme (theme &rest properties)
1094 "Define a context theme named THEME for coloring scope levels.
1095
1096 PROPERTIES is a property list specifiying the following details:
1097
1098 `:aliases': List of symbols of other custom themes that these
1099 colors are applicable to.
1100
1101 `:colors': List of colors that this context theme uses.
1102
1103 `:override': If non-nil, this context theme is intentionally
1104 overriding colors set by a custom theme. Don't set this non-nil
1105 unless there is a custom theme you want to use which sets
1106 `context-coloring-level-N-face' faces that you want to replace.
1107
1108 `:recede': If non-nil, this context theme should not apply its
1109 colors if a custom theme already sets
1110 `context-coloring-level-N-face' faces. This option is
1111 optimistic; set this non-nil if you would rather confer the duty
1112 of picking colors to a custom theme author (if / when he ever
1113 gets around to it).
1114
1115 By default, context themes will always override custom themes,
1116 even if those custom themes set `context-coloring-level-N-face'
1117 faces. If a context theme does override a custom theme, a
1118 warning will be raised, at which point you may want to enable the
1119 `:override' option, or just delete your context theme and opt to
1120 use your custom theme's author's colors instead.
1121
1122 Context themes only work for the custom theme with the highest
1123 precedence, i.e. the car of `custom-enabled-themes'."
1124 (let ((aliases (plist-get properties :aliases))
1125 (override (plist-get properties :override))
1126 (recede (plist-get properties :recede)))
1127 (dolist (name (append `(,theme) aliases))
1128 (puthash name properties context-coloring-theme-hash-table)
1129 (when (custom-theme-p name)
1130 (let ((originally-set (context-coloring-theme-originally-set-p name)))
1131 (context-coloring-cache-originally-set name originally-set)
1132 ;; In the particular case when you innocently define colors that a
1133 ;; custom theme originally set, warn. Arguably this only has to be
1134 ;; done at enable time, but it is probably more useful to do it at
1135 ;; definition time for prompter feedback.
1136 (when (and originally-set
1137 (not recede)
1138 (not override))
1139 (context-coloring-warn-theme-originally-set name))
1140 ;; Set (or overwrite) colors.
1141 (when (not (and originally-set
1142 recede))
1143 (context-coloring-apply-theme name)))))))
1144
1145 (defun context-coloring-enable-theme (theme)
1146 "Apply THEME if its colors are not already set, else just set
1147 `context-coloring-maximum-face' to the correct value for THEME."
1148 (let* ((properties (gethash theme context-coloring-theme-hash-table))
1149 (recede (plist-get properties :recede))
1150 (override (plist-get properties :override)))
1151 (cond
1152 (recede
1153 (let ((highest-level (context-coloring-theme-highest-level theme)))
1154 (cond
1155 ;; This can be true whether originally set by a custom theme or by a
1156 ;; context theme.
1157 ((> highest-level -1)
1158 (setq context-coloring-maximum-face highest-level))
1159 ;; It is possible that the corresponding custom theme did not exist at
1160 ;; the time of defining this context theme, and in that case the above
1161 ;; condition proves the custom theme did not originally set any faces,
1162 ;; so we have license to apply the context theme for the first time
1163 ;; here.
1164 (t
1165 (context-coloring-apply-theme theme)))))
1166 (t
1167 (let ((originally-set (context-coloring-theme-originally-set-p theme)))
1168 ;; Cache now in case the context theme was defined after the custom
1169 ;; theme.
1170 (context-coloring-cache-originally-set theme originally-set)
1171 (when (and originally-set
1172 (not override))
1173 (context-coloring-warn-theme-originally-set theme))
1174 (context-coloring-apply-theme theme))))))
1175
1176 (defadvice enable-theme (after context-coloring-enable-theme (theme) activate)
1177 "Enable colors for context themes just-in-time."
1178 (when (and (not (eq theme 'user)) ; Called internally by `enable-theme'.
1179 (custom-theme-p theme) ; Guard against non-existent themes.
1180 (context-coloring-theme-p theme))
1181 (when (= (length custom-enabled-themes) 1)
1182 ;; Cache because we can't reliably figure it out in reverse.
1183 (setq context-coloring-original-maximum-face
1184 context-coloring-maximum-face))
1185 (context-coloring-enable-theme theme)))
1186
1187 (defadvice disable-theme (after context-coloring-disable-theme (theme) activate)
1188 "Update `context-coloring-maximum-face'."
1189 (when (custom-theme-p theme) ; Guard against non-existent themes.
1190 (let ((enabled-theme (car custom-enabled-themes)))
1191 (if (context-coloring-theme-p enabled-theme)
1192 (progn
1193 (context-coloring-enable-theme enabled-theme))
1194 ;; Assume we are back to no theme; act as if nothing ever happened.
1195 ;; This is still prone to intervention, but rather extraordinarily.
1196 (setq context-coloring-maximum-face
1197 context-coloring-original-maximum-face)))))
1198
1199 (context-coloring-define-theme
1200 'ample
1201 :recede t
1202 :colors '("#bdbdb3"
1203 "#baba36"
1204 "#6aaf50"
1205 "#5180b3"
1206 "#ab75c3"
1207 "#cd7542"
1208 "#df9522"
1209 "#454545"))
1210
1211 (context-coloring-define-theme
1212 'anti-zenburn
1213 :recede t
1214 :colors '("#232333"
1215 "#6c1f1c"
1216 "#401440"
1217 "#0f2050"
1218 "#205070"
1219 "#336c6c"
1220 "#23733c"
1221 "#6b400c"
1222 "#603a60"
1223 "#2f4070"
1224 "#235c5c"))
1225
1226 (context-coloring-define-theme
1227 'grandshell
1228 :recede t
1229 :colors '("#bebebe"
1230 "#5af2ee"
1231 "#b2baf6"
1232 "#f09fff"
1233 "#efc334"
1234 "#f6df92"
1235 "#acfb5a"
1236 "#888888"))
1237
1238 (context-coloring-define-theme
1239 'leuven
1240 :recede t
1241 :colors '("#333333"
1242 "#0000ff"
1243 "#6434a3"
1244 "#ba36a5"
1245 "#d0372d"
1246 "#036a07"
1247 "#006699"
1248 "#006fe0"
1249 "#808080"))
1250
1251 (context-coloring-define-theme
1252 'monokai
1253 :recede t
1254 :colors '("#f8f8f2"
1255 "#66d9ef"
1256 "#a1efe4"
1257 "#a6e22e"
1258 "#e6db74"
1259 "#fd971f"
1260 "#f92672"
1261 "#fd5ff0"
1262 "#ae81ff"))
1263
1264 (context-coloring-define-theme
1265 'solarized
1266 :recede t
1267 :aliases '(solarized-light
1268 solarized-dark
1269 sanityinc-solarized-light
1270 sanityinc-solarized-dark)
1271 :colors '("#839496"
1272 "#268bd2"
1273 "#2aa198"
1274 "#859900"
1275 "#b58900"
1276 "#cb4b16"
1277 "#dc322f"
1278 "#d33682"
1279 "#6c71c4"
1280 "#69b7f0"
1281 "#69cabf"
1282 "#b4c342"
1283 "#deb542"
1284 "#f2804f"
1285 "#ff6e64"
1286 "#f771ac"
1287 "#9ea0e5"))
1288
1289 (context-coloring-define-theme
1290 'spacegray
1291 :recede t
1292 :colors '("#ffffff"
1293 "#89aaeb"
1294 "#c189eb"
1295 "#bf616a"
1296 "#dca432"
1297 "#ebcb8b"
1298 "#b4eb89"
1299 "#89ebca"))
1300
1301 (context-coloring-define-theme
1302 'tango
1303 :recede t
1304 :colors '("#2e3436"
1305 "#346604"
1306 "#204a87"
1307 "#5c3566"
1308 "#a40000"
1309 "#b35000"
1310 "#c4a000"
1311 "#8ae234"
1312 "#8cc4ff"
1313 "#ad7fa8"
1314 "#ef2929"
1315 "#fcaf3e"
1316 "#fce94f"))
1317
1318 (context-coloring-define-theme
1319 'zenburn
1320 :recede t
1321 :colors '("#dcdccc"
1322 "#93e0e3"
1323 "#bfebbf"
1324 "#f0dfaf"
1325 "#dfaf8f"
1326 "#cc9393"
1327 "#dc8cc3"
1328 "#94bff3"
1329 "#9fc59f"
1330 "#d0bf8f"
1331 "#dca3a3"))
1332
1333
1334 ;;; Change detection
1335
1336 (defvar-local context-coloring-colorize-idle-timer nil
1337 "The currently-running idle timer.")
1338
1339 (defcustom context-coloring-delay 0.25
1340 "Delay between a buffer update and colorization.
1341
1342 Increase this if your machine is high-performing. Decrease it if
1343 it ain't.
1344
1345 Supported modes: `js-mode', `js3-mode', `emacs-lisp-mode'"
1346 :group 'context-coloring)
1347
1348 (defun context-coloring-setup-idle-change-detection ()
1349 "Setup idle change detection."
1350 (add-hook
1351 'after-change-functions 'context-coloring-change-function nil t)
1352 (add-hook
1353 'kill-buffer-hook 'context-coloring-teardown-idle-change-detection nil t)
1354 (setq context-coloring-colorize-idle-timer
1355 (run-with-idle-timer
1356 context-coloring-delay
1357 t
1358 'context-coloring-maybe-colorize
1359 (current-buffer))))
1360
1361 (defun context-coloring-teardown-idle-change-detection ()
1362 "Teardown idle change detection."
1363 (context-coloring-kill-scopifier)
1364 (when context-coloring-colorize-idle-timer
1365 (cancel-timer context-coloring-colorize-idle-timer))
1366 (remove-hook
1367 'kill-buffer-hook 'context-coloring-teardown-idle-change-detection t)
1368 (remove-hook
1369 'after-change-functions 'context-coloring-change-function t))
1370
1371
1372 ;;; Built-in dispatches
1373
1374 (context-coloring-define-dispatch
1375 'javascript-node
1376 :modes '(js-mode js3-mode)
1377 :executable "scopifier"
1378 :command "scopifier"
1379 :version "v1.1.1")
1380
1381 (context-coloring-define-dispatch
1382 'javascript-js2
1383 :modes '(js2-mode)
1384 :colorizer 'context-coloring-js2-colorize
1385 :setup
1386 (lambda ()
1387 (add-hook 'js2-post-parse-callbacks 'context-coloring-colorize nil t))
1388 :teardown
1389 (lambda ()
1390 (remove-hook 'js2-post-parse-callbacks 'context-coloring-colorize t)))
1391
1392 (context-coloring-define-dispatch
1393 'emacs-lisp
1394 :modes '(emacs-lisp-mode)
1395 :colorizer 'context-coloring-elisp-colorize-buffer
1396 :setup 'context-coloring-setup-idle-change-detection
1397 :teardown 'context-coloring-teardown-idle-change-detection)
1398
1399 (defun context-coloring-dispatch (&optional callback)
1400 "Determine the optimal track for scopification / coloring of
1401 the current buffer, then execute it.
1402
1403 Invoke CALLBACK when complete. It is invoked synchronously for
1404 elisp tracks, and asynchronously for shell command tracks."
1405 (let* ((dispatch (context-coloring-get-dispatch-for-mode major-mode))
1406 (colorizer (plist-get dispatch :colorizer))
1407 (scopifier (plist-get dispatch :scopifier))
1408 (command (plist-get dispatch :command))
1409 interrupted-p)
1410 (cond
1411 ((or colorizer scopifier)
1412 (setq interrupted-p
1413 (catch 'interrupted
1414 (cond
1415 (colorizer
1416 (funcall colorizer))
1417 (scopifier
1418 (context-coloring-apply-tokens (funcall scopifier))))))
1419 (cond
1420 (interrupted-p
1421 (setq context-coloring-changed t))
1422 (t
1423 (when callback (funcall callback)))))
1424 (command
1425 (context-coloring-scopify-and-colorize command callback)))))
1426
1427
1428 ;;; Minor mode
1429
1430 ;;;###autoload
1431 (define-minor-mode context-coloring-mode
1432 "Context-based code coloring, inspired by Douglas Crockford."
1433 nil " Context" nil
1434 (if (not context-coloring-mode)
1435 (progn
1436 (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
1437 (when dispatch
1438 (let ((command (plist-get dispatch :command))
1439 (teardown (plist-get dispatch :teardown)))
1440 (when command
1441 (context-coloring-teardown-idle-change-detection))
1442 (when teardown
1443 (funcall teardown)))))
1444 (font-lock-mode)
1445 (jit-lock-mode t))
1446
1447 ;; Font lock is incompatible with this mode; the converse is also true.
1448 (font-lock-mode 0)
1449 (jit-lock-mode nil)
1450
1451 ;; ...but we do use font-lock functions here.
1452 (font-lock-set-defaults)
1453
1454 ;; Safely change the valye of this function as necessary.
1455 (make-local-variable 'font-lock-syntactic-face-function)
1456
1457 (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
1458 (if dispatch
1459 (progn
1460 (let ((command (plist-get dispatch :command))
1461 (version (plist-get dispatch :version))
1462 (executable (plist-get dispatch :executable))
1463 (setup (plist-get dispatch :setup))
1464 (colorize-initially-p t))
1465 (when command
1466 ;; Shell commands recolor on change, idly.
1467 (cond
1468 ((and executable
1469 (null (executable-find executable)))
1470 (message "Executable \"%s\" not found" executable)
1471 (setq colorize-initially-p nil))
1472 (version
1473 (context-coloring-check-scopifier-version
1474 (lambda (sufficient-p)
1475 (if sufficient-p
1476 (progn
1477 (context-coloring-setup-idle-change-detection)
1478 (context-coloring-colorize))
1479 (message "Update to the minimum version of \"%s\" (%s)"
1480 executable version))))
1481 (setq colorize-initially-p nil))
1482 (t
1483 (context-coloring-setup-idle-change-detection))))
1484 (when setup
1485 (funcall setup))
1486 ;; Colorize once initially.
1487 (when colorize-initially-p
1488 (let ((context-coloring-parse-interruptable-p nil))
1489 (context-coloring-colorize)))))
1490 (when (null dispatch)
1491 (message "Context coloring is not available for this major mode"))))))
1492
1493 (provide 'context-coloring)
1494
1495 ;;; context-coloring.el ends here