]> code.delx.au - gnu-emacs-elpa/blob - README.md
Update theme documentation.
[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 are declared.
13
14 Lexical scope information at-a-glance can assist a programmer in understanding
15 the overall structure of a program. It can help to curb nasty bugs like name
16 shadowing. A rainbow can indicate excessive complexity. State change within a
17 closure is easily monitored.
18
19 By default, Context Coloring still highlights comments and strings
20 syntactically. It is still easy to differentiate code from non-code, and strings
21 cannot be confused for variables.
22
23 This coloring strategy is probably more useful than conventional syntax
24 highlighting. Highlighting keywords can help one to detect spelling errors, but
25 a [linter][] could also spot those errors, and if integrated with [flycheck][],
26 an extra spot opens up in your editing toolbelt.
27
28 Give context coloring a try; you may find that it *changes the way you write
29 code*.
30
31 ## Features
32
33 - Supported languages: JavaScript
34 - Light and dark (customizable) color schemes.
35 - Very fast for files under 1000 lines.
36
37 ## Installation
38
39 Requires Emacs 24+.
40
41 JavaScript language support requires either [js2-mode][], or
42 [Node.js 0.10+][node] and the [scopifier][] executable.
43
44 ### ELPA
45
46 - `M-x package-refresh-contents RET`
47 - `M-x package-install RET context-coloring RET`
48
49 ### Git
50
51 - Clone this repository.
52
53 ```bash
54 cd ~/.emacs.d/
55 git clone https://github.com/jacksonrayhamilton/context-coloring.git
56 ```
57
58 - Byte-compile the package for improved speed.
59
60 ```bash
61 cd context-coloring/
62 make compile
63 ```
64
65 - Add the following to your `~/.emacs` file:
66
67 ```lisp
68 (add-to-list 'load-path "~/.emacs.d/context-coloring")
69 (require 'context-coloring)
70 ```
71
72 ### scopifier (for non-js2-mode users)
73
74 ```bash
75 npm install -g scopifier
76 ```
77
78 ## Usage
79
80 Add the following to your `~/.emacs` file:
81
82 ```lisp
83 ;; non-js2-mode users:
84 (add-hook 'js-mode-hook 'context-coloring-mode)
85
86 ;; js2-mode users:
87 (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
88 (add-hook 'js2-mode-hook 'context-coloring-mode)
89 ```
90
91 ## Customizing
92
93 You can enable different color schemes via `context-coloring-load-theme`. (The
94 screenshot above pairs the [zenburn][] color theme with the similarly-named
95 context-coloring theme.)
96
97 Built-in available themes are: `monokai`, `solarized`, `tango` and
98 `zenburn`. Contributions are welcome.
99
100 ```lisp
101 (require 'context-coloring)
102 (context-coloring-load-theme 'zenburn)
103 ```
104
105 You can define your own themes, too:
106
107 ```lisp
108 (context-coloring-define-theme
109 'zenburn
110 :colors '("#DCDCCC"
111 "#93E0E3"
112 "#BFEBBF"
113 "#F0DFAF"
114 "#DFAF8F"
115 "#CC9393"
116 "#DC8CC3"
117 "#94BFF3"
118 "#9FC59F"
119 "#D0BF8F"
120 "#DCA3A3"))
121 ```
122
123 ## Extending
124
125 To add support for a new language, write a "scopifier" for it, and define a new
126 coloring dispatch strategy with `context-coloring-define-dispatch`. Then the
127 plugin should handle the rest.
128
129 A "scopifier" is a CLI program that reads a buffer's contents from stdin and
130 writes a JSON array of numbers to stdout. Every three numbers in the array
131 represent a range of color. For instance, if I fed the following string of
132 JavaScript code to a scopifier,
133
134 ```js
135 var a = function () {};
136 ```
137
138 then the scopifier would produce the following array:
139
140 ```js
141 [1,24,0,9,23,1]
142 ```
143
144 Where, for every three numbers, the first number is a 1-indexed start [point][],
145 the second number is an exclusive end point, and the third number is a scope
146 level. The result of applying level 0 coloring to the range &#91;1, 24) and then
147 applying level 1 coloring to the range &#91;9, 23) would result in the following
148 coloring:
149
150 <p align="center">
151 <img alt="Screenshot of ranges &#91;1, 24) and &#91;9, 23)." src="scopifier.png" title="Screenshot">
152 </p>
153
154 If there is an abstract syntax tree generator for your language, you can walk
155 the syntax tree, find variables and scopes, and build their positions and levels
156 into an array like the one above.
157
158 For example, a Ruby scopifier might be defined and implemented like this:
159
160 ```lisp
161 (context-coloring-define-dispatch
162 'ruby
163 :modes '(ruby-mode)
164 :executable "ruby"
165 :command "/home/username/scopifier")
166 ```
167
168 ```ruby
169 #!/usr/bin/env ruby
170 def scopifier(code)
171 # Parse code.
172 # Return an array.
173 end
174 print scopifier ARGF.read
175 ```
176
177 When a `--version` argument is passed, a scopifier should print its version
178 number and exit. For installable scopifiers, this allows context-coloring to
179 check for updates as needed.
180
181 [linter]: http://jshint.com/about/
182 [flycheck]: http://www.flycheck.org/
183 [zenburn]: http://github.com/bbatsov/zenburn-emacs
184 [point]: http://www.gnu.org/software/emacs/manual/html_node/elisp/Point.html
185 [js2-mode]: https://github.com/mooz/js2-mode
186 [node]: http://nodejs.org/download/
187 [scopifier]: https://github.com/jacksonrayhamilton/scopifier
188 [load path]: https://www.gnu.org/software/emacs/manual/html_node/emacs/Lisp-Libraries.html