]> code.delx.au - gnu-emacs-elpa/blob - README.md
Renames. Cleanup.
[gnu-emacs-elpa] / README.md
1 # Context Coloring [![Build Status](https://travis-ci.org/jacksonrayhamilton/context-coloring.png?branch=develop)](https://travis-ci.org/jacksonrayhamilton/context-coloring)
2
3 <p align="center">
4 <img alt="Screenshot of JavaScript code highlighted by context." src="screenshot.png" title="Screenshot">
5 </p>
6
7 Highlights code according to function context.
8
9 - Code in the global scope is one color. Code in functions within the global
10 scope is a different color, and code within such functions is another color,
11 and so on.
12 - Identifiers retain the color of the scope in which they were declared.
13 - Comments are a neutral color.
14
15 Lexical scope information at-a-glance can assist a programmer in understanding
16 the overall structure of a program. It can also help curb nasty bugs like name
17 shadowing or unexpected assignment. A rainbow can indicate excessive
18 complexity. A spot of contrast followed by an assignment expression could be a
19 side-effect. The state of a closure could be undergoing change.
20
21 This coloring strategy is probably more useful than conventional *syntax*
22 highlighting. Highlighting keywords can help one to detect spelling errors, and
23 highlighting the content between quotation marks can alert one to unclosed
24 string literals. But a [linter][] could also spot those errors, and if
25 integrated via [flycheck][], an extra spot opens up in your editing toolbelt.
26
27 Give context coloring a try; you may find that it *changes the way you write
28 code*.
29
30 ## Features
31
32 - Supported languages: JavaScript
33 - Light and dark (customizable) color schemes.
34 - Insanely fast for regular files, quick for big ones too.
35 - jQuery (9191 lines): 0.20 seconds (js2-mode), 0.57 seconds (js-mode)
36 - Lodash (6786 lines): 0.07 seconds (js2-mode), 0.35 seconds (js-mode)
37 - Async (1124 lines): 0.03 seconds (js2-mode), 0.17 seconds (js-mode)
38 - mkdirp (98 lines): 0.002 seconds (js2-mode), 0.09 seconds (js-mode)
39
40 \* js2-mode parses idly, irrespective of this plugin; its benchmarks represent
41 coloring only. js-mode benchmarks represent parsing and coloring.
42
43 ## Usage
44
45 Requires Emacs 24+.
46
47 JavaScript language support requires either [js2-mode][] or
48 [Node.js 0.10+][node], respectively.
49
50 - Clone this repository.
51
52 ```bash
53 cd ~/.emacs.d/
54 git clone https://github.com/jacksonrayhamilton/context-coloring.git
55 ```
56
57 - Byte-compile the package for improved speed.
58
59 ```bash
60 cd context-coloring/
61 make compile
62 ```
63
64 - Add the following to your `~/.emacs` file:
65
66 ```lisp
67 (add-to-list 'load-path "~/.emacs.d/context-coloring")
68 (require 'context-coloring)
69 (add-hook 'js-mode-hook 'context-coloring-mode)
70 ```
71
72 ## Customizing
73
74 You can adjust the colors to your liking using
75 `context-coloring-set-colors`. The first argument is an alist of levels, and the
76 optional second argument is the new total number of levels. This plugin does not
77 figure out the total for you; you need to specify it if your number of colors is
78 different from the default (`7`).
79
80 I like to take the colors from an existing theme and use those to create a
81 rainbow that matches that theme. The end result is consistent, and usually looks
82 as good as the theme does. Here's an example for `tango`:
83
84 ```lisp
85 ;; ~/.emacs
86 (load-theme 'tango)
87 (require 'context-coloring)
88 (defun jrh-context-coloring-tango ()
89 (interactive)
90 (context-coloring-set-colors
91 '((comment . "#5f615c")
92 (0 . "#2e3436") ; Globals.
93 (1 . "#346604")
94 (2 . "#204a87")
95 (3 . "#5c3566")
96 (4 . "#a40000")
97 (5 . "#b35000")
98 (6 . "#c4a000")
99 (7 . "#8ae234") ; "You're screwed" colors.
100 (8 . "#8cc4ff")
101 (9 . "#ad7fa8")
102 (10 . "#ef2929")
103 (11 . "#fcaf3e")
104 (12 . "#fce94f"))
105 13))
106 (jrh-context-coloring-tango)
107 ```
108
109 ## Extending
110
111 To add support for a new language, write a "scopifier" for it, and add an entry
112 to `context-coloring-dispatch-plist`. Then the plugin should handle the rest.
113
114 A "scopifier" is a CLI program that reads a buffer's contents from stdin and
115 writes a JSON array of numbers to stdout. Every three numbers in the array
116 represent a range of color. For instance, if I fed the following string of
117 JavaScript code to a scopifier,
118
119 ```js
120 var a = function () {};
121 ```
122
123 then the scopifier would produce the following array:
124
125 ```js
126 [1,24,0,9,23,1]
127 ```
128
129 Where, for every three numbers, the first number is a 1-indexed start [point][],
130 the second number is an exclusive end point, and the third number is a scope
131 level. The result of applying level 0 coloring to the range &#91;1, 24) and then
132 applying level 1 coloring to the range &#91;9, 23) would result in the following
133 coloring:
134
135 <p align="center">
136 <img alt="Screenshot of ranges &#91;1, 24) and &#91;9, 23)." src="scopifier.png" title="Screenshot">
137 </p>
138
139 If there is an abstract syntax tree generator for your language, you can walk
140 the syntax tree, find variables and scopes, and build their positions and levels
141 into an array like the one above.
142
143 [linter]: http://jshint.com/about/
144 [flycheck]: http://www.flycheck.org/
145 [point]: http://www.gnu.org/software/emacs/manual/html_node/elisp/Point.html
146 [js2-mode]: https://github.com/mooz/js2-mode
147 [node]: http://nodejs.org/download/
148 [load path]: https://www.gnu.org/software/emacs/manual/html_node/emacs/Lisp-Libraries.html