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