1 ;;; cfengine.el --- mode for editing Cfengine files
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Dave Love <fx@gnu.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Provides support for editing GNU Cfengine files, including
27 ;; font-locking, Imenu and indention, but with no special keybindings.
29 ;; Possible customization for auto-mode selection:
30 ;; (push '(("^cfagent.conf\\'" . cfengine-mode)) auto-mode-alist)
31 ;; (push '(("^cf\\." . cfengine-mode)) auto-mode-alist)
33 ;; This is not the same as the mode written by Rolf Ebert
34 ;; <ebert@waporo.muc.de>, distributed with cfengine-2.0.5. It does
35 ;; better fontification and indentation, inter alia.
40 "Editing Cfengine files."
43 (defcustom cfengine-indent
2
44 "*Size of a Cfengine indentation step in columns."
48 (defcustom cfengine-mode-abbrevs nil
49 "Abbrevs for Cfengine mode."
51 :type
'(repeat (list (string :tag
"Name")
52 (string :tag
"Expansion")
53 (choice :tag
"Hook" (const nil
) function
))))
55 ;; Taken from the doc for pre-release 2.1.
57 (defconst cfengine-actions
58 '("acl" "alerts" "binservers" "broadcast" "control" "classes" "copy"
59 "defaultroute" "disks" "directories" "disable" "editfiles" "files"
60 "filters" "groups" "homeservers" "ignore" "import" "interfaces"
61 "links" "mailserver" "methods" "miscmounts" "mountables"
62 "processes" "packages" "rename" "required" "resolve"
63 "shellcommands" "tidy" "unmount"
65 "admit" "grant" "deny")
66 "List of the action keywords supported by Cfengine.
67 This includes those for cfservd as well as cfagent."))
69 (defvar cfengine-font-lock-keywords
71 ;; List the allowed actions explicitly, so that errors are more obvious.
72 (,(concat "^[ \t]*" (eval-when-compile
73 (regexp-opt cfengine-actions t
))
75 1 font-lock-keyword-face
)
77 ("^[ \t]*\\([[:alnum:]_().|!]+\\)::" 1 font-lock-function-name-face
)
79 ("$(\\([[:alnum:]_]+\\))" 1 font-lock-variable-name-face
)
80 ("${\\([[:alnum:]_]+\\)}" 1 font-lock-variable-name-face
)
81 ;; Variable definitions.
82 ("\\<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1 font-lock-variable-name-face
)
83 ;; File, acl &c in group: { token ... }
84 ("{[ \t]*\\([^ \t\n]+\\)" 1 font-lock-constant-face
)))
86 (defvar cfengine-imenu-expression
87 `((nil ,(concat "^[ \t]*" (eval-when-compile
88 (regexp-opt cfengine-actions t
))
91 ("Variables/classes" "\\<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1)
92 ("Variables/classes" "\\<define=\\([[:alnum:]_]+\\)" 1)
93 ("Variables/classes" "\\<DefineClass\\>[ \t]+\\([[:alnum:]_]+\\)" 1))
94 "`imenu-generic-expression' for Cfengine mode.")
96 (defun cfengine-outline-level ()
97 "`outline-level' function for Cfengine mode."
98 (if (looking-at "[^:]+\\(?:[:]+\\)$")
99 (length (match-string 1))))
101 (defun cfengine-beginning-of-defun ()
102 "`beginning-of-defun' function for Cfengine mode.
103 Treats actions as defuns."
104 (unless (<= (current-column) (current-indentation))
106 (if (re-search-backward "^[[:alpha:]]+: *$" nil t
)
108 (goto-char (point-min)))
111 (defun cfengine-end-of-defun ()
112 "`end-of-defun' function for Cfengine mode.
113 Treats actions as defuns."
115 (if (re-search-forward "^[[:alpha:]]+: *$" nil t
)
117 (goto-char (point-max)))
120 ;; Fixme: Should get an extra indent step in editfiles BeginGroup...s.
122 (defun cfengine-indent-line ()
123 "Indent a line in Cfengine mode.
124 Intended as the value of `indent-line-function'."
125 (let ((pos (- (point-max) (point))))
128 (back-to-indentation)
130 ;; Action selectors aren't indented; class selectors are
131 ;; indented one step.
132 ((looking-at "[[:alnum:]_().|!]+:\\(:\\)?")
134 (indent-line-to cfengine-indent
)
136 ;; Outdent leading close brackets one step.
137 ((or (eq ?\
} (char-after))
138 (eq ?\
) (char-after)))
140 (indent-line-to (save-excursion
145 ;; Inside brackets/parens: indent to start column of non-comment
146 ;; token on line following open bracket or by one step from open
149 (progn (indent-line-to (save-excursion
152 (skip-chars-forward " \t")
153 (if (looking-at "[^\n#]")
155 (skip-chars-backward " \t")
156 (+ (current-column) -
1
160 ;; Indent by two steps after a class selector.
162 (re-search-backward "^[ \t]*[[:alnum:]_().|!]+::" nil t
))
163 (indent-line-to (* 2 cfengine-indent
)))
164 ;; Indent by one step if we're after an action header.
166 (goto-char (point-min))
167 (looking-at "[[:alpha:]]+:[ \t]*$"))
168 (indent-line-to cfengine-indent
))
169 ;; Else don't indent.
171 (indent-line-to 0))))
172 ;; If initial point was within line's indentation,
173 ;; position after the indentation. Else stay at same point in text.
174 (if (> (- (point-max) pos
) (point))
175 (goto-char (- (point-max) pos
)))))
177 ;; This doesn't work too well in Emacs 21.2. See 22.1 development
179 (defun cfengine-fill-paragraph (&optional justify
)
180 "Fill `paragraphs' in Cfengine code."
182 (or (if (fboundp 'fill-comment-paragraph
)
183 (fill-comment-paragraph justify
) ; post Emacs 21.3
184 ;; else do nothing in a comment
185 (nth 4 (parse-partial-sexp (save-excursion
189 (let ((paragraph-start
190 ;; Include start of parenthesized block.
191 "\f\\|[ \t]*$\\|.*\(")
193 ;; Include action and class lines, start and end of
194 ;; bracketed blocks and end of parenthesized blocks to
195 ;; avoid including these in fill. This isn't ideal.
196 "[ \t\f]*$\\|.*#\\|.*[\){}]\\|\\s-*[[:alpha:]_().|!]+:")
197 fill-paragraph-function
)
198 (fill-paragraph justify
))
202 (define-derived-mode cfengine-mode fundamental-mode
"Cfengine"
203 "Major mode for editing cfengine input.
204 There are no special keybindings by default.
206 Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves
207 to the action header."
208 (modify-syntax-entry ?
# "<" cfengine-mode-syntax-table
)
209 (modify-syntax-entry ?
\n ">#" cfengine-mode-syntax-table
)
210 ;; Shell commands can be quoted by single, double or back quotes.
211 ;; It's debatable whether we should define string syntax, but it
212 ;; should avoid potential confusion in some cases.
213 (modify-syntax-entry ?
\" "\"" cfengine-mode-syntax-table
)
214 (modify-syntax-entry ?
\' "\"" cfengine-mode-syntax-table
)
215 (modify-syntax-entry ?\
` "\"" cfengine-mode-syntax-table
)
216 ;; variable substitution:
217 (modify-syntax-entry ?$
"." cfengine-mode-syntax-table
)
218 ;; Doze path separators:
219 (modify-syntax-entry ?
\\ "." cfengine-mode-syntax-table
)
220 ;; Otherwise, syntax defaults seem OK to give reasonable word
223 (set (make-local-variable 'parens-require-spaces
) nil
)
224 (set (make-local-variable 'require-final-newline
) mode-require-final-newline
)
225 (set (make-local-variable 'comment-start
) "# ")
226 (set (make-local-variable 'comment-start-skip
)
227 "\\(\\(?:^\\|[^\\\\\n]\\)\\(?:\\\\\\\\\\)*\\)#+[ \t]*")
228 (set (make-local-variable 'indent-line-function
) #'cfengine-indent-line
)
229 (set (make-local-variable 'outline-regexp
) "[ \t]*\\(\\sw\\|\\s_\\)+:+")
230 (set (make-local-variable 'outline-level
) #'cfengine-outline-level
)
231 (set (make-local-variable 'fill-paragraph-function
)
232 #'cfengine-fill-paragraph
)
233 (define-abbrev-table 'cfengine-mode-abbrev-table cfengine-mode-abbrevs
)
234 (setq font-lock-defaults
235 '(cfengine-font-lock-keywords nil nil nil beginning-of-line
))
236 ;; Fixme: set the args of functions in evaluated classes to string
237 ;; syntax, and then obey syntax properties.
238 (set (make-local-variable 'syntax-propertize-function
)
239 ;; In the main syntax-table, \ is marked as a punctuation, because
240 ;; of its use in DOS-style directory separators. Here we try to
241 ;; recognize the cases where \ is used as an escape inside strings.
242 (syntax-propertize-rules ("\\(\\(?:\\\\\\)+\\)\"" (1 "\\"))))
243 (setq imenu-generic-expression cfengine-imenu-expression
)
244 (set (make-local-variable 'beginning-of-defun-function
)
245 #'cfengine-beginning-of-defun
)
246 (set (make-local-variable 'end-of-defun-function
) #'cfengine-end-of-defun
)
247 ;; Like Lisp mode. Without this, we lose with, say,
248 ;; `backward-up-list' when there's an unbalanced quote in a
249 ;; preceding comment.
250 (set (make-local-variable 'parse-sexp-ignore-comments
) t
))
254 ;; arch-tag: 6b931be2-1505-4124-afa6-9675971e26d4
255 ;;; cfengine.el ends here