Skip to content

HTTPS clone URL

Subversion checkout URL

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