Skip to content

HTTPS clone URL

Subversion checkout URL

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