-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
puppet-mode.el
1293 lines (1127 loc) · 51.3 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; puppet-mode.el --- Major mode for Puppet manifests -*- lexical-binding: t; -*-
;; Copyright (C) 2013-2014, 2016 Sebastian Wiesner <swiesner@lunaryorn.com>
;; Copyright (C) 2013, 2014 Bozhidar Batsov <bozhidar@batsov.com>
;; Copyright (C) 2011 Puppet Labs Inc
;; Author: Vox Pupuli <voxpupuli@groups.io>
;; Bozhidar Batsov <bozhidar@batsov.com>
;; Sebastian Wiesner <swiesner@lunaryorn.com>
;; Russ Allbery <rra@stanford.edu>
;; Maintainer: Vox Pupuli <voxpupuli@groups.io>
;; Bozhidar Batsov <bozhidar@batsov.com>
;; Sebastian Wiesner <swiesner@lunaryorn.com>
;; Tim Meusel <tim@bastelfreak.de>
;; URL: https://github.com/voxpupuli/puppet-mode
;; Keywords: languages
;; Version: 0.4
;; Package-Requires: ((emacs "24.1") (pkg-info "0.4"))
;; 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 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 General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; This file incorporates work covered by the following copyright and
;; permission notice:
;; 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:
;; GNU Emacs 24 major mode for editing Puppet manifests.
;; Provides syntax highlighting, indentation, alignment, movement, Imenu and
;; code checking.
;; Syntax highlighting: Fontification supports all of Puppet 3 syntax, including
;; variable expansion in strings.
;; Indentation: Indent expressions automatically.
;; Alignment: Provide alignment rules for common Puppet expressions, and align
;; the current block with `puppet-align-block' on C-c C-a.
;; Movement: Move to the beginning or end of the current block with
;; `beginning-of-defun' (C-M-a) and `end-of-defun' (C-M-e) respectively.
;; Imenu: Jump to a tag in the current buffer with `imenu' on C-c C-j. Index
;; variables, resource defaults, classes, nodes, defined types and resource
;; declarations.
;; Code checking: Validate the syntax of the current buffer with
;; `puppet-validate' on C-c C-v. Lint the current buffer for semantic errors
;; with `puppet-lint' on C-c C-l. Apply the current buffer with `puppet-apply'
;; on C-c C-c.
;; Syntax checking: Flymake support is _not_ provided. See Flycheck at
;; http://www.flycheck.org for on-the-fly validation and liniting of Puppet
;; manifests.
;;; Code:
;;; Compatibility
(eval-and-compile
;; `defvar-local' for Emacs 24.2 and below
(unless (fboundp 'defvar-local)
(defmacro defvar-local (var val &optional docstring)
"Define VAR as a buffer-local variable with default value VAL.
Like `defvar' but additionally marks the variable as being automatically
buffer-local wherever it is set."
(declare (debug defvar) (doc-string 3))
`(progn
(defvar ,var ,val ,docstring)
(make-variable-buffer-local ',var))))
;; `setq-local' for Emacs 24.2 and below
(unless (fboundp 'setq-local)
(defmacro setq-local (var val)
"Set variable VAR to value VAL in current buffer."
`(set (make-local-variable ',var) ,val))))
;;; Requirements
(declare-function pkg-info-version-info "pkg-info" (library))
(eval-when-compile
(require 'rx))
(require 'align)
(require 'ansi-color)
(require 'comint)
;;; Customization
(defgroup puppet nil
"Puppet mastering in Emacs"
:prefix "puppet-"
:group 'languages
:link '(url-link :tag "Github" "https://github.com/voxpupuli/puppet-mode")
:link '(emacs-commentary-link :tag "Commentary" "puppet-mode"))
(defcustom puppet-indent-level 2
"Indentation of Puppet statements."
:type 'integer
:group 'puppet
:safe 'integerp)
(defcustom puppet-include-indent 2
"Indentation of continued Puppet include statements."
:type 'integer
:group 'puppet
:safe 'integerp)
(defcustom puppet-indent-tabs-mode nil
"Indentation can insert tabs in puppet mode if this is non-nil."
:type 'boolean
:group 'puppet
:safe 'booleanp)
(defcustom puppet-comment-column 32
"Indentation column of comments."
:type 'integer
:group 'puppet
:safe 'integerp)
(defcustom puppet-fontify-variables-in-comments nil
"When non-nil, fontify variable references in comments."
:type 'boolean
:group 'puppet
:safe 'booleanp
:package-version '(puppet-mode . "0.4"))
(defcustom puppet-validate-command "puppet parser validate --color=false"
"Command to validate the syntax of a Puppet manifest."
:type 'string
:group 'puppet)
(defcustom puppet-lint-command
(concat
"puppet-lint --with-context "
"--log-format \"%{path}:%{line}: %{kind}: %{message} (%{check})\"")
"Command to lint a Puppet manifest."
:type 'string
:group 'puppet
:package-version '(puppet-mode . "0.4"))
(defcustom puppet-apply-command "puppet apply --verbose --noop"
"Command to apply a Puppet manifest."
:type 'string
:group 'puppet
:package-version '(puppet-mode . "0.4"))
(defcustom puppet-repl-command "puppet debugger"
"The Puppet REPL command used to interact with code."
:type 'string
:group 'puppet
:package-version '(puppet-mode . "0.4"))
(defcustom puppet-repl-args '()
"The arguments to pass to `puppet-repl-command' to start a REPL."
:group 'puppet
:type '(repeat string)
:package-version '(puppet-mode . "0.4"))
(defcustom puppet-repl-buffer "*Puppet-REPL*"
"The name of the Puppet REPL buffer."
:group 'puppet
:type 'string
:package-version '(puppet-mode . "0.4"))
(defface puppet-regular-expression-literal
'((t :inherit font-lock-constant-face))
"Face for regular expression literals in Puppet."
:group 'puppet
:package-version '(puppet-mode . "0.4"))
(defface puppet-escape-sequence
'((t :inherit font-lock-constant-face))
"Face for escape sequences in double-quoted strings-consed literals in Puppet."
:group 'puppet
:package-version '(puppet-mode . "0.4"))
;;; Version information
(defun puppet-version (&optional show-version)
"Get the Puppet Mode version as string.
If called interactively or if SHOW-VERSION is non-nil, show the
version in the echo area and the messages buffer.
The returned string includes both, the version from package.el
and the library version, if both a present and different.
If the version number could not be determined, signal an error,
if called interactively, or if SHOW-VERSION is non-nil, otherwise
just return nil."
(interactive (list t))
(let ((version (pkg-info-version-info 'puppet-mode)))
(when show-version
(message "Puppet Mode version: %s" version))
version))
;;; Utilities
(defun puppet-syntax-context (&optional pos)
"Determine the syntax context at POS, defaulting to point.
Return nil, if there is no special context at POS, or one of
`comment'
POS is inside a comment
`single-quoted'
POS is inside a single-quoted string
`double-quoted'
POS is inside a double-quoted string"
(let ((state (save-excursion (syntax-ppss pos))))
(if (nth 4 state)
'comment
(pcase (nth 3 state)
(`?\' 'single-quoted)
(`?\" 'double-quoted)))))
(defun puppet-in-string-or-comment-p (&optional pos)
"Determine whether POS is inside a string or comment."
(not (null (puppet-syntax-context pos))))
(defun puppet-get-repl-proc ()
(unless (comint-check-proc puppet-repl-buffer)
(puppet-repl))
(get-buffer-process puppet-repl-buffer))
(defun puppet-repl-send-region (start end)
"Send the current region to the inferior Puppet REPL process."
(interactive "r")
(deactivate-mark t)
(let* ((string (buffer-substring-no-properties start end))
(proc (puppet-get-repl-proc)))
(comint-simple-send proc string)))
(defun puppet-repl-send-line ()
"Send the current line to the inferior Puppet REPL process."
(interactive)
(puppet-repl-send-region (line-beginning-position) (line-end-position)))
(defun puppet-repl-send-buffer ()
"Send the current buffer to the inferior Puppet REPL process."
(interactive)
(puppet-repl-send-region (point-min) (point-max)))
(defun puppet-comint-filter (string)
(ansi-color-apply string))
(defun puppet-repl ()
"Launch a Puppet REPL using `puppet-repl-command' as an inferior mode."
(interactive)
;; the command (at least for now) is 'puppet debugger'
;; but really it is 'puppet' with args 'debugger' + puppet-repl-args
;; Even if it changes, we need to not break on multi-word commands
(unless (comint-check-proc puppet-repl-buffer)
(let* ((command-list (split-string puppet-repl-command))
(real-puppet-repl-command (car command-list))
(real-puppet-repl-args (append (cdr command-list)
puppet-repl-args)))
(set-buffer
(apply 'make-comint "Puppet-REPL"
real-puppet-repl-command
nil
real-puppet-repl-args)))
;; Workaround for ansi colors
(add-hook 'comint-preoutput-filter-functions 'puppet-comint-filter nil t))
(pop-to-buffer puppet-repl-buffer))
;;; Specialized rx
(eval-when-compile
(defun puppet-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 puppet-rx-constituents
`((symbol puppet-rx-symbol 0 nil)
;; http://docs.puppetlabs.com/puppet/3/reference/lang_datatypes.html#regular-expressions
(regexp-literal . ,(rx (zero-or-more
(or
;; Not the end of a regexp
(not (any "/" "\\" "\n"))
;; Any escaped character
(and "\\" not-newline)))))
;; https://puppet.com/docs/puppet/latest/lang_reserved.html
(keyword . ,(rx (or "and" "application" "attr" "case" "class" "component"
"consumes" "default" "define" "else" "elsif"
"environment" "false" "function" "if" "import" "in"
"inherits" "node" "or" "private" "produces" "regexp"
"site" "true" "type" "undef" "unit" "unless"
;; Bolt
;; https://puppet.com/docs/bolt/0.x/writing_plans.html
"plan"
)))
;; https://puppet.com/docs/puppet/latest/function.html
(builtin-function . ,(rx (or "abs" "alert" "all" "annotate" "any"
"assert_type" "binary_file" "break" "call"
"camelcase" "capitalize" "ceiling" "chomp"
"chop" "compare" "contain" "convert_to"
"create_resources" "crit" "debug" "defined"
"dig" "digest" "downcase" "each" "emerg"
"empty" "epp" "err" "eyaml_lookup_key"
"fail" "file" "filter" "find_file"
"find_template" "flatten" "floor"
"fqdn_rand" "generate" "get" "getvar"
"group_by" "hiera" "hiera_array"
"hiera_hash" "hiera_include" "hocon_data"
"import" "include" "info" "inline_epp"
"inline_template" "join" "json_data" "keys"
"length" "lest" "lookup" "lstrip" "map"
"match" "max" "md5" "min" "new" "next"
"notice" "partition" "realize" "reduce"
"regsubst" "require" "return" "reverse_each"
"round" "rstrip" "scanf" "sha1" "sha256"
"shellquote" "size" "slice" "split"
"sprintf" "step" "strftime" "strip" "tag"
"tagged" "template" "tree_each" "then"
"type" "unique" "unwrap" "upcase" "values"
"versioncmp" "warning" "with" "yaml_data"
;; Bolt
;; https://puppet.com/docs/bolt/0.x/plan_functions.html
;; https://puppet.com/docs/bolt/0.x/writing_plans.html#concept-4926
"apply" "apply_prep" "add_facts" "facts"
"fail_plan" "file_upload" "get_targets"
"puppetdb_fact" "puppetdb_query"
"run_command" "run_plan" "run_script"
"run_task" "set_feature" "set_var" "vars"
"without_default_logging"
)))
;; http://docs.puppetlabs.com/references/latest/type.html
(builtin-type . ,(rx (or "augeas" "computer" "cron" "exec" "file"
"filebucket" "group" "host" "interface" "k5login"
"macauthorization" "mailalias" "maillist" "mcx"
"mount" "nagios_command" "nagios_contact"
"nagios_contactgroup" "nagios_host"
"nagios_hostdependency" "nagios_hostescalation"
"nagios_hostextinfo" "nagios_hostgroup"
"nagios_service" "nagios_servicedependency"
"nagios_serviceescalation" "nagios_serviceextinfo"
"nagios_servicegroup" "nagios_timeperiod" "notify"
"package" "resources" "router" "schedule"
"scheduled_task" "selboolean" "selmodule"
"service" "ssh_authorized_key" "sshkey" "stage"
"tidy" "user" "vlan" "yumrepo" "zfs" "zone"
"zpool")))
;; http://docs.puppetlabs.com/references/stable/metaparameter.html.
;; Strictly speaking, this is no meta parameter, but it's so common that
;; it got a mention in the docs, see
;; http://docs.puppetlabs.com/puppet/latest/reference/lang_resources.html#ensure,
;; so we'll consider it as metaparameter anyway
(builtin-metaparam . ,(rx (or "alias" "audit" "before" "consume" "export"
"loglevel" "noop" "notify" "require"
"schedule" "stage" "subscribe" "tag"
;; Because it's so common and important
"ensure")))
;; https://github.com/puppetlabs/puppet-specifications/blob/master/language/types_values_variables.md
(data-type . ,(rx (or
;; Data Types
"Array" "Binary" "Hash"
;; Scalar Types
"Boolean" "Float" "Integer" "Regexp" "SemVer" "String"
"Timespan" "Timestamp"
;; Catalog Types
"Class" "Resource"
;; Abstract Types
"Any" "CatalogEntry" "Collection" "Data" "Enum"
"Iterable" "Iterator" "NotUndef" "Numeric" "Optional"
"Pattern" "RichData" "Scalar" "ScalarData"
"SemVerRange" "Struct" "Tuple" "Variant"
;; Platform Types:
"Callable" "Default" "Runtime" "Sensitive" "Type"
"Undef"
;; Bolt types:
;; https://puppet.com/docs/bolt/0.x/writing_plans.html
"Error" "PlanResult" "Result" "ResultSet" "Target"
"TargetSpec"
)))
;; http://docs.puppetlabs.com/puppet/latest/reference/lang_reserved.html#classes-and-types
(resource-name . ,(rx
;; Optional top-level scope
(optional "::")
(zero-or-more (any "a-z")
(zero-or-more (any "a-z" "0-9" "_"))
"::")
;; Nested sub-scopes
(any "a-z")
(zero-or-more (any "a-z" "0-9" "_"))))
(cap-resource-name . ,(rx
;; Top-scope indicator
(optional "::")
(zero-or-more (any "A-Z")
(zero-or-more
(any "a-z" "0-9" "_"))
"::")
;; Nested sub-scopes
(any "A-Z")
(zero-or-more (any "a-z" "0-9" "_"))))
;; http://docs.puppetlabs.com/puppet/latest/reference/lang_reserved.html#nodes
(node-name . ,(rx (one-or-more (any "a-z" "0-9" ?. ?_ ?-))))
;; http://docs.puppetlabs.com/puppet/latest/reference/lang_reserved.html#variables
(simple-variable-name . ,(rx (one-or-more (any "A-Z" "a-z" "0-9" "_"))))
(variable-name . ,(rx
;; The optional scope designation
(optional "::")
(zero-or-more (any "a-z")
(zero-or-more
(any "A-Z" "a-z" "0-9" "_"))
"::")
;; The final variable name
(one-or-more (any "A-Z" "a-z" "0-9" "_"))))
;; http://docs.puppetlabs.com/puppet/latest/reference/lang_datatypes.html#double-quoted-strings
(dq-escape . ,(rx (or line-start (not (any "\\")))
(zero-or-more "\\\\")
;; We do not include \n and \', because these are
;; available in single-quoted strings as well
(group "\\" (any ?\" ?$ ?n ?r ?t ?s)))))
"Additional special sexps for `puppet-rx'")
(defmacro puppet-rx (&rest sexps)
"Specialized `rx' variant for Puppet Mode.
In addition to the standard forms of `rx', the following forms
are available:
`(symbol SEXP …)'
Match SEXPs inside symbol boundaries only
`regexp-literal'
A Puppet regexp literal, *without* surrounding slashes
`keyword'
Any valid Puppet keyword
`builtin-function'
Any built-in Puppet function
`builtin-type'
Any built-in Puppet type
`builtin-metaparam'
Any built-in meta-parameter, and `ensure'
`data-type'
Any Puppet data type
`resource-name'
Any valid resource name, including scopes
`cap-resource-name'
Any capitalized resource name, including capitalized scopes
`node-name'
Any valid node name
`simple-variable-name'
Any variable name without scopes, without leading dollar sign
`variable-name'
Any variable name including scopes, without a leading dollar sign
`dq-escape'
Special escape sequences for double-quoted strings"
(let ((rx-constituents (append puppet-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))))))
;;; Checking
(defvar-local puppet-last-validate-command nil
"The last command used for validation.")
(defvar-local puppet-last-lint-command nil
"The last command used for linting.")
(defvar-local puppet-last-apply-command nil
"The last command used to apply a manifest.")
(defun puppet-run-check-command (command buffer-name-template)
"Run COMMAND to check the current buffer."
(save-some-buffers (not compilation-ask-about-save) nil)
(compilation-start command nil (lambda (_)
(format buffer-name-template command))))
(defun puppet-read-command (prompt previous-command default-command)
"Read a command from minibuffer with PROMPT."
(let* ((buffer-file-name (or (buffer-file-name) ""))
(filename (or (file-remote-p buffer-file-name 'localname)
buffer-file-name)))
(read-string prompt (or previous-command
(concat default-command " "
(shell-quote-argument filename))))))
(defun puppet-validate (command)
"Validate the syntax of the current buffer with COMMAND.
When called interactively, prompt for COMMAND."
(interactive (list (puppet-read-command "Validate command: "
puppet-last-validate-command
puppet-validate-command)))
(setq puppet-last-validate-command command)
(puppet-run-check-command command "*Puppet Validate: %s*"))
(defun puppet-lint (command)
"Lint the current buffer with COMMAND.
When called interactively, prompt for COMMAND."
(interactive (list (puppet-read-command "Lint command: "
puppet-last-lint-command
puppet-lint-command)))
(setq puppet-last-lint-command command)
(puppet-run-check-command command "*Puppet Lint: %s*"))
(defun puppet-apply (command)
"Apply the current manifest with COMMAND.
When called interactively, prompt for COMMAND."
(interactive (list (puppet-read-command "Apply command: "
puppet-last-apply-command
puppet-apply-command)))
(setq puppet-last-apply-command command)
(puppet-run-check-command command "*Puppet Apply: %s*"))
;;; Navigation
;; TODO: Check which of these are still needed for SMIE
(defun puppet-beginning-of-defun-function (&optional arg)
"Move to the ARG'th beginning of a block."
(let* ((arg (or arg 1))
(search (if (< arg 0) #'search-forward #'search-backward))
(steps (abs arg)))
(while (> steps 0)
(let ((pos (funcall search "{" nil 'no-error)))
;; Skip over strings and comments
(while (and pos (puppet-in-string-or-comment-p pos))
(setq pos (funcall search "{" nil 'no-error)))
(if pos
(setq steps (1- steps))
;; Drop out of outer loop
(setq steps 0))))
(when (< arg 0)
(backward-char))))
;;; Indentation code
(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
(let ((opoint (nth 1 (syntax-ppss))))
(when (and opoint
(progn
(goto-char opoint)
(looking-at-p "{")))
(current-indentation)))))
(defun puppet-in-argument-list ()
"If point is in an argument list, return the position of the opening '('.
If point is not in an argument list, return nil."
(puppet--in-listlike "("))
(defun puppet-in-array ()
"If point is in an array, return the position of the opening '[' of
that array, else return nil."
(puppet--in-listlike "\\["))
(defun puppet--in-listlike (openstring)
"If point is in a listlike, return the position of the opening character of
it, else return nil.
OPENSTRING is a regexp string matching the opening character."
(save-excursion
(let ((opoint (nth 1 (syntax-ppss))))
(when (and opoint
(progn
(goto-char opoint)
(looking-at-p openstring)))
opoint))))
(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-listlike (listtype closing-regex list-start)
;; This line starts with an element from an array or parameter list.
;; Indent to the same indentation as the first element of the list:
;;
;; $example = ['string1', 'string2',
;; 'string3', 'string4']
;; $example = example('string1',
;; 'string2')
(save-excursion
(if (looking-at closing-regex)
;; Closing bracket on a line by itself. Align with opening bracket.
(progn
(goto-char list-start)
(if (or (save-excursion (forward-char) (eolp))
;; closing class parameter list:
(and (eq listtype 'arglist)
(save-excursion
(backward-sexp 2)
(looking-at "class.*"))))
(current-indentation)
(current-column)))
;; Use normal indentation if the point is at the end of the line.
;;
;; $example => [
;; 'foo',
;; 'bar',
;; ]
(goto-char list-start)
(forward-char 1)
(if (eolp)
(+ (current-indentation) puppet-indent-level)
;; Otherwise, attempt to align as described above.
(re-search-forward "\\S-")
(forward-char -1)
(current-column)))))
(defun puppet-indent-array (array-start)
(puppet-indent-listlike 'array "^\\s-*],*" array-start))
(defun puppet-indent-arglist (arglist-start)
(puppet-indent-listlike 'arglist "^\\s-*),*" arglist-start))
(defun puppet-indent-line ()
"Indent current line as puppet code."
(interactive)
(let ((pos (- (point-max) (point))))
(beginning-of-line)
(if (bobp)
(indent-line-to 0) ; First line is always non-indented
(let ((not-indented t)
(arglist-start (puppet-in-argument-list))
(array-start (puppet-in-array))
(include-start (puppet-in-include))
(block-indent (puppet-block-indent))
cur-indent)
(cond
(array-start (setq cur-indent (puppet-indent-array array-start)))
(arglist-start (setq cur-indent (puppet-indent-arglist arglist-start)))
(include-start
(setq cur-indent include-start))
((and (looking-at "^\\s-*}.*$") block-indent)
;; This line contains a closing brace 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.
((or (eq (puppet-syntax-context) 'comment)
(save-excursion (end-of-line)
(eq (puppet-syntax-context) 'comment)))
(if (bobp)
(setq not-indented nil)))
;; Closing paren. Use indentation based on start of
;; argument list
((or (looking-at "^\\s-*\)\\s-*$")
(looking-at "^[^\n\(]*[\)],?\\s-*$"))
(goto-char (puppet-in-argument-list))
(setq cur-indent (current-indentation))
(setq not-indented nil))
;; Closing bracket. Use indentation based on start of
;; array.
((looking-at "^[^[\n]*],?\\s-*$")
(goto-char (puppet-in-array))
(setq cur-indent (current-indentation))
(setq not-indented nil))
;; Brace, paren or bracket (possibly followed by a comma)
;; on a line by itself will already be indented to the
;; right level, so we can cheat and stop there.
((looking-at "^\\s-*[])}]\\(,\\|\\s-*[-~]>\\)?\\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))))
;; If initial point was within line's indentation,
;; position after the indentation. Else stay at same point in text.
(if (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos)))))
;;; Font locking
(defvar puppet-mode-syntax-table
(let ((table (make-syntax-table)))
;; Our strings
(modify-syntax-entry ?\' "\"'" table)
(modify-syntax-entry ?\" "\"\"" table)
;; C-style comments. Yes, Puppet has these!
(modify-syntax-entry ?/ ". 14b" table)
(modify-syntax-entry ?* ". 23b" table)
;; Line comments
(modify-syntax-entry ?# "<" table)
(modify-syntax-entry ?\n ">" table)
;; The backslash is our escape character
(modify-syntax-entry ?\\ "\\" table)
;; The dollar sign is an expression prefix for variables
(modify-syntax-entry ?$ "'" table)
;; Fix various operators and punctionation.
(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)
;; Our parenthesis, braces and brackets
(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.")
(defvar puppet-font-lock-keywords
`(
;; Keywords
(,(puppet-rx (symbol keyword)) 0 font-lock-keyword-face)
;; Data Types
(,(puppet-rx (symbol data-type)) 0 font-lock-type-face)
;; Variables
(,(puppet-rx "$" (symbol variable-name)) 0 font-lock-variable-name-face)
;; Class and type declarations
(,(puppet-rx (symbol (or "class" "define" "plan"))
(one-or-more space)
(group (symbol resource-name)))
1 font-lock-type-face)
;; Node declarations
(,(puppet-rx (symbol "node")
(one-or-more space)
(group node-name))
1 font-lock-type-face)
;; Resource usage, see
;; http://docs.puppetlabs.com/puppet/latest/reference/lang_resources.html
(,(puppet-rx (group (repeat 0 2 "@") ; Virtual and exported resources
(symbol resource-name))
(zero-or-more space) "{")
1 font-lock-type-face)
;; Resource defaults, see
;; http://docs.puppetlabs.com/puppet/latest/reference/lang_defaults.html
(,(puppet-rx (group (symbol cap-resource-name)) (zero-or-more space) "{")
1 font-lock-type-face)
;; Resource references, see
;; http://docs.puppetlabs.com/puppet/latest/reference/lang_datatypes.html#resource-references
(,(puppet-rx (group (symbol cap-resource-name)) (zero-or-more space) "[")
1 font-lock-type-face)
;; Resource collectors, see
;; http://docs.puppetlabs.com/puppet/latest/reference/lang_collectors.html
(,(puppet-rx (group (symbol cap-resource-name)) (zero-or-more space)
(optional "<") ; Exported collector
"<|")
1 font-lock-type-face)
;; Negation
("!" 0 font-lock-negation-char-face)
;; Builtin meta parameters
(,(puppet-rx (group (symbol builtin-metaparam)) (zero-or-more space) "=>")
1 font-lock-builtin-face)
;; Built-in functions
(,(puppet-rx (symbol builtin-function)) 0 font-lock-builtin-face)
;; Type arguments to some special built-in functions
(,(puppet-rx (symbol (or "contain" "include" "require"))
(one-or-more space)
(group (symbol resource-name)))
1 font-lock-type-face)
;; Variable expansions in strings and comments
(puppet-match-valid-expansion 1 font-lock-variable-name-face t)
(puppet-match-invalid-expansion 1 font-lock-warning-face t)
;; Escape sequences in strings
(puppet-match-valid-escape 1 'puppet-escape-sequence t)
;; Regexp literals
(puppet-match-regexp-literal (1 'puppet-regular-expression-literal t)
(2 'puppet-regular-expression-literal t)
(3 'puppet-regular-expression-literal t)))
"Font lock keywords for Puppet Mode.")
(defun puppet-match-property (property context limit)
"Match a PROPERTY in CONTEXT before LIMIT.
PROPERTY is the text property to look for. CONTEXT is one of
`single-quoted', `double-quoted', `comment' or nil, or a list
with any of these symbols. The expansion will only match if it
is in any given CONTEXT. nil means no specific syntactic context."
(when (symbolp context)
(setq context (list context)))
(let* ((pos (next-single-char-property-change (point) property nil limit)))
(when (and pos (> pos (point)))
(goto-char pos)
(let* ((value (get-text-property pos property)))
(if (and value (memq (car value) context))
(progn (set-match-data (cdr value)) t)
(puppet-match-property property context limit))))))
(defun puppet-match-valid-expansion (limit)
"Match a valid expansion before LIMIT.
A valid expansion is a variable expansion in a double-quoted
string."
(let ((valid-contexts '(double-quoted)))
(when puppet-fontify-variables-in-comments
(push 'comment valid-contexts))
(puppet-match-property 'puppet-expansion valid-contexts limit)))
(defun puppet-match-invalid-expansion (limit)
"Match an invalid expansion before LIMIT.
An invalid expansion is a variable expansion in a single-quoted
string."
(puppet-match-property 'puppet-expansion 'single-quoted limit))
(defun puppet-match-valid-escape (limit)
"Match a valid escape sequence before LIMIT."
(puppet-match-property 'puppet-escape 'double-quoted limit))
(defun puppet-match-regexp-literal (limit)
"Match a regular expression literal before LIMIT."
(puppet-match-property 'puppet-regexp-literal nil limit))
(defun puppet-syntax-propertize-match (property &optional group)
"Propertize a match with PROPERTY at GROUP's beginning.
When in a special syntax context, add PROPERTY to the first
character of the given GROUP of the current `match-data'. GROUP
defaults to the first group.
The value of PROPERTY is `(CONTEXT . MATCH-DATA)', where CONTEXT
is one of nil, `single-quoted', `double-quoted' or `comment' and
denotes the surrounding context, and MATCH-DATA is the original
match data from propertization."
(let* ((beg (match-beginning (or group 1)))
;; Syntax functions can modify the match data, so we must preserve it
(context (save-match-data (puppet-syntax-context beg))))
(put-text-property beg (1+ beg) property
(cons context (match-data)))))
(defun puppet-syntax-propertize-scope-operator (beg end)
"Mark all scope operators between BEG and END as symbols."
(save-match-data
(save-excursion
(goto-char beg)
(while (search-forward "::" end 'no-error)
(put-text-property (match-beginning 0) (match-end 0)
'syntax-table (string-to-syntax "_"))))))
(defun puppet-syntax-propertize-function (start end)
"Propertize text between START and END.
Used as `syntax-propertize-function' in Puppet Mode."
(let ((case-fold-search nil))
(goto-char start)
(remove-text-properties start end '(puppet-expansion
puppet-escape
puppet-regexp-literal))
(funcall
(syntax-propertize-rules
;; Make double colons part of the surrounding symbol. We can't put the
;; colon into symbol syntax, because the colon can appear as non-symbol
;; character as well (e.g. "package { $foo:"), but we want the
;; double-colon as part of the symbol to make symbol navigation move
;; across it, and to make stuff like `thing-at-point' behave reasonably
((rx "::" symbol-start) (0 "_"))
;; Mark regular expression literals in proper contexts (nodes, cases,
;; selectors and match operators) as strings, to make them play nicely
;; with sexp navigation and SMIE. Also propertize them for use in font
;; lock keywords, since we want to apply our own
;; puppet-regular-expression-literal face instead of the generic string
;; face.
((puppet-rx (group "/")
(group regexp-literal)
(group "/")
(zero-or-more space)
(or ":" "=>"))
;; We propertize the body of the regexp literal, not its delimiters, to
;; make sure that font lock keywords kick in when the literal gets moved
;; with point on separator. The separator is propertized by syntactic
;; font lock (since we marked it as string delimiter), so font lock
;; keywords will start in the body of the literal. If we'd propertize
;; the separator, font lock keywords would miss the property
(0 (ignore (puppet-syntax-propertize-match 'puppet-regexp-literal 2)))
(1 "|") (3 "|"))
((puppet-rx (or "=~" "!~" (symbol "node")) (zero-or-more space)
(group "/")
(group regexp-literal)
(group "/"))
(0 (ignore (puppet-syntax-propertize-match 'puppet-regexp-literal 2)))
(1 "|") (3 "|"))
;; Find escape sequences and variable expansions.
((puppet-rx dq-escape)
(1 (ignore (puppet-syntax-propertize-match 'puppet-escape))))
((puppet-rx (or line-start (not (any "\\")))