Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 396 lines (304 sloc) 12.281 kB
4a11c70 @defunkt documentation
authored
1 ;;; coffee-mode.el --- Major mode to edit CoffeeScript files in Emacs
2
3 ;; Copyright (C) 2010 Chris Wanstrath
4
5 ;; Version 0.1.0
6 ;; Keywords: CoffeeScript major mode
7 ;; Author: Chris Wanstrath <chris@ozmm.org>
8 ;; URL: http://github.com/defunkt/coffee-script
9
10 ;; This file is not part of GNU Emacs.
11
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; if not, write to the Free Software
24 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 ;;; Commentary
27
28 ;; For commentary please see the README.md or
29 ;; http://github.com/defunkt/coffee-mode#readme
30
31 ;;; Installation
32
33 ;; In your shell:
34
35 ;; $ cd ~/.emacs.d/vendor
36 ;; $ git clone git://github.com/defunkt/coffee-mode.git
37
38 ;; In your emacs config:
39
40 ;; (add-to-list 'load-path "~/.emacs.d/vendor/coffee-mode")
41 ;; (require 'coffee-mode)
42
43 ;;; Thanks
44
0480381 @defunkt mini docs
authored
45 ;; Major thanks to http://xahlee.org/emacs/elisp_syntax_coloring.html
46 ;; the instructions.
4a11c70 @defunkt documentation
authored
47
38ce8da @defunkt small tweaks
authored
48 ;; Also thanks to Jason Blevins's markdown-mode.el for guidance.
49
50 (require 'easymenu)
51 (require 'font-lock)
14ab5a0 @defunkt Use Common Lisp's `some` despite its inferior API.
authored
52 (require 'cl)
0480381 @defunkt mini docs
authored
53
7557530 @defunkt compilation commands
authored
54 ;;
38ce8da @defunkt small tweaks
authored
55 ;; Customizable Variables
7557530 @defunkt compilation commands
authored
56 ;;
57
38ce8da @defunkt small tweaks
authored
58 (defconst coffee-mode-version "0.1.0"
5f14029 @defunkt Menu
authored
59 "The version of this `coffee-mode'.")
60
ce05b32 @defunkt debug mode, t for now
authored
61 (defvar coffee-debug-mode t
62 "Whether to run in debug mode or not. Logs to `*Messages*'.")
63
38ce8da @defunkt small tweaks
authored
64 (defvar coffee-mode-hook nil
65 "A hook for you to run your own code when the mode is loaded.")
66
7557530 @defunkt compilation commands
authored
67 (defvar coffee-command "coffee"
68 "The CoffeeScript command used for evaluating code. Must be in your
69 path.")
70
28485fa @defunkt `coffee-repl'
authored
71 (defvar coffee-repl-args '("-i")
72 "The command line arguments to pass to `coffee-command' to start a REPL.")
73
5f14029 @defunkt Menu
authored
74 (defvar coffee-command-args '("-s" "-p" "--no-wrap")
75 "The command line arguments to pass to `coffee-command' to get it to
76 print the compiled JavaScript.")
77
78 (defun coffee-command-full ()
79 "The full `coffee-command' complete with args."
80 (mapconcat 'identity (append (list coffee-command) coffee-command-args) " "))
81
7557530 @defunkt compilation commands
authored
82 (defvar coffee-js-mode 'js2-mode
83 "The mode to use when viewing compiled JavaScript.")
84
85 (defvar coffee-compiled-buffer-name "*coffee-compiled*"
86 "The name of the scratch buffer used when compiling CoffeeScript.")
87
eee29e0 @defunkt defvar the keymap
authored
88 (defvar coffee-mode-map (make-keymap)
89 "Keymap for CoffeeScript major mode.")
90
38ce8da @defunkt small tweaks
authored
91 ;;
92 ;; Commands
93 ;;
94
28485fa @defunkt `coffee-repl'
authored
95 (defun coffee-repl ()
96 "Launch a CoffeeScript REPL using `coffee-command' as an inferior mode."
97 (interactive)
98
17150cf @defunkt docs and starting to implement indenters
authored
99 (unless (comint-check-proc "*CoffeeREPL*")
28485fa @defunkt `coffee-repl'
authored
100 (set-buffer
17150cf @defunkt docs and starting to implement indenters
authored
101 (apply 'make-comint "CoffeeREPL"
28485fa @defunkt `coffee-repl'
authored
102 coffee-command nil coffee-repl-args)))
103
104 (pop-to-buffer "*CoffeeScript*"))
38ce8da @defunkt small tweaks
authored
105
7557530 @defunkt compilation commands
authored
106 (defun coffee-compile-buffer ()
17150cf @defunkt docs and starting to implement indenters
authored
107 "Compiles the current buffer and displays the JS in another buffer."
7557530 @defunkt compilation commands
authored
108 (interactive)
109 (save-excursion
110 (coffee-compile-region (point-min) (point-max))))
111
112 (defun coffee-compile-region (start end)
17150cf @defunkt docs and starting to implement indenters
authored
113 "Compiles a region and displays the JS in another buffer."
7557530 @defunkt compilation commands
authored
114 (interactive "r")
115
116 (let ((buffer (get-buffer coffee-compiled-buffer-name)))
117 (when buffer
118 (kill-buffer buffer)))
119
120 (call-process-region start end coffee-command nil
121 (get-buffer-create coffee-compiled-buffer-name)
122 nil
123 "-s" "-p" "--no-wrap")
124 (switch-to-buffer-other-frame (get-buffer coffee-compiled-buffer-name))
125 (funcall coffee-js-mode)
126 (beginning-of-buffer))
3a47b28 @defunkt docs
authored
127
5f14029 @defunkt Menu
authored
128 (defun coffee-show-version ()
129 "Prints the `coffee-mode' version."
130 (interactive)
131 (message (concat "coffee-mode v" coffee-mode-version)))
132
fd314cc @defunkt menu tweaks
authored
133 (defun coffee-open-reference ()
134 "Open browser to CoffeeScript reference."
135 (interactive)
136 (browse-url "http://jashkenas.github.com/coffee-script/"))
137
5f14029 @defunkt Menu
authored
138 (defun coffee-open-github ()
fd314cc @defunkt menu tweaks
authored
139 "Open browser to `coffee-mode' project on GithHub."
5f14029 @defunkt Menu
authored
140 (interactive)
141 (browse-url "http://github.com/defunkt/coffee-mode"))
142
143 ;;
144 ;; Menubar
145 ;;
146
147 (easy-menu-define coffee-mode-menu coffee-mode-map
148 "Menu for CoffeeScript mode"
149 '("CoffeeScript"
150 ["Compile Buffer" coffee-compile-buffer]
151 ["Compile Region" coffee-compile-region]
5256afd @defunkt tweaks
authored
152 ["REPL" coffee-repl]
5f14029 @defunkt Menu
authored
153 "---"
fd314cc @defunkt menu tweaks
authored
154 ["CoffeeScript reference" coffee-open-reference]
155 ["coffee-mode on GitHub" coffee-open-github]
5f14029 @defunkt Menu
authored
156 ["Version" coffee-show-version]
157 ))
158
3a47b28 @defunkt docs
authored
159 ;;
160 ;; Define Language Syntax
161 ;;
162
163 ;; Instance variables (implicit this)
5256afd @defunkt tweaks
authored
164 (defvar coffee-this-regexp "@\\w*\\|this")
3a47b28 @defunkt docs
authored
165
88e7b95 @defunkt better regexps, @blah: and {blah: true, blah2:true}
authored
166 ;; Assignment
5256afd @defunkt tweaks
authored
167 (defvar coffee-assign-regexp "\\(\\w\\|\\.\\|_\\| \\|$\\)+?:")
88e7b95 @defunkt better regexps, @blah: and {blah: true, blah2:true}
authored
168
af34f91 @defunkt Booleans, better colors
authored
169 ;; Booleans
5256afd @defunkt tweaks
authored
170 (defvar coffee-boolean-regexp "\\b\\(true\\|false\\|yes\\|no\\|on\\|off\\)\\b")
3a47b28 @defunkt docs
authored
171
dbb4ec5 @defunkt comment tweaks
authored
172 ;; Regular Expressions
5256afd @defunkt tweaks
authored
173 (defvar coffee-regexp-regexp "\\/.+?\\/")
3a47b28 @defunkt docs
authored
174
cf028f8 @defunkt basics
authored
175 ;; JavaScript Keywords
d6b78ed @defunkt defvars and basic indentation
authored
176 (defvar coffee-js-keywords
af34f91 @defunkt Booleans, better colors
authored
177 '("if" "else" "new" "return" "try" "catch"
cf028f8 @defunkt basics
authored
178 "finally" "throw" "break" "continue" "for" "in" "while"
179 "delete" "instanceof" "typeof" "switch" "super" "extends"
180 "class"))
181
182 ;; Reserved keywords either by JS or CS.
d6b78ed @defunkt defvars and basic indentation
authored
183 (defvar coffee-js-reserved
cf028f8 @defunkt basics
authored
184 '("case" "default" "do" "function" "var" "void" "with"
185 "const" "let" "debugger" "enum" "export" "import" "native"
186 "__extends" "__hasProp"))
187
188 ;; CoffeeScript keywords.
d6b78ed @defunkt defvars and basic indentation
authored
189 (defvar coffee-cs-keywords
af34f91 @defunkt Booleans, better colors
authored
190 '("then" "unless" "and" "or" "is"
cf028f8 @defunkt basics
authored
191 "isnt" "not" "of" "by" "where" "when"))
192
0480381 @defunkt mini docs
authored
193 ;; Regular expression combining the above three lists.
d6b78ed @defunkt defvars and basic indentation
authored
194 (defvar coffee-keywords-regexp (regexp-opt
cf028f8 @defunkt basics
authored
195 (append
196 coffee-js-reserved
197 coffee-js-keywords
198 coffee-cs-keywords) 'words))
199
200
0480381 @defunkt mini docs
authored
201 ;; Create the list for font-lock.
202 ;; Each class of keyword is given a particular face
5256afd @defunkt tweaks
authored
203 (defvar coffee-font-lock-keywords
cf028f8 @defunkt basics
authored
204 `(
3ecaa32 @defunkt use more semantic names for parts of syntax
authored
205 (,coffee-this-regexp . font-lock-variable-name-face)
206 (,coffee-assign-regexp . font-lock-type-face)
028a53c @defunkt giggity
authored
207 (,coffee-regexp-regexp . font-lock-constant-face)
3ecaa32 @defunkt use more semantic names for parts of syntax
authored
208 (,coffee-boolean-regexp . font-lock-constant-face)
cf028f8 @defunkt basics
authored
209 (,coffee-keywords-regexp . font-lock-keyword-face)
210
211 ;; note: order above matters. `coffee-keywords-regexp' goes last because
212 ;; otherwise the keyword "state" in the function "state_entry"
213 ;; would be highlighted.
214 ))
215
3a47b28 @defunkt docs
authored
216 ;;
217 ;; Helper Functions
218 ;;
219
0480381 @defunkt mini docs
authored
220 ;; The command to comment/uncomment text
9ff612a @defunkt single quotes
authored
221 (defun coffee-comment-dwim (arg)
222 "Comment or uncomment current line or region in a smart way.
223 For detail, see `comment-dwim'."
224 (interactive "*P")
225 (require 'newcomment)
226 (let ((deactivate-mark nil) (comment-start "#") (comment-end ""))
227 (comment-dwim arg)))
84ab684 @defunkt comments
authored
228
ce05b32 @defunkt debug mode, t for now
authored
229 (defun coffee-debug (string &optional args)
230 "Print a message when in debug mode."
231 (when coffee-debug-mode
232 (message string args)))
233
2504ebe @defunkt explain indentation plan
authored
234 ;;
d6b78ed @defunkt defvars and basic indentation
authored
235 ;; Indentation
2504ebe @defunkt explain indentation plan
authored
236 ;;
237
dbb4ec5 @defunkt comment tweaks
authored
238 ;;; The theory is explained in the README.
97a1001 @defunkt basic indentation with TAB
authored
239
d6b78ed @defunkt defvars and basic indentation
authored
240 (defun coffee-indent-line ()
7b03a71 @defunkt indentation theory
authored
241 "Indent current line as CoffeeScript."
d6b78ed @defunkt defvars and basic indentation
authored
242 (interactive)
243
244 (save-excursion
245 (let ((prev-indent 0) (cur-indent 0))
97a1001 @defunkt basic indentation with TAB
authored
246 ;; Figure out the indentation of the previous line
d6b78ed @defunkt defvars and basic indentation
authored
247 (forward-line -1)
248 (setq prev-indent (current-indentation))
ce05b32 @defunkt debug mode, t for now
authored
249 (coffee-debug "prev-indent %s" prev-indent)
97a1001 @defunkt basic indentation with TAB
authored
250
251 ;; Figure out the current line's indentation
d6b78ed @defunkt defvars and basic indentation
authored
252 (forward-line 1)
253 (setq cur-indent (current-indentation))
ce05b32 @defunkt debug mode, t for now
authored
254 (coffee-debug "cur-indent %s" cur-indent)
d6b78ed @defunkt defvars and basic indentation
authored
255
97a1001 @defunkt basic indentation with TAB
authored
256 ;; Shift one column to the left
d6b78ed @defunkt defvars and basic indentation
authored
257 (backward-to-indentation 0)
ce05b32 @defunkt debug mode, t for now
authored
258 (coffee-debug "backward cur-indent %s" (current-indentation))
97a1001 @defunkt basic indentation with TAB
authored
259 (insert-tab)
260
261 ;; We're too far, remove all indentation.
262 (when (> (- (current-indentation) prev-indent) tab-width)
263 (backward-to-indentation 0)
264 (delete-region (point-at-bol) (point))))))
df08186 @defunkt indenters
authored
265
17150cf @defunkt docs and starting to implement indenters
authored
266 (defun coffee-newline-and-indent ()
267 "Inserts a newline and indents it to the same level as the previous line."
268 (interactive)
269
270 ;; Remember the current line indentation level,
271 ;; insert a newline, and indent the newline to the same
272 ;; level as the previous line.
273 (let ((prev-indent (current-indentation)) (indent-next nil))
274 (newline)
275 (insert-tab (/ prev-indent tab-width))
3a47b28 @defunkt docs
authored
276
17150cf @defunkt docs and starting to implement indenters
authored
277 ;; We need to insert an additional tab because the last line was special.
278 (when (coffee-line-wants-indent)
279 (insert-tab)))
280
281 ;; Last line was a comment so this one should probably be,
282 ;; too. Makes it easy to write multi-line comments (like the one I'm
283 ;; writing right now).
284 (when (coffee-previous-line-is-comment)
285 (insert "# ")))
286
287 ;; Indenters help determine whether the current line should be
288 ;; indented further based on the content of the previous line. If a
289 ;; line starts with `class', for instance, you're probably going to
290 ;; want to indent the next line.
291
292 (defvar coffee-indenters-bol '("class" "for" "if" "try")
7d338d1 @defunkt coffee-indenters
authored
293 "Keywords or syntax whose presence at the start of a line means the
294 next line should probably be indented.")
295
296 (defun coffee-indenters-bol-regexp ()
297 "Builds a regexp out of `coffee-indenters-bol' words."
298 (concat "^" (regexp-opt coffee-indenters-bol 'words)))
299
5e170be @defunkt get it working (with a fake function though)
authored
300 (defvar coffee-indenters-eol '(?> ?{ ?\[)
301 "Single characters at the end of a line that mean the next line
302 should probably be indented.")
7d338d1 @defunkt coffee-indenters
authored
303
17150cf @defunkt docs and starting to implement indenters
authored
304 (defun coffee-line-wants-indent ()
305 "Does the current line want to be indented deeper than the previous
df08186 @defunkt indenters
authored
306 line? Returns `t' or `nil'. See the README for more details."
8bfb7ed @defunkt Keep same indentation of last line when creating a new line
authored
307 (interactive)
308
17150cf @defunkt docs and starting to implement indenters
authored
309 (save-excursion
310 (let ((indenter-at-bol) (indenter-at-eol))
311 ;; Go back a line and to the first character.
312 (forward-line -1)
313 (backward-to-indentation 0)
8bfb7ed @defunkt Keep same indentation of last line when creating a new line
authored
314
17150cf @defunkt docs and starting to implement indenters
authored
315 ;; If the next few characters match one of our magic indenter
316 ;; keywords, we want to indent the line we were on originally.
317 (when (looking-at (coffee-indenters-bol-regexp))
318 (setq indenter-at-bol t))
319
320 ;; If that didn't match, go to the back of the line and check to
5e170be @defunkt get it working (with a fake function though)
authored
321 ;; see if the last character matches one of our indenter
322 ;; characters.
17150cf @defunkt docs and starting to implement indenters
authored
323 (when (not indenter-at-bol)
324 (end-of-line)
325
5e170be @defunkt get it working (with a fake function though)
authored
326 ;; Optimized for speed - checks only the last character.
14ab5a0 @defunkt Use Common Lisp's `some` despite its inferior API.
authored
327 (when (some (lambda (char)
328 (= (char-before) char))
329 coffee-indenters-eol)
17150cf @defunkt docs and starting to implement indenters
authored
330 (setq indenter-at-eol t)))
331
332 ;; If we found an indenter, return `t'.
333 (or indenter-at-bol indenter-at-eol))))
334
8bfb7ed @defunkt Keep same indentation of last line when creating a new line
authored
335 (defun coffee-previous-line-is-comment ()
336 "Returns `t' if the previous line is a CoffeeScript comment."
337 (save-excursion
338 (forward-line -1)
339 (coffee-line-is-comment)))
340
341 (defun coffee-line-is-comment ()
342 "Returns `t' if the current line is a CoffeeScript comment."
343 (save-excursion
344 (backward-to-indentation 0)
345 (= (char-after) (string-to-char "#"))))
346
3a47b28 @defunkt docs
authored
347 ;;
348 ;; Define Major Mode
349 ;;
350
cf028f8 @defunkt basics
authored
351 (define-derived-mode coffee-mode fundamental-mode
352 "coffee-mode"
353 "Major mode for editing CoffeeScript..."
354
7557530 @defunkt compilation commands
authored
355 (define-key coffee-mode-map (kbd "A-r") 'coffee-compile-buffer)
eee29e0 @defunkt defvar the keymap
authored
356 (define-key coffee-mode-map (kbd "A-R") 'coffee-execute-line)
357 (define-key coffee-mode-map (kbd "A-M-r") 'coffee-repl)
7557530 @defunkt compilation commands
authored
358 (define-key coffee-mode-map [remap comment-dwim] 'coffee-comment-dwim)
8bfb7ed @defunkt Keep same indentation of last line when creating a new line
authored
359 (define-key coffee-mode-map "\C-m" 'coffee-newline-and-indent)
7557530 @defunkt compilation commands
authored
360
cf028f8 @defunkt basics
authored
361 ;; code for syntax highlighting
362 (setq font-lock-defaults '((coffee-font-lock-keywords)))
363
84ab684 @defunkt comments
authored
364 ;; perl style comment: "# ..."
365 (modify-syntax-entry ?# "< b" coffee-mode-syntax-table)
366 (modify-syntax-entry ?\n "> b" coffee-mode-syntax-table)
0480381 @defunkt mini docs
authored
367 (setq comment-start "#")
84ab684 @defunkt comments
authored
368
9ff612a @defunkt single quotes
authored
369 ;; single quote strings
370 (modify-syntax-entry ?' "\"" coffee-mode-syntax-table)
371 (modify-syntax-entry ?' "\"" coffee-mode-syntax-table)
372
d6b78ed @defunkt defvars and basic indentation
authored
373 ;; indentation
374 (make-local-variable 'indent-line-function)
375 (setq indent-line-function 'coffee-indent-line)
376
377 ;; no tabs
378 (setq indent-tabs-mode nil)
379
cf028f8 @defunkt basics
authored
380 ;; clear memory
381 (setq coffee-keywords-regexp nil)
382 (setq coffee-types-regexp nil)
383 (setq coffee-constants-regexp nil)
384 (setq coffee-events-regexp nil)
385 (setq coffee-functions-regexp nil))
eaf28ab @defunkt register as .coffee handler
authored
386
27ffada @defunkt provide something
authored
387 (provide 'coffee-mode)
388
eaf28ab @defunkt register as .coffee handler
authored
389 ;;
390 ;; On Load
391 ;;
392
393 ;; Run coffee-mode for files ending in .coffee.
394 (add-to-list 'auto-mode-alist '("\\.coffee$" . coffee-mode))
605a330 @defunkt highlight Cakefile too
authored
395 (add-to-list 'auto-mode-alist '("Cakefile" . coffee-mode))
Something went wrong with that request. Please try again.