1 ;;; cfengine.el --- mode for editing Cfengine files
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Provides support for editing GNU Cfengine files, including
29 ;; font-locking, Imenu and indention, but with no special keybindings.
31 ;; Possible customization for auto-mode selection:
32 ;; (push '(("^cfagent.conf\\'" . cfengine-mode)) auto-mode-alist)
33 ;; (push '(("^cf\\." . cfengine-mode)) auto-mode-alist)
35 ;; This is not the same as the mode written by Rolf Ebert
36 ;; <ebert@waporo.muc.de>, distributed with cfengine-2.0.5. It does
37 ;; better fontification and indentation, inter alia.
42 "Editing Cfengine files."
45 (defcustom cfengine-indent 2
46 "*Size of a Cfengine indentation step in columns."
50 (defcustom cfengine-mode-abbrevs nil
51 "Abbrevs for Cfengine mode."
53 :type '(repeat (list (string :tag "Name")
54 (string :tag "Expansion")
55 (choice :tag "Hook" (const nil) function))))
57 ;; Taken from the doc for pre-release 2.1.
59 (defconst cfengine-actions
60 '("acl" "alerts" "binservers" "broadcast" "control" "classes" "copy"
61 "defaultroute" "disks" "directories" "disable" "editfiles" "files"
62 "filters" "groups" "homeservers" "ignore" "import" "interfaces"
63 "links" "mailserver" "methods" "miscmounts" "mountables"
64 "processes" "packages" "rename" "required" "resolve"
65 "shellcommands" "tidy" "unmount"
67 "admit" "grant" "deny")
68 "List of the action keywords supported by Cfengine.
69 This includes those for cfservd as well as cfagent."))
71 (defvar cfengine-font-lock-keywords
73 ;; List the allowed actions explicitly, so that errors are more obvious.
74 (,(concat "^[ \t]*" (eval-when-compile
75 (regexp-opt cfengine-actions t))
77 1 font-lock-keyword-face)
79 ("^[ \t]*\\([[:alnum:]_().|!]+\\)::" 1 font-lock-function-name-face)
81 ("$(\\([[:alnum:]_]+\\))" 1 font-lock-variable-name-face)
82 ("${\\([[:alnum:]_]+\\)}" 1 font-lock-variable-name-face)
83 ;; Variable definitions.
84 ("\\<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1 font-lock-variable-name-face)
85 ;; File, acl &c in group: { token ... }
86 ("{[ \t]*\\([^ \t\n]+\\)" 1 font-lock-constant-face)))
88 (defvar cfengine-imenu-expression
89 `((nil ,(concat "^[ \t]*" (eval-when-compile
90 (regexp-opt cfengine-actions t))
93 ("Variables/classes" "\\<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1)
94 ("Variables/classes" "\\<define=\\([[:alnum:]_]+\\)" 1)
95 ("Variables/classes" "\\<DefineClass\\>[ \t]+\\([[:alnum:]_]+\\)" 1))
96 "`imenu-generic-expression' for Cfengine mode.")
98 (defun cfengine-outline-level ()
99 "`outline-level' function for Cfengine mode."
100 (if (looking-at "[^:]+\\(?:[:]+\\)$")
101 (length (match-string 1))))
103 (defun cfengine-beginning-of-defun ()
104 "`beginning-of-defun' function for Cfengine mode.
105 Treats actions as defuns."
106 (unless (<= (current-column) (current-indentation))
108 (if (re-search-backward "^[[:alpha:]]+: *$" nil t)
110 (goto-char (point-min)))
113 (defun cfengine-end-of-defun ()
114 "`end-of-defun' function for Cfengine mode.
115 Treats actions as defuns."
117 (if (re-search-forward "^[[:alpha:]]+: *$" nil t)
119 (goto-char (point-max)))
122 ;; Fixme: Should get an extra indent step in editfiles BeginGroup...s.
124 (defun cfengine-indent-line ()
125 "Indent a line in Cfengine mode.
126 Intended as the value of `indent-line-function'."
127 (let ((pos (- (point-max) (point))))
130 (back-to-indentation)
132 ;; Action selectors aren't indented; class selectors are
133 ;; indented one step.
134 ((looking-at "[[:alnum:]_().|!]+:\\(:\\)?")
136 (indent-line-to cfengine-indent)
138 ;; Outdent leading close brackets one step.
139 ((or (eq ?\} (char-after))
140 (eq ?\) (char-after)))
142 (indent-line-to (save-excursion
147 ;; Inside brackets/parens: indent to start column of non-comment
148 ;; token on line following open bracket or by one step from open
151 (progn (indent-line-to (save-excursion
154 (skip-chars-forward " \t")
155 (if (looking-at "[^\n#]")
157 (skip-chars-backward " \t")
158 (+ (current-column) -1
162 ;; Indent by two steps after a class selector.
164 (re-search-backward "^[ \t]*[[:alnum:]_().|!]+::" nil t))
165 (indent-line-to (* 2 cfengine-indent)))
166 ;; Indent by one step if we're after an action header.
168 (goto-char (point-min))
169 (looking-at "[[:alpha:]]+:[ \t]*$"))
170 (indent-line-to cfengine-indent))
171 ;; Else don't indent.
173 (indent-line-to 0))))
174 ;; If initial point was within line's indentation,
175 ;; position after the indentation. Else stay at same point in text.
176 (if (> (- (point-max) pos) (point))
177 (goto-char (- (point-max) pos)))))
179 ;; This doesn't work too well in Emacs 21.2. See 22.1 development
181 (defun cfengine-fill-paragraph (&optional justify)
182 "Fill `paragraphs' in Cfengine code."
184 (or (if (fboundp 'fill-comment-paragraph)
185 (fill-comment-paragraph justify) ; post Emacs 21.3
186 ;; else do nothing in a comment
187 (nth 4 (parse-partial-sexp (save-excursion
191 (let ((paragraph-start
192 ;; Include start of parenthesized block.
193 "\f\\|[ \t]*$\\|.*\(")
195 ;; Include action and class lines, start and end of
196 ;; bracketed blocks and end of parenthesized blocks to
197 ;; avoid including these in fill. This isn't ideal.
198 "[ \t\f]*$\\|.*#\\|.*[\){}]\\|\\s-*[[:alpha:]_().|!]+:")
199 fill-paragraph-function)
200 (fill-paragraph justify))
204 (define-derived-mode cfengine-mode fundamental-mode "Cfengine"
205 "Major mode for editing cfengine input.
206 There are no special keybindings by default.
208 Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves
209 to the action header."
210 (modify-syntax-entry ?# "<" cfengine-mode-syntax-table)
211 (modify-syntax-entry ?\n ">#" cfengine-mode-syntax-table)
212 ;; Shell commands can be quoted by single, double or back quotes.
213 ;; It's debatable whether we should define string syntax, but it
214 ;; should avoid potential confusion in some cases.
215 (modify-syntax-entry ?\" "\"" cfengine-mode-syntax-table)
216 (modify-syntax-entry ?\' "\"" cfengine-mode-syntax-table)
217 (modify-syntax-entry ?\` "\"" cfengine-mode-syntax-table)
218 ;; variable substitution:
219 (modify-syntax-entry ?$ "." cfengine-mode-syntax-table)
220 ;; Doze path separators:
221 (modify-syntax-entry ?\\ "_" cfengine-mode-syntax-table)
222 ;; Otherwise, syntax defaults seem OK to give reasonable word
225 (set (make-local-variable 'parens-require-spaces) nil)
226 (set (make-local-variable 'require-final-newline) mode-require-final-newline)
227 (set (make-local-variable 'comment-start) "# ")
228 (set (make-local-variable 'comment-start-skip)
229 "\\(\\(?:^\\|[^\\\\\n]\\)\\(?:\\\\\\\\\\)*\\)#+[ \t]*")
230 (set (make-local-variable 'indent-line-function) #'cfengine-indent-line)
231 (set (make-local-variable 'outline-regexp) "[ \t]*\\(\\sw\\|\\s_\\)+:+")
232 (set (make-local-variable 'outline-level) #'cfengine-outline-level)
233 (set (make-local-variable 'fill-paragraph-function)
234 #'cfengine-fill-paragraph)
235 (define-abbrev-table 'cfengine-mode-abbrev-table cfengine-mode-abbrevs)
236 ;; Fixme: Use `font-lock-syntactic-keywords' to set the args of
237 ;; functions in evaluated classes to string syntax, and then obey
238 ;; syntax properties.
239 (setq font-lock-defaults
240 '(cfengine-font-lock-keywords nil nil nil beginning-of-line))
241 (setq imenu-generic-expression cfengine-imenu-expression)
242 (set (make-local-variable 'beginning-of-defun-function)
243 #'cfengine-beginning-of-defun)
244 (set (make-local-variable 'end-of-defun-function) #'cfengine-end-of-defun)
245 ;; Like Lisp mode. Without this, we lose with, say,
246 ;; `backward-up-list' when there's an unbalanced quote in a
247 ;; preceding comment.
248 (set (make-local-variable 'parse-sexp-ignore-comments) t))
252 ;;; arch-tag: 6b931be2-1505-4124-afa6-9675971e26d4
253 ;;; cfengine.el ends here