-
Notifications
You must be signed in to change notification settings - Fork 42
/
links-mode.el
215 lines (201 loc) · 5.55 KB
/
links-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
;;; links-mode.el -- A first gasp at an Emacs mode for Links
;;; (Horribly broken, for sure.)
;;; by Ezra Cooper 2007
;;; updated by Stefan Fehrenbach 2015
;;; Many bits copped from caml.el by Leroy et al.
;;;
;;; Things that work:
;;; * some syntax highlighting
;;; * (un)comment-region
;;;
;;; Things that don't work:
;;; * syntax highlighting of XML quasis.
;;;
(require 'compile)
(defgroup links nil
"Links-mode customization.")
(defcustom links-mode-hook nil
"Hook run when entering Links mode."
:type 'hook)
(defcustom links-executable "links"
"Path to Links executable."
:type '(string))
(defcustom links-cli-arguments ""
"Command line arguments (as a string) passed to links."
:type '(string))
(defvar links-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?\ " " st)
(modify-syntax-entry ?# "<" st)
(modify-syntax-entry ?\n ">" st)
(modify-syntax-entry ?\\ "\\" st)
(modify-syntax-entry ?( "()" st)
(modify-syntax-entry ?) ")(" st)
(modify-syntax-entry ?{ "(}" st)
(modify-syntax-entry ?} "){" st)
(modify-syntax-entry ?[ "(]" st)
(modify-syntax-entry ?] ")[" st)
(modify-syntax-entry ?_ "w" st)
(modify-syntax-entry ?' "w" st)
st))
;; Can be generated with `links --print-keywords`.
;; TODO We should do that automatically as part of the build process somehow.
(defconst links-keywords
'(
"alien"
"as"
"by"
"case"
"client"
"database"
"default"
"delete"
"delete_left"
"delete_right"
"determined"
"do"
"else"
"escape"
"effectname"
"false"
"for"
"forall"
"from"
"fun"
"formlet"
"handle"
"if"
"import"
"in"
"on"
"open"
"yields"
"insert"
"lens"
"lensget"
"lensdrop"
"lensjoin"
"lensput"
"lensselect"
"linfun"
"module"
"mu"
"mutual"
"native"
"nu"
"offer"
"orderby"
"op"
"page"
"query"
"readonly"
"receive"
"returning"
"select"
"server"
"set"
"shallowhandle"
"sig"
"spawn"
"spawnAngel"
"spawnClient"
"spawnDemon"
"spawnWait"
"switch"
"table"
"tablekeys"
"TableHandle"
"true"
"typename"
"update"
"unsafe"
"values"
"var"
"where"
"with"
))
(defconst links-font-lock-keywords
(list
;; comments
'("\\(^\\|[^</]\\)#.*$" . font-lock-comment-face)
;; XML forests
'("<#>.*</#>" . font-lock-function-name-face)
;; XML tags
'("</?[a-z][^>]*>" 0 font-lock-function-name-face t)
;; XML escapes (attributes)
'("\"{[^}]*}\"" 0 'default t)
;; special operations
`(,(regexp-opt links-keywords 'words) . font-lock-keyword-face)
;; types & variant tags
'("\\<[A-Z][A-Za-z0-9_']*\\>" . font-lock-type-face)
;; variable names
'("\\<\\(var\\) +\\([a-z][A-Za-z0-9_']*\\)\\>"
(1 font-lock-keyword-face)
(2 font-lock-variable-name-face))
;; function names
'("\\<\\(fun\\|sig\\) +\\([a-z][A-Za-z0-9_']*\\)\\>"
(1 font-lock-keyword-face)
(2 font-lock-function-name-face))
;; type operators
'("\\(-\\|~\\)\\(>\\|@\\)" . font-lock-function-name-face)
'("\\(=>\\)" . font-lock-function-name-face)
'("\\(-\\|~\\)\\([a-z]+\\)\\(-\\|~\\)\\(>\\|@\\)"
(1 font-lock-function-name-face)
(2 font-lock-variable-name-face)
(3 font-lock-function-name-face)
(4 font-lock-function-name-face))
))
(defun initialize-font-lock-defaults ()
(setq-local font-lock-defaults
'(links-font-lock-keywords)))
(defun links-compilation-errors ()
;; TODO: fix our compilation-error-regexp-alist use
;;
;; For some reason adding to c-e-r-a and c-e-r-a-a does not work. Only adding
;; to c-e-r-a also does not work. I tried these and a cople more things. If
;; you understand how this works, please tell me/fix it.
;;
;; (add-to-list 'compilation-error-regexp-alist 'links-parse)
;; (add-to-list 'compilation-error-regexp-alist 'links-type)
;; (add-to-list 'compilation-error-regexp-alist
;; '(links-parse "^*** Parse error: \\(.*\\):\\([0-9]+\\)$" 1 2))
;; (add-to-list 'compilation-error-regexp-alist-alist
;; '(links-type "^\\(.*\\):\\([0-9]+\\): Type error:" 1 2))
;;
;;
;; For now, if you don't care about breaking error parsing for all other
;; compilers, you can just uncomment the next lines. They replace the whole
;; c-e-r-a without even using c-e-r-a-a. This, of course, breaks all other
;; users. You have been warned.
;;
;; (setq compilation-error-regexp-alist
;; '(("^*** Parse error: \\(.*\\):\\([0-9]+\\)$" 1 2)
;; ("^\\(.*\\):\\([0-9]+\\): Type error:" 1 2)))
)
(defun links-compile-and-run-file ()
"Compile the current file in Links. This may execute sideeffecting code, so be careful."
(interactive)
(let ((command (combine-and-quote-strings
`(,links-executable
,links-cli-arguments
,buffer-file-name))))
(compile command)))
(defvar links-mode-map
(let ((m (make-keymap)))
(define-key m (kbd "C-c C-k") 'links-compile-and-run-file)
m))
;;;###autoload
(define-derived-mode links-mode prog-mode "Links"
"Major mode for Links."
:syntax-table links-mode-syntax-table
(setq-local mode-name "Links")
(setq-local comment-start "#")
(setq-local comment-start-skip "#+\\s-*")
(setq-local require-final-newline t)
(initialize-font-lock-defaults)
(links-compilation-errors)
(run-hooks 'links-mode-hook)
)
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.links\\'" . links-mode))
(provide 'links-mode)