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 ;; It's *highly* recommended that you enable the eldoc minor mode:
50 ;; (add-hook 'cfengine-mode-hook 'turn-on-eldoc-mode)
52 ;; This is not the same as the mode written by Rolf Ebert
53 ;; <ebert@waporo.muc.de>, distributed with cfengine-2.0.5. It does
54 ;; better fontification and indentation, inter alia.
59 "Editing CFEngine files."
62 (defcustom cfengine-indent 2
63 "Size of a CFEngine indentation step in columns."
67 (defcustom cfengine-cf-promises
68 (or (executable-find "cf-promises")
69 (executable-find "/var/cfengine/bin/cf-promises")
70 (executable-find "/usr/bin/cf-promises")
71 (executable-find "/usr/local/bin/cf-promises")
72 (executable-find "~/bin/cf-promises"))
73 "The location of the cf-promises executable.
74 Used for syntax discovery and checking. Set to nil to disable
75 the `compile-command' override and the ElDoc support."
79 (defcustom cfengine-parameters-indent '(promise pname 0)
80 "*Indentation of CFEngine3 promise parameters (hanging indent).
82 For example, say you have this code:
89 promiseparameter => ...
92 You can choose to indent promiseparameter from the beginning of
93 the line (absolutely) or from the word \"promise\" (relatively).
95 You can also choose to indent the start of the word
96 \"promiseparameter\" or the arrow that follows it.
98 Finally, you can choose the amount of the indent.
100 The default is to anchor at promise, indent parameter name, and offset 0:
107 comment => \"my netrc\",
108 perms => mog(\"600\", \"tzz\", \"tzz\");
111 Here we anchor at beginning of line, indent arrow, and offset 10:
118 comment => \"my netrc\",
119 perms => mog(\"600\", \"tzz\", \"tzz\");
122 Some, including cfengine_stdlib.cf, like to anchor at promise, indent
123 arrow, and offset 16 or so:
130 comment => \"my netrc\",
131 perms => mog(\"600\", \"tzz\", \"tzz\");
137 (choice (const :tag "Anchor at beginning of promise" promise)
138 (const :tag "Anchor at beginning of line" bol))
139 (choice (const :tag "Indent parameter name" pname)
140 (const :tag "Indent arrow" arrow))
141 (integer :tag "Indentation amount from anchor")))
143 (defvar cfengine-mode-debug nil
144 "Whether `cfengine-mode' should print debugging info.")
146 (defvar cfengine-mode-syntax-cache nil
147 "Cache for `cfengine-mode' syntax trees obtained from 'cf-promises -s json'.")
149 (defcustom cfengine-mode-abbrevs nil
150 "Abbrevs for CFEngine2 mode."
152 :type '(repeat (list (string :tag "Name")
153 (string :tag "Expansion")
154 (choice :tag "Hook" (const nil) function))))
156 (make-obsolete-variable 'cfengine-mode-abbrevs 'edit-abbrevs "24.1")
158 ;; Taken from the doc for pre-release 2.1.
160 (defconst cfengine2-actions
161 '("acl" "alerts" "binservers" "broadcast" "control" "classes" "copy"
162 "defaultroute" "disks" "directories" "disable" "editfiles" "files"
163 "filters" "groups" "homeservers" "ignore" "import" "interfaces"
164 "links" "mailserver" "methods" "miscmounts" "mountables"
165 "processes" "packages" "rename" "required" "resolve"
166 "shellcommands" "tidy" "unmount"
167 ;; Keywords for cfservd.
168 "admit" "grant" "deny")
169 "List of the action keywords supported by Cfengine.
170 This includes those for cfservd as well as cfagent.")
172 (defconst cfengine3-defuns
176 "List of the CFEngine 3.x defun headings.")
178 (defconst cfengine3-defuns-regex
179 (regexp-opt cfengine3-defuns t)
180 "Regex to match the CFEngine 3.x defuns.")
182 (defconst cfengine3-class-selector-regex "\\([[:alnum:]_().&|!:]+\\)::")
184 (defconst cfengine3-category-regex "\\([[:alnum:]_]+\\):")
186 (defconst cfengine3-vartypes
189 '(string int real slist ilist rlist irange rrange counter data))
190 "List of the CFEngine 3.x variable types."))
192 (defvar cfengine2-font-lock-keywords
194 ;; List the allowed actions explicitly, so that errors are more obvious.
195 (,(concat "^[ \t]*" (eval-when-compile
196 (regexp-opt cfengine2-actions t))
198 1 font-lock-keyword-face)
200 ("^[ \t]*\\([[:alnum:]_().|!]+\\)::" 1 font-lock-function-name-face)
202 ("$(\\([[:alnum:]_]+\\))" 1 font-lock-variable-name-face)
203 ("${\\([[:alnum:]_]+\\)}" 1 font-lock-variable-name-face)
204 ;; Variable definitions.
205 ("\\_<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1 font-lock-variable-name-face)
206 ;; File, acl &c in group: { token ... }
207 ("{[ \t]*\\([^ \t\n]+\\)" 1 font-lock-constant-face)))
209 (defvar cfengine3-font-lock-keywords
211 ;; Defuns. This happens early so they don't get caught by looser
213 (,(concat "\\_<" cfengine3-defuns-regex "\\_>"
214 "[ \t]+\\_<\\([[:alnum:]_.:]+\\)\\_>"
215 "[ \t]+\\_<\\([[:alnum:]_.:]+\\)"
216 ;; Optional parentheses with variable names inside.
217 "\\(?:(\\([^)]*\\))\\)?")
218 (1 font-lock-builtin-face)
219 (2 font-lock-constant-face)
220 (3 font-lock-function-name-face)
221 (4 font-lock-variable-name-face nil t))
224 (,(concat "^[ \t]*" cfengine3-class-selector-regex)
225 1 font-lock-keyword-face)
228 (,(concat "^[ \t]*" cfengine3-category-regex)
229 1 font-lock-builtin-face)
231 ;; Variables, including scope, e.g. module.var
232 ("[@$](\\([[:alnum:]_.:]+\\))" 1 font-lock-variable-name-face)
233 ("[@$]{\\([[:alnum:]_.:]+\\)}" 1 font-lock-variable-name-face)
235 ;; Variable definitions.
236 ("\\_<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1 font-lock-variable-name-face)
239 (,(concat "\\_<" (eval-when-compile (regexp-opt cfengine3-vartypes t)) "\\_>")
240 1 font-lock-type-face)))
242 (defvar cfengine2-imenu-expression
243 `((nil ,(concat "^[ \t]*" (eval-when-compile
244 (regexp-opt cfengine2-actions t))
247 ("Variables/classes" "\\_<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1)
248 ("Variables/classes" "\\_<define=\\([[:alnum:]_]+\\)" 1)
249 ("Variables/classes" "\\_<DefineClass\\>[ \t]+\\([[:alnum:]_]+\\)" 1))
250 "`imenu-generic-expression' for CFEngine mode.")
252 (defun cfengine2-outline-level ()
253 "`outline-level' function for CFEngine mode."
254 (if (looking-at "[^:]+\\(?:[:]+\\)$")
255 (length (match-string 1))))
257 (defun cfengine2-beginning-of-defun ()
258 "`beginning-of-defun' function for CFEngine mode.
259 Treats actions as defuns."
260 (unless (<= (current-column) (current-indentation))
262 (if (re-search-backward "^[[:alpha:]]+: *$" nil t)
264 (goto-char (point-min)))
267 (defun cfengine2-end-of-defun ()
268 "`end-of-defun' function for CFEngine mode.
269 Treats actions as defuns."
271 (if (re-search-forward "^[[:alpha:]]+: *$" nil t)
273 (goto-char (point-max)))
276 ;; Fixme: Should get an extra indent step in editfiles BeginGroup...s.
278 (defun cfengine2-indent-line ()
279 "Indent a line in Cfengine mode.
280 Intended as the value of `indent-line-function'."
281 (let ((pos (- (point-max) (point))))
284 (back-to-indentation)
286 ;; Action selectors aren't indented; class selectors are
287 ;; indented one step.
288 ((looking-at "[[:alnum:]_().|!]+:\\(:\\)?")
290 (indent-line-to cfengine-indent)
292 ;; Outdent leading close brackets one step.
293 ((or (eq ?\} (char-after))
294 (eq ?\) (char-after)))
296 (indent-line-to (save-excursion
301 ;; Inside brackets/parens: indent to start column of non-comment
302 ;; token on line following open bracket or by one step from open
305 (progn (indent-line-to (save-excursion
308 (skip-chars-forward " \t")
309 (if (looking-at "[^\n#]")
311 (skip-chars-backward " \t")
312 (+ (current-column) -1
316 ;; Indent by two steps after a class selector.
318 (re-search-backward "^[ \t]*[[:alnum:]_().|!]+::" nil t))
319 (indent-line-to (* 2 cfengine-indent)))
320 ;; Indent by one step if we're after an action header.
322 (goto-char (point-min))
323 (looking-at "[[:alpha:]]+:[ \t]*$"))
324 (indent-line-to cfengine-indent))
325 ;; Else don't indent.
327 (indent-line-to 0))))
328 ;; If initial point was within line's indentation,
329 ;; position after the indentation. Else stay at same point in text.
330 (if (> (- (point-max) pos) (point))
331 (goto-char (- (point-max) pos)))))
333 ;; This doesn't work too well in Emacs 21.2. See 22.1 development
335 (defun cfengine-fill-paragraph (&optional justify)
336 "Fill `paragraphs' in Cfengine code."
338 (or (if (fboundp 'fill-comment-paragraph)
339 (fill-comment-paragraph justify) ; post Emacs 21.3
340 ;; else do nothing in a comment
341 (nth 4 (parse-partial-sexp (save-excursion
345 (let ((paragraph-start
346 ;; Include start of parenthesized block.
347 "\f\\|[ \t]*$\\|.*\(")
349 ;; Include action and class lines, start and end of
350 ;; bracketed blocks and end of parenthesized blocks to
351 ;; avoid including these in fill. This isn't ideal.
352 "[ \t\f]*$\\|.*#\\|.*[\){}]\\|\\s-*[[:alpha:]_().|!]+:")
353 fill-paragraph-function)
354 (fill-paragraph justify))
357 (defun cfengine3-beginning-of-defun ()
358 "`beginning-of-defun' function for Cfengine 3 mode.
359 Treats body/bundle blocks as defuns."
360 (unless (<= (current-column) (current-indentation))
362 (if (re-search-backward (concat "^[ \t]*" cfengine3-defuns-regex "\\_>") nil t)
364 (goto-char (point-min)))
367 (defun cfengine3-end-of-defun ()
368 "`end-of-defun' function for Cfengine 3 mode.
369 Treats body/bundle blocks as defuns."
371 (if (re-search-forward (concat "^[ \t]*" cfengine3-defuns-regex "\\_>") nil t)
373 (goto-char (point-max)))
376 (defun cfengine3-indent-line ()
377 "Indent a line in Cfengine 3 mode.
378 Intended as the value of `indent-line-function'."
379 (let ((pos (- (point-max) (point)))
383 (back-to-indentation)
384 (setq parse (parse-partial-sexp (point-min) (point)))
385 (when cfengine-mode-debug
386 (message "%S" parse))
389 ;; Body/bundle blocks start at 0.
390 ((looking-at (concat cfengine3-defuns-regex "\\_>"))
392 ;; Categories are indented one step.
393 ((looking-at (concat cfengine3-category-regex "[ \t]*\\(#.*\\)*$"))
394 (indent-line-to cfengine-indent))
395 ;; Class selectors are indented two steps.
396 ((looking-at (concat cfengine3-class-selector-regex "[ \t]*\\(#.*\\)*$"))
397 (indent-line-to (* 2 cfengine-indent)))
398 ;; Outdent leading close brackets one step.
399 ((or (eq ?\} (char-after))
400 (eq ?\) (char-after)))
402 (indent-line-to (save-excursion
405 (move-beginning-of-line nil)
406 (skip-chars-forward " \t")
409 ;; Inside a string and it starts before this line: do nothing.
411 (< (nth 8 parse) (save-excursion (beginning-of-line) (point))))
414 ;; Inside a defun, but not a nested list (depth is 1). This is
415 ;; a promise, usually.
417 ;; Indent to cfengine-indent times the nested depth
418 ;; plus 2. That way, promises indent deeper than class
419 ;; selectors, which in turn are one deeper than categories.
421 (let ((p-anchor (nth 0 cfengine-parameters-indent))
422 (p-what (nth 1 cfengine-parameters-indent))
423 (p-indent (nth 2 cfengine-parameters-indent)))
424 ;; Do we have the parameter anchor and location and indent
425 ;; defined, and are we looking at a promise parameter?
426 (if (and p-anchor p-what p-indent
427 (looking-at "\\([[:alnum:]_]+[ \t]*\\)=>"))
428 (let* ((arrow-offset (* -1 (length (match-string 1))))
429 (extra-offset (if (eq p-what 'arrow) arrow-offset 0))
430 (base-offset (if (eq p-anchor 'promise)
431 (* (+ 2 (nth 0 parse)) cfengine-indent)
433 (indent-line-to (max 0 (+ p-indent base-offset extra-offset))))
434 ;; Else, indent to cfengine-indent times the nested depth
435 ;; plus 2. That way, promises indent deeper than class
436 ;; selectors, which in turn are one deeper than categories.
437 (indent-line-to (* (+ 2 (nth 0 parse)) cfengine-indent)))))
438 ;; Inside brackets/parens: indent to start column of non-comment
439 ;; token on line following open bracket or by one step from open
442 (progn (indent-line-to (save-excursion
445 (skip-chars-forward " \t")
447 ((looking-at "[^\n#]")
449 ((looking-at "[^\n#]")
452 (skip-chars-backward " \t")
453 (+ (current-column) -1
457 ;; Else don't indent.
458 (t (indent-line-to 0))))
459 ;; If initial point was within line's indentation,
460 ;; position after the indentation. Else stay at same point in text.
461 (if (> (- (point-max) pos) (point))
462 (goto-char (- (point-max) pos)))))
464 ;; CFEngine 3.x grammar
466 ;; specification: blocks
467 ;; blocks: block | blocks block;
468 ;; block: bundle typeid blockid bundlebody
469 ;; | bundle typeid blockid usearglist bundlebody
470 ;; | body typeid blockid bodybody
471 ;; | body typeid blockid usearglist bodybody;
475 ;; usearglist: '(' aitems ')';
476 ;; aitems: aitem | aitem ',' aitems |;
479 ;; bundlebody: '{' statements '}'
480 ;; statements: statement | statements statement;
481 ;; statement: category | classpromises;
483 ;; bodybody: '{' bodyattribs '}'
484 ;; bodyattribs: bodyattrib | bodyattribs bodyattrib;
485 ;; bodyattrib: class | selections;
486 ;; selections: selection | selections selection;
487 ;; selection: id ASSIGN rval ';' ;
489 ;; classpromises: classpromise | classpromises classpromise;
490 ;; classpromise: class | promises;
491 ;; promises: promise | promises promise;
492 ;; category: CATEGORY
493 ;; promise: promiser ARROW rval constraints ';' | promiser constraints ';';
494 ;; constraints: constraint | constraints ',' constraint |;
495 ;; constraint: id ASSIGN rval;
498 ;; rval: ID | QSTRING | NAKEDVAR | list | usefunction
499 ;; list: '{' litems '}' ;
500 ;; litems: litem | litem ',' litems |;
501 ;; litem: ID | QSTRING | NAKEDVAR | list | usefunction
503 ;; functionid: ID | NAKEDVAR
505 ;; usefunction: functionid givearglist
506 ;; givearglist: '(' gaitems ')'
507 ;; gaitems: gaitem | gaitems ',' gaitem |;
508 ;; gaitem: ID | QSTRING | NAKEDVAR | list | usefunction
515 ;; NAKEDVAR [$@][(][a-zA-Z0-9_\200-\377.]+[)]|[$@][{][a-zA-Z0-9_\200-\377.]+[}]
516 ;; ID: [a-zA-Z0-9_\200-\377]+
519 ;; QSTRING: \"((\\\")|[^"])*\"|\'((\\\')|[^'])*\'|`[^`]*`
520 ;; CLASS: [.|&!()a-zA-Z0-9_\200-\377]+::
521 ;; CATEGORY: [a-zA-Z_]+:
523 (defun cfengine3--current-word (&optional bounds)
524 "Propose a word around point in the current CFEngine 3 buffer."
525 (let ((c (char-after (point)))
527 (when (not (nth 3 s)) ; not inside a string
530 (let ((oldpoint (point))
532 (skip-syntax-backward "w_") (setq start (point))
534 (skip-syntax-forward "w_") (setq end (point))
535 (when (not (and (eq start oldpoint)
537 (list start (point)))))
539 (memq (char-syntax c) '(?_ ?w))
542 (defun cfengine3--current-function ()
543 "Look up current CFEngine 3 function"
544 (let* ((syntax (assoc cfengine-cf-promises cfengine-mode-syntax-cache))
545 (flist (assoc 'functions syntax)))
547 (let ((w (cfengine3--current-word)))
548 (and w (assq (intern w) flist))))))
550 ;; format from "cf-promises -s json", e.g. "sort" function:
551 ;; ((category . "data")
552 ;; (variadic . :json-false)
553 ;; (parameters . [((range . "[a-zA-Z0-9_$(){}\\[\\].:]+") (type . "string"))
554 ;; ((range . "lex,int,real,IP,ip,MAC,mac") (type . "option"))])
555 ;; (returnType . "slist")
556 ;; (status . "normal"))
558 (defun cfengine3-format-function-docstring (fdef)
559 (let* ((f (format "%s" (car-safe fdef)))
561 (rtype (cdr (assq 'returnType def)))
562 (plist (cdr (assq 'parameters def)))
563 (has-some-parameters (> (length plist) 0))
564 (variadic (eq t (cdr (assq 'variadic def)))))
566 ;; (format "[%S]%s %s(%s%s)" def
567 (format "%s %s(%s%s)"
569 (propertize rtype 'face 'font-lock-variable-name-face)
571 (propertize f 'face 'font-lock-function-name-face)
572 (mapconcat (lambda (p)
573 (let ((type (cdr (assq 'type p)))
574 (range (cdr (assq 'range p))))
576 ((not (stringp type)) "???type???")
577 ((not (stringp range)) "???range???")
578 ;; options are lists of possible keywords
579 ((equal type "option")
580 (propertize (concat "[" range "]")
582 'font-lock-keyword-face))
583 ;; anything else is a type name as a variable
586 'font-lock-variable-name-face)))))
590 (if has-some-parameters ", ..." "...")
593 (defun cfengine3-make-syntax-cache ()
594 "Build the CFEngine 3 syntax cache.
595 Calls `cfengine-cf-promises' with \"-s json\""
596 (when cfengine-cf-promises
597 (let ((loaded-json-lib (require 'json nil t))
598 (syntax (assoc cfengine-cf-promises cfengine-mode-syntax-cache)))
599 (if (not loaded-json-lib)
600 (message "JSON library could not be loaded!")
604 (call-process-shell-command cfengine-cf-promises
609 (goto-char (point-min))
610 (setq syntax (json-read))
611 (setq cfengine-mode-syntax-cache
612 (cons (cons cfengine-cf-promises syntax)
613 cfengine-mode-syntax-cache)))))))))
615 (defun cfengine3-documentation-function ()
616 "Document CFengine 3 functions around point.
617 Intended as the value of `eldoc-documentation-function', which
618 see. Use it by executing `turn-on-eldoc-mode'."
619 (cfengine3-make-syntax-cache)
620 (let ((fdef (cfengine3--current-function)))
622 (cfengine3-format-function-docstring fdef))))
624 (defun cfengine3-completion-function ()
625 "Return completions for function name around or before point."
626 (cfengine3-make-syntax-cache)
627 (let* ((bounds (cfengine3--current-word t))
628 (syntax (assoc cfengine-cf-promises cfengine-mode-syntax-cache))
629 (flist (assoc 'functions syntax)))
631 (append bounds (list (cdr flist))))))
633 (defun cfengine-common-settings ()
634 (set (make-local-variable 'syntax-propertize-function)
635 ;; In the main syntax-table, \ is marked as a punctuation, because
636 ;; of its use in DOS-style directory separators. Here we try to
637 ;; recognize the cases where \ is used as an escape inside strings.
638 (syntax-propertize-rules ("\\(\\(?:\\\\\\)+\\)\"" (1 "\\"))))
639 (set (make-local-variable 'parens-require-spaces) nil)
640 (set (make-local-variable 'comment-start) "# ")
641 (set (make-local-variable 'comment-start-skip)
642 "\\(\\(?:^\\|[^\\\\\n]\\)\\(?:\\\\\\\\\\)*\\)#+[ \t]*")
643 ;; Like Lisp mode. Without this, we lose with, say,
644 ;; `backward-up-list' when there's an unbalanced quote in a
645 ;; preceding comment.
646 (set (make-local-variable 'parse-sexp-ignore-comments) t))
648 (defun cfengine-common-syntax (table)
649 ;; The syntax defaults seem OK to give reasonable word movement.
650 (modify-syntax-entry ?# "<" table)
651 (modify-syntax-entry ?\n ">#" table)
652 (modify-syntax-entry ?\" "\"" table) ; "string"
653 (modify-syntax-entry ?\' "\"" table) ; 'string'
654 ;; Variable substitution.
655 (modify-syntax-entry ?$ "." table)
656 ;; Doze path separators.
657 (modify-syntax-entry ?\\ "." table))
659 (defconst cfengine3--prettify-symbols-alist
665 (define-derived-mode cfengine3-mode prog-mode "CFE3"
666 "Major mode for editing CFEngine3 input.
667 There are no special keybindings by default.
669 Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves
670 to the action header."
671 (cfengine-common-settings)
672 (cfengine-common-syntax cfengine3-mode-syntax-table)
674 (set (make-local-variable 'indent-line-function) #'cfengine3-indent-line)
676 (setq font-lock-defaults
677 '(cfengine3-font-lock-keywords
678 nil nil nil beginning-of-defun))
679 (setq-local prettify-symbols-alist cfengine3--prettify-symbols-alist)
681 ;; `compile-command' is almost never a `make' call with CFEngine so
683 (when cfengine-cf-promises
684 (set (make-local-variable 'compile-command)
685 (concat cfengine-cf-promises
687 (when buffer-file-name
688 (shell-quote-argument buffer-file-name)))))
690 (set (make-local-variable 'eldoc-documentation-function)
691 #'cfengine3-documentation-function)
693 (add-hook 'completion-at-point-functions
694 #'cfengine3-completion-function nil t)
696 ;; Use defuns as the essential syntax block.
697 (set (make-local-variable 'beginning-of-defun-function)
698 #'cfengine3-beginning-of-defun)
699 (set (make-local-variable 'end-of-defun-function)
700 #'cfengine3-end-of-defun))
703 (define-derived-mode cfengine2-mode prog-mode "CFE2"
704 "Major mode for editing CFEngine2 input.
705 There are no special keybindings by default.
707 Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves
708 to the action header."
709 (cfengine-common-settings)
710 (cfengine-common-syntax cfengine2-mode-syntax-table)
712 ;; Shell commands can be quoted by single, double or back quotes.
713 ;; It's debatable whether we should define string syntax, but it
714 ;; should avoid potential confusion in some cases.
715 (modify-syntax-entry ?\` "\"" cfengine2-mode-syntax-table)
717 (set (make-local-variable 'indent-line-function) #'cfengine2-indent-line)
718 (set (make-local-variable 'outline-regexp) "[ \t]*\\(\\sw\\|\\s_\\)+:+")
719 (set (make-local-variable 'outline-level) #'cfengine2-outline-level)
720 (set (make-local-variable 'fill-paragraph-function)
721 #'cfengine-fill-paragraph)
722 (define-abbrev-table 'cfengine2-mode-abbrev-table cfengine-mode-abbrevs)
723 (setq font-lock-defaults
724 '(cfengine2-font-lock-keywords nil nil nil beginning-of-line))
725 ;; Fixme: set the args of functions in evaluated classes to string
726 ;; syntax, and then obey syntax properties.
727 (setq imenu-generic-expression cfengine2-imenu-expression)
728 (set (make-local-variable 'beginning-of-defun-function)
729 #'cfengine2-beginning-of-defun)
730 (set (make-local-variable 'end-of-defun-function) #'cfengine2-end-of-defun))
733 (defun cfengine-auto-mode ()
734 "Choose between `cfengine2-mode' and `cfengine3-mode' depending
735 on the buffer contents"
738 (goto-char (point-min))
739 (while (not (or (eobp) v3))
740 (setq v3 (looking-at (concat cfengine3-defuns-regex "\\_>")))
742 (if v3 (cfengine3-mode) (cfengine2-mode))))
744 (defalias 'cfengine-mode 'cfengine3-mode)
749 ;;; cfengine.el ends here