]> code.delx.au - gnu-emacs-elpa/blob - packages/context-coloring/context-coloring.el
Add 'packages/loc-changes/' from commit '8447baff7cb4839ef8d1d747a14e5da85d0cee5b'
[gnu-emacs-elpa] / packages / context-coloring / context-coloring.el
1 ;;; context-coloring.el --- Syntax highlighting, except not for syntax. -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
4
5 ;; Author: Jackson Ray Hamilton <jackson@jacksonrayhamilton.com>
6 ;; URL: https://github.com/jacksonrayhamilton/context-coloring
7 ;; Keywords: context coloring syntax highlighting
8 ;; Version: 6.0.0
9 ;; Package-Requires: ((emacs "24") (js2-mode "20150126"))
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 according to function context.
29
30 ;; - Code in the global scope is one color. Code in functions within the global
31 ;; scope is a different color, and code within such functions is another
32 ;; color, and so on.
33 ;; - Identifiers retain the color of the scope in which they are declared.
34
35 ;; Lexical scope information at-a-glance can assist a programmer in
36 ;; understanding the overall structure of a program. It can help to curb nasty
37 ;; bugs like name shadowing. A rainbow can indicate excessive complexity.
38 ;; State change within a closure is easily monitored.
39
40 ;; By default, Context Coloring still highlights comments and strings
41 ;; syntactically. It is still easy to differentiate code from non-code, and
42 ;; strings cannot be confused for variables.
43
44 ;; To use, add the following to your ~/.emacs:
45
46 ;; (require 'context-coloring)
47 ;; (add-hook 'js2-mode-hook 'context-coloring-mode)
48
49 ;; js-mode or js3-mode support requires Node.js 0.10+ and the scopifier
50 ;; executable.
51
52 ;; $ npm install -g scopifier
53
54 ;;; Code:
55
56 (require 'js2-mode)
57
58
59 ;;; Local variables
60
61 (defvar-local context-coloring-buffer nil
62 "Reference to this buffer (for timers).")
63
64
65 ;;; Faces
66
67 (defun context-coloring-defface (level tty light dark)
68 "Define a face for LEVEL with colors for TTY, LIGHT and DARK
69 backgrounds."
70 (let ((face (intern (format "context-coloring-level-%s-face" level)))
71 (doc (format "Context coloring face, level %s." level)))
72 (custom-declare-face
73 face
74 `((((type tty)) (:foreground ,tty))
75 (((background light)) (:foreground ,light))
76 (((background dark)) (:foreground ,dark)))
77 doc
78 :group 'context-coloring)))
79
80 (defun context-coloring-defface-neutral (level)
81 "Define a face for LEVEL with the default neutral colors."
82 (context-coloring-defface level nil "#3f3f3f" "#cdcdcd"))
83
84 (context-coloring-defface 0 nil "#000000" "#ffffff")
85 (context-coloring-defface 1 "yellow" "#007f80" "#ffff80")
86 (context-coloring-defface 2 "green" "#001580" "#cdfacd")
87 (context-coloring-defface 3 "cyan" "#550080" "#d8d8ff")
88 (context-coloring-defface 4 "blue" "#802b00" "#e7c7ff")
89 (context-coloring-defface 5 "magenta" "#6a8000" "#ffcdcd")
90 (context-coloring-defface 6 "red" "#008000" "#ffe390")
91 (context-coloring-defface-neutral 7)
92
93 (defvar context-coloring-maximum-face nil
94 "Index of the highest face available for coloring.")
95
96 (defvar context-coloring-original-maximum-face nil
97 "Fallback value for `context-coloring-maximum-face' when all
98 themes have been disabled.")
99
100 (setq context-coloring-maximum-face 7)
101
102 (setq context-coloring-original-maximum-face
103 context-coloring-maximum-face)
104
105 ;; Theme authors can have up to 26 levels: 1 (0th) for globals, 24 (1st-24th)
106 ;; for nested levels, and 1 (25th) for infinity.
107 (dotimes (number 18)
108 (context-coloring-defface-neutral (+ number context-coloring-maximum-face 1)))
109
110
111 ;;; Face functions
112
113 (defsubst context-coloring-level-face (level)
114 "Return the symbol for a face with LEVEL."
115 ;; `concat' is faster than `format' here.
116 (intern-soft
117 (concat "context-coloring-level-" (number-to-string level) "-face")))
118
119 (defsubst context-coloring-bounded-level-face (level)
120 "Return the symbol for a face with LEVEL, bounded by
121 `context-coloring-maximum-face'."
122 (context-coloring-level-face (min level context-coloring-maximum-face)))
123
124
125 ;;; Colorization utilities
126
127 (defsubst context-coloring-colorize-region (start end level)
128 "Color characters from the 1-indexed START point (inclusive) to
129 the END point (exclusive) with the face corresponding to LEVEL."
130 (add-text-properties
131 start
132 end
133 `(face ,(context-coloring-bounded-level-face level))))
134
135 (defcustom context-coloring-comments-and-strings t
136 "If non-nil, also color comments and strings using `font-lock'."
137 :group 'context-coloring)
138
139 (defsubst context-coloring-maybe-colorize-comments-and-strings ()
140 "Color the current buffer's comments and strings if
141 `context-coloring-comments-and-strings' is non-nil."
142 (when context-coloring-comments-and-strings
143 (save-excursion
144 (font-lock-fontify-syntactically-region (point-min) (point-max)))))
145
146
147 ;;; js2-mode colorization
148
149 (defvar-local context-coloring-js2-scope-level-hash-table nil
150 "Associate `js2-scope' structures and with their scope
151 levels.")
152
153 (defcustom context-coloring-js-block-scopes nil
154 "If non-nil, also color block scopes in the scope hierarchy in JavaScript.
155
156 The block-scoped `let' and `const' are introduced in ES6. Enable
157 this for ES6 code; disable it elsewhere.
158
159 Supported modes: `js2-mode'"
160 :group 'context-coloring)
161
162 (defsubst context-coloring-js2-scope-level (scope)
163 "Return the level of SCOPE."
164 (cond ((gethash scope context-coloring-js2-scope-level-hash-table))
165 (t
166 (let ((level 0)
167 (current-scope scope)
168 enclosing-scope)
169 (while (and current-scope
170 (js2-node-parent current-scope)
171 (setq enclosing-scope
172 (js2-node-get-enclosing-scope current-scope)))
173 (when (or context-coloring-js-block-scopes
174 (let ((type (js2-scope-type current-scope)))
175 (or (= type js2-SCRIPT)
176 (= type js2-FUNCTION)
177 (= type js2-CATCH))))
178 (setq level (+ level 1)))
179 (setq current-scope enclosing-scope))
180 (puthash scope level context-coloring-js2-scope-level-hash-table)))))
181
182 (defsubst context-coloring-js2-local-name-node-p (node)
183 "Determine if NODE is a `js2-name-node' representing a local
184 variable."
185 (and (js2-name-node-p node)
186 (let ((parent (js2-node-parent node)))
187 (not (or (and (js2-object-prop-node-p parent)
188 (eq node (js2-object-prop-node-left parent)))
189 (and (js2-prop-get-node-p parent)
190 ;; For nested property lookup, the node on the left is a
191 ;; `js2-prop-get-node', so this always works.
192 (eq node (js2-prop-get-node-right parent))))))))
193
194 (defsubst context-coloring-js2-colorize-node (node level)
195 "Color NODE with the color for LEVEL."
196 (let ((start (js2-node-abs-pos node)))
197 (context-coloring-colorize-region
198 start
199 (+ start (js2-node-len node)) ; End
200 level)))
201
202 (defun context-coloring-js2-colorize ()
203 "Color the current buffer using the abstract syntax tree
204 generated by `js2-mode'."
205 ;; Reset the hash table; the old one could be obsolete.
206 (setq context-coloring-js2-scope-level-hash-table (make-hash-table :test 'eq))
207 (with-silent-modifications
208 (js2-visit-ast
209 js2-mode-ast
210 (lambda (node end-p)
211 (when (null end-p)
212 (cond
213 ((js2-scope-p node)
214 (context-coloring-js2-colorize-node
215 node
216 (context-coloring-js2-scope-level node)))
217 ((context-coloring-js2-local-name-node-p node)
218 (let* ((enclosing-scope (js2-node-get-enclosing-scope node))
219 (defining-scope (js2-get-defining-scope
220 enclosing-scope
221 (js2-name-node-name node))))
222 ;; The tree seems to be walked lexically, so an entire scope will
223 ;; be colored, including its name nodes, before they are reached.
224 ;; Coloring the nodes defined in that scope would be redundant, so
225 ;; don't do it.
226 (when (not (eq defining-scope enclosing-scope))
227 (context-coloring-js2-colorize-node
228 node
229 (context-coloring-js2-scope-level defining-scope))))))
230 ;; The `t' indicates to search children.
231 t)))
232 (context-coloring-maybe-colorize-comments-and-strings)))
233
234
235 ;;; Shell command scopification / colorization
236
237 (defun context-coloring-apply-tokens (tokens)
238 "Process a vector of TOKENS to apply context-based coloring to
239 the current buffer. Tokens are 3 integers: start, end, level.
240 The vector is flat, with a new token occurring after every 3rd
241 element."
242 (with-silent-modifications
243 (let ((i 0)
244 (len (length tokens)))
245 (while (< i len)
246 (context-coloring-colorize-region
247 (elt tokens i)
248 (elt tokens (+ i 1))
249 (elt tokens (+ i 2)))
250 (setq i (+ i 3))))
251 (context-coloring-maybe-colorize-comments-and-strings)))
252
253 (defun context-coloring-parse-array (array)
254 "Parse ARRAY as a flat JSON array of numbers."
255 (vconcat
256 (mapcar 'string-to-number (split-string (substring array 1 -1) ","))))
257
258 (defvar-local context-coloring-scopifier-process nil
259 "The single scopifier process that can be running.")
260
261 (defun context-coloring-kill-scopifier ()
262 "Kill the currently-running scopifier process."
263 (when (not (null context-coloring-scopifier-process))
264 (delete-process context-coloring-scopifier-process)
265 (setq context-coloring-scopifier-process nil)))
266
267 (defun context-coloring-scopify-shell-command (command &optional callback)
268 "Invoke a scopifier via COMMAND with the current buffer's contents,
269 read the scopifier's response asynchronously and apply a parsed
270 list of tokens to `context-coloring-apply-tokens'.
271
272 Invoke CALLBACK when complete."
273
274 ;; Prior running tokenization is implicitly obsolete if this function is
275 ;; called.
276 (context-coloring-kill-scopifier)
277
278 ;; Start the process.
279 (setq context-coloring-scopifier-process
280 (start-process-shell-command "scopifier" nil command))
281
282 (let ((output "")
283 (buffer context-coloring-buffer))
284
285 ;; The process may produce output in multiple chunks. This filter
286 ;; accumulates the chunks into a message.
287 (set-process-filter
288 context-coloring-scopifier-process
289 (lambda (_process chunk)
290 (setq output (concat output chunk))))
291
292 ;; When the process's message is complete, this sentinel parses it as JSON
293 ;; and applies the tokens to the buffer.
294 (set-process-sentinel
295 context-coloring-scopifier-process
296 (lambda (_process event)
297 (when (equal "finished\n" event)
298 (let ((tokens (context-coloring-parse-array output)))
299 (with-current-buffer buffer
300 (context-coloring-apply-tokens tokens))
301 (setq context-coloring-scopifier-process nil)
302 (when callback (funcall callback)))))))
303
304 ;; Give the process its input so it can begin.
305 (process-send-region
306 context-coloring-scopifier-process
307 (point-min) (point-max))
308 (process-send-eof
309 context-coloring-scopifier-process))
310
311
312 ;;; Dispatch
313
314 (defvar context-coloring-dispatch-hash-table (make-hash-table :test 'eq)
315 "Map dispatch strategy names to their corresponding property
316 lists, which contain details about the strategies.")
317
318 (defvar context-coloring-mode-hash-table (make-hash-table :test 'eq)
319 "Map major mode names to dispatch property lists.")
320
321 (defun context-coloring-select-dispatch (mode dispatch)
322 "Use DISPATCH for MODE."
323 (puthash
324 mode
325 (gethash
326 dispatch
327 context-coloring-dispatch-hash-table)
328 context-coloring-mode-hash-table))
329
330 (defun context-coloring-define-dispatch (symbol &rest properties)
331 "Define a new dispatch named SYMBOL with PROPERTIES.
332
333 A \"dispatch\" is a property list describing a strategy for
334 coloring a buffer. There are three possible strategies: Parse
335 and color in a single function (`:colorizer'), parse in a
336 function that returns scope data (`:scopifier'), or parse with a
337 shell command that returns scope data (`:command'). In the
338 latter two cases, the scope data will be used to automatically
339 color the buffer.
340
341 PROPERTIES must include `:modes' and one of `:colorizer',
342 `:scopifier' or `:command'.
343
344 `:modes' - List of major modes this dispatch is valid for.
345
346 `:colorizer' - Symbol referring to a function that parses and
347 colors the buffer.
348
349 `:scopifier' - Symbol referring to a function that parses the
350 buffer a returns a flat vector of start, end and level data.
351
352 `:executable' - Optional name of an executable required by
353 `:command'.
354
355 `:command' - Shell command to execute with the current buffer
356 sent via stdin, and with a flat JSON array of start, end and
357 level data returned via stdout.
358
359 `:setup' - Arbitrary code to set up this dispatch when
360 `context-coloring-mode' is enabled.
361
362 `:teardown' - Arbitrary code to tear down this dispatch when
363 `context-coloring-mode' is disabled."
364 (let ((modes (plist-get properties :modes))
365 (colorizer (plist-get properties :colorizer))
366 (scopifier (plist-get properties :scopifier))
367 (command (plist-get properties :command)))
368 (when (null modes)
369 (error "No mode defined for dispatch"))
370 (when (not (or colorizer
371 scopifier
372 command))
373 (error "No colorizer, scopifier or command defined for dispatch"))
374 (puthash symbol properties context-coloring-dispatch-hash-table)
375 (dolist (mode modes)
376 (when (null (gethash mode context-coloring-mode-hash-table))
377 (puthash mode properties context-coloring-mode-hash-table)))))
378
379 (context-coloring-define-dispatch
380 'javascript-node
381 :modes '(js-mode js3-mode)
382 :executable "scopifier"
383 :command "scopifier")
384
385 (context-coloring-define-dispatch
386 'javascript-js2
387 :modes '(js2-mode)
388 :colorizer 'context-coloring-js2-colorize
389 :setup
390 (lambda ()
391 (add-hook 'js2-post-parse-callbacks 'context-coloring-colorize nil t))
392 :teardown
393 (lambda ()
394 (remove-hook 'js2-post-parse-callbacks 'context-coloring-colorize t)))
395
396 (defun context-coloring-dispatch (&optional callback)
397 "Determine the optimal track for scopification / coloring of
398 the current buffer, then execute it.
399
400 Invoke CALLBACK when complete. It is invoked synchronously for
401 elisp tracks, and asynchronously for shell command tracks."
402 (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
403 (when (null dispatch)
404 (message "%s" "Context coloring is not available for this major mode"))
405 (let (colorizer
406 scopifier
407 command
408 executable)
409 (cond
410 ((setq colorizer (plist-get dispatch :colorizer))
411 (funcall colorizer)
412 (when callback (funcall callback)))
413 ((setq scopifier (plist-get dispatch :scopifier))
414 (context-coloring-apply-tokens (funcall scopifier))
415 (when callback (funcall callback)))
416 ((setq command (plist-get dispatch :command))
417 (setq executable (plist-get dispatch :executable))
418 (if (and executable
419 (null (executable-find executable)))
420 (progn
421 (message "Executable \"%s\" not found" executable))
422 (context-coloring-scopify-shell-command command callback)))))))
423
424
425 ;;; Colorization
426
427 (defun context-coloring-colorize (&optional callback)
428 "Color the current buffer by function context.
429
430 Invoke CALLBACK when complete; see `context-coloring-dispatch'."
431 (interactive)
432 (context-coloring-dispatch
433 (lambda ()
434 (when callback (funcall callback)))))
435
436 (defvar-local context-coloring-changed nil
437 "Indication that the buffer has changed recently, which implies
438 that it should be colored again by
439 `context-coloring-colorize-idle-timer' if that timer is being
440 used.")
441
442 (defun context-coloring-change-function (_start _end _length)
443 "Register a change so that a buffer can be colorized soon."
444 ;; Tokenization is obsolete if there was a change.
445 (context-coloring-kill-scopifier)
446 (setq context-coloring-changed t))
447
448 (defun context-coloring-maybe-colorize ()
449 "Colorize the current buffer if it has changed."
450 (when (and (eq context-coloring-buffer (window-buffer (selected-window)))
451 context-coloring-changed)
452 (setq context-coloring-changed nil)
453 (context-coloring-colorize)))
454
455
456 ;;; Themes
457
458 (defvar context-coloring-theme-hash-table (make-hash-table :test 'eq)
459 "Map theme names to theme properties.")
460
461 (defun context-coloring-theme-p (theme)
462 "Return t if THEME is defined, nil otherwise."
463 (and (gethash theme context-coloring-theme-hash-table)))
464
465 (defconst context-coloring-level-face-regexp
466 "context-coloring-level-\\([[:digit:]]+\\)-face"
467 "Extract a level from a face.")
468
469 (defvar context-coloring-originally-set-theme-hash-table
470 (make-hash-table :test 'eq)
471 "Cache custom themes who originally set their own
472 `context-coloring-level-N-face' faces.")
473
474 (defun context-coloring-theme-originally-set-p (theme)
475 "Return t if there is a `context-coloring-level-N-face'
476 originally set for THEME, nil otherwise."
477 (let (originally-set)
478 (cond
479 ;; `setq' might return a non-nil value for the sake of this `cond'.
480 ((setq
481 originally-set
482 (gethash
483 theme
484 context-coloring-originally-set-theme-hash-table))
485 (eq originally-set 'yes))
486 (t
487 (let* ((settings (get theme 'theme-settings))
488 (tail settings)
489 found)
490 (while (and tail (not found))
491 (and (eq (nth 0 (car tail)) 'theme-face)
492 (string-match
493 context-coloring-level-face-regexp
494 (symbol-name (nth 1 (car tail))))
495 (setq found t))
496 (setq tail (cdr tail)))
497 found)))))
498
499 (defun context-coloring-cache-originally-set (theme originally-set)
500 "Remember if THEME had colors originally set for it. If
501 ORIGINALLY-SET is non-nil, it did, otherwise it didn't."
502 ;; Caching whether a theme was originally set is kind of dirty, but we have to
503 ;; do it to remember the past state of the theme. There are probably some
504 ;; edge cases where caching will be an issue, but they are probably rare.
505 (puthash
506 theme
507 (if originally-set 'yes 'no)
508 context-coloring-originally-set-theme-hash-table))
509
510 (defun context-coloring-warn-theme-originally-set (theme)
511 "Warn the user that the colors for THEME are already originally
512 set."
513 (warn "Context coloring colors for theme `%s' are already defined" theme))
514
515 (defun context-coloring-theme-highest-level (theme)
516 "Return the highest level N of a face like
517 `context-coloring-level-N-face' set for THEME, or `-1' if there
518 is none."
519 (let* ((settings (get theme 'theme-settings))
520 (tail settings)
521 face-string
522 number
523 (found -1))
524 (while tail
525 (and (eq (nth 0 (car tail)) 'theme-face)
526 (setq face-string (symbol-name (nth 1 (car tail))))
527 (string-match
528 context-coloring-level-face-regexp
529 face-string)
530 (setq number (string-to-number
531 (substring face-string
532 (match-beginning 1)
533 (match-end 1))))
534 (> number found)
535 (setq found number))
536 (setq tail (cdr tail)))
537 found))
538
539 (defun context-coloring-apply-theme (theme)
540 "Apply THEME's properties to its respective custom theme,
541 which must already exist and which *should* already be enabled."
542 (let* ((properties (gethash theme context-coloring-theme-hash-table))
543 (colors (plist-get properties :colors))
544 (level -1))
545 (setq context-coloring-maximum-face (- (length colors) 1))
546 (apply
547 'custom-theme-set-faces
548 theme
549 (mapcar
550 (lambda (color)
551 (setq level (+ level 1))
552 `(,(context-coloring-level-face level) ((t (:foreground ,color)))))
553 colors))))
554
555 (defun context-coloring-define-theme (theme &rest properties)
556 "Define a context theme named THEME for coloring scope levels.
557
558 PROPERTIES is a property list specifiying the following details:
559
560 `:aliases': List of symbols of other custom themes that these
561 colors are applicable to.
562
563 `:colors': List of colors that this context theme uses.
564
565 `:override': If non-nil, this context theme is intentionally
566 overriding colors set by a custom theme. Don't set this non-nil
567 unless there is a custom theme you want to use which sets
568 `context-coloring-level-N-face' faces that you want to replace.
569
570 `:recede': If non-nil, this context theme should not apply its
571 colors if a custom theme already sets
572 `context-coloring-level-N-face' faces. This option is
573 optimistic; set this non-nil if you would rather confer the duty
574 of picking colors to a custom theme author (if / when he ever
575 gets around to it).
576
577 By default, context themes will always override custom themes,
578 even if those custom themes set `context-coloring-level-N-face'
579 faces. If a context theme does override a custom theme, a
580 warning will be raised, at which point you may want to enable the
581 `:override' option, or just delete your context theme and opt to
582 use your custom theme's author's colors instead.
583
584 Context themes only work for the custom theme with the highest
585 precedence, i.e. the car of `custom-enabled-themes'."
586 (let ((aliases (plist-get properties :aliases))
587 (override (plist-get properties :override))
588 (recede (plist-get properties :recede)))
589 (dolist (name (append `(,theme) aliases))
590 (puthash name properties context-coloring-theme-hash-table)
591 (when (custom-theme-p name)
592 (let ((originally-set (context-coloring-theme-originally-set-p name)))
593 (context-coloring-cache-originally-set name originally-set)
594 ;; In the particular case when you innocently define colors that a
595 ;; custom theme originally set, warn. Arguably this only has to be
596 ;; done at enable time, but it is probably more useful to do it at
597 ;; definition time for prompter feedback.
598 (when (and originally-set
599 (not recede)
600 (not override))
601 (context-coloring-warn-theme-originally-set name))
602 ;; Set (or overwrite) colors.
603 (when (not (and originally-set
604 recede))
605 (context-coloring-apply-theme name)))))))
606
607 (defun context-coloring-enable-theme (theme)
608 "Apply THEME if its colors are not already set, else just set
609 `context-coloring-maximum-face' to the correct value for THEME."
610 (let* ((properties (gethash theme context-coloring-theme-hash-table))
611 (recede (plist-get properties :recede))
612 (override (plist-get properties :override)))
613 (cond
614 (recede
615 (let ((highest-level (context-coloring-theme-highest-level theme)))
616 (cond
617 ;; This can be true whether originally set by a custom theme or by a
618 ;; context theme.
619 ((> highest-level -1)
620 (setq context-coloring-maximum-face highest-level))
621 ;; It is possible that the corresponding custom theme did not exist at
622 ;; the time of defining this context theme, and in that case the above
623 ;; condition proves the custom theme did not originally set any faces,
624 ;; so we have license to apply the context theme for the first time
625 ;; here.
626 (t
627 (context-coloring-apply-theme theme)))))
628 (t
629 (let ((originally-set (context-coloring-theme-originally-set-p theme)))
630 ;; Cache now in case the context theme was defined after the custom
631 ;; theme.
632 (context-coloring-cache-originally-set theme originally-set)
633 (when (and originally-set
634 (not override))
635 (context-coloring-warn-theme-originally-set theme))
636 (context-coloring-apply-theme theme))))))
637
638 (defadvice enable-theme (after context-coloring-enable-theme (theme) activate)
639 "Enable colors for context themes just-in-time."
640 (when (and (not (eq theme 'user)) ; Called internally by `enable-theme'.
641 (custom-theme-p theme) ; Guard against non-existent themes.
642 (context-coloring-theme-p theme))
643 (when (= (length custom-enabled-themes) 0)
644 ;; Cache because we can't reliably figure it out in reverse.
645 (setq context-coloring-original-maximum-face
646 context-coloring-maximum-face))
647 (context-coloring-enable-theme theme)))
648
649 (defadvice disable-theme (after context-coloring-disable-theme (theme) activate)
650 "Update `context-coloring-maximum-face'."
651 (when (custom-theme-p theme) ; Guard against non-existent themes.
652 (let ((enabled-theme (car custom-enabled-themes)))
653 (if (context-coloring-theme-p enabled-theme)
654 (progn
655 (context-coloring-enable-theme enabled-theme))
656 ;; Assume we are back to no theme; act as if nothing ever happened.
657 ;; This is still prone to intervention, but rather extraordinarily.
658 (setq context-coloring-maximum-face
659 context-coloring-original-maximum-face)))))
660
661 (context-coloring-define-theme
662 'ample
663 :recede t
664 :colors '("#bdbdb3"
665 "#baba36"
666 "#6aaf50"
667 "#5180b3"
668 "#ab75c3"
669 "#cd7542"
670 "#dF9522"
671 "#454545"))
672
673 (context-coloring-define-theme
674 'anti-zenburn
675 :recede t
676 :colors '("#232333"
677 "#6c1f1c"
678 "#401440"
679 "#0f2050"
680 "#205070"
681 "#437c7c"
682 "#23733c"
683 "#6b400c"
684 "#603a60"
685 "#2f4070"
686 "#235c5c"))
687
688 (context-coloring-define-theme
689 'grandshell
690 :recede t
691 :colors '("#bebebe"
692 "#5af2ee"
693 "#b2baf6"
694 "#f09fff"
695 "#efc334"
696 "#f6df92"
697 "#acfb5a"
698 "#888888"))
699
700 (context-coloring-define-theme
701 'leuven
702 :recede t
703 :colors '("#333333"
704 "#0000FF"
705 "#6434A3"
706 "#BA36A5"
707 "#D0372D"
708 "#036A07"
709 "#006699"
710 "#006FE0"
711 "#808080"))
712
713 (context-coloring-define-theme
714 'monokai
715 :recede t
716 :colors '("#F8F8F2"
717 "#66D9EF"
718 "#A1EFE4"
719 "#A6E22E"
720 "#E6DB74"
721 "#FD971F"
722 "#F92672"
723 "#FD5FF0"
724 "#AE81FF"))
725
726 (context-coloring-define-theme
727 'solarized
728 :recede t
729 :aliases '(solarized-light
730 solarized-dark
731 sanityinc-solarized-light
732 sanityinc-solarized-dark)
733 :colors '("#839496"
734 "#268bd2"
735 "#2aa198"
736 "#859900"
737 "#b58900"
738 "#cb4b16"
739 "#dc322f"
740 "#d33682"
741 "#6c71c4"
742 "#69B7F0"
743 "#69CABF"
744 "#B4C342"
745 "#DEB542"
746 "#F2804F"
747 "#FF6E64"
748 "#F771AC"
749 "#9EA0E5"))
750
751 (context-coloring-define-theme
752 'spacegray
753 :recede t
754 :colors '("#ffffff"
755 "#89AAEB"
756 "#C189EB"
757 "#bf616a"
758 "#DCA432"
759 "#ebcb8b"
760 "#B4EB89"
761 "#89EBCA"))
762
763 (context-coloring-define-theme
764 'tango
765 :recede t
766 :colors '("#2e3436"
767 "#346604"
768 "#204a87"
769 "#5c3566"
770 "#a40000"
771 "#b35000"
772 "#c4a000"
773 "#8ae234"
774 "#8cc4ff"
775 "#ad7fa8"
776 "#ef2929"
777 "#fcaf3e"
778 "#fce94f"))
779
780 (context-coloring-define-theme
781 'zenburn
782 :recede t
783 :colors '("#DCDCCC"
784 "#93E0E3"
785 "#BFEBBF"
786 "#F0DFAF"
787 "#DFAF8F"
788 "#BC8383"
789 "#DC8CC3"
790 "#94BFF3"
791 "#9FC59F"
792 "#D0BF8F"
793 "#DCA3A3"))
794
795
796 ;;; Minor mode
797
798 (defvar-local context-coloring-colorize-idle-timer nil
799 "The currently-running idle timer.")
800
801 (defcustom context-coloring-delay 0.25
802 "Delay between a buffer update and colorization.
803
804 Increase this if your machine is high-performing. Decrease it if
805 it ain't.
806
807 Supported modes: `js-mode', `js3-mode'"
808 :group 'context-coloring)
809
810 ;;;###autoload
811 (define-minor-mode context-coloring-mode
812 "Context-based code coloring, inspired by Douglas Crockford."
813 nil " Context" nil
814 (if (not context-coloring-mode)
815 (progn
816 (context-coloring-kill-scopifier)
817 (when context-coloring-colorize-idle-timer
818 (cancel-timer context-coloring-colorize-idle-timer))
819 (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
820 (when dispatch
821 (let ((command (plist-get dispatch :command))
822 (teardown (plist-get dispatch :teardown)))
823 (when command
824 (remove-hook
825 'after-change-functions 'context-coloring-change-function t))
826 (when teardown
827 (funcall teardown)))))
828 (font-lock-mode)
829 (jit-lock-mode t))
830
831 ;; Remember this buffer. This value should not be dynamically-bound.
832 (setq context-coloring-buffer (current-buffer))
833
834 ;; Font lock is incompatible with this mode; the converse is also true.
835 (font-lock-mode 0)
836 (jit-lock-mode nil)
837
838 (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
839 (when dispatch
840 (let ((command (plist-get dispatch :command))
841 (setup (plist-get dispatch :setup)))
842 (when command
843 ;; Shell commands recolor on change, idly.
844 (add-hook
845 'after-change-functions 'context-coloring-change-function nil t)
846 (setq context-coloring-colorize-idle-timer
847 (run-with-idle-timer
848 context-coloring-delay
849 t
850 'context-coloring-maybe-colorize)))
851 (when setup
852 (funcall setup)))))
853
854 ;; Colorize once initially.
855 (context-coloring-colorize)))
856
857 (provide 'context-coloring)
858
859 ;;; context-coloring.el ends here