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