Sync to HEAD
[bpt/emacs.git] / lisp / progmodes / cfengine.el
1 ;;; cfengine.el --- mode for editing Cfengine files
2
3 ;; Copyright (C) 2003 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 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.
68 This 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.
104 Treats actions as defuns."
105 (end-of-line)
106 (if (re-search-backward "^[[:alpha:]]+: *$" nil t)
107 (beginning-of-line)
108 (goto-char (point-min)))
109 t)
110
111 (defun cfengine-end-of-defun ()
112 "`end-of-defun' function for Cfengine mode.
113 Treats actions as defuns."
114 (end-of-line)
115 (if (re-search-forward "^[[:alpha:]]+: *$" nil t)
116 (progn (forward-line -1) (end-of-line))
117 (goto-char (point-max)))
118 t)
119
120 ;; Fixme: Should get an extra indent step in editfiles BeginGroup...s.
121
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))))
126 (save-restriction
127 (narrow-to-defun)
128 (back-to-indentation)
129 (cond
130 ;; Action selectors aren't indented; class selectors are
131 ;; indented one step.
132 ((looking-at "[[:alnum:]_().|!]+:\\(:\\)?")
133 (if (match-string 1)
134 (indent-line-to cfengine-indent)
135 (indent-line-to 0)))
136 ;; Outdent leading close brackets one step.
137 ((or (eq ?\} (char-after))
138 (eq ?\) (char-after)))
139 (condition-case ()
140 (indent-line-to (save-excursion
141 (forward-char)
142 (backward-sexp)
143 (current-column)))
144 (error nil)))
145 ;; Inside brackets/parens: indent to start column of non-comment
146 ;; token on line following open bracket or by one step from open
147 ;; bracket's column.
148 ((condition-case ()
149 (progn (indent-line-to (save-excursion
150 (backward-up-list)
151 (forward-char)
152 (skip-chars-forward " \t")
153 (if (looking-at "[^\n#]")
154 (current-column)
155 (skip-chars-backward " \t")
156 (+ (current-column) -1
157 cfengine-indent))))
158 t)
159 (error nil)))
160 ;; Indent by two steps after a class selector.
161 ((save-excursion
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.
165 ((save-excursion
166 (goto-char (point-min))
167 (looking-at "[[:alpha:]]+:[ \t]*$"))
168 (indent-line-to cfengine-indent))
169 ;; Else don't indent.
170 (t
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)))))
176
177 ;; This doesn't work too well in Emacs 21.2. See 21.4 development
178 ;; code.
179 (defun cfengine-fill-paragraph (&optional justify)
180 "Fill `paragraphs' in Cfengine code."
181 (interactive "P")
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
186 (beginning-of-defun)
187 (point))
188 (point))))
189 (let ((paragraph-start
190 ;; Include start of parenthesized block.
191 "\f\\|[ \t]*$\\|.*\(")
192 (paragraph-separate
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))
199 t))
200
201 ;;;###autoload
202 (define-derived-mode cfengine-mode fundamental-mode "Cfengine"
203 "Major mode for editing cfengine input.
204 There are no special keybindings by default.
205
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
221 ;; movement.
222
223 (set (make-local-variable 'parens-require-spaces) nil)
224 (set (make-local-variable 'require-final-newline) t)
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 ;; Fixme: Use `font-lock-syntactic-keywords' to set the args of
235 ;; functions in evaluated classes to string syntax, and then obey
236 ;; syntax properties.
237 (setq font-lock-defaults
238 '(cfengine-font-lock-keywords nil nil nil beginning-of-line))
239 (setq imenu-generic-expression cfengine-imenu-expression)
240 (set (make-local-variable 'beginning-of-defun-function)
241 #'cfengine-beginning-of-defun)
242 (set (make-local-variable 'end-of-defun-function) #'cfengine-end-of-defun)
243 ;; Like Lisp mode. Without this, we lose with, say,
244 ;; `backward-up-list' when there's an unbalanced quote in a
245 ;; preceding comment.
246 (set (make-local-variable 'parse-sexp-ignore-comments) t))
247
248 (provide 'cfengine)
249
250 ;;; arch-tag: 6b931be2-1505-4124-afa6-9675971e26d4
251 ;;; cfengine.el ends here