forked from sail-pl/SAIL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsail.el
92 lines (73 loc) · 2.71 KB
/
sail.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
(defconst sail-keywords-regex
(regexp-opt '("if" "then" "else" "return" "while" "await" "emit" "watching" "run" "and" "signal" "var" "global" "when" "watching" "case") 'words))
(defconst sail-types-regex
(regexp-opt '("float" "int" "string" "char" "bool") 'words))
(defconst sail-decls-regex
(regexp-opt '("struct" "enum" "method" "module" "process" "where" "with" "as") 'words))
(defconst sail-constant-regex
(regexp-opt '( "true" "false") 'words))
(defconst sail-font-lock-keywords
(list
(cons sail-keywords-regex 'font-lock-keyword-face)
(cons sail-types-regex 'font-lock-type-face)
(cons sail-decls-regex 'font-lock-function-name-face)
(cons sail-constant-regex 'font-lock-constant-face)
))
(setq-default tab-width 4)
(setq starter (concat "^[ \t]*"(regexp-opt '("struct" "enum" "method" "module") t)))
(defun next-indent()
(let ((counter 0))
(save-excursion
(beginning-of-line)
(if (looking-at "^[ \t]*}") (incf counter))
(while (< (point) (line-end-position))
(when (looking-at (rx "{"))
(incf counter))
(when (looking-at (rx "}"))
(decf counter))
(forward-char 1)))
counter
)
)
(defun sail-indent-line ()
"Indent current line as SAIL code"
(interactive)
(beginning-of-line)
(if (or (bobp) (looking-at starter))(indent-line-to 0)
(let ((cur-indent 0))
(save-excursion
(forward-line -1)
(setq cur-indent (+ (current-indentation)
(* (next-indent) tab-width))))
(beginning-of-line)
(if (looking-at "^[ \t]*}")
(setq cur-indent (- cur-indent tab-width)))
(if (< cur-indent 0) (setq cur-indent 0))
(indent-line-to cur-indent)
)))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.sl\\'" . sail-mode))
(define-derived-mode sail-mode fundamental-mode "SAIL"
"Major mode for editing SAIL files."
(set (make-local-variable 'font-lock-defaults) '(sail-font-lock-keywords))
(set (make-local-variable 'indent-line-function) 'sail-indent-line)
)
(provide 'sail-mode)
;; (defvar sail-mode-syntax-table
;; (let ((table (make-syntax-table)))
;; (modify-syntax-entry ?\{ "(} " table)
;; (modify-syntax-entry ?\} "){ " table)
;; (modify-syntax-entry ?\( "() " table)
;; (modify-syntax-entry ?\) ")( " table)
;; (modify-syntax-entry ?\[ "(] " table)
;; (modify-syntax-entry ?\] ")[ " table)
;; (modify-syntax-entry ?< "." table)
;; (modify-syntax-entry ?> "." table)
;; (modify-syntax-entry ?* "." table)
;; (modify-syntax-entry ?/ "." table)
;; (modify-syntax-entry ?+ "." table)
;; (modify-syntax-entry ?- "." table)
;; (modify-syntax-entry ?% "." table)
;; table)
;; "Syntax table for sail-mode")
;;; sail-mode.el ends here