]> code.delx.au - gnu-emacs-elpa/blob - README.md
Merge branch 'feature/dispatch' into develop
[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 ## Usage
38
39 Requires Emacs 24+.
40
41 JavaScript language support requires either [js2-mode][] or
42 [Node.js 0.10+][node], respectively.
43
44 - Clone this repository.
45
46 ```bash
47 cd ~/.emacs.d/
48 git clone https://github.com/jacksonrayhamilton/context-coloring.git
49 ```
50
51 - Byte-compile the package for improved speed.
52
53 ```bash
54 cd context-coloring/
55 make compile
56 ```
57
58 - Add the following to your `~/.emacs` file:
59
60 ```lisp
61 (add-to-list 'load-path "~/.emacs.d/context-coloring")
62 (require 'context-coloring)
63 (add-hook 'js-mode-hook 'context-coloring-mode)
64 ```
65
66 ## Customizing
67
68 You can adjust the colors to your liking using `context-coloring-set-colors`.
69
70 I like to take the colors from an existing theme and use those to create a
71 rainbow that matches that theme. Here's an example for [`zenburn`][zenburn] (which is the
72 theme used in the screenshot above).
73
74 ```lisp
75 ;; ~/.emacs
76 (load-theme 'zenburn t)
77 (require 'context-coloring)
78 (context-coloring-set-colors
79 "#DCDCCC"
80 "#93E0E3"
81 "#BFEBBF"
82 "#F0DFAF"
83 "#DFAF8F"
84 "#CC9393"
85 "#DC8CC3"
86 "#94BFF3"
87 "#9FC59F"
88 "#D0BF8F"
89 "#DCA3A3")
90 ```
91
92 ## Extending
93
94 To add support for a new language, write a "scopifier" for it, and define a new
95 coloring dispatch strategy with `context-coloring-define-dispatch`. Then the
96 plugin should handle the rest.
97
98 A "scopifier" is a CLI program that reads a buffer's contents from stdin and
99 writes a JSON array of numbers to stdout. Every three numbers in the array
100 represent a range of color. For instance, if I fed the following string of
101 JavaScript code to a scopifier,
102
103 ```js
104 var a = function () {};
105 ```
106
107 then the scopifier would produce the following array:
108
109 ```js
110 [1,24,0,9,23,1]
111 ```
112
113 Where, for every three numbers, the first number is a 1-indexed start [point][],
114 the second number is an exclusive end point, and the third number is a scope
115 level. The result of applying level 0 coloring to the range &#91;1, 24) and then
116 applying level 1 coloring to the range &#91;9, 23) would result in the following
117 coloring:
118
119 <p align="center">
120 <img alt="Screenshot of ranges &#91;1, 24) and &#91;9, 23)." src="scopifier.png" title="Screenshot">
121 </p>
122
123 If there is an abstract syntax tree generator for your language, you can walk
124 the syntax tree, find variables and scopes, and build their positions and levels
125 into an array like the one above.
126
127 For example, a Ruby scopifier might be defined and implemented like this:
128
129 ```lisp
130 (context-coloring-define-dispatch 'ruby
131 :modes '(ruby-mode)
132 :executable "ruby"
133 :command "/home/username/scopifier")
134 ```
135
136 ```ruby
137 #!/usr/bin/env ruby
138 def scopifier(code)
139 # Parse code.
140 # Return an array.
141 end
142 print scopifier ARGF.read
143 ```
144
145 [linter]: http://jshint.com/about/
146 [flycheck]: http://www.flycheck.org/
147 [zenburn]: http://github.com/bbatsov/zenburn-emacs
148 [point]: http://www.gnu.org/software/emacs/manual/html_node/elisp/Point.html
149 [js2-mode]: https://github.com/mooz/js2-mode
150 [node]: http://nodejs.org/download/
151 [load path]: https://www.gnu.org/software/emacs/manual/html_node/emacs/Lisp-Libraries.html