1 ;;; context-coloring.el --- Syntax highlighting, except not for syntax. -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2014 Jackson Ray Hamilton
5 ;; Author: Jackson Ray Hamilton <jackson@jacksonrayhamilton.com>
6 ;; Keywords: context coloring syntax highlighting
8 ;; Package-Requires: ((emacs "24") (js2-mode "20141228"))
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;; Highlights code according to function context.
27 ;; To use, add the following to your ~/.emacs:
29 ;; (require 'context-coloring)
30 ;; (add-hook 'js-mode-hook 'context-coloring-mode)
39 (defconst context-coloring-path
40 (file-name-directory (or load-file-name buffer-file-name))
41 "This file's directory.")
44 ;;; Customizable options
46 (defcustom context-coloring-delay 0.25
47 "Delay between a buffer update and colorization.
49 Increase this if your machine is high-performing. Decrease it if it ain't.
51 Supported modes: `js-mode', `js3-mode'"
52 :group 'context-coloring)
54 (defcustom context-coloring-comments-and-strings t
55 "If non-nil, also color comments and strings using `font-lock'."
56 :group 'context-coloring)
58 (defcustom context-coloring-js-block-scopes nil
59 "If non-nil, also color block scopes in the scope hierarchy in JavaScript.
61 The block-scope-inducing `let' and `const' are introduced in
62 ES6. If you are writing ES6 code, enable this; otherwise, don't.
64 Supported modes: `js2-mode'"
65 :group 'context-coloring)
67 (defcustom context-coloring-benchmark-colorization nil
68 "If non-nil, track how long colorization takes and print
69 messages with the colorization duration."
70 :group 'context-coloring)
75 (defvar-local context-coloring-buffer nil
76 "Reference to this buffer (for timers).")
78 (defvar-local context-coloring-scopifier-process nil
79 "Reference to the single scopifier process that can be
82 (defvar-local context-coloring-colorize-idle-timer nil
83 "Reference to the currently-running idle timer.")
85 (defvar-local context-coloring-changed nil
86 "Indication that the buffer has changed recently, which would
87 imply that it should be colorized again by
88 `context-coloring-colorize-idle-timer' if that timer is being
94 (defun context-coloring-defface (level tty light dark)
95 (let ((face (intern (format "context-coloring-level-%s-face" level)))
96 (doc (format "Context coloring face, level %s." level)))
97 (eval (macroexpand `(defface ,face
98 '((((type tty)) (:foreground ,tty))
99 (((background light)) (:foreground ,light))
100 (((background dark)) (:foreground ,dark)))
102 :group 'context-coloring)))))
104 (defvar context-coloring-face-count nil
105 "Number of faces available for context coloring.")
107 (defun context-coloring-defface-default (level)
108 (context-coloring-defface level "white" "#3f3f3f" "#cdcdcd"))
110 (defun context-coloring-set-colors-default ()
111 (context-coloring-defface 0 "white" "#000000" "#ffffff")
112 (context-coloring-defface 1 "yellow" "#007f80" "#ffff80")
113 (context-coloring-defface 2 "green" "#001580" "#cdfacd")
114 (context-coloring-defface 3 "cyan" "#550080" "#d8d8ff")
115 (context-coloring-defface 4 "blue" "#802b00" "#e7c7ff")
116 (context-coloring-defface 5 "magenta" "#6a8000" "#ffcdcd")
117 (context-coloring-defface 6 "red" "#008000" "#ffe390")
118 (context-coloring-defface-default 7)
119 (setq context-coloring-face-count 8))
121 (context-coloring-set-colors-default)
125 (defsubst context-coloring-face-symbol (level)
126 "Returns a symbol for a face with LEVEL."
127 ;; `concat' is faster than `format' here.
128 (intern-soft (concat "context-coloring-level-" (number-to-string level) "-face")))
130 (defun context-coloring-set-colors (&rest colors)
131 "Set context coloring's levels' coloring to COLORS, where the
132 Nth element of COLORS is level N's color."
133 (setq context-coloring-face-count (length colors))
135 (dolist (color colors)
136 ;; Ensure there are available faces to contain new colors.
137 (when (not (context-coloring-face-symbol level))
138 (context-coloring-defface-default level))
139 (set-face-foreground (context-coloring-face-symbol level) color)
140 (setq level (+ level 1)))))
142 (defsubst context-coloring-level-face (level)
143 "Returns the face name for LEVEL."
144 (context-coloring-face-symbol (min level context-coloring-face-count)))
147 ;;; Colorization utilities
149 (defsubst context-coloring-colorize-region (start end level)
150 "Colorizes characters from the 1-indexed START (inclusive) to
151 END (exclusive) with the face corresponding to LEVEL."
155 `(face ,(context-coloring-level-face level))))
157 (defsubst context-coloring-maybe-colorize-comments-and-strings ()
158 (when context-coloring-comments-and-strings
160 (font-lock-fontify-syntactically-region (point-min) (point-max)))))
163 ;;; js2-mode colorization
165 (defvar-local context-coloring-js2-scope-level-hash-table nil
166 "Associates `js2-scope' structures and with their scope
169 (defsubst context-coloring-js2-scope-level (scope)
170 "Gets the level of SCOPE."
171 (cond ((gethash scope context-coloring-js2-scope-level-hash-table))
174 (current-scope scope)
176 (while (and current-scope
177 (js2-node-parent current-scope)
178 (setq enclosing-scope (js2-node-get-enclosing-scope current-scope)))
179 (when (or context-coloring-js-block-scopes
180 (let ((type (js2-scope-type current-scope)))
181 (or (= type js2-SCRIPT)
182 (= type js2-FUNCTION)
183 (= type js2-CATCH))))
184 (setq level (+ level 1)))
185 (setq current-scope enclosing-scope))
186 (puthash scope level context-coloring-js2-scope-level-hash-table)))))
188 (defsubst context-coloring-js2-local-name-node-p (node)
189 "Determines if NODE is a js2-name-node representing a local
191 (and (js2-name-node-p node)
192 (let ((parent (js2-node-parent node)))
193 (not (or (and (js2-object-prop-node-p parent)
194 (eq node (js2-object-prop-node-left parent)))
195 (and (js2-prop-get-node-p parent)
196 ;; For nested property lookup, the node on the left is a
197 ;; `js2-prop-get-node', so this always works.
198 (eq node (js2-prop-get-node-right parent))))))))
200 (defsubst context-coloring-js2-colorize-node (node level)
201 "Colors NODE with the color for LEVEL."
202 (let ((start (js2-node-abs-pos node)))
203 (context-coloring-colorize-region
205 (+ start (js2-node-len node)) ; End
208 (defun context-coloring-js2-colorize ()
209 "Colorizes the current buffer using the abstract syntax tree
210 generated by js2-mode."
211 ;; Reset the hash table; the old one could be obsolete.
212 (setq context-coloring-js2-scope-level-hash-table (make-hash-table :test 'eq))
213 (with-silent-modifications
220 (context-coloring-js2-colorize-node
222 (context-coloring-js2-scope-level node)))
223 ((context-coloring-js2-local-name-node-p node)
224 (let* ((enclosing-scope (js2-node-get-enclosing-scope node))
225 (defining-scope (js2-get-defining-scope
227 (js2-name-node-name node))))
228 ;; The tree seems to be walked lexically, so an entire scope will
229 ;; be colored, including its name nodes, before they are
230 ;; reached. Coloring the nodes defined in that scope would be
231 ;; redundant, so don't do it.
232 (when (not (eq defining-scope enclosing-scope))
233 (context-coloring-js2-colorize-node
235 (context-coloring-js2-scope-level defining-scope))))))
236 ;; The `t' indicates to search children.
238 (context-coloring-maybe-colorize-comments-and-strings)))
241 ;;; Shell command scopification / colorization
243 (defun context-coloring-apply-tokens (tokens)
244 "Processes a vector of TOKENS to apply context-based coloring
245 to the current buffer. Tokens are 3 integers: start, end,
246 level. The vector is flat, with a new token occurring after every
248 (with-silent-modifications
250 (len (length tokens)))
252 (context-coloring-colorize-region
255 (elt tokens (+ i 2)))
257 (context-coloring-maybe-colorize-comments-and-strings)))
259 (defun context-coloring-parse-array (input)
260 "Specialized JSON parser for a flat array of numbers."
261 (vconcat (mapcar 'string-to-number (split-string (substring input 1 -1) ","))))
263 (defun context-coloring-kill-scopifier ()
264 "Kills the currently-running scopifier process for this
266 (when (not (null context-coloring-scopifier-process))
267 (delete-process context-coloring-scopifier-process)
268 (setq context-coloring-scopifier-process nil)))
270 (defun context-coloring-scopify-shell-command (command &optional callback)
271 "Invokes a scopifier with the current buffer's contents,
272 reading the scopifier's response asynchronously and applying a
273 parsed list of tokens to `context-coloring-apply-tokens'.
275 Invokes CALLBACK when complete."
277 ;; Prior running tokenization is implicitly obsolete if this function is
279 (context-coloring-kill-scopifier)
281 ;; Start the process.
282 (setq context-coloring-scopifier-process
283 (start-process-shell-command "scopifier" nil command))
286 (buffer context-coloring-buffer))
288 ;; The process may produce output in multiple chunks. This filter
289 ;; accumulates the chunks into a message.
291 context-coloring-scopifier-process
292 (lambda (_process chunk)
293 (setq output (concat output chunk))))
295 ;; When the process's message is complete, this sentinel parses it as JSON
296 ;; and applies the tokens to the buffer.
297 (set-process-sentinel
298 context-coloring-scopifier-process
299 (lambda (_process event)
300 (when (equal "finished\n" event)
301 (let ((tokens (context-coloring-parse-array output)))
302 (with-current-buffer buffer
303 (context-coloring-apply-tokens tokens))
304 (setq context-coloring-scopifier-process nil)
305 (if callback (funcall callback)))))))
307 ;; Give the process its input so it can begin.
308 (process-send-region context-coloring-scopifier-process (point-min) (point-max))
309 (process-send-eof context-coloring-scopifier-process))
314 (defvar context-coloring-javascript-scopifier
315 `(:type shell-command
317 :command ,(expand-file-name
318 "./languages/javascript/binaries/scopifier"
319 context-coloring-path)))
321 (defvar context-coloring-js2-colorizer
323 :colorizer context-coloring-js2-colorize))
325 (defcustom context-coloring-dispatch-plist
326 `(js-mode ,context-coloring-javascript-scopifier
327 js2-mode ,context-coloring-js2-colorizer
328 js3-mode ,context-coloring-javascript-scopifier)
329 "Property list mapping major modes to scopification and
330 colorization programs."
331 :group 'context-coloring)
333 (defun context-coloring-dispatch (&optional callback)
334 "Determines the optimal track for scopification / colorization
335 of the current buffer, then executes it.
337 Invokes CALLBACK when complete. It is invoked synchronously for
338 elisp tracks, and asynchronously for shell command tracks."
339 (let ((dispatch (plist-get context-coloring-dispatch-plist major-mode)))
341 (message "%s" "Context coloring is not available for this major mode"))
342 (let ((type (plist-get dispatch :type)))
345 (let ((colorizer (plist-get dispatch :colorizer))
346 (scopifier (plist-get dispatch :scopifier)))
350 (if callback (funcall callback)))
352 (context-coloring-apply-tokens (funcall scopifier))
353 (if callback (funcall callback)))
355 (error "No `:colorizer' nor `:scopifier' specified for dispatch of `:type' elisp")))))
356 ((eq type 'shell-command)
357 (let ((executable (plist-get dispatch :executable))
358 (command (plist-get dispatch :command)))
360 (error "No `:command' specified for dispatch of `:type' shell-command"))
361 (if (and (not (null executable))
362 (null (executable-find executable)))
363 (message "Executable \"%s\" not found" executable))
364 (context-coloring-scopify-shell-command command callback)))))))
369 (defun context-coloring-colorize (&optional callback)
370 "Colors the current buffer by function context.
372 Invokes CALLBACK when complete; see `context-coloring-dispatch'."
374 (let ((start-time (float-time)))
375 (context-coloring-dispatch
377 (when context-coloring-benchmark-colorization
378 (message "Colorization took %.3f seconds" (- (float-time) start-time)))
379 (if callback (funcall callback))))))
381 (defun context-coloring-change-function (_start _end _length)
382 "Registers a change so that a buffer can be colorized soon."
383 ;; Tokenization is obsolete if there was a change.
384 (context-coloring-kill-scopifier)
385 (setq context-coloring-changed t))
387 (defun context-coloring-maybe-colorize ()
388 "Colorize unders certain conditions. This will run as an idle
389 timer, so firstly the buffer must not be some other
390 buffer. Additionally, the buffer must have changed, otherwise
391 colorizing would be redundant."
392 (when (and (eq context-coloring-buffer (window-buffer (selected-window)))
393 context-coloring-changed)
394 (setq context-coloring-changed nil)
395 (context-coloring-colorize)))
401 (define-minor-mode context-coloring-mode
402 "Context-based code coloring, inspired by Douglas Crockford."
404 (if (not context-coloring-mode)
406 (context-coloring-kill-scopifier)
407 (when context-coloring-colorize-idle-timer
408 (cancel-timer context-coloring-colorize-idle-timer))
409 (remove-hook 'js2-post-parse-callbacks 'context-coloring-colorize t)
410 (remove-hook 'after-change-functions 'context-coloring-change-function t)
414 ;; Remember this buffer. This value should not be dynamically-bound.
415 (setq context-coloring-buffer (current-buffer))
417 ;; Font lock is incompatible with this mode; the converse is also true.
421 ;; Colorize once initially.
422 (context-coloring-colorize)
425 ((equal major-mode 'js2-mode)
426 ;; Only recolor on reparse.
427 (add-hook 'js2-post-parse-callbacks 'context-coloring-colorize nil t))
429 ;; Only recolor on change.
430 (add-hook 'after-change-functions 'context-coloring-change-function nil t)))
432 (when (not (equal major-mode 'js2-mode))
433 ;; Only recolor idly.
434 (setq context-coloring-colorize-idle-timer
435 (run-with-idle-timer context-coloring-delay t 'context-coloring-maybe-colorize)))))
437 (provide 'context-coloring)
440 ;; eval: (when (fboundp 'rainbow-mode) (rainbow-mode 1))
443 ;;; context-coloring.el ends here