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