Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 533 lines (420 sloc) 16.498 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
12bd5d0 @defunkt todo
authored
50 ;; TODO:
51 ;; - Fix indentation toggling on blank (pure whitespace) lines
52 ;; - imenu support
53 ;; - Make prototype accessor assignments like `String::length: -> 10` pretty.
5b00ae2 @defunkt more todo
authored
54 ;; - Automatically `delete-trailing-whitespace' on save, configurable.
55 ;; - mirror-mode - close brackets and parens automatically
12bd5d0 @defunkt todo
authored
56
57 ;;; Code:
58
38ce8da @defunkt small tweaks
authored
59 (require 'easymenu)
60 (require 'font-lock)
14ab5a0 @defunkt Use Common Lisp's `some` despite its inferior API.
authored
61 (require 'cl)
0480381 @defunkt mini docs
authored
62
7557530 @defunkt compilation commands
authored
63 ;;
38ce8da @defunkt small tweaks
authored
64 ;; Customizable Variables
7557530 @defunkt compilation commands
authored
65 ;;
66
38ce8da @defunkt small tweaks
authored
67 (defconst coffee-mode-version "0.1.0"
5f14029 @defunkt Menu
authored
68 "The version of this `coffee-mode'.")
69
c063681 @defunkt no more debug mode
authored
70 (defvar coffee-debug-mode nil
ce05b32 @defunkt debug mode, t for now
authored
71 "Whether to run in debug mode or not. Logs to `*Messages*'.")
72
38ce8da @defunkt small tweaks
authored
73 (defvar coffee-mode-hook nil
74 "A hook for you to run your own code when the mode is loaded.")
75
7557530 @defunkt compilation commands
authored
76 (defvar coffee-command "coffee"
77 "The CoffeeScript command used for evaluating code. Must be in your
78 path.")
79
28485fa @defunkt `coffee-repl'
authored
80 (defvar coffee-repl-args '("-i")
81 "The command line arguments to pass to `coffee-command' to start a REPL.")
82
5f14029 @defunkt Menu
authored
83 (defvar coffee-command-args '("-s" "-p" "--no-wrap")
84 "The command line arguments to pass to `coffee-command' to get it to
85 print the compiled JavaScript.")
86
7557530 @defunkt compilation commands
authored
87 (defvar coffee-js-mode 'js2-mode
88 "The mode to use when viewing compiled JavaScript.")
89
90 (defvar coffee-compiled-buffer-name "*coffee-compiled*"
91 "The name of the scratch buffer used when compiling CoffeeScript.")
92
eee29e0 @defunkt defvar the keymap
authored
93 (defvar coffee-mode-map (make-keymap)
94 "Keymap for CoffeeScript major mode.")
95
38ce8da @defunkt small tweaks
authored
96 ;;
c5ffe04 @defunkt setd macro for logging variable values when setting them
authored
97 ;; Macros
5b00ae2 @defunkt more todo
authored
98 ;;
99
c5ffe04 @defunkt setd macro for logging variable values when setting them
authored
100 (defmacro setd (var val)
101 "Like setq but optionally logs the variable's value using `coffee-debug'."
21ce2da @defunkt The kind of optimization that turns on every Lisp hacker: macro-time.
authored
102 (if coffee-debug-mode
c6562af @defunkt Make `coffee-debug' a macro
authored
103 `(progn
104 (coffee-debug "%s: %s" ',var ,val)
105 (setq ,var ,val))
106 `(setq ,var ,val)))
107
108 (defmacro coffee-debug (string &rest args)
109 "Print a message when in debug mode."
110 (when coffee-debug-mode
111 `(apply 'message (append (list ,string) ,args))))
c5ffe04 @defunkt setd macro for logging variable values when setting them
authored
112
5b00ae2 @defunkt more todo
authored
113 ;;
38ce8da @defunkt small tweaks
authored
114 ;; Commands
115 ;;
116
28485fa @defunkt `coffee-repl'
authored
117 (defun coffee-repl ()
118 "Launch a CoffeeScript REPL using `coffee-command' as an inferior mode."
119 (interactive)
120
17150cf @defunkt docs and starting to implement indenters
authored
121 (unless (comint-check-proc "*CoffeeREPL*")
28485fa @defunkt `coffee-repl'
authored
122 (set-buffer
17150cf @defunkt docs and starting to implement indenters
authored
123 (apply 'make-comint "CoffeeREPL"
28485fa @defunkt `coffee-repl'
authored
124 coffee-command nil coffee-repl-args)))
125
126 (pop-to-buffer "*CoffeeScript*"))
38ce8da @defunkt small tweaks
authored
127
7557530 @defunkt compilation commands
authored
128 (defun coffee-compile-buffer ()
17150cf @defunkt docs and starting to implement indenters
authored
129 "Compiles the current buffer and displays the JS in another buffer."
7557530 @defunkt compilation commands
authored
130 (interactive)
131 (save-excursion
132 (coffee-compile-region (point-min) (point-max))))
133
134 (defun coffee-compile-region (start end)
17150cf @defunkt docs and starting to implement indenters
authored
135 "Compiles a region and displays the JS in another buffer."
7557530 @defunkt compilation commands
authored
136 (interactive "r")
137
138 (let ((buffer (get-buffer coffee-compiled-buffer-name)))
139 (when buffer
140 (kill-buffer buffer)))
141
142 (call-process-region start end coffee-command nil
143 (get-buffer-create coffee-compiled-buffer-name)
144 nil
145 "-s" "-p" "--no-wrap")
146 (switch-to-buffer-other-frame (get-buffer coffee-compiled-buffer-name))
147 (funcall coffee-js-mode)
148 (beginning-of-buffer))
3a47b28 @defunkt docs
authored
149
5f14029 @defunkt Menu
authored
150 (defun coffee-show-version ()
151 "Prints the `coffee-mode' version."
152 (interactive)
153 (message (concat "coffee-mode v" coffee-mode-version)))
154
fd314cc @defunkt menu tweaks
authored
155 (defun coffee-open-reference ()
156 "Open browser to CoffeeScript reference."
157 (interactive)
158 (browse-url "http://jashkenas.github.com/coffee-script/"))
159
5f14029 @defunkt Menu
authored
160 (defun coffee-open-github ()
fd314cc @defunkt menu tweaks
authored
161 "Open browser to `coffee-mode' project on GithHub."
5f14029 @defunkt Menu
authored
162 (interactive)
163 (browse-url "http://github.com/defunkt/coffee-mode"))
164
165 ;;
166 ;; Menubar
167 ;;
168
169 (easy-menu-define coffee-mode-menu coffee-mode-map
170 "Menu for CoffeeScript mode"
171 '("CoffeeScript"
172 ["Compile Buffer" coffee-compile-buffer]
173 ["Compile Region" coffee-compile-region]
5256afd @defunkt tweaks
authored
174 ["REPL" coffee-repl]
5f14029 @defunkt Menu
authored
175 "---"
fd314cc @defunkt menu tweaks
authored
176 ["CoffeeScript reference" coffee-open-reference]
177 ["coffee-mode on GitHub" coffee-open-github]
5f14029 @defunkt Menu
authored
178 ["Version" coffee-show-version]
179 ))
180
3a47b28 @defunkt docs
authored
181 ;;
182 ;; Define Language Syntax
183 ;;
184
185 ;; Instance variables (implicit this)
5256afd @defunkt tweaks
authored
186 (defvar coffee-this-regexp "@\\w*\\|this")
3a47b28 @defunkt docs
authored
187
88e7b95 @defunkt better regexps, @blah: and {blah: true, blah2:true}
authored
188 ;; Assignment
30d2af5 @defunkt Basic imenu support - makes things like `textmate-goto-symbol' work!
authored
189 (defvar coffee-assign-regexp "\\(\\(\\w\\|\\.\\|_\\| \\|$\\)+?\\):")
190
191 ;; Lambda
192 (defvar coffee-lambda-regexp "\\((.+)\\)?\s *\\(->\\|=>\\)")
88e7b95 @defunkt better regexps, @blah: and {blah: true, blah2:true}
authored
193
76ed341 @defunkt class support in imenu
authored
194 ;; Namespaces
195 (defvar coffee-namespace-regexp "\\b\\(class\\s +\\(\\S +\\)\\)\\b")
196
af34f91 @defunkt Booleans, better colors
authored
197 ;; Booleans
5256afd @defunkt tweaks
authored
198 (defvar coffee-boolean-regexp "\\b\\(true\\|false\\|yes\\|no\\|on\\|off\\)\\b")
3a47b28 @defunkt docs
authored
199
dbb4ec5 @defunkt comment tweaks
authored
200 ;; Regular Expressions
5256afd @defunkt tweaks
authored
201 (defvar coffee-regexp-regexp "\\/.+?\\/")
3a47b28 @defunkt docs
authored
202
cf028f8 @defunkt basics
authored
203 ;; JavaScript Keywords
d6b78ed @defunkt defvars and basic indentation
authored
204 (defvar coffee-js-keywords
af34f91 @defunkt Booleans, better colors
authored
205 '("if" "else" "new" "return" "try" "catch"
cf028f8 @defunkt basics
authored
206 "finally" "throw" "break" "continue" "for" "in" "while"
207 "delete" "instanceof" "typeof" "switch" "super" "extends"
208 "class"))
209
210 ;; Reserved keywords either by JS or CS.
d6b78ed @defunkt defvars and basic indentation
authored
211 (defvar coffee-js-reserved
cf028f8 @defunkt basics
authored
212 '("case" "default" "do" "function" "var" "void" "with"
213 "const" "let" "debugger" "enum" "export" "import" "native"
214 "__extends" "__hasProp"))
215
216 ;; CoffeeScript keywords.
d6b78ed @defunkt defvars and basic indentation
authored
217 (defvar coffee-cs-keywords
af34f91 @defunkt Booleans, better colors
authored
218 '("then" "unless" "and" "or" "is"
cf028f8 @defunkt basics
authored
219 "isnt" "not" "of" "by" "where" "when"))
220
0480381 @defunkt mini docs
authored
221 ;; Regular expression combining the above three lists.
d6b78ed @defunkt defvars and basic indentation
authored
222 (defvar coffee-keywords-regexp (regexp-opt
cf028f8 @defunkt basics
authored
223 (append
224 coffee-js-reserved
225 coffee-js-keywords
226 coffee-cs-keywords) 'words))
227
228
ff61700 @defunkt that was bothering me
authored
229 ;; Create the list for font-lock. Each class of keyword is given a
230 ;; particular face.
5256afd @defunkt tweaks
authored
231 (defvar coffee-font-lock-keywords
ff61700 @defunkt that was bothering me
authored
232 ;; *Note*: order below matters. `coffee-keywords-regexp' goes last
233 ;; because otherwise the keyword "state" in the function
234 ;; "state_entry" would be highlighted.
235 `((,coffee-this-regexp . font-lock-variable-name-face)
236 (,coffee-assign-regexp . font-lock-type-face)
237 (,coffee-regexp-regexp . font-lock-constant-face)
238 (,coffee-boolean-regexp . font-lock-constant-face)
239 (,coffee-keywords-regexp . font-lock-keyword-face)))
cf028f8 @defunkt basics
authored
240
3a47b28 @defunkt docs
authored
241 ;;
242 ;; Helper Functions
243 ;;
244
0480381 @defunkt mini docs
authored
245 ;; The command to comment/uncomment text
9ff612a @defunkt single quotes
authored
246 (defun coffee-comment-dwim (arg)
247 "Comment or uncomment current line or region in a smart way.
248 For detail, see `comment-dwim'."
249 (interactive "*P")
250 (require 'newcomment)
251 (let ((deactivate-mark nil) (comment-start "#") (comment-end ""))
252 (comment-dwim arg)))
84ab684 @defunkt comments
authored
253
5b00ae2 @defunkt more todo
authored
254 (defun coffee-command-full ()
255 "The full `coffee-command' complete with args."
256 (mapconcat 'identity (append (list coffee-command) coffee-command-args) " "))
257
2504ebe @defunkt explain indentation plan
authored
258 ;;
30d2af5 @defunkt Basic imenu support - makes things like `textmate-goto-symbol' work!
authored
259 ;; imenu support
260 ;;
261
262 ;; This is a pretty naive but workable way of doing it. First we look
263 ;; for any lines that starting with `coffee-assign-regexp' that include
264 ;; `coffee-lambda-regexp' then add those tokens to the list.
265 ;;
266 ;; Should cover cases like these:
267 ;;
268 ;; minus: (x, y) -> x - y
269 ;; String::length: -> 10
270 ;; block: ->
271 ;; print('potion')
272 ;;
273 ;; Next we look for any line that starts with `class' or
274 ;; `coffee-assign-regexp' followed by `{` and drop into a
275 ;; namespace. This means we search one indentation level deeper for
276 ;; more assignments and add them to the alist prefixed with the
277 ;; namespace name.
278 ;;
279 ;; Should cover cases like these:
280 ;;
281 ;; class Person
282 ;; print: ->
283 ;; print 'My name is ' + this.name + '.'
284 ;;
285 ;; class Policeman extends Person
286 ;; constructor: (rank) ->
287 ;; @rank: rank
288 ;; print: ->
289 ;; print 'My name is ' + this.name + " and I'm a " + this.rank + '.'
290 ;;
76ed341 @defunkt class support in imenu
authored
291 ;; TODO:
30d2af5 @defunkt Basic imenu support - makes things like `textmate-goto-symbol' work!
authored
292 ;; app = {
293 ;; window: {width: 200, height: 200}
294 ;; para: -> 'Welcome.'
295 ;; button: -> 'OK'
296 ;; }
297
298 (defun coffee-imenu-create-index ()
299 "Create an imenu index of all methods in the buffer."
300 (interactive)
301
302 ;; This function is called within a `save-excursion' so we're safe.
303 (beginning-of-buffer)
304
76ed341 @defunkt class support in imenu
authored
305 (let ((index-alist '()) assign pos indent ns-name ns-indent)
30d2af5 @defunkt Basic imenu support - makes things like `textmate-goto-symbol' work!
authored
306 ;; Go through every assignment that includes -> or => on the same
c6562af @defunkt Make `coffee-debug' a macro
authored
307 ;; line or starts with `class'.
30d2af5 @defunkt Basic imenu support - makes things like `textmate-goto-symbol' work!
authored
308 (while (re-search-forward
76ed341 @defunkt class support in imenu
authored
309 (concat "^\\(\\s *\\)"
310 "\\("
311 coffee-assign-regexp
312 ".+?"
313 coffee-lambda-regexp
314 "\\|"
315 coffee-namespace-regexp
316 "\\)")
30d2af5 @defunkt Basic imenu support - makes things like `textmate-goto-symbol' work!
authored
317 (point-max)
318 t)
319
76ed341 @defunkt class support in imenu
authored
320 ;; If this is the start of a new namespace, save the namespace's
321 ;; indentation level and name.
322 (when (and (not ns-name) (setq ns-name (match-string 7)))
323 ;; If this is a class declaration, add :: to the namespace.
324 (setq ns-name (concat ns-name "::"))
325
326 ;; Save the indentation level.
327 (setq ns-indent (length (match-string 1)))
328
329 ;; Debug
330 (coffee-debug "ns: Found %s with indent %s" ns-name ns-indent))
331
332 ;; If this is an assignment, save the token being
333 ;; assigned. `Please.print:` will be `Please.print`, `block:`
334 ;; will be `block`, etc.
335 (when (setq assign (match-string 3))
336 ;; The position of the match in the buffer.
337 (setq pos (match-beginning 3))
338
339 ;; The indent level of this match
340 (setq indent (length (match-string 1)))
341
342 ;; If we're within the context of a namespace, add that to the
343 ;; front of the assign, e.g.
344 ;; constructor: => Policeman::constructor
345 (when (and ns-name (> indent ns-indent))
346 (setq assign (concat ns-name assign)))
347
348 (coffee-debug "=: Found %s with indent %s" assign indent)
349
350 ;; Clear the namespace if we're no longer indented deeper
351 ;; than it.
352 (when (and ns-name (<= indent ns-indent))
353 (coffee-debug "ns: Clearing %s" ns-name)
354 (setq ns-name nil)
355 (setq ns-indent nil))
356
357 ;; Add this to the alist. Done.
358 (push (cons assign pos) index-alist)))
30d2af5 @defunkt Basic imenu support - makes things like `textmate-goto-symbol' work!
authored
359
360 ;; Return the alist.
361 index-alist))
362
363 ;;
d6b78ed @defunkt defvars and basic indentation
authored
364 ;; Indentation
2504ebe @defunkt explain indentation plan
authored
365 ;;
366
dbb4ec5 @defunkt comment tweaks
authored
367 ;;; The theory is explained in the README.
97a1001 @defunkt basic indentation with TAB
authored
368
d6b78ed @defunkt defvars and basic indentation
authored
369 (defun coffee-indent-line ()
7b03a71 @defunkt indentation theory
authored
370 "Indent current line as CoffeeScript."
d6b78ed @defunkt defvars and basic indentation
authored
371 (interactive)
372
97ef92b @defunkt Bugfix: Indentation works when point is at beginning of the line
authored
373 ;; Bail early by indenting if point as the front of the line.
374 (if (= (point) (point-at-bol))
97a1001 @defunkt basic indentation with TAB
authored
375 (insert-tab)
97ef92b @defunkt Bugfix: Indentation works when point is at beginning of the line
authored
376 (save-excursion
377 (let ((prev-indent 0) (cur-indent 0))
378 ;; Figure out the indentation of the previous line
379 (forward-line -1)
c5ffe04 @defunkt setd macro for logging variable values when setting them
authored
380 (setd prev-indent (current-indentation))
97ef92b @defunkt Bugfix: Indentation works when point is at beginning of the line
authored
381
382 ;; Figure out the current line's indentation
383 (forward-line 1)
c5ffe04 @defunkt setd macro for logging variable values when setting them
authored
384 (setd cur-indent (current-indentation))
97ef92b @defunkt Bugfix: Indentation works when point is at beginning of the line
authored
385
386 ;; Shift one column to the left
97a1001 @defunkt basic indentation with TAB
authored
387 (backward-to-indentation 0)
97ef92b @defunkt Bugfix: Indentation works when point is at beginning of the line
authored
388 (insert-tab)
389
390 ;; We're too far, remove all indentation.
391 (when (> (- (current-indentation) prev-indent) tab-width)
392 (backward-to-indentation 0)
393 (delete-region (point-at-bol) (point)))))))
df08186 @defunkt indenters
authored
394
17150cf @defunkt docs and starting to implement indenters
authored
395 (defun coffee-newline-and-indent ()
396 "Inserts a newline and indents it to the same level as the previous line."
397 (interactive)
398
399 ;; Remember the current line indentation level,
400 ;; insert a newline, and indent the newline to the same
401 ;; level as the previous line.
402 (let ((prev-indent (current-indentation)) (indent-next nil))
403 (newline)
404 (insert-tab (/ prev-indent tab-width))
3a47b28 @defunkt docs
authored
405
17150cf @defunkt docs and starting to implement indenters
authored
406 ;; We need to insert an additional tab because the last line was special.
407 (when (coffee-line-wants-indent)
408 (insert-tab)))
409
410 ;; Last line was a comment so this one should probably be,
411 ;; too. Makes it easy to write multi-line comments (like the one I'm
412 ;; writing right now).
413 (when (coffee-previous-line-is-comment)
414 (insert "# ")))
415
416 ;; Indenters help determine whether the current line should be
417 ;; indented further based on the content of the previous line. If a
418 ;; line starts with `class', for instance, you're probably going to
419 ;; want to indent the next line.
420
421 (defvar coffee-indenters-bol '("class" "for" "if" "try")
7d338d1 @defunkt coffee-indenters
authored
422 "Keywords or syntax whose presence at the start of a line means the
423 next line should probably be indented.")
424
425 (defun coffee-indenters-bol-regexp ()
426 "Builds a regexp out of `coffee-indenters-bol' words."
427 (concat "^" (regexp-opt coffee-indenters-bol 'words)))
428
5e170be @defunkt get it working (with a fake function though)
authored
429 (defvar coffee-indenters-eol '(?> ?{ ?\[)
430 "Single characters at the end of a line that mean the next line
431 should probably be indented.")
7d338d1 @defunkt coffee-indenters
authored
432
17150cf @defunkt docs and starting to implement indenters
authored
433 (defun coffee-line-wants-indent ()
434 "Does the current line want to be indented deeper than the previous
df08186 @defunkt indenters
authored
435 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
436 (interactive)
437
17150cf @defunkt docs and starting to implement indenters
authored
438 (save-excursion
439 (let ((indenter-at-bol) (indenter-at-eol))
440 ;; Go back a line and to the first character.
441 (forward-line -1)
442 (backward-to-indentation 0)
8bfb7ed @defunkt Keep same indentation of last line when creating a new line
authored
443
17150cf @defunkt docs and starting to implement indenters
authored
444 ;; If the next few characters match one of our magic indenter
445 ;; keywords, we want to indent the line we were on originally.
446 (when (looking-at (coffee-indenters-bol-regexp))
c5ffe04 @defunkt setd macro for logging variable values when setting them
authored
447 (setd indenter-at-bol t))
17150cf @defunkt docs and starting to implement indenters
authored
448
449 ;; 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
450 ;; see if the last character matches one of our indenter
451 ;; characters.
17150cf @defunkt docs and starting to implement indenters
authored
452 (when (not indenter-at-bol)
453 (end-of-line)
454
5e170be @defunkt get it working (with a fake function though)
authored
455 ;; Optimized for speed - checks only the last character.
14ab5a0 @defunkt Use Common Lisp's `some` despite its inferior API.
authored
456 (when (some (lambda (char)
457 (= (char-before) char))
458 coffee-indenters-eol)
c5ffe04 @defunkt setd macro for logging variable values when setting them
authored
459 (setd indenter-at-eol t)))
17150cf @defunkt docs and starting to implement indenters
authored
460
461 ;; If we found an indenter, return `t'.
462 (or indenter-at-bol indenter-at-eol))))
463
8bfb7ed @defunkt Keep same indentation of last line when creating a new line
authored
464 (defun coffee-previous-line-is-comment ()
465 "Returns `t' if the previous line is a CoffeeScript comment."
466 (save-excursion
467 (forward-line -1)
468 (coffee-line-is-comment)))
469
470 (defun coffee-line-is-comment ()
471 "Returns `t' if the current line is a CoffeeScript comment."
472 (save-excursion
473 (backward-to-indentation 0)
474 (= (char-after) (string-to-char "#"))))
475
3a47b28 @defunkt docs
authored
476 ;;
477 ;; Define Major Mode
478 ;;
479
af021a5 @josh magic autoload comments
josh authored
480 ;;;###autoload
cf028f8 @defunkt basics
authored
481 (define-derived-mode coffee-mode fundamental-mode
482 "coffee-mode"
483 "Major mode for editing CoffeeScript..."
484
7557530 @defunkt compilation commands
authored
485 (define-key coffee-mode-map (kbd "A-r") 'coffee-compile-buffer)
eee29e0 @defunkt defvar the keymap
authored
486 (define-key coffee-mode-map (kbd "A-R") 'coffee-execute-line)
487 (define-key coffee-mode-map (kbd "A-M-r") 'coffee-repl)
7557530 @defunkt compilation commands
authored
488 (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
489 (define-key coffee-mode-map "\C-m" 'coffee-newline-and-indent)
7557530 @defunkt compilation commands
authored
490
cf028f8 @defunkt basics
authored
491 ;; code for syntax highlighting
492 (setq font-lock-defaults '((coffee-font-lock-keywords)))
493
84ab684 @defunkt comments
authored
494 ;; perl style comment: "# ..."
495 (modify-syntax-entry ?# "< b" coffee-mode-syntax-table)
496 (modify-syntax-entry ?\n "> b" coffee-mode-syntax-table)
0480381 @defunkt mini docs
authored
497 (setq comment-start "#")
84ab684 @defunkt comments
authored
498
9ff612a @defunkt single quotes
authored
499 ;; single quote strings
500 (modify-syntax-entry ?' "\"" coffee-mode-syntax-table)
501 (modify-syntax-entry ?' "\"" coffee-mode-syntax-table)
502
d6b78ed @defunkt defvars and basic indentation
authored
503 ;; indentation
504 (make-local-variable 'indent-line-function)
505 (setq indent-line-function 'coffee-indent-line)
506
30d2af5 @defunkt Basic imenu support - makes things like `textmate-goto-symbol' work!
authored
507 ;; imenu
508 (make-local-variable 'imenu-create-index-function)
509 (setq imenu-create-index-function 'coffee-imenu-create-index)
510
d6b78ed @defunkt defvars and basic indentation
authored
511 ;; no tabs
512 (setq indent-tabs-mode nil)
513
cf028f8 @defunkt basics
authored
514 ;; clear memory
30d2af5 @defunkt Basic imenu support - makes things like `textmate-goto-symbol' work!
authored
515 ;; TODO: make these accurate
cf028f8 @defunkt basics
authored
516 (setq coffee-keywords-regexp nil)
517 (setq coffee-types-regexp nil)
518 (setq coffee-constants-regexp nil)
519 (setq coffee-events-regexp nil)
520 (setq coffee-functions-regexp nil))
eaf28ab @defunkt register as .coffee handler
authored
521
27ffada @defunkt provide something
authored
522 (provide 'coffee-mode)
523
eaf28ab @defunkt register as .coffee handler
authored
524 ;;
525 ;; On Load
526 ;;
527
528 ;; Run coffee-mode for files ending in .coffee.
af021a5 @josh magic autoload comments
josh authored
529 ;;;###autoload
eaf28ab @defunkt register as .coffee handler
authored
530 (add-to-list 'auto-mode-alist '("\\.coffee$" . coffee-mode))
af021a5 @josh magic autoload comments
josh authored
531 ;;;###autoload
605a330 @defunkt highlight Cakefile too
authored
532 (add-to-list 'auto-mode-alist '("Cakefile" . coffee-mode))
Something went wrong with that request. Please try again.