Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 274 lines (218 sloc) 7.658 kB
7557530 @defunkt compilation commands
authored
1 ;;
0480381 @defunkt mini docs
authored
2 ;; Major thanks to http://xahlee.org/emacs/elisp_syntax_coloring.html
3 ;; the instructions.
38ce8da @defunkt small tweaks
authored
4 ;;
5 ;; Also thanks to Jason Blevins's markdown-mode.el for guidance.
6
7 (require 'easymenu)
8 (require 'font-lock)
0480381 @defunkt mini docs
authored
9
7557530 @defunkt compilation commands
authored
10 ;;
38ce8da @defunkt small tweaks
authored
11 ;; Customizable Variables
7557530 @defunkt compilation commands
authored
12 ;;
13
38ce8da @defunkt small tweaks
authored
14 (defconst coffee-mode-version "0.1.0"
5f14029 @defunkt Menu
authored
15 "The version of this `coffee-mode'.")
16
38ce8da @defunkt small tweaks
authored
17 (defvar coffee-mode-hook nil
18 "A hook for you to run your own code when the mode is loaded.")
19
7557530 @defunkt compilation commands
authored
20 (defvar coffee-command "coffee"
21 "The CoffeeScript command used for evaluating code. Must be in your
22 path.")
23
5f14029 @defunkt Menu
authored
24 (defvar coffee-command-args '("-s" "-p" "--no-wrap")
25 "The command line arguments to pass to `coffee-command' to get it to
26 print the compiled JavaScript.")
27
28 (defun coffee-command-full ()
29 "The full `coffee-command' complete with args."
30 (mapconcat 'identity (append (list coffee-command) coffee-command-args) " "))
31
7557530 @defunkt compilation commands
authored
32 (defvar coffee-js-mode 'js2-mode
33 "The mode to use when viewing compiled JavaScript.")
34
35 (defvar coffee-compiled-buffer-name "*coffee-compiled*"
36 "The name of the scratch buffer used when compiling CoffeeScript.")
37
eee29e0 @defunkt defvar the keymap
authored
38 (defvar coffee-mode-map (make-keymap)
39 "Keymap for CoffeeScript major mode.")
40
38ce8da @defunkt small tweaks
authored
41 ;;
42 ;; Commands
43 ;;
44
45
7557530 @defunkt compilation commands
authored
46 (defun coffee-compile-buffer ()
47 "Compiles the current buffer and displays the JS in the other buffer."
48 (interactive)
49 (save-excursion
50 (coffee-compile-region (point-min) (point-max))))
51
52 (defun coffee-compile-region (start end)
53 "Compiles a region and displays the JS in the other buffer."
54 (interactive "r")
55
56 (let ((buffer (get-buffer coffee-compiled-buffer-name)))
57 (when buffer
58 (kill-buffer buffer)))
59
60 (call-process-region start end coffee-command nil
61 (get-buffer-create coffee-compiled-buffer-name)
62 nil
63 "-s" "-p" "--no-wrap")
64 (switch-to-buffer-other-frame (get-buffer coffee-compiled-buffer-name))
65 (funcall coffee-js-mode)
66 (beginning-of-buffer))
3a47b28 @defunkt docs
authored
67
5f14029 @defunkt Menu
authored
68 (defun coffee-show-version ()
69 "Prints the `coffee-mode' version."
70 (interactive)
71 (message (concat "coffee-mode v" coffee-mode-version)))
72
fd314cc @defunkt menu tweaks
authored
73 (defun coffee-open-reference ()
74 "Open browser to CoffeeScript reference."
75 (interactive)
76 (browse-url "http://jashkenas.github.com/coffee-script/"))
77
5f14029 @defunkt Menu
authored
78 (defun coffee-open-github ()
fd314cc @defunkt menu tweaks
authored
79 "Open browser to `coffee-mode' project on GithHub."
5f14029 @defunkt Menu
authored
80 (interactive)
81 (browse-url "http://github.com/defunkt/coffee-mode"))
82
83 ;;
84 ;; Menubar
85 ;;
86
87 (easy-menu-define coffee-mode-menu coffee-mode-map
88 "Menu for CoffeeScript mode"
89 '("CoffeeScript"
90 ["Compile Buffer" coffee-compile-buffer]
91 ["Compile Region" coffee-compile-region]
92 "---"
fd314cc @defunkt menu tweaks
authored
93 ["CoffeeScript reference" coffee-open-reference]
94 ["coffee-mode on GitHub" coffee-open-github]
5f14029 @defunkt Menu
authored
95 ["Version" coffee-show-version]
96 ))
97
3a47b28 @defunkt docs
authored
98 ;;
99 ;; Define Language Syntax
100 ;;
101
102 ;; Assignment
d6b78ed @defunkt defvars and basic indentation
authored
103 (defvar coffee-type-regexp ".+?:")
3a47b28 @defunkt docs
authored
104
105 ;; Instance variables (implicit this)
d6b78ed @defunkt defvars and basic indentation
authored
106 (defvar coffee-constant-regexp "@\\w*\\|this")
3a47b28 @defunkt docs
authored
107
af34f91 @defunkt Booleans, better colors
authored
108 ;; Booleans
d6b78ed @defunkt defvars and basic indentation
authored
109 (defvar coffee-functions-regexp "\\b\\(true\\|false\\|yes\\|no\\|on\\|off\\)\\b")
3a47b28 @defunkt docs
authored
110
111 ;; Unused
d6b78ed @defunkt defvars and basic indentation
authored
112 (defvar coffee-event-regexp "")
3a47b28 @defunkt docs
authored
113
cf028f8 @defunkt basics
authored
114 ;; JavaScript Keywords
d6b78ed @defunkt defvars and basic indentation
authored
115 (defvar coffee-js-keywords
af34f91 @defunkt Booleans, better colors
authored
116 '("if" "else" "new" "return" "try" "catch"
cf028f8 @defunkt basics
authored
117 "finally" "throw" "break" "continue" "for" "in" "while"
118 "delete" "instanceof" "typeof" "switch" "super" "extends"
119 "class"))
120
121 ;; Reserved keywords either by JS or CS.
d6b78ed @defunkt defvars and basic indentation
authored
122 (defvar coffee-js-reserved
cf028f8 @defunkt basics
authored
123 '("case" "default" "do" "function" "var" "void" "with"
124 "const" "let" "debugger" "enum" "export" "import" "native"
125 "__extends" "__hasProp"))
126
127 ;; CoffeeScript keywords.
d6b78ed @defunkt defvars and basic indentation
authored
128 (defvar coffee-cs-keywords
af34f91 @defunkt Booleans, better colors
authored
129 '("then" "unless" "and" "or" "is"
cf028f8 @defunkt basics
authored
130 "isnt" "not" "of" "by" "where" "when"))
131
0480381 @defunkt mini docs
authored
132 ;; Regular expression combining the above three lists.
d6b78ed @defunkt defvars and basic indentation
authored
133 (defvar coffee-keywords-regexp (regexp-opt
cf028f8 @defunkt basics
authored
134 (append
135 coffee-js-reserved
136 coffee-js-keywords
137 coffee-cs-keywords) 'words))
138
139
0480381 @defunkt mini docs
authored
140 ;; Create the list for font-lock.
141 ;; Each class of keyword is given a particular face
d6b78ed @defunkt defvars and basic indentation
authored
142 (defvar coffee-font-lock-keywords
cf028f8 @defunkt basics
authored
143 `(
144 (,coffee-type-regexp . font-lock-type-face)
af34f91 @defunkt Booleans, better colors
authored
145 (,coffee-constant-regexp . font-lock-variable-name-face)
cf028f8 @defunkt basics
authored
146 (,coffee-event-regexp . font-lock-builtin-face)
af34f91 @defunkt Booleans, better colors
authored
147 (,coffee-functions-regexp . font-lock-constant-face)
cf028f8 @defunkt basics
authored
148 (,coffee-keywords-regexp . font-lock-keyword-face)
149
150 ;; note: order above matters. `coffee-keywords-regexp' goes last because
151 ;; otherwise the keyword "state" in the function "state_entry"
152 ;; would be highlighted.
153 ))
154
3a47b28 @defunkt docs
authored
155 ;;
156 ;; Helper Functions
157 ;;
158
0480381 @defunkt mini docs
authored
159 ;; The command to comment/uncomment text
9ff612a @defunkt single quotes
authored
160 (defun coffee-comment-dwim (arg)
161 "Comment or uncomment current line or region in a smart way.
162 For detail, see `comment-dwim'."
163 (interactive "*P")
164 (require 'newcomment)
165 (let ((deactivate-mark nil) (comment-start "#") (comment-end ""))
166 (comment-dwim arg)))
84ab684 @defunkt comments
authored
167
2504ebe @defunkt explain indentation plan
authored
168 ;;
d6b78ed @defunkt defvars and basic indentation
authored
169 ;; Indentation
2504ebe @defunkt explain indentation plan
authored
170 ;;
171
172 ;; The theory here is simple:
173 ;; When you press TAB, indent the line unless doing so would make the
174 ;; current line more than two indentation levels deepers than the
175 ;; previous line. If that's the case, remove all indentation.
176 ;;
177 ;; Consider this code, with point at the position indicated by the
178 ;; carot:
179 ;;
180 ;; line1()
181 ;; line2()
182 ;; line3()
183 ;; ^
184 ;; Pressing TAB will produce the following code:
185 ;;
186 ;; line1()
187 ;; line2()
188 ;; line3()
189 ;; ^
190 ;;
191 ;; Pressing TAB again will produce this code:
192 ;;
193 ;; line1()
194 ;; line2()
195 ;; line3()
196 ;; ^
197 ;;
198 ;; And so on.
97a1001 @defunkt basic indentation with TAB
authored
199
d6b78ed @defunkt defvars and basic indentation
authored
200 (defun coffee-indent-line ()
201 "Indent current line as CoffeeScript"
202 (interactive)
203
204 (save-excursion
205 (let ((prev-indent 0) (cur-indent 0))
97a1001 @defunkt basic indentation with TAB
authored
206 ;; Figure out the indentation of the previous line
d6b78ed @defunkt defvars and basic indentation
authored
207 (forward-line -1)
208 (setq prev-indent (current-indentation))
97a1001 @defunkt basic indentation with TAB
authored
209
210 ;; Figure out the current line's indentation
d6b78ed @defunkt defvars and basic indentation
authored
211 (forward-line 1)
212 (setq cur-indent (current-indentation))
213
97a1001 @defunkt basic indentation with TAB
authored
214 ;; Shift one column to the left
d6b78ed @defunkt defvars and basic indentation
authored
215 (backward-to-indentation 0)
97a1001 @defunkt basic indentation with TAB
authored
216 (insert-tab)
217
218 ;; We're too far, remove all indentation.
219 (when (> (- (current-indentation) prev-indent) tab-width)
220 (backward-to-indentation 0)
221 (delete-region (point-at-bol) (point))))))
3a47b28 @defunkt docs
authored
222
223 ;;
224 ;; Define Major Mode
225 ;;
226
cf028f8 @defunkt basics
authored
227 (define-derived-mode coffee-mode fundamental-mode
228 "coffee-mode"
229 "Major mode for editing CoffeeScript..."
230
7557530 @defunkt compilation commands
authored
231 (define-key coffee-mode-map (kbd "A-r") 'coffee-compile-buffer)
eee29e0 @defunkt defvar the keymap
authored
232 (define-key coffee-mode-map (kbd "A-R") 'coffee-execute-line)
233 (define-key coffee-mode-map (kbd "A-M-r") 'coffee-repl)
7557530 @defunkt compilation commands
authored
234 (define-key coffee-mode-map [remap comment-dwim] 'coffee-comment-dwim)
235
cf028f8 @defunkt basics
authored
236 ;; code for syntax highlighting
237 (setq font-lock-defaults '((coffee-font-lock-keywords)))
238
84ab684 @defunkt comments
authored
239 ;; perl style comment: "# ..."
240 (modify-syntax-entry ?# "< b" coffee-mode-syntax-table)
241 (modify-syntax-entry ?\n "> b" coffee-mode-syntax-table)
0480381 @defunkt mini docs
authored
242 (setq comment-start "#")
84ab684 @defunkt comments
authored
243
9ff612a @defunkt single quotes
authored
244 ;; single quote strings
245 (modify-syntax-entry ?' "\"" coffee-mode-syntax-table)
246 (modify-syntax-entry ?' "\"" coffee-mode-syntax-table)
247
5ca4229 @defunkt Regular expressions
authored
248 ;; regular expressions
249 (modify-syntax-entry ?/ "\"" coffee-mode-syntax-table)
250 (modify-syntax-entry ?/ "\"" coffee-mode-syntax-table)
251
d6b78ed @defunkt defvars and basic indentation
authored
252 ;; indentation
253 (make-local-variable 'indent-line-function)
254 (setq indent-line-function 'coffee-indent-line)
255
256 ;; no tabs
257 (setq indent-tabs-mode nil)
258
cf028f8 @defunkt basics
authored
259 ;; clear memory
260 (setq coffee-keywords-regexp nil)
261 (setq coffee-types-regexp nil)
262 (setq coffee-constants-regexp nil)
263 (setq coffee-events-regexp nil)
264 (setq coffee-functions-regexp nil))
eaf28ab @defunkt register as .coffee handler
authored
265
27ffada @defunkt provide something
authored
266 (provide 'coffee-mode)
267
eaf28ab @defunkt register as .coffee handler
authored
268 ;;
269 ;; On Load
270 ;;
271
272 ;; Run coffee-mode for files ending in .coffee.
273 (add-to-list 'auto-mode-alist '("\\.coffee$" . coffee-mode))
Something went wrong with that request. Please try again.