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 ;; You can set it up so either `cfengine2-mode' (2.x and earlier) or
34 ;; `cfengine3-mode' (3.x) will be picked, depending on the buffer
37 ;; (add-to-list 'auto-mode-alist '("\\.cf\\'" . cfengine-mode))
39 ;; OR you can choose to always use a specific version, if you prefer
42 ;; (add-to-list 'auto-mode-alist '("\\.cf\\'" . cfengine3-mode))
43 ;; (add-to-list 'auto-mode-alist '("^cf\\." . cfengine2-mode))
44 ;; (add-to-list 'auto-mode-alist '("^cfagent.conf\\'" . cfengine2-mode))
46 ;; This is not the same as the mode written by Rolf Ebert
47 ;; <ebert@waporo.muc.de>, distributed with cfengine-2.0.5. It does
48 ;; better fontification and indentation, inter alia.
53 "Editing CFEngine files."
56 (defcustom cfengine-indent
2
57 "Size of a CFEngine indentation step in columns."
61 (defcustom cfengine-parameters-indent
'(promise pname
0)
62 "*Indentation of CFEngine3 promise parameters (hanging indent).
64 For example, say you have this code:
71 promiseparameter => ...
74 You can choose to indent promiseparameter from the beginning of
75 the line (absolutely) or from the word \"promise\" (relatively).
77 You can also choose to indent the start of the word
78 \"promiseparameter\" or the arrow that follows it.
80 Finally, you can choose the amount of the indent.
82 The default is to anchor at promise, indent parameter name, and offset 0:
89 comment => \"my netrc\",
90 perms => mog(\"600\", \"tzz\", \"tzz\");
93 Here we anchor at beginning of line, indent arrow, and offset 10:
100 comment => \"my netrc\",
101 perms => mog(\"600\", \"tzz\", \"tzz\");
104 Some, including cfengine_stdlib.cf, like to anchor at promise, indent
105 arrow, and offset 16 or so:
112 comment => \"my netrc\",
113 perms => mog(\"600\", \"tzz\", \"tzz\");
119 (choice (const :tag
"Anchor at beginning of promise" promise
)
120 (const :tag
"Anchor at beginning of line" bol
))
121 (choice (const :tag
"Indent parameter name" pname
)
122 (const :tag
"Indent arrow" arrow
))
123 (integer :tag
"Indentation amount from anchor")))
125 (defvar cfengine-mode-debug nil
126 "Whether `cfengine-mode' should print debugging info.")
128 (defcustom cfengine-mode-abbrevs nil
129 "Abbrevs for CFEngine2 mode."
131 :type
'(repeat (list (string :tag
"Name")
132 (string :tag
"Expansion")
133 (choice :tag
"Hook" (const nil
) function
))))
135 (make-obsolete-variable 'cfengine-mode-abbrevs
'edit-abbrevs
"24.1")
137 ;; Taken from the doc for pre-release 2.1.
139 (defconst cfengine2-actions
140 '("acl" "alerts" "binservers" "broadcast" "control" "classes" "copy"
141 "defaultroute" "disks" "directories" "disable" "editfiles" "files"
142 "filters" "groups" "homeservers" "ignore" "import" "interfaces"
143 "links" "mailserver" "methods" "miscmounts" "mountables"
144 "processes" "packages" "rename" "required" "resolve"
145 "shellcommands" "tidy" "unmount"
146 ;; Keywords for cfservd.
147 "admit" "grant" "deny")
148 "List of the action keywords supported by Cfengine.
149 This includes those for cfservd as well as cfagent.")
151 (defconst cfengine3-defuns
155 "List of the CFEngine 3.x defun headings.")
157 (defconst cfengine3-defuns-regex
158 (regexp-opt cfengine3-defuns t
)
159 "Regex to match the CFEngine 3.x defuns.")
161 (defconst cfengine3-class-selector-regex
"\\([[:alnum:]_().&|!:]+\\)::")
163 (defconst cfengine3-category-regex
"\\([[:alnum:]_]+\\):")
165 (defconst cfengine3-vartypes
168 '(string int real slist ilist rlist irange rrange counter
))
169 "List of the CFEngine 3.x variable types."))
171 (defvar cfengine2-font-lock-keywords
173 ;; List the allowed actions explicitly, so that errors are more obvious.
174 (,(concat "^[ \t]*" (eval-when-compile
175 (regexp-opt cfengine2-actions t
))
177 1 font-lock-keyword-face
)
179 ("^[ \t]*\\([[:alnum:]_().|!]+\\)::" 1 font-lock-function-name-face
)
181 ("$(\\([[:alnum:]_]+\\))" 1 font-lock-variable-name-face
)
182 ("${\\([[:alnum:]_]+\\)}" 1 font-lock-variable-name-face
)
183 ;; Variable definitions.
184 ("\\<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1 font-lock-variable-name-face
)
185 ;; File, acl &c in group: { token ... }
186 ("{[ \t]*\\([^ \t\n]+\\)" 1 font-lock-constant-face
)))
188 (defvar cfengine3-font-lock-keywords
190 ;; Defuns. This happens early so they don't get caught by looser
192 (,(concat "\\<" cfengine3-defuns-regex
"\\>"
193 "[ \t]+\\<\\([[:alnum:]_.:]+\\)\\>"
194 "[ \t]+\\<\\([[:alnum:]_.:]+\\)"
195 ;; Optional parentheses with variable names inside.
196 "\\(?:(\\([^)]*\\))\\)?")
197 (1 font-lock-builtin-face
)
198 (2 font-lock-constant-face
)
199 (3 font-lock-function-name-face
)
200 (4 font-lock-variable-name-face nil t
))
203 (,(concat "^[ \t]*" cfengine3-class-selector-regex
)
204 1 font-lock-keyword-face
)
207 (,(concat "^[ \t]*" cfengine3-category-regex
)
208 1 font-lock-builtin-face
)
210 ;; Variables, including scope, e.g. module.var
211 ("[@$](\\([[:alnum:]_.:]+\\))" 1 font-lock-variable-name-face
)
212 ("[@$]{\\([[:alnum:]_.:]+\\)}" 1 font-lock-variable-name-face
)
214 ;; Variable definitions.
215 ("\\<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1 font-lock-variable-name-face
)
218 (,(concat "\\<" (eval-when-compile (regexp-opt cfengine3-vartypes t
)) "\\>")
219 1 font-lock-type-face
)))
221 (defvar cfengine2-imenu-expression
222 `((nil ,(concat "^[ \t]*" (eval-when-compile
223 (regexp-opt cfengine2-actions t
))
226 ("Variables/classes" "\\<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1)
227 ("Variables/classes" "\\<define=\\([[:alnum:]_]+\\)" 1)
228 ("Variables/classes" "\\<DefineClass\\>[ \t]+\\([[:alnum:]_]+\\)" 1))
229 "`imenu-generic-expression' for CFEngine mode.")
231 (defun cfengine2-outline-level ()
232 "`outline-level' function for CFEngine mode."
233 (if (looking-at "[^:]+\\(?:[:]+\\)$")
234 (length (match-string 1))))
236 (defun cfengine2-beginning-of-defun ()
237 "`beginning-of-defun' function for CFEngine mode.
238 Treats actions as defuns."
239 (unless (<= (current-column) (current-indentation))
241 (if (re-search-backward "^[[:alpha:]]+: *$" nil t
)
243 (goto-char (point-min)))
246 (defun cfengine2-end-of-defun ()
247 "`end-of-defun' function for CFEngine mode.
248 Treats actions as defuns."
250 (if (re-search-forward "^[[:alpha:]]+: *$" nil t
)
252 (goto-char (point-max)))
255 ;; Fixme: Should get an extra indent step in editfiles BeginGroup...s.
257 (defun cfengine2-indent-line ()
258 "Indent a line in Cfengine mode.
259 Intended as the value of `indent-line-function'."
260 (let ((pos (- (point-max) (point))))
263 (back-to-indentation)
265 ;; Action selectors aren't indented; class selectors are
266 ;; indented one step.
267 ((looking-at "[[:alnum:]_().|!]+:\\(:\\)?")
269 (indent-line-to cfengine-indent
)
271 ;; Outdent leading close brackets one step.
272 ((or (eq ?\
} (char-after))
273 (eq ?\
) (char-after)))
275 (indent-line-to (save-excursion
280 ;; Inside brackets/parens: indent to start column of non-comment
281 ;; token on line following open bracket or by one step from open
284 (progn (indent-line-to (save-excursion
287 (skip-chars-forward " \t")
288 (if (looking-at "[^\n#]")
290 (skip-chars-backward " \t")
291 (+ (current-column) -
1
295 ;; Indent by two steps after a class selector.
297 (re-search-backward "^[ \t]*[[:alnum:]_().|!]+::" nil t
))
298 (indent-line-to (* 2 cfengine-indent
)))
299 ;; Indent by one step if we're after an action header.
301 (goto-char (point-min))
302 (looking-at "[[:alpha:]]+:[ \t]*$"))
303 (indent-line-to cfengine-indent
))
304 ;; Else don't indent.
306 (indent-line-to 0))))
307 ;; If initial point was within line's indentation,
308 ;; position after the indentation. Else stay at same point in text.
309 (if (> (- (point-max) pos
) (point))
310 (goto-char (- (point-max) pos
)))))
312 ;; This doesn't work too well in Emacs 21.2. See 22.1 development
314 (defun cfengine-fill-paragraph (&optional justify
)
315 "Fill `paragraphs' in Cfengine code."
317 (or (if (fboundp 'fill-comment-paragraph
)
318 (fill-comment-paragraph justify
) ; post Emacs 21.3
319 ;; else do nothing in a comment
320 (nth 4 (parse-partial-sexp (save-excursion
324 (let ((paragraph-start
325 ;; Include start of parenthesized block.
326 "\f\\|[ \t]*$\\|.*\(")
328 ;; Include action and class lines, start and end of
329 ;; bracketed blocks and end of parenthesized blocks to
330 ;; avoid including these in fill. This isn't ideal.
331 "[ \t\f]*$\\|.*#\\|.*[\){}]\\|\\s-*[[:alpha:]_().|!]+:")
332 fill-paragraph-function
)
333 (fill-paragraph justify
))
336 (defun cfengine3-beginning-of-defun ()
337 "`beginning-of-defun' function for Cfengine 3 mode.
338 Treats body/bundle blocks as defuns."
339 (unless (<= (current-column) (current-indentation))
341 (if (re-search-backward (concat "^[ \t]*" cfengine3-defuns-regex
"\\>") nil t
)
343 (goto-char (point-min)))
346 (defun cfengine3-end-of-defun ()
347 "`end-of-defun' function for Cfengine 3 mode.
348 Treats body/bundle blocks as defuns."
350 (if (re-search-forward (concat "^[ \t]*" cfengine3-defuns-regex
"\\>") nil t
)
352 (goto-char (point-max)))
355 (defun cfengine3-indent-line ()
356 "Indent a line in Cfengine 3 mode.
357 Intended as the value of `indent-line-function'."
358 (let ((pos (- (point-max) (point)))
362 (back-to-indentation)
363 (setq parse
(parse-partial-sexp (point-min) (point)))
364 (when cfengine-mode-debug
365 (message "%S" parse
))
368 ;; Body/bundle blocks start at 0.
369 ((looking-at (concat cfengine3-defuns-regex
"\\>"))
371 ;; Categories are indented one step.
372 ((looking-at (concat cfengine3-category-regex
"[ \t]*\\(#.*\\)*$"))
373 (indent-line-to cfengine-indent
))
374 ;; Class selectors are indented two steps.
375 ((looking-at (concat cfengine3-class-selector-regex
"[ \t]*\\(#.*\\)*$"))
376 (indent-line-to (* 2 cfengine-indent
)))
377 ;; Outdent leading close brackets one step.
378 ((or (eq ?\
} (char-after))
379 (eq ?\
) (char-after)))
381 (indent-line-to (save-excursion
384 (move-beginning-of-line nil
)
385 (skip-chars-forward " \t")
388 ;; Inside a string and it starts before this line.
390 (< (nth 8 parse
) (save-excursion (beginning-of-line) (point))))
393 ;; Inside a defun, but not a nested list (depth is 1). This is
394 ;; a promise, usually.
396 ;; Indent to cfengine-indent times the nested depth
397 ;; plus 2. That way, promises indent deeper than class
398 ;; selectors, which in turn are one deeper than categories.
400 (let ((p-anchor (nth 0 cfengine-parameters-indent
))
401 (p-what (nth 1 cfengine-parameters-indent
))
402 (p-indent (nth 2 cfengine-parameters-indent
)))
403 ;; Do we have the parameter anchor and location and indent
404 ;; defined, and are we looking at a promise parameter?
405 (if (and p-anchor p-what p-indent
406 (looking-at "\\([[:alnum:]_]+[ \t]*\\)=>"))
407 (let* ((arrow-offset (* -
1 (length (match-string 1))))
408 (extra-offset (if (eq p-what
'arrow
) arrow-offset
0))
409 (base-offset (if (eq p-anchor
'promise
)
410 (* (+ 2 (nth 0 parse
)) cfengine-indent
)
412 (indent-line-to (max 0 (+ p-indent base-offset extra-offset
))))
413 ;; Else, indent to cfengine-indent times the nested depth
414 ;; plus 2. That way, promises indent deeper than class
415 ;; selectors, which in turn are one deeper than categories.
416 (indent-line-to (* (+ 2 (nth 0 parse
)) cfengine-indent
)))))
417 ;; Inside brackets/parens: indent to start column of non-comment
418 ;; token on line following open bracket or by one step from open
421 (progn (indent-line-to (save-excursion
424 (skip-chars-forward " \t")
426 ((looking-at "[^\n#]")
428 ((looking-at "[^\n#]")
431 (skip-chars-backward " \t")
432 (+ (current-column) -
1
436 ;; Else don't indent.
437 (t (indent-line-to 0))))
438 ;; If initial point was within line's indentation,
439 ;; position after the indentation. Else stay at same point in text.
440 (if (> (- (point-max) pos
) (point))
441 (goto-char (- (point-max) pos
)))))
443 ;; CFEngine 3.x grammar
445 ;; specification: blocks
446 ;; blocks: block | blocks block;
447 ;; block: bundle typeid blockid bundlebody
448 ;; | bundle typeid blockid usearglist bundlebody
449 ;; | body typeid blockid bodybody
450 ;; | body typeid blockid usearglist bodybody;
454 ;; usearglist: '(' aitems ')';
455 ;; aitems: aitem | aitem ',' aitems |;
458 ;; bundlebody: '{' statements '}'
459 ;; statements: statement | statements statement;
460 ;; statement: category | classpromises;
462 ;; bodybody: '{' bodyattribs '}'
463 ;; bodyattribs: bodyattrib | bodyattribs bodyattrib;
464 ;; bodyattrib: class | selections;
465 ;; selections: selection | selections selection;
466 ;; selection: id ASSIGN rval ';' ;
468 ;; classpromises: classpromise | classpromises classpromise;
469 ;; classpromise: class | promises;
470 ;; promises: promise | promises promise;
471 ;; category: CATEGORY
472 ;; promise: promiser ARROW rval constraints ';' | promiser constraints ';';
473 ;; constraints: constraint | constraints ',' constraint |;
474 ;; constraint: id ASSIGN rval;
477 ;; rval: ID | QSTRING | NAKEDVAR | list | usefunction
478 ;; list: '{' litems '}' ;
479 ;; litems: litem | litem ',' litems |;
480 ;; litem: ID | QSTRING | NAKEDVAR | list | usefunction
482 ;; functionid: ID | NAKEDVAR
484 ;; usefunction: functionid givearglist
485 ;; givearglist: '(' gaitems ')'
486 ;; gaitems: gaitem | gaitems ',' gaitem |;
487 ;; gaitem: ID | QSTRING | NAKEDVAR | list | usefunction
494 ;; NAKEDVAR [$@][(][a-zA-Z0-9_\200-\377.]+[)]|[$@][{][a-zA-Z0-9_\200-\377.]+[}]
495 ;; ID: [a-zA-Z0-9_\200-\377]+
498 ;; QSTRING: \"((\\\")|[^"])*\"|\'((\\\')|[^'])*\'|`[^`]*`
499 ;; CLASS: [.|&!()a-zA-Z0-9_\200-\377]+::
500 ;; CATEGORY: [a-zA-Z_]+:
502 (defun cfengine-common-settings ()
503 (set (make-local-variable 'syntax-propertize-function
)
504 ;; In the main syntax-table, \ is marked as a punctuation, because
505 ;; of its use in DOS-style directory separators. Here we try to
506 ;; recognize the cases where \ is used as an escape inside strings.
507 (syntax-propertize-rules ("\\(\\(?:\\\\\\)+\\)\"" (1 "\\"))))
508 (set (make-local-variable 'parens-require-spaces
) nil
)
509 (set (make-local-variable 'comment-start
) "# ")
510 (set (make-local-variable 'comment-start-skip
)
511 "\\(\\(?:^\\|[^\\\\\n]\\)\\(?:\\\\\\\\\\)*\\)#+[ \t]*")
512 ;; Like Lisp mode. Without this, we lose with, say,
513 ;; `backward-up-list' when there's an unbalanced quote in a
514 ;; preceding comment.
515 (set (make-local-variable 'parse-sexp-ignore-comments
) t
))
517 (defun cfengine-common-syntax (table)
518 ;; The syntax defaults seem OK to give reasonable word movement.
519 (modify-syntax-entry ?
# "<" table
)
520 (modify-syntax-entry ?
\n ">#" table
)
521 (modify-syntax-entry ?
\" "\"" table
) ; "string"
522 (modify-syntax-entry ?
\' "\"" table
) ; 'string'
523 ;; Variable substitution.
524 (modify-syntax-entry ?$
"." table
)
525 ;; Doze path separators.
526 (modify-syntax-entry ?
\\ "." table
))
529 (define-derived-mode cfengine3-mode prog-mode
"CFE3"
530 "Major mode for editing CFEngine3 input.
531 There are no special keybindings by default.
533 Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves
534 to the action header."
535 (cfengine-common-settings)
536 (cfengine-common-syntax cfengine3-mode-syntax-table
)
538 (set (make-local-variable 'indent-line-function
) #'cfengine3-indent-line
)
539 (setq font-lock-defaults
540 '(cfengine3-font-lock-keywords nil nil nil beginning-of-defun
))
542 ;; Use defuns as the essential syntax block.
543 (set (make-local-variable 'beginning-of-defun-function
)
544 #'cfengine3-beginning-of-defun
)
545 (set (make-local-variable 'end-of-defun-function
)
546 #'cfengine3-end-of-defun
))
549 (define-derived-mode cfengine2-mode prog-mode
"CFE2"
550 "Major mode for editing CFEngine2 input.
551 There are no special keybindings by default.
553 Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves
554 to the action header."
555 (cfengine-common-settings)
556 (cfengine-common-syntax cfengine2-mode-syntax-table
)
558 ;; Shell commands can be quoted by single, double or back quotes.
559 ;; It's debatable whether we should define string syntax, but it
560 ;; should avoid potential confusion in some cases.
561 (modify-syntax-entry ?\
` "\"" cfengine2-mode-syntax-table
)
563 (set (make-local-variable 'indent-line-function
) #'cfengine2-indent-line
)
564 (set (make-local-variable 'outline-regexp
) "[ \t]*\\(\\sw\\|\\s_\\)+:+")
565 (set (make-local-variable 'outline-level
) #'cfengine2-outline-level
)
566 (set (make-local-variable 'fill-paragraph-function
)
567 #'cfengine-fill-paragraph
)
568 (define-abbrev-table 'cfengine2-mode-abbrev-table cfengine-mode-abbrevs
)
569 (setq font-lock-defaults
570 '(cfengine2-font-lock-keywords nil nil nil beginning-of-line
))
571 ;; Fixme: set the args of functions in evaluated classes to string
572 ;; syntax, and then obey syntax properties.
573 (setq imenu-generic-expression cfengine2-imenu-expression
)
574 (set (make-local-variable 'beginning-of-defun-function
)
575 #'cfengine2-beginning-of-defun
)
576 (set (make-local-variable 'end-of-defun-function
) #'cfengine2-end-of-defun
))
579 (defun cfengine-auto-mode ()
580 "Choose between `cfengine2-mode' and `cfengine3-mode' depending
581 on the buffer contents"
584 (goto-char (point-min))
585 (while (not (or (eobp) v3
))
586 (setq v3
(looking-at (concat cfengine3-defuns-regex
"\\>")))
588 (if v3
(cfengine3-mode) (cfengine2-mode))))
590 (defalias 'cfengine-mode
'cfengine3-mode
)
595 ;;; cfengine.el ends here