1 ;;; cfengine.el --- mode for editing Cfengine files
3 ;; Copyright (C) 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Maintainer: Ted Zlatanov <tzz@lifelogs.com>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Provides support for editing GNU Cfengine files, including
28 ;; font-locking, Imenu and indentation, but with no special keybindings.
30 ;; The CFEngine 3.x support doesn't have Imenu support but patches are
33 ;; By default, CFEngine 3.x syntax is used.
35 ;; You can set it up so either `cfengine2-mode' (2.x and earlier) or
36 ;; `cfengine3-mode' (3.x) will be picked, depending on the buffer
39 ;; (add-to-list 'auto-mode-alist '("\\.cf\\'" . cfengine-auto-mode))
41 ;; OR you can choose to always use a specific version, if you prefer
44 ;; (add-to-list 'auto-mode-alist '("\\.cf\\'" . cfengine3-mode))
45 ;; (add-to-list 'auto-mode-alist '("^cf\\." . cfengine2-mode))
46 ;; (add-to-list 'auto-mode-alist '("^cfagent.conf\\'" . cfengine2-mode))
48 ;; This is not the same as the mode written by Rolf Ebert
49 ;; <ebert@waporo.muc.de>, distributed with cfengine-2.0.5. It does
50 ;; better fontification and indentation, inter alia.
55 "Editing CFEngine files."
58 (defcustom cfengine-indent
2
59 "Size of a CFEngine indentation step in columns."
63 (defcustom cfengine-parameters-indent
'(promise pname
0)
64 "*Indentation of CFEngine3 promise parameters (hanging indent).
66 For example, say you have this code:
73 promiseparameter => ...
76 You can choose to indent promiseparameter from the beginning of
77 the line (absolutely) or from the word \"promise\" (relatively).
79 You can also choose to indent the start of the word
80 \"promiseparameter\" or the arrow that follows it.
82 Finally, you can choose the amount of the indent.
84 The default is to anchor at promise, indent parameter name, and offset 0:
91 comment => \"my netrc\",
92 perms => mog(\"600\", \"tzz\", \"tzz\");
95 Here we anchor at beginning of line, indent arrow, and offset 10:
102 comment => \"my netrc\",
103 perms => mog(\"600\", \"tzz\", \"tzz\");
106 Some, including cfengine_stdlib.cf, like to anchor at promise, indent
107 arrow, and offset 16 or so:
114 comment => \"my netrc\",
115 perms => mog(\"600\", \"tzz\", \"tzz\");
121 (choice (const :tag
"Anchor at beginning of promise" promise
)
122 (const :tag
"Anchor at beginning of line" bol
))
123 (choice (const :tag
"Indent parameter name" pname
)
124 (const :tag
"Indent arrow" arrow
))
125 (integer :tag
"Indentation amount from anchor")))
127 (defvar cfengine-mode-debug nil
128 "Whether `cfengine-mode' should print debugging info.")
130 (defcustom cfengine-mode-abbrevs nil
131 "Abbrevs for CFEngine2 mode."
133 :type
'(repeat (list (string :tag
"Name")
134 (string :tag
"Expansion")
135 (choice :tag
"Hook" (const nil
) function
))))
137 (make-obsolete-variable 'cfengine-mode-abbrevs
'edit-abbrevs
"24.1")
139 ;; Taken from the doc for pre-release 2.1.
141 (defconst cfengine2-actions
142 '("acl" "alerts" "binservers" "broadcast" "control" "classes" "copy"
143 "defaultroute" "disks" "directories" "disable" "editfiles" "files"
144 "filters" "groups" "homeservers" "ignore" "import" "interfaces"
145 "links" "mailserver" "methods" "miscmounts" "mountables"
146 "processes" "packages" "rename" "required" "resolve"
147 "shellcommands" "tidy" "unmount"
148 ;; Keywords for cfservd.
149 "admit" "grant" "deny")
150 "List of the action keywords supported by Cfengine.
151 This includes those for cfservd as well as cfagent.")
153 (defconst cfengine3-defuns
157 "List of the CFEngine 3.x defun headings.")
159 (defconst cfengine3-defuns-regex
160 (regexp-opt cfengine3-defuns t
)
161 "Regex to match the CFEngine 3.x defuns.")
163 (defconst cfengine3-class-selector-regex
"\\([[:alnum:]_().&|!:]+\\)::")
165 (defconst cfengine3-category-regex
"\\([[:alnum:]_]+\\):")
167 (defconst cfengine3-vartypes
170 '(string int real slist ilist rlist irange rrange counter
))
171 "List of the CFEngine 3.x variable types."))
173 (defvar cfengine2-font-lock-keywords
175 ;; List the allowed actions explicitly, so that errors are more obvious.
176 (,(concat "^[ \t]*" (eval-when-compile
177 (regexp-opt cfengine2-actions t
))
179 1 font-lock-keyword-face
)
181 ("^[ \t]*\\([[:alnum:]_().|!]+\\)::" 1 font-lock-function-name-face
)
183 ("$(\\([[:alnum:]_]+\\))" 1 font-lock-variable-name-face
)
184 ("${\\([[:alnum:]_]+\\)}" 1 font-lock-variable-name-face
)
185 ;; Variable definitions.
186 ("\\_<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1 font-lock-variable-name-face
)
187 ;; File, acl &c in group: { token ... }
188 ("{[ \t]*\\([^ \t\n]+\\)" 1 font-lock-constant-face
)))
190 (defvar cfengine3-font-lock-keywords
192 ;; Defuns. This happens early so they don't get caught by looser
194 (,(concat "\\_<" cfengine3-defuns-regex
"\\_>"
195 "[ \t]+\\_<\\([[:alnum:]_.:]+\\)\\_>"
196 "[ \t]+\\_<\\([[:alnum:]_.:]+\\)"
197 ;; Optional parentheses with variable names inside.
198 "\\(?:(\\([^)]*\\))\\)?")
199 (1 font-lock-builtin-face
)
200 (2 font-lock-constant-face
)
201 (3 font-lock-function-name-face
)
202 (4 font-lock-variable-name-face nil t
))
205 (,(concat "^[ \t]*" cfengine3-class-selector-regex
)
206 1 font-lock-keyword-face
)
209 (,(concat "^[ \t]*" cfengine3-category-regex
)
210 1 font-lock-builtin-face
)
212 ;; Variables, including scope, e.g. module.var
213 ("[@$](\\([[:alnum:]_.:]+\\))" 1 font-lock-variable-name-face
)
214 ("[@$]{\\([[:alnum:]_.:]+\\)}" 1 font-lock-variable-name-face
)
216 ;; Variable definitions.
217 ("\\_<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1 font-lock-variable-name-face
)
220 (,(concat "\\_<" (eval-when-compile (regexp-opt cfengine3-vartypes t
)) "\\_>")
221 1 font-lock-type-face
)))
223 (defvar cfengine2-imenu-expression
224 `((nil ,(concat "^[ \t]*" (eval-when-compile
225 (regexp-opt cfengine2-actions t
))
228 ("Variables/classes" "\\_<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1)
229 ("Variables/classes" "\\_<define=\\([[:alnum:]_]+\\)" 1)
230 ("Variables/classes" "\\_<DefineClass\\>[ \t]+\\([[:alnum:]_]+\\)" 1))
231 "`imenu-generic-expression' for CFEngine mode.")
233 (defun cfengine2-outline-level ()
234 "`outline-level' function for CFEngine mode."
235 (if (looking-at "[^:]+\\(?:[:]+\\)$")
236 (length (match-string 1))))
238 (defun cfengine2-beginning-of-defun ()
239 "`beginning-of-defun' function for CFEngine mode.
240 Treats actions as defuns."
241 (unless (<= (current-column) (current-indentation))
243 (if (re-search-backward "^[[:alpha:]]+: *$" nil t
)
245 (goto-char (point-min)))
248 (defun cfengine2-end-of-defun ()
249 "`end-of-defun' function for CFEngine mode.
250 Treats actions as defuns."
252 (if (re-search-forward "^[[:alpha:]]+: *$" nil t
)
254 (goto-char (point-max)))
257 ;; Fixme: Should get an extra indent step in editfiles BeginGroup...s.
259 (defun cfengine2-indent-line ()
260 "Indent a line in Cfengine mode.
261 Intended as the value of `indent-line-function'."
262 (let ((pos (- (point-max) (point))))
265 (back-to-indentation)
267 ;; Action selectors aren't indented; class selectors are
268 ;; indented one step.
269 ((looking-at "[[:alnum:]_().|!]+:\\(:\\)?")
271 (indent-line-to cfengine-indent
)
273 ;; Outdent leading close brackets one step.
274 ((or (eq ?\
} (char-after))
275 (eq ?\
) (char-after)))
277 (indent-line-to (save-excursion
282 ;; Inside brackets/parens: indent to start column of non-comment
283 ;; token on line following open bracket or by one step from open
286 (progn (indent-line-to (save-excursion
289 (skip-chars-forward " \t")
290 (if (looking-at "[^\n#]")
292 (skip-chars-backward " \t")
293 (+ (current-column) -
1
297 ;; Indent by two steps after a class selector.
299 (re-search-backward "^[ \t]*[[:alnum:]_().|!]+::" nil t
))
300 (indent-line-to (* 2 cfengine-indent
)))
301 ;; Indent by one step if we're after an action header.
303 (goto-char (point-min))
304 (looking-at "[[:alpha:]]+:[ \t]*$"))
305 (indent-line-to cfengine-indent
))
306 ;; Else don't indent.
308 (indent-line-to 0))))
309 ;; If initial point was within line's indentation,
310 ;; position after the indentation. Else stay at same point in text.
311 (if (> (- (point-max) pos
) (point))
312 (goto-char (- (point-max) pos
)))))
314 ;; This doesn't work too well in Emacs 21.2. See 22.1 development
316 (defun cfengine-fill-paragraph (&optional justify
)
317 "Fill `paragraphs' in Cfengine code."
319 (or (if (fboundp 'fill-comment-paragraph
)
320 (fill-comment-paragraph justify
) ; post Emacs 21.3
321 ;; else do nothing in a comment
322 (nth 4 (parse-partial-sexp (save-excursion
326 (let ((paragraph-start
327 ;; Include start of parenthesized block.
328 "\f\\|[ \t]*$\\|.*\(")
330 ;; Include action and class lines, start and end of
331 ;; bracketed blocks and end of parenthesized blocks to
332 ;; avoid including these in fill. This isn't ideal.
333 "[ \t\f]*$\\|.*#\\|.*[\){}]\\|\\s-*[[:alpha:]_().|!]+:")
334 fill-paragraph-function
)
335 (fill-paragraph justify
))
338 (defun cfengine3-beginning-of-defun ()
339 "`beginning-of-defun' function for Cfengine 3 mode.
340 Treats body/bundle blocks as defuns."
341 (unless (<= (current-column) (current-indentation))
343 (if (re-search-backward (concat "^[ \t]*" cfengine3-defuns-regex
"\\_>") nil t
)
345 (goto-char (point-min)))
348 (defun cfengine3-end-of-defun ()
349 "`end-of-defun' function for Cfengine 3 mode.
350 Treats body/bundle blocks as defuns."
352 (if (re-search-forward (concat "^[ \t]*" cfengine3-defuns-regex
"\\_>") nil t
)
354 (goto-char (point-max)))
357 (defun cfengine3-indent-line ()
358 "Indent a line in Cfengine 3 mode.
359 Intended as the value of `indent-line-function'."
360 (let ((pos (- (point-max) (point)))
364 (back-to-indentation)
365 (setq parse
(parse-partial-sexp (point-min) (point)))
366 (when cfengine-mode-debug
367 (message "%S" parse
))
370 ;; Body/bundle blocks start at 0.
371 ((looking-at (concat cfengine3-defuns-regex
"\\_>"))
373 ;; Categories are indented one step.
374 ((looking-at (concat cfengine3-category-regex
"[ \t]*\\(#.*\\)*$"))
375 (indent-line-to cfengine-indent
))
376 ;; Class selectors are indented two steps.
377 ((looking-at (concat cfengine3-class-selector-regex
"[ \t]*\\(#.*\\)*$"))
378 (indent-line-to (* 2 cfengine-indent
)))
379 ;; Outdent leading close brackets one step.
380 ((or (eq ?\
} (char-after))
381 (eq ?\
) (char-after)))
383 (indent-line-to (save-excursion
386 (move-beginning-of-line nil
)
387 (skip-chars-forward " \t")
390 ;; Inside a string and it starts before this line.
392 (< (nth 8 parse
) (save-excursion (beginning-of-line) (point))))
395 ;; Inside a defun, but not a nested list (depth is 1). This is
396 ;; a promise, usually.
398 ;; Indent to cfengine-indent times the nested depth
399 ;; plus 2. That way, promises indent deeper than class
400 ;; selectors, which in turn are one deeper than categories.
402 (let ((p-anchor (nth 0 cfengine-parameters-indent
))
403 (p-what (nth 1 cfengine-parameters-indent
))
404 (p-indent (nth 2 cfengine-parameters-indent
)))
405 ;; Do we have the parameter anchor and location and indent
406 ;; defined, and are we looking at a promise parameter?
407 (if (and p-anchor p-what p-indent
408 (looking-at "\\([[:alnum:]_]+[ \t]*\\)=>"))
409 (let* ((arrow-offset (* -
1 (length (match-string 1))))
410 (extra-offset (if (eq p-what
'arrow
) arrow-offset
0))
411 (base-offset (if (eq p-anchor
'promise
)
412 (* (+ 2 (nth 0 parse
)) cfengine-indent
)
414 (indent-line-to (max 0 (+ p-indent base-offset extra-offset
))))
415 ;; Else, indent to cfengine-indent times the nested depth
416 ;; plus 2. That way, promises indent deeper than class
417 ;; selectors, which in turn are one deeper than categories.
418 (indent-line-to (* (+ 2 (nth 0 parse
)) cfengine-indent
)))))
419 ;; Inside brackets/parens: indent to start column of non-comment
420 ;; token on line following open bracket or by one step from open
423 (progn (indent-line-to (save-excursion
426 (skip-chars-forward " \t")
428 ((looking-at "[^\n#]")
430 ((looking-at "[^\n#]")
433 (skip-chars-backward " \t")
434 (+ (current-column) -
1
438 ;; Else don't indent.
439 (t (indent-line-to 0))))
440 ;; If initial point was within line's indentation,
441 ;; position after the indentation. Else stay at same point in text.
442 (if (> (- (point-max) pos
) (point))
443 (goto-char (- (point-max) pos
)))))
445 ;; CFEngine 3.x grammar
447 ;; specification: blocks
448 ;; blocks: block | blocks block;
449 ;; block: bundle typeid blockid bundlebody
450 ;; | bundle typeid blockid usearglist bundlebody
451 ;; | body typeid blockid bodybody
452 ;; | body typeid blockid usearglist bodybody;
456 ;; usearglist: '(' aitems ')';
457 ;; aitems: aitem | aitem ',' aitems |;
460 ;; bundlebody: '{' statements '}'
461 ;; statements: statement | statements statement;
462 ;; statement: category | classpromises;
464 ;; bodybody: '{' bodyattribs '}'
465 ;; bodyattribs: bodyattrib | bodyattribs bodyattrib;
466 ;; bodyattrib: class | selections;
467 ;; selections: selection | selections selection;
468 ;; selection: id ASSIGN rval ';' ;
470 ;; classpromises: classpromise | classpromises classpromise;
471 ;; classpromise: class | promises;
472 ;; promises: promise | promises promise;
473 ;; category: CATEGORY
474 ;; promise: promiser ARROW rval constraints ';' | promiser constraints ';';
475 ;; constraints: constraint | constraints ',' constraint |;
476 ;; constraint: id ASSIGN rval;
479 ;; rval: ID | QSTRING | NAKEDVAR | list | usefunction
480 ;; list: '{' litems '}' ;
481 ;; litems: litem | litem ',' litems |;
482 ;; litem: ID | QSTRING | NAKEDVAR | list | usefunction
484 ;; functionid: ID | NAKEDVAR
486 ;; usefunction: functionid givearglist
487 ;; givearglist: '(' gaitems ')'
488 ;; gaitems: gaitem | gaitems ',' gaitem |;
489 ;; gaitem: ID | QSTRING | NAKEDVAR | list | usefunction
496 ;; NAKEDVAR [$@][(][a-zA-Z0-9_\200-\377.]+[)]|[$@][{][a-zA-Z0-9_\200-\377.]+[}]
497 ;; ID: [a-zA-Z0-9_\200-\377]+
500 ;; QSTRING: \"((\\\")|[^"])*\"|\'((\\\')|[^'])*\'|`[^`]*`
501 ;; CLASS: [.|&!()a-zA-Z0-9_\200-\377]+::
502 ;; CATEGORY: [a-zA-Z_]+:
504 (defun cfengine-common-settings ()
505 (set (make-local-variable 'syntax-propertize-function
)
506 ;; In the main syntax-table, \ is marked as a punctuation, because
507 ;; of its use in DOS-style directory separators. Here we try to
508 ;; recognize the cases where \ is used as an escape inside strings.
509 (syntax-propertize-rules ("\\(\\(?:\\\\\\)+\\)\"" (1 "\\"))))
510 (set (make-local-variable 'parens-require-spaces
) nil
)
511 (set (make-local-variable 'comment-start
) "# ")
512 (set (make-local-variable 'comment-start-skip
)
513 "\\(\\(?:^\\|[^\\\\\n]\\)\\(?:\\\\\\\\\\)*\\)#+[ \t]*")
514 ;; Like Lisp mode. Without this, we lose with, say,
515 ;; `backward-up-list' when there's an unbalanced quote in a
516 ;; preceding comment.
517 (set (make-local-variable 'parse-sexp-ignore-comments
) t
))
519 (defun cfengine-common-syntax (table)
520 ;; The syntax defaults seem OK to give reasonable word movement.
521 (modify-syntax-entry ?
# "<" table
)
522 (modify-syntax-entry ?
\n ">#" table
)
523 (modify-syntax-entry ?
\" "\"" table
) ; "string"
524 (modify-syntax-entry ?
\' "\"" table
) ; 'string'
525 ;; Variable substitution.
526 (modify-syntax-entry ?$
"." table
)
527 ;; Doze path separators.
528 (modify-syntax-entry ?
\\ "." table
))
530 (defconst cfengine3--prettify-symbols-alist
536 (define-derived-mode cfengine3-mode prog-mode
"CFE3"
537 "Major mode for editing CFEngine3 input.
538 There are no special keybindings by default.
540 Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves
541 to the action header."
542 (cfengine-common-settings)
543 (cfengine-common-syntax cfengine3-mode-syntax-table
)
545 (set (make-local-variable 'indent-line-function
) #'cfengine3-indent-line
)
547 (setq font-lock-defaults
548 '(cfengine3-font-lock-keywords
549 nil nil nil beginning-of-defun
))
550 (prog-prettify-install cfengine3--prettify-symbols-alist
)
552 ;; Use defuns as the essential syntax block.
553 (set (make-local-variable 'beginning-of-defun-function
)
554 #'cfengine3-beginning-of-defun
)
555 (set (make-local-variable 'end-of-defun-function
)
556 #'cfengine3-end-of-defun
))
559 (define-derived-mode cfengine2-mode prog-mode
"CFE2"
560 "Major mode for editing CFEngine2 input.
561 There are no special keybindings by default.
563 Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves
564 to the action header."
565 (cfengine-common-settings)
566 (cfengine-common-syntax cfengine2-mode-syntax-table
)
568 ;; Shell commands can be quoted by single, double or back quotes.
569 ;; It's debatable whether we should define string syntax, but it
570 ;; should avoid potential confusion in some cases.
571 (modify-syntax-entry ?\
` "\"" cfengine2-mode-syntax-table
)
573 (set (make-local-variable 'indent-line-function
) #'cfengine2-indent-line
)
574 (set (make-local-variable 'outline-regexp
) "[ \t]*\\(\\sw\\|\\s_\\)+:+")
575 (set (make-local-variable 'outline-level
) #'cfengine2-outline-level
)
576 (set (make-local-variable 'fill-paragraph-function
)
577 #'cfengine-fill-paragraph
)
578 (define-abbrev-table 'cfengine2-mode-abbrev-table cfengine-mode-abbrevs
)
579 (setq font-lock-defaults
580 '(cfengine2-font-lock-keywords nil nil nil beginning-of-line
))
581 ;; Fixme: set the args of functions in evaluated classes to string
582 ;; syntax, and then obey syntax properties.
583 (setq imenu-generic-expression cfengine2-imenu-expression
)
584 (set (make-local-variable 'beginning-of-defun-function
)
585 #'cfengine2-beginning-of-defun
)
586 (set (make-local-variable 'end-of-defun-function
) #'cfengine2-end-of-defun
))
589 (defun cfengine-auto-mode ()
590 "Choose between `cfengine2-mode' and `cfengine3-mode' depending
591 on the buffer contents"
594 (goto-char (point-min))
595 (while (not (or (eobp) v3
))
596 (setq v3
(looking-at (concat cfengine3-defuns-regex
"\\_>")))
598 (if v3
(cfengine3-mode) (cfengine2-mode))))
600 (defalias 'cfengine-mode
'cfengine3-mode
)
605 ;;; cfengine.el ends here