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