]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cfengine.el
(calc-embedded-make-info): Use `math-read-expr' when
[gnu-emacs] / lisp / progmodes / cfengine.el
1 ;;; cfengine.el --- mode for editing Cfengine files
2
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Dave Love <fx@gnu.org>
7 ;; Keywords: languages
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; Provides support for editing GNU Cfengine files, including
29 ;; font-locking, Imenu and indention, but with no special keybindings.
30
31 ;; Possible customization for auto-mode selection:
32 ;; (push '(("^cfagent.conf\\'" . cfengine-mode)) auto-mode-alist)
33 ;; (push '(("^cf\\." . cfengine-mode)) auto-mode-alist)
34
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.
38
39 ;;; Code:
40
41 (defgroup cfengine ()
42 "Editing Cfengine files."
43 :group 'languages)
44
45 (defcustom cfengine-indent 2
46 "*Size of a Cfengine indentation step in columns."
47 :group 'cfengine
48 :type 'integer)
49
50 (defcustom cfengine-mode-abbrevs nil
51 "Abbrevs for Cfengine mode."
52 :group 'cfengine
53 :type '(repeat (list (string :tag "Name")
54 (string :tag "Expansion")
55 (choice :tag "Hook" (const nil) function))))
56
57 ;; Taken from the doc for pre-release 2.1.
58 (eval-and-compile
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"
66 ;; cfservd
67 "admit" "grant" "deny")
68 "List of the action keywords supported by Cfengine.
69 This includes those for cfservd as well as cfagent."))
70
71 (defvar cfengine-font-lock-keywords
72 `(;; Actions.
73 ;; List the allowed actions explicitly, so that errors are more obvious.
74 (,(concat "^[ \t]*" (eval-when-compile
75 (regexp-opt cfengine-actions t))
76 ":")
77 1 font-lock-keyword-face)
78 ;; Classes.
79 ("^[ \t]*\\([[:alnum:]_().|!]+\\)::" 1 font-lock-function-name-face)
80 ;; Variables.
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)))
87
88 (defvar cfengine-imenu-expression
89 `((nil ,(concat "^[ \t]*" (eval-when-compile
90 (regexp-opt cfengine-actions t))
91 ":[^:]")
92 1)
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.")
97
98 (defun cfengine-outline-level ()
99 "`outline-level' function for Cfengine mode."
100 (if (looking-at "[^:]+\\(?:[:]+\\)$")
101 (length (match-string 1))))
102
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))
107 (end-of-line))
108 (if (re-search-backward "^[[:alpha:]]+: *$" nil t)
109 (beginning-of-line)
110 (goto-char (point-min)))
111 t)
112
113 (defun cfengine-end-of-defun ()
114 "`end-of-defun' function for Cfengine mode.
115 Treats actions as defuns."
116 (end-of-line)
117 (if (re-search-forward "^[[:alpha:]]+: *$" nil t)
118 (beginning-of-line)
119 (goto-char (point-max)))
120 t)
121
122 ;; Fixme: Should get an extra indent step in editfiles BeginGroup...s.
123
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))))
128 (save-restriction
129 (narrow-to-defun)
130 (back-to-indentation)
131 (cond
132 ;; Action selectors aren't indented; class selectors are
133 ;; indented one step.
134 ((looking-at "[[:alnum:]_().|!]+:\\(:\\)?")
135 (if (match-string 1)
136 (indent-line-to cfengine-indent)
137 (indent-line-to 0)))
138 ;; Outdent leading close brackets one step.
139 ((or (eq ?\} (char-after))
140 (eq ?\) (char-after)))
141 (condition-case ()
142 (indent-line-to (save-excursion
143 (forward-char)
144 (backward-sexp)
145 (current-column)))
146 (error nil)))
147 ;; Inside brackets/parens: indent to start column of non-comment
148 ;; token on line following open bracket or by one step from open
149 ;; bracket's column.
150 ((condition-case ()
151 (progn (indent-line-to (save-excursion
152 (backward-up-list)
153 (forward-char)
154 (skip-chars-forward " \t")
155 (if (looking-at "[^\n#]")
156 (current-column)
157 (skip-chars-backward " \t")
158 (+ (current-column) -1
159 cfengine-indent))))
160 t)
161 (error nil)))
162 ;; Indent by two steps after a class selector.
163 ((save-excursion
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.
167 ((save-excursion
168 (goto-char (point-min))
169 (looking-at "[[:alpha:]]+:[ \t]*$"))
170 (indent-line-to cfengine-indent))
171 ;; Else don't indent.
172 (t
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)))))
178
179 ;; This doesn't work too well in Emacs 21.2. See 22.1 development
180 ;; code.
181 (defun cfengine-fill-paragraph (&optional justify)
182 "Fill `paragraphs' in Cfengine code."
183 (interactive "P")
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
188 (beginning-of-defun)
189 (point))
190 (point))))
191 (let ((paragraph-start
192 ;; Include start of parenthesized block.
193 "\f\\|[ \t]*$\\|.*\(")
194 (paragraph-separate
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))
201 t))
202
203 ;;;###autoload
204 (define-derived-mode cfengine-mode fundamental-mode "Cfengine"
205 "Major mode for editing cfengine input.
206 There are no special keybindings by default.
207
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
223 ;; movement.
224
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))
249
250 (provide 'cfengine)
251
252 ;;; arch-tag: 6b931be2-1505-4124-afa6-9675971e26d4
253 ;;; cfengine.el ends here