-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplumbing-mode.el
245 lines (197 loc) · 7.46 KB
/
plumbing-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
;;; plumbing-mode.el --- font lock for Plan 9's plumber configuration files
;; Copyright © 2017, by diamond-lizard
;; Author: diamond-lizard
;; Version: 0.0.1
;; Keywords: color, convenience
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 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 Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Syntax highlighting for Plan 9's plumber configuration files
;; https://9fans.github.io/plan9port/man/man7/plumb.html
;;; Code:
(eval-when-compile
(require 'rx))
;;; Customization
(defgroup plumbing nil
"Plumbing mastering in Emacs"
:prefix "plumbing-"
:group 'languages
:link '(url-link :tag "Github" "https://github.com/diamond-lizard/plumbing-mode")
:link '(emacs-commentary-link :tag "Commentary" "plumbing-mode"))
;;; Font locking
(defvar plumbing-mode-syntax-table
(let ((table (make-syntax-table)))
table))
; "Syntax table in use in `plumbing-mode' buffers.")
(defface plumbing-string-delimiter
'((t :inherit font-lock-negation-char-face))
"Face for string delimiters in plumbing-mode."
:group 'plumbing)
(defface plumbing-string
'((t :inherit font-lock-string-face))
"Face for strings in plumbing-mode."
:group 'plumbing)
(defface plumbing-equals-sign
'((t :inherit font-lock-builtin-face))
"Face for equals-sign in plumbing-mode."
:group 'plumbing)
(defface plumbing-include
'((t :inherit font-lock-keyword-face))
"Face for include keyword in plumbing-mode."
:group 'plumbing)
(defface plumbing-message-type
'((t :inherit font-lock-keyword-face))
"Face for message-type keywords in plumbing-mode."
:group 'plumbing)
(defface plumbing-variable-name
'((t :inherit font-lock-variable-name-face))
"Face for variable names in plumbing-mode."
:group 'plumbing)
(defface plumbing-verb
'((t :inherit font-lock-preprocessor-face))
"Face for verb keywords in plumbing-mode."
:group 'plumbing)
(defface plumbing-object
'((t :inherit font-lock-constant-face))
"Face for object keywords in plumbing-mode."
:group 'plumbing)
(defface plumbing-action-verb
'((t :inherit font-lock-type-face))
"Face for action verb keywords in plumbing-mode."
:group 'plumbing)
(defface plumbing-text
'((t :inherit font-lock-function-name-face))
"Face for text literal in plumbing-mode."
:group 'plumbing)
(defface plumbing-variable-assignment
'((t :inherit font-lock-variable-name-face))
"Face for variable assignment in plumbing-mode."
:group 'plumbing)
;;; Specialized rx
(eval-when-compile
(defun plumbing-rx-symbol (form)
"Translate FORM into a regular expression."
(let ((body (cdr form)))
(rx-to-string `(and symbol-start ,@body symbol-end) 'no-group)))
(defconst plumbing-rx-constituents
`((symbol plumbing-rx-symbol 0 nil)
(string-delimiter . ,(rx "'"))
(string . ,(rx "'"
(group (one-or-more (not (any "'\n"))))
"'"))
(equals-sign . ,(rx "="))
(include . ,(rx "include"))
(message-type . ,(rx (or "arg"
"attr"
"data"
"dst"
"ndata"
"src"
"type"
"wdir")))
(verb . ,(rx (or "add"
"delete"
"is"
"isdir"
"isfile"
"matches"
"set")))
(object . ,(rx (or "plumb")))
(action-verb . ,(rx (or "client"
"start"
"to")))
(text . ,(rx bol "type is " (group "text") eol))
"Additional special sexps for `plumbing-rx'"))
(defmacro plumbing-rx (&rest sexps)
"Specialized `rx' variant for plumbing-mode.
In addition to the standard forms of `rx', the following forms
are available:
`(symbol SEXP …)'
Match SEXPs inside symbol boundaries only
`string'
A string, excluding the string delimiters
`string-delimiter'
The delimiters delimiting a string
`include'
The include keyword
`message-type'
Any valid plumbing message type
`verb'
Any built-in plumbing verb
`object'
Any built-in plumbing object
`action-verb'
Any built-in plumbing action verb
`variable-name'
Any variable name, without a leading dollar sign"
(let ((rx-constituents (append plumbing-rx-constituents rx-constituents)))
(cond ((null sexps)
(error "No regexp"))
((cdr sexps)
(rx-to-string `(and ,@sexps) t))
(t
(rx-to-string (car sexps) t))))))
(defvar plumbing-font-lock-keywords
`(
;; comments
(,"^#.*" 0 font-lock-comment-face)
;; strings
(,(plumbing-rx string) 1 'plumbing-string)
;; string delimiters
(,(plumbing-rx string-delimiter) 0 'plumbing-string-delimiter)
;; equals sign
(,(plumbing-rx equals-sign) 0 'plumbing-equals-sign)
;; include
(,(plumbing-rx (symbol include)) 0 'plumbing-include)
;; message types
(,(plumbing-rx (symbol message-type)) 0 'plumbing-message-type)
;; Variables
(,"\$[A-Za-z0-9_]+" 0 'plumbing-variable-name)
;; Verbs
(,(plumbing-rx (symbol verb)) 0 'plumbing-verb)
;; Objects
(,(plumbing-rx (symbol object)) 0 'plumbing-object)
;; Action verbs
(,(plumbing-rx (symbol action-verb)) 0 'plumbing-action-verb)
;; Text
(,(plumbing-rx text) 1 'plumbing-text)
;; Variable assignment
("\\([^[:blank:]=\n]+\\)[[:blank:]]*?=" 1 'plumbing-variable-assignment))
"Font lock keywords for plumbing-mode.")
;;;###autoload
(define-derived-mode plumbing-mode prog-mode "Plumbing" ()
"Major mode for syntax highlighting of Plan 9's plumber configuration files.
\\{plumbing-mode-map}"
;; Font locking
(setq font-lock-defaults '((plumbing-font-lock-keywords) t nil))
;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^
;; / \ \
;; name of variable which holds | \__ case-insensitive
;; our mode's keywords | keywords?
;; |
;; keyword-only font lock?
;; (disable syntactic fontification)
;;
;; Note: Setting the keyword-only argument to t in the font-lock-defaults above
;; is necessary because if it's set to nil, the font-lock defaults will
;; screw up comments with double-quoted strings in them and will also
;; font-lock double-quoted strings, which is not what we want.
)
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.plumbing\\'" . plumbing-mode))
(provide 'plumbing-mode)
;; Local Variables:
;; coding: utf-8
;; End:
;;; plumbing-mode.el ends here