-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclick-mode.el
307 lines (265 loc) · 9.03 KB
/
click-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
;;; click-mode.el --- Major mode for the Click Modular Router Project
;; Copyright (c) 2016 Brian Malehorn. All rights reserved.
;; Use of this source code is governed by a MIT-style
;; license that can be found in the LICENSE.txt file.
;; Author: Brian Malehorn <bmalehorn@gmail.com>
;; Version: 0.0.5
;; Package-Requires: ((emacs "24"))
;; Keywords: click router
;; URL: https://github.com/bmalehorn/click-mode
;; This file is not part of Emacs.
;;; Commentary:
;; `click-mode' is an Emacs major mode for editting Click Modular Router
;; configuration files. It provides basic syntax highlighting and
;; indentation, making click files nearly comprehensible!
;; Install
;;
;; `click-mode' is available on melpa. You can install it with:
;;
;; M-x package-install RET click-mode RET
;;
;; Then simply open a `.click` file and enjoy!
;;
;; You may also want to add this to your `.emacs`:
;;
;; (add-to-list 'auto-mode-alist '("\\.template\\'" . click-mode))
;; (add-to-list 'auto-mode-alist '("\\.inc\\'" . click-mode))
;;; Code:
;; pilfered from https://www.emacswiki.org/emacs/wpdl-mode.el
(defvar click-mode-syntax-table
(let ((click-mode-syntax-table (make-syntax-table)))
;; Comment styles are same as C++
(modify-syntax-entry ?/ ". 124b" click-mode-syntax-table)
(modify-syntax-entry ?* ". 23" click-mode-syntax-table)
(modify-syntax-entry ?\n "> b" click-mode-syntax-table)
click-mode-syntax-table)
"Syntax table for `click-mode'.")
(defvar click-arguments
(concat
"append\\|" "end\\|" "error\\|" "errorq\\|" "exit\\|" "export\\|"
"exportq\\|" "goto\\|" "init\\|" "initq\\|" "label\\|" "loop\\|"
"pause\\|" "print\\|" "printn\\|" "printnq\\|" "printq\\|" "read\\|"
"readq\\|" "return\\|" "returnq\\|" "save\\|" "set\\|" "setq\\|"
"stop\\|" "wait\\|" "wait_for\\|" "wait_step\\|" "wait_stop\\|"
"wait_time\\|" "write\\|" "writeq\\|" "[A-Z][A-Z_0-9]*")
"Argument names for `click-mode', e.g.
Paint(ANNO FOO, COLOR BAR)
---- -----
We also include
Script(write foo.x 1, write foo.y 2)
...Script arguments, since those are typically non-capitalized."
)
(defun click-indent-line ()
"\"Correct\" the indentation for the current line."
(save-excursion
(back-to-indentation)
(or (when (looking-at "#\\|elementclass\\|}") (indent-line-to 0) t)
(click-indent-copycat "\\[")
(click-indent-copycat "->")
(click-indent-copycat "=>")
(click-indent-paren)
(click-indent-after-semicolon)
(indent-line-to (save-excursion
(click-previous-interesting-line)
(current-indentation)))))
;; ))
(when (< (current-column) (current-indentation))
(back-to-indentation)))
(defun click-indent-copycat (regexp)
"Indent the same as the previous line.
e.g. (click-indent-copycat 1 2 [) will indent this:
1: => ( [0] -> foo;
2: [1] -> bar; )
to this:
1: => ( [0] -> foo;
2: [1] -> bar; )
If the line with different indentation does not contain REGEXP,
returns nil. Otherwise, returns the new indentation.
"
(let* ((indent
(save-excursion
(back-to-indentation)
(when (and (not (bobp))
(looking-at regexp)
(progn
(click-previous-interesting-line)
(back-to-indentation)
(looking-at (concat ".*" regexp))))
(while (not (looking-at regexp))
(forward-char))
(current-column)))))
(when indent
(indent-line-to indent)
indent)))
(defun click-previous-interesting-line (&optional neglect-parens)
"Moves the point back until reaching a line, skipping blank lines and
comment lines."
(forward-line -1)
(while (and (click-boring-line) (not (bobp)))
(forward-line -1))
(end-of-line)
(forward-char -1)
(while (looking-at "[; ]")
(forward-char -1))
(when (and (not neglect-parens) (looking-at "[})]\\|]"))
(end-of-line)
(backward-list)))
(defun click-boring-line ()
(save-excursion
(back-to-indentation)
(or
(looking-at "#")
(looking-at "$")
(looking-at "//"))))
(defun click-indent-paren ()
"
Idents:
foo (
bar
to:
foo (
bar
"
(let* ((indent
(save-excursion
(let* ((this-paren-count (click-paren-count))
(previous-paren-count
(progn
(click-previous-interesting-line)
(click-paren-count)))
(previous-indent (current-indentation)))
(if (< this-paren-count 0)
;; bar)
;; indent to the same indentation as the matching )
(max 0
(+ previous-indent
(* previous-paren-count click-basic-offset)))
nil
(when (< 0 previous-paren-count)
;; foo {
;; bar
;; indent +1 level
(+ previous-indent click-basic-offset)))))))
(when indent
(indent-line-to indent)
indent)))
(defun click-paren-count ()
"\"({}[\" -> 2
\"}][]) -> 3
"
(save-excursion
(let* ((c 0))
(back-to-indentation)
(while (not (eolp))
(when (looking-at "[{([]")
(setq c (1+ c)))
(when (looking-at "[})]\\|]")
(setq c (1- c)))
(forward-char))
c)))
(defun click-beginning-of-statement-p ()
"Did the previous line end with a semicolon?"
(save-excursion
(click-previous-interesting-line)
(beginning-of-line)
(looking-at ".*;$")))
(defun click-in-element-class-p ()
"If I search backwards, do I find \"elementclass\" before I find a
0-indentation line?"
(save-excursion
(let* ((i 0)
(ret nil))
(while (and i (< i 100))
(click-previous-interesting-line t)
(beginning-of-line)
(if (looking-at "^elementclass")
(setq i nil ret t)
(when (eq (current-indentation) 0)
(setq i nil ret nil))))
ret)))
(defun click-indent-after-semicolon ()
"If the previous line ended in a semicolon, we're at the start of a
statement. Indent this to `click-basic-offset' if you're in an
elementclass, otherwise indent to 0."
(when (click-beginning-of-statement-p)
(if (click-in-element-class-p)
(progn
(indent-line-to click-basic-offset)
click-basic-offset)
(indent-line-to 0)
0)))
(defvar click-basic-offset 4
"How many spaces to \"correct\" indentation to.
Analogous to `c-basic-offset'.")
(defvar click-highlights
`(
;; #define FOO 5
("#\\s-*[a-z]*" . font-lock-preprocessor-face)
;; %file foo.click
("^%[a-z]+" . font-lock-keyword-face)
;; elementclass
(,(concat
"\\(^\\|[^a-zA-Z_0-9.]\\)\\("
"elementclass\\|output\\|input\\|require"
"\\)\\([^a-zA-Z_0-9.]\\|$\\)")
. (2 font-lock-keyword-face))
;; Foo(
("\\([a-zA-Z_][a-zA-Z_/0-9]*\\)("
. (1 font-lock-function-name-face))
;; :: Foo
("::\\s-*\\([a-zA-Z_][a-zA-Z_/0-9]*\\)"
. (1 font-lock-function-name-face))
;; -> Foo
("->\\s-*\\([A-Z][a-zA-Z_/0-9]*\\)"
. (1 font-lock-function-name-face))
;; Foo ->
("\\([A-Z][a-zA-Z_/0-9]*\\)\\s-*->"
. (1 font-lock-function-name-face))
;; foo
("^\\s-*\\([a-z_][a-zA-Z_/0-9]*\\)\\s-*$"
. font-lock-variable-name-face)
;; foo ::
("\\([a-zA-Z_][a-zA-Z_/0-9]*\\) *\\(, *[a-zA-Z_][a-zA-Z_/0-9]*\\)* *::"
. (1 font-lock-variable-name-face))
;; [0,1,2] foo
("\\(\\[[0-9, ]*\\]\\) *\\([a-z_][a-zA-Z_/0-9]*\\)"
. (2 font-lock-variable-name-face))
;; foo [0,1,2]
("\\([a-z_][a-zA-Z_/0-9]*\\) *\\(\\[[0-9, ]*\\]\\)"
. (1 font-lock-variable-name-face))
;; -> foo
("-> *\\([a-z_][a-zA-Z_/0-9]*\\)"
. (1 font-lock-variable-name-face))
;; foo ->
("\\([a-z_][a-zA-Z_/0-9]*\\) *->"
. (1 font-lock-variable-name-face))
;; elementclass Foo {
("elementclass *\\([a-zA-Z_][a-zA-Z_/0-9]*\\) "
. (1 font-lock-type-face))
;; Foo(BAR bar)
(,(concat "(\\(" click-arguments "\\) ")
. (1 font-lock-constant-face))
;; Foo(BAR bar, ACK ack)
(,(concat ", *\\(" click-arguments "\\) ")
. (1 font-lock-constant-face))
;; ACTIVE false,
(,(concat
"^\\s-*\\(" click-arguments "\\) .*\\(,\\|);?\\) *\\(//.*\\)?$")
. (1 font-lock-constant-face))
)
"Syntax highlighting for `click-mode'."
)
;;;###autoload
(define-derived-mode click-mode prog-mode "Click"
(setq comment-start "// ")
(setq comment-start-skip "//+\\s-*")
(set (make-local-variable 'indent-line-function) 'click-indent-line)
(setq font-lock-defaults '(click-highlights)))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.click\\'" . click-mode))
(add-to-list 'auto-mode-alist '("\\.testie\\'" . click-mode))
;; you may also want:
;; (add-to-list 'auto-mode-alist '("\\.template\\'" . click-mode))
;; (add-to-list 'auto-mode-alist '("\\.inc\\'" . click-mode))
(provide 'click-mode)
;;; click-mode.el ends here