Change release version from 21.4 to 22.1 throughout.
[bpt/emacs.git] / lisp / progmodes / cfengine.el
CommitLineData
5467ec88
DL
1;;; cfengine.el --- mode for editing Cfengine files
2
7d5f942b 3;; Copyright (C) 2003, 2004 Free Software Foundation, Inc.
5467ec88
DL
4
5;; Author: Dave Love <fx@gnu.org>
6;; Keywords: languages
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
25;;; Commentary:
26
27;; Provides support for editing GNU Cfengine files, including
28;; font-locking, Imenu and indention, but with no special keybindings.
29
30;; Possible customization for auto-mode selection:
31;; (push '(("^cfagent.conf\\'" . cfengine-mode)) auto-mode-alist)
32;; (push '(("^cf\\." . cfengine-mode)) auto-mode-alist)
33
34;; This is not the same as the mode written by Rolf Ebert
35;; <ebert@waporo.muc.de>, distributed with cfengine-2.0.5. It does
36;; better fontification and indentation, inter alia.
37
38;;; Code:
39
40(defgroup cfengine ()
41 "Editing Cfengine files."
42 :group 'languages)
43
44(defcustom cfengine-indent 2
45 "*Size of a Cfengine indentation step in columns."
46 :group 'cfengine
47 :type 'integer)
48
49(defcustom cfengine-mode-abbrevs nil
50 "Abbrevs for Cfengine mode."
51 :group 'cfengine
52 :type '(repeat (list (string :tag "Name")
53 (string :tag "Expansion")
54 (choice :tag "Hook" (const nil) function))))
55
56;; Taken from the doc for pre-release 2.1.
57(eval-and-compile
58 (defconst cfengine-actions
59 '("acl" "alerts" "binservers" "broadcast" "control" "classes" "copy"
60 "defaultroute" "disks" "directories" "disable" "editfiles" "files"
61 "filters" "groups" "homeservers" "ignore" "import" "interfaces"
62 "links" "mailserver" "methods" "miscmounts" "mountables"
63 "processes" "packages" "rename" "required" "resolve"
64 "shellcommands" "tidy" "unmount"
65 ;; cfservd
66 "admit" "grant" "deny")
67 "List of the action keywords supported by Cfengine.
68This includes those for cfservd as well as cfagent."))
69
70(defvar cfengine-font-lock-keywords
71 `(;; Actions.
72 ;; List the allowed actions explicitly, so that errors are more obvious.
73 (,(concat "^[ \t]*" (eval-when-compile
74 (regexp-opt cfengine-actions t))
75 ":")
76 1 font-lock-keyword-face)
77 ;; Classes.
78 ("^[ \t]*\\([[:alnum:]_().|!]+\\)::" 1 font-lock-function-name-face)
79 ;; Variables.
80 ("$(\\([[:alnum:]_]+\\))" 1 font-lock-variable-name-face)
81 ("${\\([[:alnum:]_]+\\)}" 1 font-lock-variable-name-face)
82 ;; Variable definitions.
83 ("\\<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1 font-lock-variable-name-face)
84 ;; File, acl &c in group: { token ... }
85 ("{[ \t]*\\([^ \t\n]+\\)" 1 font-lock-constant-face)))
86
87(defvar cfengine-imenu-expression
88 `((nil ,(concat "^[ \t]*" (eval-when-compile
89 (regexp-opt cfengine-actions t))
90 ":[^:]")
91 1)
92 ("Variables/classes" "\\<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1)
93 ("Variables/classes" "\\<define=\\([[:alnum:]_]+\\)" 1)
94 ("Variables/classes" "\\<DefineClass\\>[ \t]+\\([[:alnum:]_]+\\)" 1))
95 "`imenu-generic-expression' for Cfengine mode.")
96
97(defun cfengine-outline-level ()
98 "`outline-level' function for Cfengine mode."
99 (if (looking-at "[^:]+\\(?:[:]+\\)$")
100 (length (match-string 1))))
101
102(defun cfengine-beginning-of-defun ()
103 "`beginning-of-defun' function for Cfengine mode.
104Treats actions as defuns."
7d5f942b
SM
105 (unless (<= (current-column) (current-indentation))
106 (end-of-line))
5467ec88
DL
107 (if (re-search-backward "^[[:alpha:]]+: *$" nil t)
108 (beginning-of-line)
109 (goto-char (point-min)))
110 t)
111
112(defun cfengine-end-of-defun ()
113 "`end-of-defun' function for Cfengine mode.
114Treats actions as defuns."
115 (end-of-line)
116 (if (re-search-forward "^[[:alpha:]]+: *$" nil t)
7d5f942b 117 (beginning-of-line)
5467ec88
DL
118 (goto-char (point-max)))
119 t)
120
121;; Fixme: Should get an extra indent step in editfiles BeginGroup...s.
122
123(defun cfengine-indent-line ()
124 "Indent a line in Cfengine mode.
125Intended as the value of `indent-line-function'."
126 (let ((pos (- (point-max) (point))))
127 (save-restriction
128 (narrow-to-defun)
129 (back-to-indentation)
130 (cond
131 ;; Action selectors aren't indented; class selectors are
132 ;; indented one step.
133 ((looking-at "[[:alnum:]_().|!]+:\\(:\\)?")
134 (if (match-string 1)
135 (indent-line-to cfengine-indent)
136 (indent-line-to 0)))
137 ;; Outdent leading close brackets one step.
138 ((or (eq ?\} (char-after))
139 (eq ?\) (char-after)))
140 (condition-case ()
141 (indent-line-to (save-excursion
142 (forward-char)
143 (backward-sexp)
144 (current-column)))
145 (error nil)))
146 ;; Inside brackets/parens: indent to start column of non-comment
147 ;; token on line following open bracket or by one step from open
148 ;; bracket's column.
149 ((condition-case ()
150 (progn (indent-line-to (save-excursion
151 (backward-up-list)
152 (forward-char)
153 (skip-chars-forward " \t")
154 (if (looking-at "[^\n#]")
155 (current-column)
156 (skip-chars-backward " \t")
157 (+ (current-column) -1
158 cfengine-indent))))
159 t)
160 (error nil)))
161 ;; Indent by two steps after a class selector.
162 ((save-excursion
163 (re-search-backward "^[ \t]*[[:alnum:]_().|!]+::" nil t))
164 (indent-line-to (* 2 cfengine-indent)))
165 ;; Indent by one step if we're after an action header.
166 ((save-excursion
167 (goto-char (point-min))
168 (looking-at "[[:alpha:]]+:[ \t]*$"))
169 (indent-line-to cfengine-indent))
170 ;; Else don't indent.
171 (t
172 (indent-line-to 0))))
173 ;; If initial point was within line's indentation,
174 ;; position after the indentation. Else stay at same point in text.
175 (if (> (- (point-max) pos) (point))
176 (goto-char (- (point-max) pos)))))
177
bf247b6e 178;; This doesn't work too well in Emacs 21.2. See 22.1 development
5467ec88
DL
179;; code.
180(defun cfengine-fill-paragraph (&optional justify)
181 "Fill `paragraphs' in Cfengine code."
182 (interactive "P")
183 (or (if (fboundp 'fill-comment-paragraph)
184 (fill-comment-paragraph justify) ; post Emacs 21.3
185 ;; else do nothing in a comment
186 (nth 4 (parse-partial-sexp (save-excursion
187 (beginning-of-defun)
188 (point))
189 (point))))
190 (let ((paragraph-start
191 ;; Include start of parenthesized block.
192 "\f\\|[ \t]*$\\|.*\(")
193 (paragraph-separate
194 ;; Include action and class lines, start and end of
195 ;; bracketed blocks and end of parenthesized blocks to
196 ;; avoid including these in fill. This isn't ideal.
197 "[ \t\f]*$\\|.*#\\|.*[\){}]\\|\\s-*[[:alpha:]_().|!]+:")
198 fill-paragraph-function)
199 (fill-paragraph justify))
200 t))
201
202;;;###autoload
203(define-derived-mode cfengine-mode fundamental-mode "Cfengine"
204 "Major mode for editing cfengine input.
205There are no special keybindings by default.
206
207Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves
208to the action header."
209 (modify-syntax-entry ?# "<" cfengine-mode-syntax-table)
210 (modify-syntax-entry ?\n ">#" cfengine-mode-syntax-table)
211 ;; Shell commands can be quoted by single, double or back quotes.
212 ;; It's debatable whether we should define string syntax, but it
213 ;; should avoid potential confusion in some cases.
214 (modify-syntax-entry ?\" "\"" cfengine-mode-syntax-table)
215 (modify-syntax-entry ?\' "\"" cfengine-mode-syntax-table)
216 (modify-syntax-entry ?\` "\"" cfengine-mode-syntax-table)
217 ;; variable substitution:
218 (modify-syntax-entry ?$ "." cfengine-mode-syntax-table)
219 ;; Doze path separators:
220 (modify-syntax-entry ?\\ "_" cfengine-mode-syntax-table)
221 ;; Otherwise, syntax defaults seem OK to give reasonable word
222 ;; movement.
223
224 (set (make-local-variable 'parens-require-spaces) nil)
10902d69 225 (set (make-local-variable 'require-final-newline) mode-require-final-newline)
5467ec88
DL
226 (set (make-local-variable 'comment-start) "# ")
227 (set (make-local-variable 'comment-start-skip)
228 "\\(\\(?:^\\|[^\\\\\n]\\)\\(?:\\\\\\\\\\)*\\)#+[ \t]*")
229 (set (make-local-variable 'indent-line-function) #'cfengine-indent-line)
230 (set (make-local-variable 'outline-regexp) "[ \t]*\\(\\sw\\|\\s_\\)+:+")
231 (set (make-local-variable 'outline-level) #'cfengine-outline-level)
232 (set (make-local-variable 'fill-paragraph-function)
233 #'cfengine-fill-paragraph)
234 (define-abbrev-table 'cfengine-mode-abbrev-table cfengine-mode-abbrevs)
235 ;; Fixme: Use `font-lock-syntactic-keywords' to set the args of
236 ;; functions in evaluated classes to string syntax, and then obey
237 ;; syntax properties.
238 (setq font-lock-defaults
239 '(cfengine-font-lock-keywords nil nil nil beginning-of-line))
240 (setq imenu-generic-expression cfengine-imenu-expression)
241 (set (make-local-variable 'beginning-of-defun-function)
242 #'cfengine-beginning-of-defun)
f7da2617
JB
243 (set (make-local-variable 'end-of-defun-function) #'cfengine-end-of-defun)
244 ;; Like Lisp mode. Without this, we lose with, say,
245 ;; `backward-up-list' when there's an unbalanced quote in a
246 ;; preceding comment.
247 (set (make-local-variable 'parse-sexp-ignore-comments) t))
5467ec88
DL
248
249(provide 'cfengine)
250
7ae0df40 251;;; arch-tag: 6b931be2-1505-4124-afa6-9675971e26d4
5467ec88 252;;; cfengine.el ends here