Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 399 lines (307 sloc) 12.434 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
97ef92b @defunkt Bugfix: Indentation works when point is at beginning of the line
authored
244 ;; Bail early by indenting if point as the front of the line.
245 (if (= (point) (point-at-bol))
97a1001 @defunkt basic indentation with TAB
authored
246 (insert-tab)
97ef92b @defunkt Bugfix: Indentation works when point is at beginning of the line
authored
247 (save-excursion
248 (let ((prev-indent 0) (cur-indent 0))
249 ;; Figure out the indentation of the previous line
250 (forward-line -1)
251 (setq prev-indent (current-indentation))
252 (coffee-debug "prev-indent %s" prev-indent)
253
254 ;; Figure out the current line's indentation
255 (forward-line 1)
256 (setq cur-indent (current-indentation))
257 (coffee-debug "cur-indent %s" cur-indent)
258
259 ;; Shift one column to the left
97a1001 @defunkt basic indentation with TAB
authored
260 (backward-to-indentation 0)
97ef92b @defunkt Bugfix: Indentation works when point is at beginning of the line
authored
261 (coffee-debug "backward cur-indent %s" (current-indentation))
262 (insert-tab)
263
264 ;; We're too far, remove all indentation.
265 (when (> (- (current-indentation) prev-indent) tab-width)
266 (backward-to-indentation 0)
267 (delete-region (point-at-bol) (point)))))))
df08186 @defunkt indenters
authored
268
17150cf @defunkt docs and starting to implement indenters
authored
269 (defun coffee-newline-and-indent ()
270 "Inserts a newline and indents it to the same level as the previous line."
271 (interactive)
272
273 ;; Remember the current line indentation level,
274 ;; insert a newline, and indent the newline to the same
275 ;; level as the previous line.
276 (let ((prev-indent (current-indentation)) (indent-next nil))
277 (newline)
278 (insert-tab (/ prev-indent tab-width))
3a47b28 @defunkt docs
authored
279
17150cf @defunkt docs and starting to implement indenters
authored
280 ;; We need to insert an additional tab because the last line was special.
281 (when (coffee-line-wants-indent)
282 (insert-tab)))
283
284 ;; Last line was a comment so this one should probably be,
285 ;; too. Makes it easy to write multi-line comments (like the one I'm
286 ;; writing right now).
287 (when (coffee-previous-line-is-comment)
288 (insert "# ")))
289
290 ;; Indenters help determine whether the current line should be
291 ;; indented further based on the content of the previous line. If a
292 ;; line starts with `class', for instance, you're probably going to
293 ;; want to indent the next line.
294
295 (defvar coffee-indenters-bol '("class" "for" "if" "try")
7d338d1 @defunkt coffee-indenters
authored
296 "Keywords or syntax whose presence at the start of a line means the
297 next line should probably be indented.")
298
299 (defun coffee-indenters-bol-regexp ()
300 "Builds a regexp out of `coffee-indenters-bol' words."
301 (concat "^" (regexp-opt coffee-indenters-bol 'words)))
302
5e170be @defunkt get it working (with a fake function though)
authored
303 (defvar coffee-indenters-eol '(?> ?{ ?\[)
304 "Single characters at the end of a line that mean the next line
305 should probably be indented.")
7d338d1 @defunkt coffee-indenters
authored
306
17150cf @defunkt docs and starting to implement indenters
authored
307 (defun coffee-line-wants-indent ()
308 "Does the current line want to be indented deeper than the previous
df08186 @defunkt indenters
authored
309 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
310 (interactive)
311
17150cf @defunkt docs and starting to implement indenters
authored
312 (save-excursion
313 (let ((indenter-at-bol) (indenter-at-eol))
314 ;; Go back a line and to the first character.
315 (forward-line -1)
316 (backward-to-indentation 0)
8bfb7ed @defunkt Keep same indentation of last line when creating a new line
authored
317
17150cf @defunkt docs and starting to implement indenters
authored
318 ;; If the next few characters match one of our magic indenter
319 ;; keywords, we want to indent the line we were on originally.
320 (when (looking-at (coffee-indenters-bol-regexp))
321 (setq indenter-at-bol t))
322
323 ;; 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
324 ;; see if the last character matches one of our indenter
325 ;; characters.
17150cf @defunkt docs and starting to implement indenters
authored
326 (when (not indenter-at-bol)
327 (end-of-line)
328
5e170be @defunkt get it working (with a fake function though)
authored
329 ;; Optimized for speed - checks only the last character.
14ab5a0 @defunkt Use Common Lisp's `some` despite its inferior API.
authored
330 (when (some (lambda (char)
331 (= (char-before) char))
332 coffee-indenters-eol)
17150cf @defunkt docs and starting to implement indenters
authored
333 (setq indenter-at-eol t)))
334
335 ;; If we found an indenter, return `t'.
336 (or indenter-at-bol indenter-at-eol))))
337
8bfb7ed @defunkt Keep same indentation of last line when creating a new line
authored
338 (defun coffee-previous-line-is-comment ()
339 "Returns `t' if the previous line is a CoffeeScript comment."
340 (save-excursion
341 (forward-line -1)
342 (coffee-line-is-comment)))
343
344 (defun coffee-line-is-comment ()
345 "Returns `t' if the current line is a CoffeeScript comment."
346 (save-excursion
347 (backward-to-indentation 0)
348 (= (char-after) (string-to-char "#"))))
349
3a47b28 @defunkt docs
authored
350 ;;
351 ;; Define Major Mode
352 ;;
353
cf028f8 @defunkt basics
authored
354 (define-derived-mode coffee-mode fundamental-mode
355 "coffee-mode"
356 "Major mode for editing CoffeeScript..."
357
7557530 @defunkt compilation commands
authored
358 (define-key coffee-mode-map (kbd "A-r") 'coffee-compile-buffer)
eee29e0 @defunkt defvar the keymap
authored
359 (define-key coffee-mode-map (kbd "A-R") 'coffee-execute-line)
360 (define-key coffee-mode-map (kbd "A-M-r") 'coffee-repl)
7557530 @defunkt compilation commands
authored
361 (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
362 (define-key coffee-mode-map "\C-m" 'coffee-newline-and-indent)
7557530 @defunkt compilation commands
authored
363
cf028f8 @defunkt basics
authored
364 ;; code for syntax highlighting
365 (setq font-lock-defaults '((coffee-font-lock-keywords)))
366
84ab684 @defunkt comments
authored
367 ;; perl style comment: "# ..."
368 (modify-syntax-entry ?# "< b" coffee-mode-syntax-table)
369 (modify-syntax-entry ?\n "> b" coffee-mode-syntax-table)
0480381 @defunkt mini docs
authored
370 (setq comment-start "#")
84ab684 @defunkt comments
authored
371
9ff612a @defunkt single quotes
authored
372 ;; single quote strings
373 (modify-syntax-entry ?' "\"" coffee-mode-syntax-table)
374 (modify-syntax-entry ?' "\"" coffee-mode-syntax-table)
375
d6b78ed @defunkt defvars and basic indentation
authored
376 ;; indentation
377 (make-local-variable 'indent-line-function)
378 (setq indent-line-function 'coffee-indent-line)
379
380 ;; no tabs
381 (setq indent-tabs-mode nil)
382
cf028f8 @defunkt basics
authored
383 ;; clear memory
384 (setq coffee-keywords-regexp nil)
385 (setq coffee-types-regexp nil)
386 (setq coffee-constants-regexp nil)
387 (setq coffee-events-regexp nil)
388 (setq coffee-functions-regexp nil))
eaf28ab @defunkt register as .coffee handler
authored
389
27ffada @defunkt provide something
authored
390 (provide 'coffee-mode)
391
eaf28ab @defunkt register as .coffee handler
authored
392 ;;
393 ;; On Load
394 ;;
395
396 ;; Run coffee-mode for files ending in .coffee.
397 (add-to-list 'auto-mode-alist '("\\.coffee$" . coffee-mode))
605a330 @defunkt highlight Cakefile too
authored
398 (add-to-list 'auto-mode-alist '("Cakefile" . coffee-mode))
Something went wrong with that request. Please try again.