1 CoffeeScript Major Mode
2 =======================
4 An Emacs major mode for [CoffeeScript][cs], unfancy JavaScript.
6 Provides syntax highlighting, indentation support, and a few cute
9 ![Screenshot](http://img.skitch.com/20100307-qmcr7kij6fx7qmsx6w12dgfs2x.png)
15 $ cd ~/.emacs.d/vendor
16 $ git clone git://github.com/defunkt/coffee-mode.git
20 (add-to-list 'load-path "~/.emacs.d/vendor/coffee-mode")
21 (require 'coffee-mode)
23 `coffee-mode` will be enabled automatically for any files ending in
24 ".coffee" or named "Cakefile".
30 Lines are indented according to the `tab-width` variable. If you're
31 like me, you probably have this set in your Emacs config globally:
33 (setq-default tab-width 4)
35 Well, idiomatic CoffeeScript uses two spaces. We can set our
36 `tab-width` to two for `coffee-mode` using the `coffee-mode-hook`:
38 (defun coffee-custom ()
40 (set (make-local-variable 'tab-width) 2))
42 (add-hook coffee-mode-hook
43 '(lambda() (coffee-custom)))
45 Another example of this hook is given further down.
49 When you press `TAB`, indent the line unless doing so would make the
50 current line more than two indentation levels deepers than the
51 previous line. If that's the case, remove all indentation.
53 Consider this code, with point at the position indicated by the
61 Pressing `TAB` will produce the following code:
68 Pressing `TAB` again will produce this code:
75 And so on. I think this is a pretty good way of getting decent
76 indentation with a whitespace-sensitive
78 ### Newline and Indent
80 As for indentation after newlines, given this code and cursor
88 Pressing `RET` would insert a newline and place our cursor at the
97 In other words, the level of indentation is maintained. This
98 applies to comments as well. Combined with the `TAB` you should be
99 able to get things where you want them pretty easily.
103 `class`, `for`, `if`, and possibly other keywords cause the next line
104 to be indented automatically, however.
106 For example, given this code and cursor position::
111 Pressing enter would produce the following:
117 That is, indented a column deeper.
119 This also applies to lines ending in `->`, `=>`, `{`, `[`, and
120 possibly more characters.
122 So this code and cursor position:
127 On enter would produce this:
137 ### coffee-compile-buffer
139 Compiles the current buffer to JavaScript using the command specified
140 by the `coffee-command` variable and opens the contents in a new
141 buffer using your JavaScript mode of choice. The JavaScript mode is
142 determined by the `coffee-js-mode` variable and defaults to `js2-mode`.
146 (define-key coffee-mode-map [(meta r)] 'coffee-compile-buffer)
148 ### coffee-compiler-region
150 Compiles the selected region to JavaScript using the same
151 configuration variables as `coffee-compile-buffer`.
155 (define-key coffee-mode-map [(meta R)] 'coffee-compile-region)
159 Starts a repl in a new buffer using `coffee-command`.
167 (defun coffee-custom ()
170 ;; CoffeeScript uses two spaces.
171 (set (make-local-variable 'tab-width) 2)
173 ;; If you don't have js2-mode
174 (setq coffee-js-mode 'javascript-mode)
177 (setq coffee-debug-mode t)
180 (setq coffee-command "~/dev/coffee"))
182 (add-hook 'coffee-mode-hook '(lambda () (coffee-custom)))
186 * <http://xahlee.org/emacs/elisp_syntax_coloring.html> for instructions.
187 * Jason Blevins for the guidance his markdown-mode.el gave.
191 It's tested on Aquamacs 1.9 (Emacs 22) for OS X Snow Leopard so it may
192 not work on your environment. Please file a bug at
193 <http://github.com/defunkt/coffee-mode/issues> and maybe we can fix
196 This is the author's first major mode. Any sort of style feedback is
199 [cs]: http://jashkenas.github.com/coffee-script/