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