forked from torus/alloy-mode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
alloy-mode.el
265 lines (242 loc) · 9 KB
/
alloy-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
; This is an emacs major mode for Alloy3 that does keyword coloring
; and indentation.
; Copyright (C) 2002 Allison L Waingold (skippy AT mit DOT edu)
; 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
; of the License, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
(defvar alloy-font-lock-keywords
(let ((kw1 (mapconcat 'identity
'("sig" "fun" "det" "let" "extends" "abstract"
"static" "disj" "option" "set" "all"
"one" "some" "sole" "open"
"uses" "run" "check" "eval" "for" "but" "none" "exactly"
"univ" "iden" "in" "no" "not"
"with" "sum" "if" "then" "else"
"pred" "iff" "implies"
"and" "or" ;; "=>" "=" "+" "-"
)
"\\|"))
(kw2 (mapconcat 'identity
'("module" "fact" "assert")
"\\|"))
)
(list
;; keywords
(cons (concat "\\b\\(" kw1 "\\)\\b[ \n\t(]") 1)
;; block introducing keywords with immediately following colons.
(cons (concat "\\b\\(" kw2 "\\)[ \n\t(]") 1)
;; classes
'("\\bsig[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
1 font-lock-type-face)
'("\\bextends[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
1 font-lock-type-face)
;; functions
'("\\bfun[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
1 font-lock-function-name-face)
'("\\bmodule[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
1 font-lock-function-name-face)
))
"Additional expressions to highlight in Alloy mode."
)
(defvar alloy-mode-map ()
"Keymap used in `alloy-mode' buffers.")
(if alloy-mode-map
nil
(progn
(setq alloy-mode-map (make-sparse-keymap))
(define-key alloy-mode-map "\C-m" 'alloy-return)
))
(defun alloy-return (&optional arg)
(interactive)
(alloy-indent-line)
(newline-and-indent)
)
(defvar alloy-mode-syntax-table nil
"Syntax table used in `alloy-mode' buffers.")
(if alloy-mode-syntax-table
nil
(setq alloy-mode-syntax-table (make-syntax-table))
(modify-syntax-entry ?\( "()" alloy-mode-syntax-table)
(modify-syntax-entry ?\) ")(" alloy-mode-syntax-table)
(modify-syntax-entry ?\[ "(]" alloy-mode-syntax-table)
(modify-syntax-entry ?\] ")[" alloy-mode-syntax-table)
(modify-syntax-entry ?\{ "(}" alloy-mode-syntax-table)
(modify-syntax-entry ?\} "){" alloy-mode-syntax-table)
;; Add operator symbols misassigned in the std table
(modify-syntax-entry ?\$ "." alloy-mode-syntax-table)
(modify-syntax-entry ?\% "." alloy-mode-syntax-table)
(modify-syntax-entry ?\& "." alloy-mode-syntax-table)
(modify-syntax-entry ?\* "." alloy-mode-syntax-table)
(modify-syntax-entry ?\+ "." alloy-mode-syntax-table)
(modify-syntax-entry ?\- "." alloy-mode-syntax-table)
(modify-syntax-entry ?\/ "." alloy-mode-syntax-table)
(modify-syntax-entry ?\< "." alloy-mode-syntax-table)
(modify-syntax-entry ?\= "." alloy-mode-syntax-table)
(modify-syntax-entry ?\> "." alloy-mode-syntax-table)
(modify-syntax-entry ?\| "." alloy-mode-syntax-table)
(modify-syntax-entry ?\_ "w" alloy-mode-syntax-table)
(modify-syntax-entry ?\' "w" alloy-mode-syntax-table)
(modify-syntax-entry ?\" "w" alloy-mode-syntax-table)
;; backquote is open and close paren
(modify-syntax-entry ?\` "$" alloy-mode-syntax-table)
;; comment delimiters
(modify-syntax-entry ?/ ". 124b" alloy-mode-syntax-table)
(modify-syntax-entry ?* ". 23" alloy-mode-syntax-table)
(modify-syntax-entry ?- ". 12b" alloy-mode-syntax-table)
(modify-syntax-entry ?\n "> b " alloy-mode-syntax-table)
(modify-syntax-entry ? " " alloy-mode-syntax-table)
(modify-syntax-entry ?\t " " alloy-mode-syntax-table)
(modify-syntax-entry ?\r " " alloy-mode-syntax-table)
(modify-syntax-entry ?\f " " alloy-mode-syntax-table))
(defvar alloy-indent-expr "[{|]")
(defvar same-indent-expr "&&\\|||")
(defun in-comment()
(save-excursion
(if (looking-at "//\\|/\\*") ;; if we are at the start of a comment, true
t
(let ((startpoint (point)))
(re-search-backward "//" (- (point) (current-column)) t 1)
(if (< (point) startpoint) ;; if there is a // before us in the line
t ;; in comment
(progn
(re-search-backward "/\\*" nil t 1)
(if (< (point) startpoint) ;; if there is a /* somewhere before us
(let ((opencomm (point)))
;;(insert "before")
(goto-char startpoint)
(re-search-backward "\\*/" opencomm t 1)
(if (< (point) startpoint) ;; and a */ between /* and us
nil ;; not in comment
t) ;; in comment
)
)
)
)
)
)
)
)
(defgroup alloy nil
"Alloy-mode customizations."
:group 'languages)
(defcustom alloy-basic-offset 3
"Basic indentation offset."
:type 'integer :group 'alloy)
(defun alloy-indent-line (&optional whole-exp)
"Indent current line as Lisp code.
With argument, indent any additional lines of the same expression
rigidly along with this one."
(interactive "P")
(let ((indentcol -1))
(save-excursion
(let ((oldpoint (point)) bolast last-indent last-end bol eol
(obrace 0) (cbrace 0) (sameindent 0))
(when (in-comment)
(setq sameindent 1))
(beginning-of-line)
(setq bol (point))
(condition-case nil
(re-search-backward "[^\000-\040]")
(error (setq indentcol 0)))
(when (< indentcol 0)
(if (looking-at alloy-indent-expr)
(setq obrace 1)
(setq obrace 0))
(when (looking-at ",")
(setq sameindent 1))
(when (> (current-column) 0)
(backward-char))
(when (looking-at "=>")
(setq obrace 1))
(when (looking-at same-indent-expr)
(setq sameindent 1))
(beginning-of-line)
(setq bolast (current-column))
(re-search-forward "[^\000-\040]")
(backward-char)
(setq last-indent (- (current-column) bolast))
(goto-char oldpoint)
(end-of-line)
(setq eol (point))
(beginning-of-line)
(re-search-forward "[^\000-\040]" eol t 1)
(if (> (point) bol) (backward-char) nil)
(if (looking-at "}")
(setq cbrace 1)
(setq cbrace 0))
(when (looking-at "{")
(setq sameindent 1))
(if (= sameindent 1)
(setq indentcol last-indent)
(if (= obrace 1)
(if (= cbrace 1)
(setq indentcol last-indent)
(setq indentcol (+ last-indent alloy-basic-offset)))
;; if there isn't an opening brace at the end of the last row,
;; use the nearest enclosing sexp to determine indentation
;; if the enclosing sexp starts with ( or [
(save-excursion
(condition-case nil
(progn
(up-list 1)
(backward-sexp 1)
(if (looking-at "[\\(]")
(setq indentcol (+ 1 (current-column)))
;; if enclosing sexp starts with {, indent three from the line
;; with the {
(progn
(beginning-of-line)
(re-search-forward "[^\000-\040]")
(backward-char)
(if (= cbrace 1)
(setq indentcol (current-column))
(setq indentcol (+ alloy-basic-offset (current-column)))))))
(error (setq indentcol last-indent)))
)))
)
(if (> (current-column) indentcol)
(delete-region bol (point))
())
(indent-to indentcol)
))
(if (< (current-column) indentcol)
(move-to-column indentcol)
nil
)
)
)
(defvar alloy-mode-hook nil
"*List of functions to call when Alloy mode is invoked.
This hook is automatically executed after the `alloy-mode' is
fully loaded.
This is a good place to add Alloy environment specific bindings.")
;;;###autoload
(define-derived-mode alloy-mode prog-mode "Alloy"
:group 'alloy
(make-local-variable 'font-lock-defaults)
(make-local-variable 'comment-start)
(make-local-variable 'comment-end)
(make-local-variable 'comment-start-skip)
(make-local-variable 'comment-column)
(make-local-variable 'comment-indent-function)
(make-local-variable 'indent-line-function)
(setq
font-lock-defaults '(alloy-font-lock-keywords)
comment-start "/* "
comment-end " */"
comment-start-skip "/\\*+ *"
comment-column 40
comment-indent-function 'java-comment-indent
indent-line-function 'alloy-indent-line
indent-tabs-mode t)
(use-local-map alloy-mode-map))
(add-to-list 'auto-mode-alist '("\\.als\\'" . alloy-mode))
(provide 'alloy-mode)