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