1;;; cfengine.el --- mode for editing Cfengine files 2 3;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 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. 69This 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(defconst cfengine-font-lock-syntactic-keywords 89 ;; In the main syntax-table, backslash is marked as a punctuation, because 90 ;; of its use in DOS-style directory separators. Here we try to recognize 91 ;; the cases where backslash is used as an escape inside strings. 92 '(("\\(\\(?:\\\\\\)+\\)\"" 1 "\\"))) 93 94(defvar cfengine-imenu-expression 95 `((nil ,(concat "^[ \t]*" (eval-when-compile 96 (regexp-opt cfengine-actions t)) 97 ":[^:]") 98 1) 99 ("Variables/classes" "\\<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1) 100 ("Variables/classes" "\\<define=\\([[:alnum:]_]+\\)" 1) 101 ("Variables/classes" "\\<DefineClass\\>[ \t]+\\([[:alnum:]_]+\\)" 1)) 102 "`imenu-generic-expression' for Cfengine mode.") 103 104(defun cfengine-outline-level () 105 "`outline-level' function for Cfengine mode." 106 (if (looking-at "[^:]+\\(?:[:]+\\)$") 107 (length (match-string 1)))) 108 109(defun cfengine-beginning-of-defun () 110 "`beginning-of-defun' function for Cfengine mode. 111Treats actions as defuns." 112 (unless (<= (current-column) (current-indentation)) 113 (end-of-line)) 114 (if (re-search-backward "^[[:alpha:]]+: *$" nil t) 115 (beginning-of-line) 116 (goto-char (point-min))) 117 t) 118 119(defun cfengine-end-of-defun () 120 "`end-of-defun' function for Cfengine mode. 121Treats actions as defuns." 122 (end-of-line) 123 (if (re-search-forward "^[[:alpha:]]+: *$" nil t) 124 (beginning-of-line) 125 (goto-char (point-max))) 126 t) 127 128;; Fixme: Should get an extra indent step in editfiles BeginGroup...s. 129 130(defun cfengine-indent-line () 131 "Indent a line in Cfengine mode. 132Intended as the value of `indent-line-function'." 133 (let ((pos (- (point-max) (point)))) 134 (save-restriction 135 (narrow-to-defun) 136 (back-to-indentation) 137 (cond 138 ;; Action selectors aren't indented; class selectors are 139 ;; indented one step. 140 ((looking-at "[[:alnum:]_().|!]+:\\(:\\)?") 141 (if (match-string 1) 142 (indent-line-to cfengine-indent) 143 (indent-line-to 0))) 144 ;; Outdent leading close brackets one step. 145 ((or (eq ?\} (char-after)) 146 (eq ?\) (char-after))) 147 (condition-case () 148 (indent-line-to (save-excursion 149 (forward-char) 150 (backward-sexp) 151 (current-column))) 152 (error nil))) 153 ;; Inside brackets/parens: indent to start column of non-comment 154 ;; token on line following open bracket or by one step from open 155 ;; bracket's column. 156 ((condition-case () 157 (progn (indent-line-to (save-excursion 158 (backward-up-list) 159 (forward-char) 160 (skip-chars-forward " \t") 161 (if (looking-at "[^\n#]") 162 (current-column) 163 (skip-chars-backward " \t") 164 (+ (current-column) -1 165 cfengine-indent)))) 166 t) 167 (error nil))) 168 ;; Indent by two steps after a class selector. 169 ((save-excursion 170 (re-search-backward "^[ \t]*[[:alnum:]_().|!]+::" nil t)) 171 (indent-line-to (* 2 cfengine-indent))) 172 ;; Indent by one step if we're after an action header. 173 ((save-excursion 174 (goto-char (point-min)) 175 (looking-at "[[:alpha:]]+:[ \t]*$")) 176 (indent-line-to cfengine-indent)) 177 ;; Else don't indent. 178 (t 179 (indent-line-to 0)))) 180 ;; If initial point was within line's indentation, 181 ;; position after the indentation. Else stay at same point in text. 182 (if (> (- (point-max) pos) (point)) 183 (goto-char (- (point-max) pos))))) 184 185;; This doesn't work too well in Emacs 21.2. See 22.1 development 186;; code. 187(defun cfengine-fill-paragraph (&optional justify) 188 "Fill `paragraphs' in Cfengine code." 189 (interactive "P") 190 (or (if (fboundp 'fill-comment-paragraph) 191 (fill-comment-paragraph justify) ; post Emacs 21.3 192 ;; else do nothing in a comment 193 (nth 4 (parse-partial-sexp (save-excursion 194 (beginning-of-defun) 195 (point)) 196 (point)))) 197 (let ((paragraph-start 198 ;; Include start of parenthesized block. 199 "\f\\|[ \t]*$\\|.*\(") 200 (paragraph-separate 201 ;; Include action and class lines, start and end of 202 ;; bracketed blocks and end of parenthesized blocks to 203 ;; avoid including these in fill. This isn't ideal. 204 "[ \t\f]*$\\|.*#\\|.*[\){}]\\|\\s-*[[:alpha:]_().|!]+:") 205 fill-paragraph-function) 206 (fill-paragraph justify)) 207 t)) 208 209;;;###autoload 210(define-derived-mode cfengine-mode fundamental-mode "Cfengine" 211 "Major mode for editing cfengine input. 212There are no special keybindings by default. 213 214Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves 215to the action header." 216 (modify-syntax-entry ?# "<" cfengine-mode-syntax-table) 217 (modify-syntax-entry ?\n ">#" cfengine-mode-syntax-table) 218 ;; Shell commands can be quoted by single, double or back quotes. 219 ;; It's debatable whether we should define string syntax, but it 220 ;; should avoid potential confusion in some cases. 221 (modify-syntax-entry ?\" "\"" cfengine-mode-syntax-table) 222 (modify-syntax-entry ?\' "\"" cfengine-mode-syntax-table) 223 (modify-syntax-entry ?\` "\"" cfengine-mode-syntax-table) 224 ;; variable substitution: 225 (modify-syntax-entry ?$ "." cfengine-mode-syntax-table) 226 ;; Doze path separators: 227 (modify-syntax-entry ?\\ "." cfengine-mode-syntax-table) 228 ;; Otherwise, syntax defaults seem OK to give reasonable word 229 ;; movement. 230 231 (set (make-local-variable 'parens-require-spaces) nil) 232 (set (make-local-variable 'require-final-newline) mode-require-final-newline) 233 (set (make-local-variable 'comment-start) "# ") 234 (set (make-local-variable 'comment-start-skip) 235 "\\(\\(?:^\\|[^\\\\\n]\\)\\(?:\\\\\\\\\\)*\\)#+[ \t]*") 236 (set (make-local-variable 'indent-line-function) #'cfengine-indent-line) 237 (set (make-local-variable 'outline-regexp) "[ \t]*\\(\\sw\\|\\s_\\)+:+") 238 (set (make-local-variable 'outline-level) #'cfengine-outline-level) 239 (set (make-local-variable 'fill-paragraph-function) 240 #'cfengine-fill-paragraph) 241 (define-abbrev-table 'cfengine-mode-abbrev-table cfengine-mode-abbrevs) 242 ;; Fixme: Use `font-lock-syntactic-keywords' to set the args of 243 ;; functions in evaluated classes to string syntax, and then obey 244 ;; syntax properties. 245 (setq font-lock-defaults 246 '(cfengine-font-lock-keywords nil nil nil beginning-of-line 247 (font-lock-syntactic-keywords 248 . cfengine-font-lock-syntactic-keywords))) 249 (setq imenu-generic-expression cfengine-imenu-expression) 250 (set (make-local-variable 'beginning-of-defun-function) 251 #'cfengine-beginning-of-defun) 252 (set (make-local-variable 'end-of-defun-function) #'cfengine-end-of-defun) 253 ;; Like Lisp mode. Without this, we lose with, say, 254 ;; `backward-up-list' when there's an unbalanced quote in a 255 ;; preceding comment. 256 (set (make-local-variable 'parse-sexp-ignore-comments) t)) 257 258(provide 'cfengine) 259 260;; arch-tag: 6b931be2-1505-4124-afa6-9675971e26d4 261;;; cfengine.el ends here 262