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