forked from k1complete/elixir-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelixir-mode.el
448 lines (404 loc) · 13.1 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
;;; elixir-mode.el --- Major mode for editing Elixir files
;; Copyright (c) 2011 secondplanet
;; Author: Humza Yaqoob
;; URL: https://github.com/secondplanet/elixir-mode
;; Created: Mon Nov 7 2011
;; Keywords: languages elixir
;; Version: 1.0.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.
;;; Code:
(require 'comint) ; for interactive REPL
(require 'easymenu) ; for menubar features
(defconst elixir-mode-version "2.0.0"
"Elixir mode version number.")
(defconst elixir-mode-date "2012-08-16"
"Elixir mode version date.")
(defvar elixir-mode-hook nil)
(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-debug-flag nil)
(defun elixir-mode-message (s &rest objects)
(if elixir-mode-debug-flag
(message s objects)))
(defvar elixir-mode-define-names '(
"def"
"defdelegate"
"defimpl"
"defmacro"
"defmacrop"
"defmodule"
"defoverridable"
"defp"
"defprotocol"
"defrecord"
"destructure")
"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"
"refer"
"require"
"import"
"use"
"if"
"true"
"false"
"when"
"case"
"cond"
"throw"
"then"
"else"
"elsif"
"try"
"catch"
"rescue"
"fn"
"function"
"%r"
"%R"
"%b"
"%B"
"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"
"OptrionParser"
"OrdDict"
"Port"
"Process"
"Record"
"Regexp"
"System"
"Tuple"
"URI"
"UnboundMethod")
"Elixir mode modules.")
(defvar elixir-mode-builtin-names '(
"Erlang"
"__MODULE__"
"__FUNCTION__"
"__LINE__"
"__FILE__"
"__LOCAL__"
)
"Elixir mode builtins.")
(defvar elixir-mode-operator-names '(
"+"
"++"
"<>"
"-"
"/"
"*"
"div"
"rem"
"=="
"!="
"<="
"<"
">="
">"
"==="
"!=="
"and"
"or"
"andalso"
"orelse"
"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
'("#.*$" . font-lock-comment-face) ; comments
`(,(concat "^\\s *\\<" (regexp-opt elixir-mode-define-names t) "\\>\\s +\\([^( \t\n]+\\)") 2 font-lock-function-name-face) ; methods
`(,(concat "\\<" (regexp-opt elixir-mode-keyword-names t) "\\>") . font-lock-keyword-face) ; keywords
`(,(concat "\\<" (regexp-opt elixir-mode-builtin-names t) "\\>") . font-lock-builtin-face) ; builtins
`(,(concat "\\<" (regexp-opt elixir-mode-module-names t) "\\>") . font-lock-type-face) ; core modules
(when elixir-mode-highlight-operators `(,(concat "\\<" (regexp-opt elixir-mode-operator-names t) "\\>") . font-lock-operator-face)) ; operators
'("\\(\\w*\\)\\s-*:?=" . font-lock-variable-name-face) ; variables
'("-[Rr].*[ \n\t]" . font-lock-constant-face) ; regexes
'("\\<\\(true\\|false\\|nil\\)\\>" . font-lock-reference-face) ; atoms, boolean
'("\\w*:\s" . font-lock-reference-face)
'(":\\w*" . font-lock-reference-face)) ; atoms, generic
"Highlighting for Elixir mode.")
(defun elixir-mode-takewhile (f l)
(let ((not-done t) (n l) (ret))
(while (and n not-done)
(setq ret (funcall f (car n)))
(setq n (cdr n))
(setq not-done (car ret)))
ret))
(defvar elixir-mode-endmark "^[ \t]*\\<end\\>")
(defvar elixir-mode-beginmark ".*\\(\\<fn.*(.*).*->\\|\\<do\\>\\)$")
(defvar elixir-mode-mbeginmark ".*\\<\\(fn\\|\\(cond\\|case\\|receive\\).*\\)[ \t\+do$")
(defvar elixir-mode-onelinermark ".*->.*\\<end\\>$")
(defvar elixir-mode-keymark "^[ \t]*\\(else\\|after\\)$")
(defvar elixir-mode-labelmark ".*->.*")
(defun elixir-mode-previous-line-last-offset ()
"find last block + basic-offset"
(let ((level 1))
(progn
(while (and (> level 0) (not (bobp)))
(forward-line -1)
(cond ((looking-at elixir-mode-endmark)
(setq level (+ level 1)))
((looking-at elixir-mode-beginmark)
(setq level (- level 1)))))
(cond ((bobp)
0)
(t
(+ (current-indentation) elixir-basic-offset))))))
(defun elixir-mode-find-type ()
"find keyword type in (begin|end|normal|label|key)"
(progn
(elixir-mode-message "%s" "find-type")
(elixir-mode-message "%s" (thing-at-point 'line))
(cond ((looking-at elixir-mode-endmark)
'end)
((looking-at elixir-mode-beginmark)
'begin)
((looking-at elixir-mode-onelinermark)
'normal)
((looking-at elixir-mode-labelmark)
'label)
((looking-at elixir-mode-keymark)
'key)
(t
'normal))))
(defun elixir-mode-previous-line-offset ()
"calc offset by previous line type in (begin|label|key|other)"
(save-excursion
(let ((type))
(forward-line -1)
(setq type (elixir-mode-find-type))
(cond ((eq type 'begin)
(+ (current-indentation) elixir-basic-offset))
((eq type 'label)
(+ (current-indentation) elixir-basic-offset))
((eq type 'key)
(+ (- (current-indentation) elixir-key-label-offset)
elixir-basic-offset))
(t
(current-indentation))))))
(defun elixir-mode-indent-line ()
"Indent current line as Elixir code."
(interactive)
(beginning-of-line)
(let ((ttype) (poffset) (noffset))
(save-excursion
(setq ttype (elixir-mode-find-type))
(setq noffset (cond ((eq ttype 'label)
(+ (- (elixir-mode-previous-line-last-offset)
elixir-basic-offset)
elixir-match-label-offset))
((eq ttype 'key)
(+ (- (elixir-mode-previous-line-last-offset)
elixir-basic-offset)
elixir-key-label-offset))
((eq ttype 'end)
(- (elixir-mode-previous-line-last-offset)
elixir-basic-offset))
((or (eq ttype 'normal) (eq ttype 'begin))
(elixir-mode-previous-line-offset)))))
(cond ((> 0 noffset)
(setq noffset 0)))
(indent-line-to noffset)))
(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/secondplanet/elixir-mode"))
(defun elixir-mode-open-elixir-home ()
"Elixir mode go to language home."
(interactive)
(browse-url "https://github.com/josevalim/elixir#README"))
(defun elixir-mode-show-version ()
"Elixir mode print version."
(interactive)
(message "%s" (concat "elixir-mode v" elixir-mode-version " " elixir-mode-date " by Humza Yaqoob")))
(defvar elixir-mode-syntax-table
(let ((elixir-mode-syntax-table (make-syntax-table)))
(modify-syntax-entry ?? "\\" elixir-mode-syntax-table)
(modify-syntax-entry ?_ "w" elixir-mode-syntax-table)
; (modify-syntax-entry ?# "<" elixir-mode-syntax-table)
; (modify-syntax-entry ?\n ">" elixir-mode-syntax-table)
(modify-syntax-entry ?\( "()" elixir-mode-syntax-table)
(modify-syntax-entry ?\) ")(" elixir-mode-syntax-table)
(modify-syntax-entry ?\{ "(}" elixir-mode-syntax-table)
(modify-syntax-entry ?\} "){" elixir-mode-syntax-table)
(modify-syntax-entry ?\[ "(]" elixir-mode-syntax-table)
(modify-syntax-entry ?\] ")[" elixir-mode-syntax-table)
(modify-syntax-entry ?\: "'" elixir-mode-syntax-table)
elixir-mode-syntax-table)
"Elixir mode syntax table.")
(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]
))
(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) "")
(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))))
(require 'elixir-smie)
(provide 'elixir-mode)
;; automatically opening ex and exs in elixir-mode
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.ex\\'" . elixir-mode))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.exs\\'" . elixir-mode))
;;; elixir-mode.el ends here