This repository has been archived by the owner on May 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
puppet-mode.el
378 lines (342 loc) · 13.6 KB
/
puppet-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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
;;; puppet-mode.el --- A simple mode for editing puppet manifests
;; Copyright (C) 2011 Puppet Labs Inc
;; Author: Russ Allbery <rra@stanford.edu>
;; Maintainer: <info@puppetlabs.com>
;; This file is not part of GNU Emacs.
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;; A simple mode for editing puppet manifests.
;;; Code:
(defconst puppet-mode-version "0.2")
(defvar puppet-mode-abbrev-table nil
"Abbrev table in use in puppet-mode buffers.")
(define-abbrev-table 'puppet-mode-abbrev-table ())
(defcustom puppet-indent-level 2
"*Indentation of Puppet statements."
:type 'integer :group 'puppet)
(defcustom puppet-include-indent 2
"*Indentation of continued Puppet include statements."
:type 'integer :group 'puppet)
(defvar puppet-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-j" 'newline-and-indent)
(define-key map "\C-m" 'newline-and-indent)
map)
"Key map used in puppet-mode buffers.")
(defvar puppet-mode-syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?\' "\"'" table)
(modify-syntax-entry ?\" "\"\"" table)
(modify-syntax-entry ?# "<" table)
(modify-syntax-entry ?\n ">#" 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 in use in puppet-mode buffers.")
(defcustom puppet-indent-tabs-mode nil
"*Indentation can insert tabs in puppet mode if this is non-nil."
:type 'boolean :group 'puppet)
(defcustom puppet-comment-column 32
"*Indentation column of comments."
:type 'integer :group 'puppet)
(defun puppet-count-matches (re start end)
"The same as Emacs 22 count-matches, for portability to other versions
of Emacs."
(save-excursion
(let ((n 0))
(goto-char start)
(while (re-search-forward re end t) (setq n (1+ n)))
n)))
(defun puppet-comment-line-p ()
"Return non-nil iff this line is a comment."
(save-excursion
(save-match-data
(beginning-of-line)
(looking-at (format "\\s-*%s" comment-start)))))
(defun puppet-block-indent ()
"If point is in a block, return the indentation of the first line of that
block (the line containing the opening brace). Used to set the indentation
of the closing brace of a block."
(save-excursion
(save-match-data
(let ((opoint (point))
(apoint (search-backward "{" nil t)))
(when apoint
;; This is a bit of a hack and doesn't allow for strings. We really
;; want to parse by sexps at some point.
(let ((close-braces (puppet-count-matches "}" apoint opoint))
(open-braces 0))
(while (and apoint (> close-braces open-braces))
(setq apoint (search-backward "{" nil t))
(when apoint
(setq close-braces (puppet-count-matches "}" apoint opoint))
(setq open-braces (1+ open-braces)))))
(if apoint
(current-indentation)
nil))))))
(defun puppet-in-array ()
"If point is in an array, return the position of the opening '[' of
that array, else return nil."
(save-excursion
(save-match-data
(let ((opoint (point))
(apoint (search-backward "[" nil t)))
(when apoint
;; This is a bit of a hack and doesn't allow for strings. We really
;; want to parse by sexps at some point.
(let ((close-brackets (puppet-count-matches "]" apoint opoint))
(open-brackets 0))
(while (and apoint (> close-brackets open-brackets))
(setq apoint (search-backward "[" nil t))
(when apoint
(setq close-brackets (puppet-count-matches "]" apoint opoint))
(setq open-brackets (1+ open-brackets)))))
apoint)))))
(defun puppet-in-include ()
"If point is in a continued list of include statements, return the position
of the initial include plus puppet-include-indent."
(save-excursion
(save-match-data
(let ((include-column nil)
(not-found t))
(while not-found
(forward-line -1)
(cond
((bobp)
(setq not-found nil))
((looking-at "^\\s-*include\\s-+.*,\\s-*$")
(setq include-column
(+ (current-indentation) puppet-include-indent))
(setq not-found nil))
((not (looking-at ".*,\\s-*$"))
(setq not-found nil))))
include-column))))
(defun puppet-indent-line ()
"Indent current line as puppet code."
(interactive)
(beginning-of-line)
(if (bobp)
(indent-line-to 0) ; First line is always non-indented
(let ((not-indented t)
(array-start (puppet-in-array))
(include-start (puppet-in-include))
(block-indent (puppet-block-indent))
cur-indent)
(cond
(array-start
;; This line probably starts with an element from an array.
;; Indent the line to the same indentation as the first
;; element in that array. That is, this...
;;
;; exec {
;; "add_puppetmaster_mongrel_startup_links":
;; command => "string1",
;; creates => [ "string2", "string3",
;; "string4", "string5",
;; "string6", "string7",
;; "string3" ],
;; refreshonly => true,
;; }
;;
;; ...should instead look like this:
;;
;; exec {
;; "add_puppetmaster_mongrel_startup_links":
;; command => "string1",
;; creates => [ "string2", "string3",
;; "string4", "string5",
;; "string6", "string7",
;; "string8" ],
;; refreshonly => true,
;; }
(save-excursion
(goto-char array-start)
(forward-char 1)
(re-search-forward "\\S-")
(forward-char -1)
(setq cur-indent (current-column))))
(include-start
(setq cur-indent include-start))
((and (looking-at "^\\s-*},?\\s-*$") block-indent)
;; This line contains a closing brace or a closing brace followed by a
;; comma and we're at the inner block, so we should indent it matching
;; the indentation of the opening brace of the block.
(setq cur-indent block-indent))
(t
;; Otherwise, we did not start on a block-ending-only line.
(save-excursion
;; Iterate backwards until we find an indentation hint
(while not-indented
(forward-line -1)
(cond
;; Comment lines are ignored unless we're at the start of the
;; buffer.
((puppet-comment-line-p)
(if (bobp)
(setq not-indented nil)))
;; Brace or paren on a line by itself will already be indented to
;; the right level, so we can cheat and stop there.
((looking-at "^\\s-*[\)}]\\s-*")
(setq cur-indent (current-indentation))
(setq not-indented nil))
;; Brace (possibly followed by a comma) or paren not on a line by
;; itself will be indented one level too much, but don't catch
;; cases where the block is started and closed on the same line.
((looking-at "^[^\n\({]*[\)}],?\\s-*$")
(setq cur-indent (- (current-indentation) puppet-indent-level))
(setq not-indented nil))
;; Indent by one level more than the start of our block. We lose
;; if there is more than one block opened and closed on the same
;; line but it's still unbalanced; hopefully people don't do that.
((looking-at "^.*{[^\n}]*$")
(setq cur-indent (+ (current-indentation) puppet-indent-level))
(setq not-indented nil))
;; Indent by one level if the line ends with an open paren.
((looking-at "^.*\(\\s-*$")
(setq cur-indent (+ (current-indentation) puppet-indent-level))
(setq not-indented nil))
;; Semicolon ends a block for a resource when multiple resources
;; are defined in the same block, but try not to get the case of
;; a complete resource on a single line wrong.
((looking-at "^\\([^'\":\n]\\|\"[^\n\"]*\"\\|'[^\n']*'\\)*;\\s-*$")
(setq cur-indent (- (current-indentation) puppet-indent-level))
(setq not-indented nil))
;; Indent an extra level after : since it introduces a resource.
((looking-at "^.*:\\s-*$")
(setq cur-indent (+ (current-indentation) puppet-indent-level))
(setq not-indented nil))
;; Start of buffer.
((bobp)
(setq not-indented nil)))))
;; If this line contains only a closing paren, we should lose one
;; level of indentation.
(if (looking-at "^\\s-*\)\\s-*$")
(setq cur-indent (- cur-indent puppet-indent-level)))))
;; We've figured out the indentation, so do it.
(if (and cur-indent (> cur-indent 0))
(indent-line-to cur-indent)
(indent-line-to 0)))))
(defvar puppet-font-lock-syntax-table
(let* ((tbl (copy-syntax-table puppet-mode-syntax-table)))
(modify-syntax-entry ?_ "w" tbl)
tbl))
(defvar puppet-font-lock-keywords
(list
;; defines, classes, and nodes
'("^\\s *\\(class\\|define\\|node\\)\\s +\\([^( \t\n]+\\)"
2 font-lock-function-name-face)
;; inheritence
'("\\s +inherits\\s +\\([^( \t\n]+\\)"
1 font-lock-function-name-face)
;; include
'("\\(^\\|\\s +\\)include\\s +\\(\\([a-zA-Z0-9:_-]+\\(,[ \t\n]*\\)?\\)+\\)"
2 font-lock-reference-face)
;; keywords
(cons (concat
"\\b\\(\\("
(mapconcat
'identity
'("alert"
"case"
"class"
"crit"
"debug"
"default"
"define"
"defined"
"else"
"emerg"
"err"
"fail"
"false"
"file"
"filebucket"
"generate"
"if"
"import"
"include"
"info"
"inherits"
"node"
"notice"
"realize"
"search"
"tag"
"tagged"
"template"
"true"
"warning"
)
"\\|")
"\\)\\>\\)")
1)
;; variables
'("\\(^\\|[^_:.@$]\\)\\b\\(true\\|false\\)\\>"
2 font-lock-variable-name-face)
'("\\$[a-zA-Z0-9_:]+"
0 font-lock-variable-name-face)
;; usage of types
'("^\\s *\\([a-z][a-zA-Z0-9_:-]*\\)\\s +{"
1 font-lock-type-face)
;; overrides and type references
'("\\s +\\([A-Z][a-zA-Z0-9_:-]*\\)\\["
1 font-lock-type-face)
;; general delimited string
'("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)"
(2 font-lock-string-face)))
"*Additional expressions to highlight in puppet mode.")
;;;###autoload
(defun puppet-mode ()
"Major mode for editing puppet manifests.
The variable puppet-indent-level controls the amount of indentation.
\\{puppet-mode-map}"
(interactive)
(kill-all-local-variables)
(use-local-map puppet-mode-map)
(setq mode-name "Puppet")
(setq major-mode 'puppet-mode)
(set-syntax-table puppet-mode-syntax-table)
(set (make-local-variable 'local-abbrev-table) puppet-mode-abbrev-table)
(set (make-local-variable 'comment-start) "# ")
(set (make-local-variable 'comment-start-skip) "#+ *")
(set (make-local-variable 'comment-use-syntax) t)
(set (make-local-variable 'comment-end) "")
(set (make-local-variable 'comment-auto-fill-only-comments) t)
(set (make-local-variable 'comment-column) puppet-comment-column)
(set (make-local-variable 'indent-line-function) 'puppet-indent-line)
(set (make-local-variable 'indent-tabs-mode) puppet-indent-tabs-mode)
(set (make-local-variable 'require-final-newline) t)
(set (make-local-variable 'paragraph-ignore-fill-prefix) t)
(set (make-local-variable 'paragraph-start) "\f\\|[ ]*$\\|#$")
(set (make-local-variable 'paragraph-separate) "\\([ \f]*\\|#\\)$")
(or (boundp 'font-lock-variable-name-face)
(setq font-lock-variable-name-face font-lock-type-face))
(set (make-local-variable 'font-lock-keywords) puppet-font-lock-keywords)
(set (make-local-variable 'font-lock-multiline) t)
(set (make-local-variable 'font-lock-defaults)
'((puppet-font-lock-keywords) nil nil))
(set (make-local-variable 'font-lock-syntax-table)
puppet-font-lock-syntax-table)
(run-hooks 'puppet-mode-hook))
(provide 'puppet-mode)