]> code.delx.au - gnu-emacs-elpa/blob - context-coloring.el
b000fd22f9167fd34700ed791b698ca65d84dfed
[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-get-syntax-code ()
284 (syntax-class (syntax-after (point))))
285
286 (defsubst context-coloring-exact-regexp (word)
287 "Create a regexp that matches exactly WORD."
288 (concat "\\`" (regexp-quote word) "\\'"))
289
290 (defsubst context-coloring-exact-or-regexp (words)
291 "Create a regexp that matches any exact word in WORDS."
292 (context-coloring-join
293 (mapcar 'context-coloring-exact-regexp words) "\\|"))
294
295 (defconst context-coloring-elisp-defun-regexp
296 (context-coloring-exact-or-regexp
297 '("defun" "defun*" "defsubst" "defmacro"
298 "cl-defun" "cl-defsubst" "cl-defmacro")))
299
300 (defconst context-coloring-elisp-lambda-regexp
301 (context-coloring-exact-regexp "lambda"))
302
303 (defconst context-coloring-elisp-let-regexp
304 (context-coloring-exact-regexp "let"))
305
306 (defconst context-coloring-elisp-let*-regexp
307 (context-coloring-exact-regexp "let*"))
308
309 (defconst context-coloring-elisp-arglist-arg-regexp
310 "\\`[^&:]")
311
312 (defconst context-coloring-ignored-word-regexp
313 (context-coloring-join (list "\\`[-+]?[0-9]"
314 "\\`[&:].+"
315 (context-coloring-exact-or-regexp
316 '("t" "nil" "." "?")))
317 "\\|"))
318
319 (defconst context-coloring-WORD-CODE 2)
320 (defconst context-coloring-SYMBOL-CODE 3)
321 (defconst context-coloring-OPEN-PARENTHESIS-CODE 4)
322 (defconst context-coloring-CLOSE-PARENTHESIS-CODE 5)
323 (defconst context-coloring-EXPRESSION-PREFIX-CODE 6)
324 (defconst context-coloring-STRING-QUOTE-CODE 7)
325 (defconst context-coloring-ESCAPE-CODE 9)
326 (defconst context-coloring-COMMENT-START-CODE 11)
327 (defconst context-coloring-COMMENT-END-CODE 12)
328
329 (defconst context-coloring-APOSTROPHE-CHAR (string-to-char "'"))
330 (defconst context-coloring-OPEN-PARENTHESIS-CHAR (string-to-char "("))
331 (defconst context-coloring-COMMA-CHAR (string-to-char ","))
332 (defconst context-coloring-BACKTICK-CHAR (string-to-char "`"))
333
334 (defvar context-coloring-elisp-scope-stack '())
335
336 (defsubst context-coloring-elisp-make-scope (level)
337 (list
338 :level level
339 :variables '()))
340
341 (defsubst context-coloring-elisp-scope-get-level (scope)
342 (plist-get scope :level))
343
344 (defsubst context-coloring-elisp-scope-add-variable (scope variable)
345 (plist-put scope :variables (cons variable (plist-get scope :variables))))
346
347 (defsubst context-coloring-elisp-scope-has-variable (scope variable)
348 (member variable (plist-get scope :variables)))
349
350 (defsubst context-coloring-elisp-get-variable-level (variable)
351 (let* ((scope-stack context-coloring-elisp-scope-stack)
352 scope
353 level)
354 (while (and scope-stack (not level))
355 (setq scope (car scope-stack))
356 (cond
357 ((context-coloring-elisp-scope-has-variable scope variable)
358 (setq level (context-coloring-elisp-scope-get-level scope)))
359 (t
360 (setq scope-stack (cdr scope-stack)))))
361 ;; Assume a global variable.
362 (or level 0)))
363
364 (defsubst context-coloring-elisp-current-scope-level ()
365 (cond
366 ((car context-coloring-elisp-scope-stack)
367 (context-coloring-elisp-scope-get-level (car context-coloring-elisp-scope-stack)))
368 (t
369 0)))
370
371 (defsubst context-coloring-elisp-push-scope ()
372 (push (context-coloring-elisp-make-scope
373 (1+ (context-coloring-elisp-current-scope-level)))
374 context-coloring-elisp-scope-stack))
375
376 (defsubst context-coloring-elisp-pop-scope ()
377 (pop context-coloring-elisp-scope-stack))
378
379 (defsubst context-coloring-elisp-add-variable (variable)
380 (context-coloring-elisp-scope-add-variable
381 (car context-coloring-elisp-scope-stack)
382 variable))
383
384 (defun context-coloring-elisp-parse-arg (callback)
385 (let (arg-pos
386 arg-end
387 arg-string)
388 (setq arg-pos (point))
389 (forward-sexp)
390 (setq arg-end (point))
391 (setq arg-string (buffer-substring-no-properties
392 arg-pos
393 arg-end))
394 (when (string-match-p
395 context-coloring-elisp-arglist-arg-regexp
396 arg-string)
397 (funcall callback arg-string))))
398
399 (defun context-coloring-elisp-parse-let-varlist (type)
400 (let ((varlist '())
401 syntax-code)
402 ;; Enter.
403 (forward-char)
404 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
405 context-coloring-CLOSE-PARENTHESIS-CODE)
406 (cond
407 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
408 (forward-char)
409 (context-coloring-forward-sws)
410 (setq syntax-code (context-coloring-get-syntax-code))
411 (when (or (= syntax-code context-coloring-WORD-CODE)
412 (= syntax-code context-coloring-SYMBOL-CODE))
413 (context-coloring-elisp-parse-arg
414 (lambda (var)
415 (push var varlist)))
416 (context-coloring-forward-sws)
417 (setq syntax-code (context-coloring-get-syntax-code))
418 (when (/= syntax-code context-coloring-CLOSE-PARENTHESIS-CODE)
419 (context-coloring-elisp-colorize-sexp)))
420 (context-coloring-forward-sws)
421 ;; Skip past the closing parenthesis.
422 (forward-char))
423 ((or (= syntax-code context-coloring-WORD-CODE)
424 (= syntax-code context-coloring-SYMBOL-CODE))
425 (context-coloring-elisp-parse-arg
426 (lambda (var)
427 (push var varlist)))))
428 (when (eq type 'let*)
429 (context-coloring-elisp-add-variable (pop varlist)))
430 (context-coloring-forward-sws))
431 (when (eq type 'let)
432 (while varlist
433 (context-coloring-elisp-add-variable (pop varlist))))
434 ;; Exit.
435 (forward-char)))
436
437 (defun context-coloring-elisp-parse-arglist ()
438 (let (syntax-code)
439 ;; Enter.
440 (forward-char)
441 (while (/= (setq syntax-code (context-coloring-get-syntax-code))
442 context-coloring-CLOSE-PARENTHESIS-CODE)
443 (cond
444 ((or (= syntax-code context-coloring-WORD-CODE)
445 (= syntax-code context-coloring-SYMBOL-CODE))
446 (context-coloring-elisp-parse-arg
447 (lambda (arg)
448 (context-coloring-elisp-add-variable arg))))
449 (t
450 (forward-sexp)))
451 (context-coloring-forward-sws))
452 ;; Exit.
453 (forward-char)))
454
455 (defun context-coloring-elisp-colorize-defun (&optional anonymous-p
456 let-type)
457 (let ((start (point))
458 end
459 stop
460 syntax-code
461 defun-name-pos
462 defun-name-end)
463 (context-coloring-elisp-push-scope)
464 ;; Color the whole sexp.
465 (forward-sexp)
466 (setq end (point))
467 (context-coloring-colorize-region
468 start
469 end
470 (context-coloring-elisp-current-scope-level))
471 (goto-char start)
472 ;; Skip past the "defun".
473 (skip-syntax-forward "^w_")
474 (forward-sexp)
475 (context-coloring-forward-sws)
476 (setq stop nil)
477 (unless anonymous-p
478 ;; Check for the defun's name.
479 (setq syntax-code (context-coloring-get-syntax-code))
480 (cond
481 ((or (= syntax-code context-coloring-WORD-CODE)
482 (= syntax-code context-coloring-SYMBOL-CODE))
483 ;; Color the defun's name with the top-level color.
484 (setq defun-name-pos (point))
485 (forward-sexp)
486 (setq defun-name-end (point))
487 (context-coloring-colorize-region defun-name-pos defun-name-end 0)
488 (context-coloring-forward-sws))
489 (t
490 (setq stop t))))
491 (cond
492 (stop
493 ;; Skip it.
494 (goto-char start)
495 (forward-sexp))
496 (t
497 (setq syntax-code (context-coloring-get-syntax-code))
498 (cond
499 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
500 (cond
501 (let-type
502 (context-coloring-elisp-parse-let-varlist let-type))
503 (t
504 (context-coloring-elisp-parse-arglist)))
505 ;; Colorize the rest of the function.
506 (context-coloring-elisp-colorize-region (point) (1- end))
507 ;; Exit the defun.
508 (forward-char))
509 (t
510 ;; Skip it.
511 (goto-char start)
512 (forward-sexp)))))
513 (context-coloring-elisp-pop-scope)))
514
515 (defun context-coloring-elisp-colorize-lambda ()
516 (context-coloring-elisp-colorize-defun t))
517
518 (defun context-coloring-elisp-colorize-let ()
519 (context-coloring-elisp-colorize-defun t 'let))
520
521 (defun context-coloring-elisp-colorize-let* ()
522 (context-coloring-elisp-colorize-defun t 'let*))
523
524 (defun context-coloring-elisp-colorize-parenthesized-sexp ()
525 (let ((start (point))
526 end
527 syntax-code
528 child-0-pos
529 child-0-end
530 child-0-string)
531 (forward-sexp)
532 (setq end (point))
533 (goto-char start)
534 (forward-char)
535 (context-coloring-forward-sws)
536 (setq syntax-code (context-coloring-get-syntax-code))
537 ;; Figure out if the sexp is a special form.
538 (cond
539 ((when (or (= syntax-code context-coloring-WORD-CODE)
540 (= syntax-code context-coloring-SYMBOL-CODE))
541 (setq child-0-pos (point))
542 (forward-sexp)
543 (setq child-0-end (point))
544 (setq child-0-string (buffer-substring-no-properties
545 child-0-pos
546 child-0-end))
547 (cond
548 ((string-match-p context-coloring-elisp-defun-regexp child-0-string)
549 (goto-char start)
550 (context-coloring-elisp-colorize-defun)
551 t)
552 ((string-match-p context-coloring-elisp-lambda-regexp child-0-string)
553 (goto-char start)
554 (context-coloring-elisp-colorize-lambda)
555 t)
556 ((string-match-p context-coloring-elisp-let-regexp child-0-string)
557 (goto-char start)
558 (context-coloring-elisp-colorize-let)
559 t)
560 ((string-match-p context-coloring-elisp-let*-regexp child-0-string)
561 (goto-char start)
562 (context-coloring-elisp-colorize-let*)
563 t)
564 (t
565 nil))))
566 ;; Not a special form; just colorize the remaining region.
567 (t
568 (context-coloring-colorize-region
569 start
570 end
571 (context-coloring-elisp-current-scope-level))
572 (context-coloring-elisp-colorize-region (point) (1- end))
573 (forward-char)))))
574
575 (defun context-coloring-elisp-colorize-symbol ()
576 (let (symbol-pos
577 symbol-end
578 symbol-string)
579 (setq symbol-pos (point))
580 (forward-sexp)
581 (setq symbol-end (point))
582 (setq symbol-string (buffer-substring-no-properties
583 symbol-pos
584 symbol-end))
585 (cond
586 ((string-match-p context-coloring-ignored-word-regexp symbol-string))
587 (t
588 (context-coloring-colorize-region
589 symbol-pos
590 symbol-end
591 (context-coloring-elisp-get-variable-level
592 symbol-string))))))
593
594 (defun context-coloring-elisp-colorize-expression-prefix ()
595 (let (start
596 end
597 char)
598 (setq char (char-after))
599 (cond
600 ((= char context-coloring-APOSTROPHE-CHAR)
601 (forward-sexp))
602 ((= char context-coloring-BACKTICK-CHAR)
603 (setq start (point))
604 (forward-sexp)
605 (setq end (point))
606 (goto-char start)
607 (while (> end (progn (forward-char)
608 (point)))
609 (setq char (char-after))
610 (when (= char context-coloring-COMMA-CHAR)
611 (forward-char)
612 (context-coloring-forward-sws)
613 (context-coloring-elisp-colorize-sexp)))))))
614
615 (defvar context-coloring-parse-interruptable-p t
616 "Set this to nil to force parse to continue until finished.")
617
618 (defconst context-coloring-elisp-sexps-per-pause 1000
619 "Pause after this many iterations to check for user input.
620 If user input is pending, stop the parse. This makes for a
621 smoother user experience for large files.
622
623 As of this writing, emacs lisp colorization seems to run at about
624 60,000 iterations per second. A default value of 1000 should
625 provide visually \"instant\" updates at 60 frames per second.")
626
627 (defvar context-coloring-elisp-sexp-count 0)
628
629 (defun context-coloring-elisp-increment-sexp-count ()
630 (setq context-coloring-elisp-sexp-count
631 (1+ context-coloring-elisp-sexp-count))
632 (when (and (zerop (% context-coloring-elisp-sexp-count
633 context-coloring-elisp-sexps-per-pause))
634 context-coloring-parse-interruptable-p
635 (input-pending-p))
636 (throw 'interrupted t)))
637
638 (defun context-coloring-elisp-colorize-sexp ()
639 (let (syntax-code)
640 (context-coloring-elisp-increment-sexp-count)
641 (setq syntax-code (context-coloring-get-syntax-code))
642 (cond
643 ((= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
644 (context-coloring-elisp-colorize-parenthesized-sexp))
645 ((or (= syntax-code context-coloring-WORD-CODE)
646 (= syntax-code context-coloring-SYMBOL-CODE))
647 (context-coloring-elisp-colorize-symbol))
648 ((= syntax-code context-coloring-EXPRESSION-PREFIX-CODE)
649 (context-coloring-elisp-colorize-expression-prefix))
650 (t
651 (forward-char)))))
652
653 (defun context-coloring-elisp-colorize-comment ()
654 (let ((start (point)))
655 (context-coloring-elisp-increment-sexp-count)
656 (skip-syntax-forward "^>")
657 (context-coloring-maybe-colorize-comments-and-strings
658 start
659 (point))))
660
661 (defun context-coloring-elisp-colorize-string ()
662 (let ((start (point))
663 syntax-code)
664 (context-coloring-elisp-increment-sexp-count)
665 ;; Move past the opening string delimiter.
666 (forward-char)
667 (while (progn
668 (skip-syntax-forward "^\\\"")
669 (setq syntax-code (context-coloring-get-syntax-code))
670 (cond
671 ((= syntax-code context-coloring-ESCAPE-CODE)
672 ;; If there was an escape char, keep going.
673 (forward-char 2)
674 t)
675 ((= syntax-code context-coloring-STRING-QUOTE-CODE)
676 ;; If the string ended, move outside it.
677 (forward-char)
678 nil))))
679 (context-coloring-maybe-colorize-comments-and-strings
680 start
681 (point))))
682
683 (defun context-coloring-elisp-colorize-region (start end)
684 (let (syntax-code)
685 (goto-char start)
686 (while (> end (progn (skip-syntax-forward "^()w_'<\"" end)
687 (point)))
688 (setq syntax-code (context-coloring-get-syntax-code))
689 (cond
690 ((or (= syntax-code context-coloring-OPEN-PARENTHESIS-CODE)
691 (= syntax-code context-coloring-WORD-CODE)
692 (= syntax-code context-coloring-SYMBOL-CODE)
693 (= syntax-code context-coloring-EXPRESSION-PREFIX-CODE))
694 (context-coloring-elisp-colorize-sexp))
695 ((= syntax-code context-coloring-COMMENT-START-CODE)
696 (context-coloring-elisp-colorize-comment))
697 ((= syntax-code context-coloring-STRING-QUOTE-CODE)
698 (context-coloring-elisp-colorize-string))
699 (t
700 (forward-char))))))
701
702 (defun context-coloring-elisp-colorize (start end)
703 (setq context-coloring-elisp-sexp-count 0)
704 (setq context-coloring-elisp-scope-stack '())
705 (context-coloring-elisp-colorize-region start end))
706
707 (defun context-coloring-elisp-colorize-changed-region (start end)
708 (with-silent-modifications
709 (save-excursion
710 (let ((start (progn (goto-char start)
711 (beginning-of-defun)
712 (point)))
713 (end (progn (goto-char end)
714 (end-of-defun)
715 (point))))
716 (context-coloring-elisp-colorize start end)))))
717
718 (defun context-coloring-elisp-colorize-buffer ()
719 (interactive)
720 (with-silent-modifications
721 (save-excursion
722 (context-coloring-elisp-colorize (point-min) (point-max)))))
723
724 (defalias 'ccecb 'context-coloring-elisp-colorize-buffer)
725
726
727 ;;; Shell command scopification / colorization
728
729 (defun context-coloring-apply-tokens (tokens)
730 "Process a vector of TOKENS to apply context-based coloring to
731 the current buffer. Tokens are 3 integers: start, end, level.
732 The vector is flat, with a new token occurring after every 3rd
733 element."
734 (with-silent-modifications
735 (let ((i 0)
736 (len (length tokens)))
737 (while (< i len)
738 (context-coloring-colorize-region
739 (elt tokens i)
740 (elt tokens (+ i 1))
741 (elt tokens (+ i 2)))
742 (setq i (+ i 3))))
743 (context-coloring-maybe-colorize-comments-and-strings)))
744
745 (defun context-coloring-parse-array (array)
746 "Parse ARRAY as a flat JSON array of numbers."
747 (let ((braceless (substring (context-coloring-trim array) 1 -1)))
748 (cond
749 ((> (length braceless) 0)
750 (vconcat
751 (mapcar 'string-to-number (split-string braceless ","))))
752 (t
753 (vector)))))
754
755 (defvar-local context-coloring-scopifier-process nil
756 "The single scopifier process that can be running.")
757
758 (defun context-coloring-kill-scopifier ()
759 "Kill the currently-running scopifier process."
760 (when (not (null context-coloring-scopifier-process))
761 (delete-process context-coloring-scopifier-process)
762 (setq context-coloring-scopifier-process nil)))
763
764 (defun context-coloring-scopify-shell-command (command callback)
765 "Invoke a scopifier via COMMAND, read its response
766 asynchronously and invoke CALLBACK with its output."
767
768 ;; Prior running tokenization is implicitly obsolete if this function is
769 ;; called.
770 (context-coloring-kill-scopifier)
771
772 ;; Start the process.
773 (setq context-coloring-scopifier-process
774 (start-process-shell-command "scopifier" nil command))
775
776 (let ((output ""))
777
778 ;; The process may produce output in multiple chunks. This filter
779 ;; accumulates the chunks into a message.
780 (set-process-filter
781 context-coloring-scopifier-process
782 (lambda (_process chunk)
783 (setq output (concat output chunk))))
784
785 ;; When the process's message is complete, this sentinel parses it as JSON
786 ;; and applies the tokens to the buffer.
787 (set-process-sentinel
788 context-coloring-scopifier-process
789 (lambda (_process event)
790 (when (equal "finished\n" event)
791 (funcall callback output))))))
792
793 (defun context-coloring-send-buffer-to-scopifier ()
794 "Give the scopifier process its input so it can begin
795 scopifying."
796 (process-send-region
797 context-coloring-scopifier-process
798 (point-min) (point-max))
799 (process-send-eof
800 context-coloring-scopifier-process))
801
802 (defun context-coloring-scopify-and-colorize (command &optional callback)
803 "Invoke a scopifier via COMMAND with the current buffer's contents,
804 read the scopifier's response asynchronously and apply a parsed
805 list of tokens to `context-coloring-apply-tokens'.
806
807 Invoke CALLBACK when complete."
808 (let ((buffer (current-buffer)))
809 (context-coloring-scopify-shell-command
810 command
811 (lambda (output)
812 (let ((tokens (context-coloring-parse-array output)))
813 (with-current-buffer buffer
814 (context-coloring-apply-tokens tokens))
815 (setq context-coloring-scopifier-process nil)
816 (when callback (funcall callback))))))
817 (context-coloring-send-buffer-to-scopifier))
818
819
820 ;;; Dispatch
821
822 (defvar context-coloring-dispatch-hash-table (make-hash-table :test 'eq)
823 "Map dispatch strategy names to their corresponding property
824 lists, which contain details about the strategies.")
825
826 (defvar context-coloring-mode-hash-table (make-hash-table :test 'eq)
827 "Map major mode names to dispatch property lists.")
828
829 (defun context-coloring-get-dispatch-for-mode (mode)
830 "Return the dispatch for MODE (or a derivative mode)."
831 (let ((parent mode)
832 dispatch)
833 (while (and parent
834 (not (setq dispatch (gethash parent context-coloring-mode-hash-table)))
835 (setq parent (get parent 'derived-mode-parent))))
836 dispatch))
837
838 (defun context-coloring-define-dispatch (symbol &rest properties)
839 "Define a new dispatch named SYMBOL with PROPERTIES.
840
841 A \"dispatch\" is a property list describing a strategy for
842 coloring a buffer. There are three possible strategies: Parse
843 and color in a single function (`:colorizer'), parse in a
844 function that returns scope data (`:scopifier'), or parse with a
845 shell command that returns scope data (`:command'). In the
846 latter two cases, the scope data will be used to automatically
847 color the buffer.
848
849 PROPERTIES must include `:modes' and one of `:colorizer',
850 `:scopifier' or `:command'.
851
852 `:modes' - List of major modes this dispatch is valid for.
853
854 `:colorizer' - Symbol referring to a function that parses and
855 colors the buffer.
856
857 `:scopifier' - Symbol referring to a function that parses the
858 buffer a returns a flat vector of start, end and level data.
859
860 `:executable' - Optional name of an executable required by
861 `:command'.
862
863 `:command' - Shell command to execute with the current buffer
864 sent via stdin, and with a flat JSON array of start, end and
865 level data returned via stdout.
866
867 `:version' - Minimum required version that should be printed when
868 executing `:command' with a \"--version\" flag. The version
869 should be numeric, e.g. \"2\", \"19700101\", \"1.2.3\",
870 \"v1.2.3\" etc.
871
872 `:setup' - Arbitrary code to set up this dispatch when
873 `context-coloring-mode' is enabled.
874
875 `:teardown' - Arbitrary code to tear down this dispatch when
876 `context-coloring-mode' is disabled."
877 (let ((modes (plist-get properties :modes))
878 (colorizer (plist-get properties :colorizer))
879 (scopifier (plist-get properties :scopifier))
880 (command (plist-get properties :command)))
881 (when (null modes)
882 (error "No mode defined for dispatch"))
883 (when (not (or colorizer
884 scopifier
885 command))
886 (error "No colorizer, scopifier or command defined for dispatch"))
887 (puthash symbol properties context-coloring-dispatch-hash-table)
888 (dolist (mode modes)
889 (puthash mode properties context-coloring-mode-hash-table))))
890
891
892 ;;; Colorization
893
894 (defvar context-coloring-colorize-hook nil
895 "Hooks to run after coloring a buffer.")
896
897 (defun context-coloring-colorize (&optional callback)
898 "Color the current buffer by function context.
899
900 Invoke CALLBACK when complete; see `context-coloring-dispatch'."
901 (interactive)
902 (context-coloring-dispatch
903 (lambda ()
904 (when callback (funcall callback))
905 (run-hooks 'context-coloring-colorize-hook))))
906
907 (defvar-local context-coloring-changed nil
908 "Indication that the buffer has changed recently, which implies
909 that it should be colored again by
910 `context-coloring-colorize-idle-timer' if that timer is being
911 used.")
912
913 (defun context-coloring-change-function (_start _end _length)
914 "Register a change so that a buffer can be colorized soon."
915 ;; Tokenization is obsolete if there was a change.
916 (context-coloring-kill-scopifier)
917 (setq context-coloring-changed t))
918
919 (defun context-coloring-maybe-colorize (buffer)
920 "Colorize the current buffer if it has changed."
921 (when (and (eq buffer (current-buffer))
922 context-coloring-changed)
923 (setq context-coloring-changed nil)
924 (context-coloring-colorize)))
925
926
927 ;;; Versioning
928
929 (defun context-coloring-parse-version (string)
930 "Extract segments of a version STRING into a list. \"v1.0.0\"
931 produces (1 0 0), \"19700101\" produces (19700101), etc."
932 (let (version)
933 (while (string-match "[0-9]+" string)
934 (setq version (append version
935 (list (string-to-number (match-string 0 string)))))
936 (setq string (substring string (match-end 0))))
937 version))
938
939 (defun context-coloring-check-version (expected actual)
940 "Check that version EXPECTED is less than or equal to ACTUAL."
941 (let ((expected (context-coloring-parse-version expected))
942 (actual (context-coloring-parse-version actual))
943 (continue t)
944 (acceptable t))
945 (while (and continue expected)
946 (let ((an-expected (car expected))
947 (an-actual (car actual)))
948 (cond
949 ((> an-actual an-expected)
950 (setq acceptable t)
951 (setq continue nil))
952 ((< an-actual an-expected)
953 (setq acceptable nil)
954 (setq continue nil))))
955 (setq expected (cdr expected))
956 (setq actual (cdr actual)))
957 acceptable))
958
959 (defvar context-coloring-check-scopifier-version-hook nil
960 "Hooks to run after checking the scopifier version.")
961
962 (defun context-coloring-check-scopifier-version (&optional callback)
963 "Asynchronously invoke CALLBACK with a predicate indicating
964 whether the current scopifier version satisfies the minimum
965 version number required for the current major mode."
966 (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
967 (when dispatch
968 (let ((version (plist-get dispatch :version))
969 (command (plist-get dispatch :command)))
970 (context-coloring-scopify-shell-command
971 (context-coloring-join (list command "--version") " ")
972 (lambda (output)
973 (if (context-coloring-check-version version output)
974 (progn
975 (when callback (funcall callback t)))
976 (when callback (funcall callback nil)))
977 (run-hooks 'context-coloring-check-scopifier-version-hook)))))))
978
979
980 ;;; Themes
981
982 (defvar context-coloring-theme-hash-table (make-hash-table :test 'eq)
983 "Map theme names to theme properties.")
984
985 (defun context-coloring-theme-p (theme)
986 "Return t if THEME is defined, nil otherwise."
987 (and (gethash theme context-coloring-theme-hash-table)))
988
989 (defconst context-coloring-level-face-regexp
990 "context-coloring-level-\\([[:digit:]]+\\)-face"
991 "Extract a level from a face.")
992
993 (defvar context-coloring-originally-set-theme-hash-table
994 (make-hash-table :test 'eq)
995 "Cache custom themes who originally set their own
996 `context-coloring-level-N-face' faces.")
997
998 (defun context-coloring-theme-originally-set-p (theme)
999 "Return t if there is a `context-coloring-level-N-face'
1000 originally set for THEME, nil otherwise."
1001 (let (originally-set)
1002 (cond
1003 ;; `setq' might return a non-nil value for the sake of this `cond'.
1004 ((setq
1005 originally-set
1006 (gethash
1007 theme
1008 context-coloring-originally-set-theme-hash-table))
1009 (eq originally-set 'yes))
1010 (t
1011 (let* ((settings (get theme 'theme-settings))
1012 (tail settings)
1013 found)
1014 (while (and tail (not found))
1015 (and (eq (nth 0 (car tail)) 'theme-face)
1016 (string-match
1017 context-coloring-level-face-regexp
1018 (symbol-name (nth 1 (car tail))))
1019 (setq found t))
1020 (setq tail (cdr tail)))
1021 found)))))
1022
1023 (defun context-coloring-cache-originally-set (theme originally-set)
1024 "Remember if THEME had colors originally set for it. If
1025 ORIGINALLY-SET is non-nil, it did, otherwise it didn't."
1026 ;; Caching whether a theme was originally set is kind of dirty, but we have to
1027 ;; do it to remember the past state of the theme. There are probably some
1028 ;; edge cases where caching will be an issue, but they are probably rare.
1029 (puthash
1030 theme
1031 (if originally-set 'yes 'no)
1032 context-coloring-originally-set-theme-hash-table))
1033
1034 (defun context-coloring-warn-theme-originally-set (theme)
1035 "Warn the user that the colors for THEME are already originally
1036 set."
1037 (warn "Context coloring colors for theme `%s' are already defined" theme))
1038
1039 (defun context-coloring-theme-highest-level (theme)
1040 "Return the highest level N of a face like
1041 `context-coloring-level-N-face' set for THEME, or `-1' if there
1042 is none."
1043 (let* ((settings (get theme 'theme-settings))
1044 (tail settings)
1045 face-string
1046 number
1047 (found -1))
1048 (while tail
1049 (and (eq (nth 0 (car tail)) 'theme-face)
1050 (setq face-string (symbol-name (nth 1 (car tail))))
1051 (string-match
1052 context-coloring-level-face-regexp
1053 face-string)
1054 (setq number (string-to-number
1055 (substring face-string
1056 (match-beginning 1)
1057 (match-end 1))))
1058 (> number found)
1059 (setq found number))
1060 (setq tail (cdr tail)))
1061 found))
1062
1063 (defun context-coloring-apply-theme (theme)
1064 "Apply THEME's properties to its respective custom theme,
1065 which must already exist and which *should* already be enabled."
1066 (let* ((properties (gethash theme context-coloring-theme-hash-table))
1067 (colors (plist-get properties :colors))
1068 (level -1))
1069 ;; Only clobber when we have to.
1070 (when (custom-theme-enabled-p theme)
1071 (setq context-coloring-maximum-face (- (length colors) 1)))
1072 (apply
1073 'custom-theme-set-faces
1074 theme
1075 (mapcar
1076 (lambda (color)
1077 (setq level (+ level 1))
1078 `(,(context-coloring-level-face level) ((t (:foreground ,color)))))
1079 colors))))
1080
1081 (defun context-coloring-define-theme (theme &rest properties)
1082 "Define a context theme named THEME for coloring scope levels.
1083
1084 PROPERTIES is a property list specifiying the following details:
1085
1086 `:aliases': List of symbols of other custom themes that these
1087 colors are applicable to.
1088
1089 `:colors': List of colors that this context theme uses.
1090
1091 `:override': If non-nil, this context theme is intentionally
1092 overriding colors set by a custom theme. Don't set this non-nil
1093 unless there is a custom theme you want to use which sets
1094 `context-coloring-level-N-face' faces that you want to replace.
1095
1096 `:recede': If non-nil, this context theme should not apply its
1097 colors if a custom theme already sets
1098 `context-coloring-level-N-face' faces. This option is
1099 optimistic; set this non-nil if you would rather confer the duty
1100 of picking colors to a custom theme author (if / when he ever
1101 gets around to it).
1102
1103 By default, context themes will always override custom themes,
1104 even if those custom themes set `context-coloring-level-N-face'
1105 faces. If a context theme does override a custom theme, a
1106 warning will be raised, at which point you may want to enable the
1107 `:override' option, or just delete your context theme and opt to
1108 use your custom theme's author's colors instead.
1109
1110 Context themes only work for the custom theme with the highest
1111 precedence, i.e. the car of `custom-enabled-themes'."
1112 (let ((aliases (plist-get properties :aliases))
1113 (override (plist-get properties :override))
1114 (recede (plist-get properties :recede)))
1115 (dolist (name (append `(,theme) aliases))
1116 (puthash name properties context-coloring-theme-hash-table)
1117 (when (custom-theme-p name)
1118 (let ((originally-set (context-coloring-theme-originally-set-p name)))
1119 (context-coloring-cache-originally-set name originally-set)
1120 ;; In the particular case when you innocently define colors that a
1121 ;; custom theme originally set, warn. Arguably this only has to be
1122 ;; done at enable time, but it is probably more useful to do it at
1123 ;; definition time for prompter feedback.
1124 (when (and originally-set
1125 (not recede)
1126 (not override))
1127 (context-coloring-warn-theme-originally-set name))
1128 ;; Set (or overwrite) colors.
1129 (when (not (and originally-set
1130 recede))
1131 (context-coloring-apply-theme name)))))))
1132
1133 (defun context-coloring-enable-theme (theme)
1134 "Apply THEME if its colors are not already set, else just set
1135 `context-coloring-maximum-face' to the correct value for THEME."
1136 (let* ((properties (gethash theme context-coloring-theme-hash-table))
1137 (recede (plist-get properties :recede))
1138 (override (plist-get properties :override)))
1139 (cond
1140 (recede
1141 (let ((highest-level (context-coloring-theme-highest-level theme)))
1142 (cond
1143 ;; This can be true whether originally set by a custom theme or by a
1144 ;; context theme.
1145 ((> highest-level -1)
1146 (setq context-coloring-maximum-face highest-level))
1147 ;; It is possible that the corresponding custom theme did not exist at
1148 ;; the time of defining this context theme, and in that case the above
1149 ;; condition proves the custom theme did not originally set any faces,
1150 ;; so we have license to apply the context theme for the first time
1151 ;; here.
1152 (t
1153 (context-coloring-apply-theme theme)))))
1154 (t
1155 (let ((originally-set (context-coloring-theme-originally-set-p theme)))
1156 ;; Cache now in case the context theme was defined after the custom
1157 ;; theme.
1158 (context-coloring-cache-originally-set theme originally-set)
1159 (when (and originally-set
1160 (not override))
1161 (context-coloring-warn-theme-originally-set theme))
1162 (context-coloring-apply-theme theme))))))
1163
1164 (defadvice enable-theme (after context-coloring-enable-theme (theme) activate)
1165 "Enable colors for context themes just-in-time."
1166 (when (and (not (eq theme 'user)) ; Called internally by `enable-theme'.
1167 (custom-theme-p theme) ; Guard against non-existent themes.
1168 (context-coloring-theme-p theme))
1169 (when (= (length custom-enabled-themes) 1)
1170 ;; Cache because we can't reliably figure it out in reverse.
1171 (setq context-coloring-original-maximum-face
1172 context-coloring-maximum-face))
1173 (context-coloring-enable-theme theme)))
1174
1175 (defadvice disable-theme (after context-coloring-disable-theme (theme) activate)
1176 "Update `context-coloring-maximum-face'."
1177 (when (custom-theme-p theme) ; Guard against non-existent themes.
1178 (let ((enabled-theme (car custom-enabled-themes)))
1179 (if (context-coloring-theme-p enabled-theme)
1180 (progn
1181 (context-coloring-enable-theme enabled-theme))
1182 ;; Assume we are back to no theme; act as if nothing ever happened.
1183 ;; This is still prone to intervention, but rather extraordinarily.
1184 (setq context-coloring-maximum-face
1185 context-coloring-original-maximum-face)))))
1186
1187 (context-coloring-define-theme
1188 'ample
1189 :recede t
1190 :colors '("#bdbdb3"
1191 "#baba36"
1192 "#6aaf50"
1193 "#5180b3"
1194 "#ab75c3"
1195 "#cd7542"
1196 "#df9522"
1197 "#454545"))
1198
1199 (context-coloring-define-theme
1200 'anti-zenburn
1201 :recede t
1202 :colors '("#232333"
1203 "#6c1f1c"
1204 "#401440"
1205 "#0f2050"
1206 "#205070"
1207 "#336c6c"
1208 "#23733c"
1209 "#6b400c"
1210 "#603a60"
1211 "#2f4070"
1212 "#235c5c"))
1213
1214 (context-coloring-define-theme
1215 'grandshell
1216 :recede t
1217 :colors '("#bebebe"
1218 "#5af2ee"
1219 "#b2baf6"
1220 "#f09fff"
1221 "#efc334"
1222 "#f6df92"
1223 "#acfb5a"
1224 "#888888"))
1225
1226 (context-coloring-define-theme
1227 'leuven
1228 :recede t
1229 :colors '("#333333"
1230 "#0000ff"
1231 "#6434a3"
1232 "#ba36a5"
1233 "#d0372d"
1234 "#036a07"
1235 "#006699"
1236 "#006fe0"
1237 "#808080"))
1238
1239 (context-coloring-define-theme
1240 'monokai
1241 :recede t
1242 :colors '("#f8f8f2"
1243 "#66d9ef"
1244 "#a1efe4"
1245 "#a6e22e"
1246 "#e6db74"
1247 "#fd971f"
1248 "#f92672"
1249 "#fd5ff0"
1250 "#ae81ff"))
1251
1252 (context-coloring-define-theme
1253 'solarized
1254 :recede t
1255 :aliases '(solarized-light
1256 solarized-dark
1257 sanityinc-solarized-light
1258 sanityinc-solarized-dark)
1259 :colors '("#839496"
1260 "#268bd2"
1261 "#2aa198"
1262 "#859900"
1263 "#b58900"
1264 "#cb4b16"
1265 "#dc322f"
1266 "#d33682"
1267 "#6c71c4"
1268 "#69b7f0"
1269 "#69cabf"
1270 "#b4c342"
1271 "#deb542"
1272 "#f2804f"
1273 "#ff6e64"
1274 "#f771ac"
1275 "#9ea0e5"))
1276
1277 (context-coloring-define-theme
1278 'spacegray
1279 :recede t
1280 :colors '("#ffffff"
1281 "#89aaeb"
1282 "#c189eb"
1283 "#bf616a"
1284 "#dca432"
1285 "#ebcb8b"
1286 "#b4eb89"
1287 "#89ebca"))
1288
1289 (context-coloring-define-theme
1290 'tango
1291 :recede t
1292 :colors '("#2e3436"
1293 "#346604"
1294 "#204a87"
1295 "#5c3566"
1296 "#a40000"
1297 "#b35000"
1298 "#c4a000"
1299 "#8ae234"
1300 "#8cc4ff"
1301 "#ad7fa8"
1302 "#ef2929"
1303 "#fcaf3e"
1304 "#fce94f"))
1305
1306 (context-coloring-define-theme
1307 'zenburn
1308 :recede t
1309 :colors '("#dcdccc"
1310 "#93e0e3"
1311 "#bfebbf"
1312 "#f0dfaf"
1313 "#dfaf8f"
1314 "#cc9393"
1315 "#dc8cc3"
1316 "#94bff3"
1317 "#9fc59f"
1318 "#d0bf8f"
1319 "#dca3a3"))
1320
1321
1322 ;;; Change detection
1323
1324 (defvar-local context-coloring-colorize-idle-timer nil
1325 "The currently-running idle timer.")
1326
1327 (defcustom context-coloring-delay 0.25
1328 "Delay between a buffer update and colorization.
1329
1330 Increase this if your machine is high-performing. Decrease it if
1331 it ain't.
1332
1333 Supported modes: `js-mode', `js3-mode', `emacs-lisp-mode'"
1334 :group 'context-coloring)
1335
1336 (defun context-coloring-setup-idle-change-detection ()
1337 "Setup idle change detection."
1338 (add-hook
1339 'after-change-functions 'context-coloring-change-function nil t)
1340 (add-hook
1341 'kill-buffer-hook 'context-coloring-teardown-idle-change-detection nil t)
1342 (setq context-coloring-colorize-idle-timer
1343 (run-with-idle-timer
1344 context-coloring-delay
1345 t
1346 'context-coloring-maybe-colorize
1347 (current-buffer))))
1348
1349 (defun context-coloring-teardown-idle-change-detection ()
1350 "Teardown idle change detection."
1351 (context-coloring-kill-scopifier)
1352 (when context-coloring-colorize-idle-timer
1353 (cancel-timer context-coloring-colorize-idle-timer))
1354 (remove-hook
1355 'kill-buffer-hook 'context-coloring-teardown-idle-change-detection t)
1356 (remove-hook
1357 'after-change-functions 'context-coloring-change-function t))
1358
1359
1360 ;;; Built-in dispatches
1361
1362 (context-coloring-define-dispatch
1363 'javascript-node
1364 :modes '(js-mode js3-mode)
1365 :executable "scopifier"
1366 :command "scopifier"
1367 :version "v1.1.1")
1368
1369 (context-coloring-define-dispatch
1370 'javascript-js2
1371 :modes '(js2-mode)
1372 :colorizer 'context-coloring-js2-colorize
1373 :setup
1374 (lambda ()
1375 (add-hook 'js2-post-parse-callbacks 'context-coloring-colorize nil t))
1376 :teardown
1377 (lambda ()
1378 (remove-hook 'js2-post-parse-callbacks 'context-coloring-colorize t)))
1379
1380 (context-coloring-define-dispatch
1381 'emacs-lisp
1382 :modes '(emacs-lisp-mode)
1383 :colorizer 'context-coloring-elisp-colorize-buffer
1384 :setup 'context-coloring-setup-idle-change-detection
1385 :teardown 'context-coloring-teardown-idle-change-detection)
1386
1387 (defun context-coloring-dispatch (&optional callback)
1388 "Determine the optimal track for scopification / coloring of
1389 the current buffer, then execute it.
1390
1391 Invoke CALLBACK when complete. It is invoked synchronously for
1392 elisp tracks, and asynchronously for shell command tracks."
1393 (let* ((dispatch (context-coloring-get-dispatch-for-mode major-mode))
1394 (colorizer (plist-get dispatch :colorizer))
1395 (scopifier (plist-get dispatch :scopifier))
1396 (command (plist-get dispatch :command))
1397 interrupted-p)
1398 (cond
1399 ((or colorizer scopifier)
1400 (setq interrupted-p
1401 (catch 'interrupted
1402 (cond
1403 (colorizer
1404 (funcall colorizer))
1405 (scopifier
1406 (context-coloring-apply-tokens (funcall scopifier))))))
1407 (cond
1408 (interrupted-p
1409 (setq context-coloring-changed t))
1410 (t
1411 (when callback (funcall callback)))))
1412 (command
1413 (context-coloring-scopify-and-colorize command callback)))))
1414
1415
1416 ;;; Minor mode
1417
1418 ;;;###autoload
1419 (define-minor-mode context-coloring-mode
1420 "Context-based code coloring, inspired by Douglas Crockford."
1421 nil " Context" nil
1422 (if (not context-coloring-mode)
1423 (progn
1424 (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
1425 (when dispatch
1426 (let ((command (plist-get dispatch :command))
1427 (teardown (plist-get dispatch :teardown)))
1428 (when command
1429 (context-coloring-teardown-idle-change-detection))
1430 (when teardown
1431 (funcall teardown)))))
1432 (font-lock-mode)
1433 (jit-lock-mode t))
1434
1435 ;; Font lock is incompatible with this mode; the converse is also true.
1436 (font-lock-mode 0)
1437 (jit-lock-mode nil)
1438
1439 ;; ...but we do use font-lock functions here.
1440 (font-lock-set-defaults)
1441
1442 ;; Safely change the valye of this function as necessary.
1443 (make-local-variable 'font-lock-syntactic-face-function)
1444
1445 (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
1446 (if dispatch
1447 (progn
1448 (let ((command (plist-get dispatch :command))
1449 (version (plist-get dispatch :version))
1450 (executable (plist-get dispatch :executable))
1451 (setup (plist-get dispatch :setup))
1452 (colorize-initially-p t))
1453 (when command
1454 ;; Shell commands recolor on change, idly.
1455 (cond
1456 ((and executable
1457 (null (executable-find executable)))
1458 (message "Executable \"%s\" not found" executable)
1459 (setq colorize-initially-p nil))
1460 (version
1461 (context-coloring-check-scopifier-version
1462 (lambda (sufficient-p)
1463 (if sufficient-p
1464 (progn
1465 (context-coloring-setup-idle-change-detection)
1466 (context-coloring-colorize))
1467 (message "Update to the minimum version of \"%s\" (%s)"
1468 executable version))))
1469 (setq colorize-initially-p nil))
1470 (t
1471 (context-coloring-setup-idle-change-detection))))
1472 (when setup
1473 (funcall setup))
1474 ;; Colorize once initially.
1475 (when colorize-initially-p
1476 (let ((context-coloring-parse-interruptable-p nil))
1477 (context-coloring-colorize)))))
1478 (when (null dispatch)
1479 (message "Context coloring is not available for this major mode"))))))
1480
1481 (provide 'context-coloring)
1482
1483 ;;; context-coloring.el ends here