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