]> code.delx.au - gnu-emacs-elpa/blob - context-coloring.el
Simplify `context-coloring-set-colors'.
[gnu-emacs-elpa] / context-coloring.el
1 ;;; context-coloring.el --- Syntax highlighting, except not for syntax. -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2014 Jackson Ray Hamilton
4
5 ;; Author: Jackson Ray Hamilton <jackson@jacksonrayhamilton.com>
6 ;; Keywords: context coloring syntax highlighting
7 ;; Version: 2.1.0
8 ;; Package-Requires: ((emacs "24") (js2-mode "20141228"))
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;; Highlights code according to function context.
26
27 ;; To use, add the following to your ~/.emacs:
28
29 ;; (require 'context-coloring)
30 ;; (add-hook 'js-mode-hook 'context-coloring-mode)
31
32 ;;; Code:
33
34 (require 'js2-mode)
35
36
37 ;;; Constants
38
39 (defconst context-coloring-path
40 (file-name-directory (or load-file-name buffer-file-name))
41 "This file's directory.")
42
43
44 ;;; Customizable options
45
46 (defcustom context-coloring-delay 0.25
47 "Delay between a buffer update and colorization.
48
49 Increase this if your machine is high-performing. Decrease it if it ain't.
50
51 Supported modes: `js-mode', `js3-mode'"
52 :group 'context-coloring)
53
54 (defcustom context-coloring-comments-and-strings t
55 "If non-nil, also color comments and strings using `font-lock'."
56 :group 'context-coloring)
57
58 (defcustom context-coloring-js-block-scopes nil
59 "If non-nil, also color block scopes in the scope hierarchy in JavaScript.
60
61 The block-scope-inducing `let' and `const' are introduced in
62 ES6. If you are writing ES6 code, enable this; otherwise, don't.
63
64 Supported modes: `js2-mode'"
65 :group 'context-coloring)
66
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)
71
72
73 ;;; Local variables
74
75 (defvar-local context-coloring-buffer nil
76 "Reference to this buffer (for timers).")
77
78 (defvar-local context-coloring-scopifier-process nil
79 "Reference to the single scopifier process that can be
80 running.")
81
82 (defvar-local context-coloring-colorize-idle-timer nil
83 "Reference to the currently-running idle timer.")
84
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
89 used.")
90
91
92 ;;; Faces
93
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)))
101 ,doc
102 :group 'context-coloring)))))
103
104 (defvar context-coloring-face-count nil
105 "Number of faces available for context coloring.")
106
107 (defun context-coloring-defface-default (level)
108 (context-coloring-defface level "white" "#3f3f3f" "#cdcdcd"))
109
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))
120
121 (context-coloring-set-colors-default)
122
123 ;;; Face functions
124
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")))
129
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))
134 (let ((level 0))
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)))))
141
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)))
145
146
147 ;;; Colorization utilities
148
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."
152 (add-text-properties
153 start
154 end
155 `(face ,(context-coloring-level-face level))))
156
157 (defsubst context-coloring-maybe-colorize-comments-and-strings ()
158 (when context-coloring-comments-and-strings
159 (save-excursion
160 (font-lock-fontify-syntactically-region (point-min) (point-max)))))
161
162
163 ;;; js2-mode colorization
164
165 (defvar-local context-coloring-js2-scope-level-hash-table nil
166 "Associates `js2-scope' structures and with their scope
167 levels.")
168
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))
172 (t
173 (let ((level 0)
174 (current-scope scope)
175 enclosing-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)))))
187
188 (defsubst context-coloring-js2-local-name-node-p (node)
189 "Determines if NODE is a js2-name-node representing a local
190 variable."
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))))))))
199
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
204 start
205 (+ start (js2-node-len node)) ; End
206 level)))
207
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
214 (js2-visit-ast
215 js2-mode-ast
216 (lambda (node end-p)
217 (when (null end-p)
218 (cond
219 ((js2-scope-p node)
220 (context-coloring-js2-colorize-node
221 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
226 enclosing-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
234 node
235 (context-coloring-js2-scope-level defining-scope))))))
236 ;; The `t' indicates to search children.
237 t)))
238 (context-coloring-maybe-colorize-comments-and-strings)))
239
240
241 ;;; Shell command scopification / colorization
242
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
247 3rd element."
248 (with-silent-modifications
249 (let ((i 0)
250 (len (length tokens)))
251 (while (< i len)
252 (context-coloring-colorize-region
253 (elt tokens i)
254 (elt tokens (+ i 1))
255 (elt tokens (+ i 2)))
256 (setq i (+ i 3))))
257 (context-coloring-maybe-colorize-comments-and-strings)))
258
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) ","))))
262
263 (defun context-coloring-kill-scopifier ()
264 "Kills the currently-running scopifier process for this
265 buffer."
266 (when (not (null context-coloring-scopifier-process))
267 (delete-process context-coloring-scopifier-process)
268 (setq context-coloring-scopifier-process nil)))
269
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'.
274
275 Invokes CALLBACK when complete."
276
277 ;; Prior running tokenization is implicitly obsolete if this function is
278 ;; called.
279 (context-coloring-kill-scopifier)
280
281 ;; Start the process.
282 (setq context-coloring-scopifier-process
283 (start-process-shell-command "scopifier" nil command))
284
285 (let ((output "")
286 (buffer context-coloring-buffer))
287
288 ;; The process may produce output in multiple chunks. This filter
289 ;; accumulates the chunks into a message.
290 (set-process-filter
291 context-coloring-scopifier-process
292 (lambda (_process chunk)
293 (setq output (concat output chunk))))
294
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)))))))
306
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))
310
311
312 ;;; Dispatch
313
314 (defvar context-coloring-javascript-scopifier
315 `(:type shell-command
316 :executable "node"
317 :command ,(expand-file-name
318 "./languages/javascript/binaries/scopifier"
319 context-coloring-path)))
320
321 (defvar context-coloring-js2-colorizer
322 `(:type elisp
323 :colorizer context-coloring-js2-colorize))
324
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)
332
333 (defun context-coloring-dispatch (&optional callback)
334 "Determines the optimal track for scopification / colorization
335 of the current buffer, then executes it.
336
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)))
340 (if (null dispatch)
341 (message "%s" "Context coloring is not available for this major mode"))
342 (let ((type (plist-get dispatch :type)))
343 (cond
344 ((eq type 'elisp)
345 (let ((colorizer (plist-get dispatch :colorizer))
346 (scopifier (plist-get dispatch :scopifier)))
347 (cond
348 (colorizer
349 (funcall colorizer)
350 (if callback (funcall callback)))
351 (scopifier
352 (context-coloring-apply-tokens (funcall scopifier))
353 (if callback (funcall callback)))
354 (t
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)))
359 (if (null 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)))))))
365
366
367 ;;; Colorization
368
369 (defun context-coloring-colorize (&optional callback)
370 "Colors the current buffer by function context.
371
372 Invokes CALLBACK when complete; see `context-coloring-dispatch'."
373 (interactive)
374 (let ((start-time (float-time)))
375 (context-coloring-dispatch
376 (lambda ()
377 (when context-coloring-benchmark-colorization
378 (message "Colorization took %.3f seconds" (- (float-time) start-time)))
379 (if callback (funcall callback))))))
380
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))
386
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)))
396
397
398 ;;; Minor mode
399
400 ;;;###autoload
401 (define-minor-mode context-coloring-mode
402 "Context-based code coloring, inspired by Douglas Crockford."
403 nil " Context" nil
404 (if (not context-coloring-mode)
405 (progn
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)
411 (font-lock-mode)
412 (jit-lock-mode t))
413
414 ;; Remember this buffer. This value should not be dynamically-bound.
415 (setq context-coloring-buffer (current-buffer))
416
417 ;; Font lock is incompatible with this mode; the converse is also true.
418 (font-lock-mode 0)
419 (jit-lock-mode nil)
420
421 ;; Colorize once initially.
422 (context-coloring-colorize)
423
424 (cond
425 ((equal major-mode 'js2-mode)
426 ;; Only recolor on reparse.
427 (add-hook 'js2-post-parse-callbacks 'context-coloring-colorize nil t))
428 (t
429 ;; Only recolor on change.
430 (add-hook 'after-change-functions 'context-coloring-change-function nil t)))
431
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)))))
436
437 (provide 'context-coloring)
438
439 ;; Local Variables:
440 ;; eval: (when (fboundp 'rainbow-mode) (rainbow-mode 1))
441 ;; End:
442
443 ;;; context-coloring.el ends here