forked from syohex/elixir-mode
/
elixir-mode.el
357 lines (311 loc) · 9.13 KB
/
elixir-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
;;; elixir-mode.el --- Major mode for editing Elixir files
;; Copyright (c) 2011 secondplanet
;; Author: Humza Yaqoob, Andreas Fuchs <asf@boinkor.net>
;; URL: https://github.com/elixir-lang/emacs-elixir
;; Created: Mon Nov 7 2011
;; Keywords: languages elixir
;; Version: 1.1.0
;; This file is not a part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;; Commentary:
;; Provides font-locking, indentation support, and navigation for Elixir programs.
;;
;; To install, add the put this directory on the `load-path', and load
;; `elixir-mode-setup.el'. Then, run:
;;
;; (elixir-mode-setup)
;;
;; To auto-load the mode and set up file associations.
;;; Code:
(require 'comint) ; for interactive REPL
(require 'easymenu) ; for menubar features
(require 'elixir-smie) ; syntax and indentation support
(require 'elixir-mode-setup) ; Contains only the elixir-mode-setup function.
;;;###autoload
(defvar elixir-mode-hook nil)
;;;###autoload
(defvar elixir-mode-map (make-keymap)
"Elixir mode keymap.")
(defgroup elixir nil
"Elixir major mode."
:group 'languages)
(defcustom elixir-compiler-command "elixirc"
"Elixir mode command to compile code. Must be in your path."
:type 'string
:group 'elixir)
(defcustom elixir-iex-command "iex"
"Elixir mode command for interactive REPL. Must be in your path."
:type 'string
:group 'elixir)
(defcustom elixir-mode-highlight-operators t
"Elixir mode option for whether or not to highlight operators."
:type 'boolean
:group 'elixir)
(defcustom elixir-mode-cygwin-paths t
"Elixir mode use Cygwin style paths on Windows operating systems."
:type 'boolean
:group 'elixir)
(defcustom elixir-mode-cygwin-prefix "/cygdrive/C"
"Elixir mode Cygwin prefix."
:type 'string
:group 'elixir)
(defvar elixir-mode-define-names
'("def"
"defdelegate"
"defmacro"
"defmacrop"
"defoverridable"
"defp"
"defmacrop")
"Elixir mode def-like keywords.")
(defvar elixir-mode-keyword-names
'("->"
"bc"
"lc"
"in"
"inbits"
"inlist"
"quote"
"unquote"
"unquote_splicing"
"var"
"do"
"after"
"for"
"def"
"defdelegate"
"defimpl"
"defmacro"
"defmacrop"
"defmodule"
"defoverridable"
"defp"
"defprotocol"
"defrecord"
"destructure"
"alias"
"require"
"import"
"use"
"if"
"true"
"false"
"when"
"case"
"cond"
"throw"
"then"
"else"
"elsif"
"try"
"catch"
"rescue"
"fn"
"function"
"receive"
"end")
"Elixir mode keywords.")
(defvar elixir-mode-module-names
'("Behavior"
"Binary"
"Bitwise"
"Builtin"
"Elixir"
"Code"
"EEx"
"Enum"
"ExUnit"
"Exception"
"File"
"GenServer"
"Function"
"GenServer"
"GenTCP"
"HashDict"
"IO"
"Keyword"
"List"
"Math"
"Module"
"Node"
"OptionParser"
"OrdDict"
"Port"
"Process"
"Record"
"Regexp"
"System"
"Tuple"
"URI"
"UnboundMethod")
"Elixir mode modules.")
(defvar elixir-mode-builtin-names
'("Erlang"
"__MODULE__"
"__LINE__"
"__FILE__"
"__ENV__")
"Elixir mode builtins.")
(defvar elixir-mode-operator-names
'("+"
"++"
"<>"
"-"
"/"
"*"
"div"
"rem"
"=="
"!="
"<="
"<"
">="
">"
"==="
"!=="
"and"
"or"
"not"
"&&"
"||"
"!"
"."
"#"
"="
":="
"<-")
"Elixir mode operators.")
(defvar elixir-basic-offset 2)
(defvar elixir-key-label-offset 0)
(defvar elixir-match-label-offset 2)
(defvar font-lock-operator-face 'font-lock-operator-face)
(defface font-lock-operator-face
'((((type tty) (class color)) nil)
(((class color) (background light))
(:foreground "darkred"))
(t nil))
"For use with operators."
:group 'font-lock-faces)
(defconst elixir-mode-font-lock-defaults
(list
;; comments:
'("#.*$" . font-lock-comment-face)
;; records and modules at point of definition:
'("^\\s *def\\(module\\|record\\|protocol\\|impl\\)\\s +\\([^( \t\n,]+\\)" 2 font-lock-type-face)
;; methods:
`(,(concat "^\\s *\\<" (regexp-opt elixir-mode-define-names t) "\\>\\s +\\([^( \t\n]+\\)") 2 font-lock-function-name-face)
;; keywords:
`(,(concat "\\<" (regexp-opt elixir-mode-keyword-names t) "\\>") . font-lock-keyword-face)
;; builtins:
`(,(concat "\\<" (regexp-opt elixir-mode-builtin-names t) "\\>") . font-lock-builtin-face)
;; core modules:
`(,(concat "\\<" (regexp-opt elixir-mode-module-names t) "\\>") . font-lock-type-face)
;; operators:
(when elixir-mode-highlight-operators
`(,(concat "\\<" (regexp-opt elixir-mode-operator-names t) "\\>") . font-lock-operator-face))
;; variables:
'("\\(\\w*\\)\\s-*:?=" . font-lock-variable-name-face)
;; regexes:
'("-[Rr].*[ \n\t]" . font-lock-constant-face)
;; atoms, boolean:
'("\\<\\(true\\|false\\|nil\\)\\>" . font-lock-reference-face)
;; atoms, generic
'(":\\w*" . font-lock-reference-face)
'("\\w*:\s" . font-lock-reference-face)
'(":\\w*" . font-lock-reference-face))
"Highlighting for Elixir mode.")
(defun elixir-mode-cygwin-path (expanded-file-name)
"Elixir mode get Cygwin absolute path name."
(replace-regexp-in-string "^[a-zA-Z]:" elixir-mode-cygwin-prefix expanded-file-name t))
(defun elixir-mode-universal-path (file-name)
"Elixir mode multi-OS path handler."
(let ((full-file-name (expand-file-name file-name)))
(if (and (equal system-type 'windows-nt)
elixir-mode-cygwin-paths)
(elixir-mode-cygwin-path full-file-name)
full-file-name)))
(defun elixir-mode-command-compile (file-name)
"Elixir mode command to compile a file."
(let ((full-file-name (elixir-mode-universal-path file-name)))
(mapconcat 'identity (append (list elixir-compiler-command) (list full-file-name)) " ")))
(defun elixir-mode-compiled-file-name (&optional filename)
"Elixir mode compiled filename."
(concat (file-name-sans-extension (or filename (buffer-file-name))) ".beam"))
(defun elixir-mode-compile-file ()
"Elixir mode compile and save current file."
(interactive)
(let ((compiler-output (shell-command-to-string (elixir-mode-command-compile (buffer-file-name)))))
(when (string= compiler-output "")
(message "Compiled and saved as %s" (elixir-mode-compiled-file-name)))))
(defun elixir-mode-iex ()
"Elixir mode interactive REPL."
(interactive)
(unless (comint-check-proc "*IEX*")
(set-buffer
(apply 'make-comint "IEX"
elixir-iex-command nil '())))
(pop-to-buffer "*IEX*"))
(defun elixir-mode-open-modegithub ()
"Elixir mode open GitHub page."
(interactive)
(browse-url "https://github.com/elixir-lang/emacs-elixir"))
(defun elixir-mode-open-elixir-home ()
"Elixir mode go to language home."
(interactive)
(browse-url "http://elixir-lang.org"))
(defun elixir-mode-show-version ()
"Elixir mode print version."
(interactive)
(message (concat "elixir-mode v" elixir-mode-version " " elixir-mode-date " by Humza Yaqoob")))
(easy-menu-define elixir-mode-menu elixir-mode-map
"Elixir mode menu."
'("Elixir"
["Indent line" smie-indent-line]
["Compile file" elixir-mode-compile-file]
["IEX" elixir-mode-iex]
"---"
["elixir-mode on GitHub" elixir-mode-open-modegithub]
["Elixir homepage" elixir-mode-open-elixirhome]
["About" elixir-mode-show-version]
))
;;;###autoload
(defun elixir-mode ()
"Major mode for editing Elixir files."
(interactive)
(kill-all-local-variables)
(set-syntax-table elixir-mode-syntax-table)
(set (make-local-variable 'font-lock-defaults) '(elixir-mode-font-lock-defaults))
(setq major-mode 'elixir-mode)
(setq mode-name "Elixir")
(set (make-local-variable 'comment-start) "# ")
(set (make-local-variable 'comment-end) "")
(set (make-variable-buffer-local 'tab-width) elixir-basic-offset)
(set (make-variable-buffer-local 'default-tab-width) elixir-basic-offset)
(smie-setup elixir-smie-grammar 'verbose-elixir-smie-rules ; 'elixir-smie-rules
:forward-token 'elixir-smie-forward-token
:backward-token 'elixir-smie-backward-token)
(run-hooks 'elixir-mode-hook)
(run-hooks 'prog-mode-hook))
(define-minor-mode elixir-cos-mode
"Elixir mode toggle compile on save."
:group 'elixir-cos :lighter " CoS"
(cond
(elixir-cos-mode
(add-hook 'after-save-hook 'elixir-mode-compile-file nil t))
(t
(remove-hook 'after-save-hook 'elixir-mode-compile-file t))))
(provide 'elixir-mode)
;;;***
;; Local variables:
;; generated-autoload-file: elixir-mode-setup.el
;;; elixir-mode.el ends here