cfengine.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. ;;; cfengine.el --- mode for editing Cfengine files
  2. ;; Copyright (C) 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Dave Love <fx@gnu.org>
  4. ;; Maintainer: Ted Zlatanov <tzz@lifelogs.com>
  5. ;; Keywords: languages
  6. ;; Version: 1.1
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Provides support for editing GNU Cfengine files, including
  20. ;; font-locking, Imenu and indentation, but with no special keybindings.
  21. ;; The CFEngine 3.x support doesn't have Imenu support but patches are
  22. ;; welcome.
  23. ;; You can set it up so either `cfengine2-mode' (2.x and earlier) or
  24. ;; `cfengine3-mode' (3.x) will be picked, depending on the buffer
  25. ;; contents:
  26. ;; (add-to-list 'auto-mode-alist '("\\.cf\\'" . cfengine-mode))
  27. ;; OR you can choose to always use a specific version, if you prefer
  28. ;; it:
  29. ;; (add-to-list 'auto-mode-alist '("\\.cf\\'" . cfengine3-mode))
  30. ;; (add-to-list 'auto-mode-alist '("^cf\\." . cfengine2-mode))
  31. ;; (add-to-list 'auto-mode-alist '("^cfagent.conf\\'" . cfengine2-mode))
  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. ;;; Code:
  36. (defgroup cfengine ()
  37. "Editing CFEngine files."
  38. :group 'languages)
  39. (defcustom cfengine-indent 2
  40. "*Size of a CFEngine indentation step in columns."
  41. :group 'cfengine
  42. :type 'integer)
  43. (defvar cfengine-mode-debug nil
  44. "Whether `cfengine-mode' should print debugging info.")
  45. (defcustom cfengine-mode-abbrevs nil
  46. "Abbrevs for CFEngine2 mode."
  47. :group 'cfengine
  48. :type '(repeat (list (string :tag "Name")
  49. (string :tag "Expansion")
  50. (choice :tag "Hook" (const nil) function))))
  51. (make-obsolete-variable 'cfengine-mode-abbrevs 'edit-abbrevs "24.1")
  52. ;; Taken from the doc for pre-release 2.1.
  53. (eval-and-compile
  54. (defconst cfengine2-actions
  55. '("acl" "alerts" "binservers" "broadcast" "control" "classes" "copy"
  56. "defaultroute" "disks" "directories" "disable" "editfiles" "files"
  57. "filters" "groups" "homeservers" "ignore" "import" "interfaces"
  58. "links" "mailserver" "methods" "miscmounts" "mountables"
  59. "processes" "packages" "rename" "required" "resolve"
  60. "shellcommands" "tidy" "unmount"
  61. ;; Keywords for cfservd.
  62. "admit" "grant" "deny")
  63. "List of the action keywords supported by Cfengine.
  64. This includes those for cfservd as well as cfagent.")
  65. (defconst cfengine3-defuns
  66. (mapcar
  67. 'symbol-name
  68. '(bundle body))
  69. "List of the CFEngine 3.x defun headings.")
  70. (defconst cfengine3-defuns-regex
  71. (regexp-opt cfengine3-defuns t)
  72. "Regex to match the CFEngine 3.x defuns.")
  73. (defconst cfengine3-class-selector-regex "\\([[:alnum:]_().&|!]+\\)::")
  74. (defconst cfengine3-category-regex "\\([[:alnum:]_]+\\):")
  75. (defconst cfengine3-vartypes
  76. (mapcar
  77. 'symbol-name
  78. '(string int real slist ilist rlist irange rrange counter))
  79. "List of the CFEngine 3.x variable types."))
  80. (defvar cfengine2-font-lock-keywords
  81. `(;; Actions.
  82. ;; List the allowed actions explicitly, so that errors are more obvious.
  83. (,(concat "^[ \t]*" (eval-when-compile
  84. (regexp-opt cfengine2-actions t))
  85. ":")
  86. 1 font-lock-keyword-face)
  87. ;; Classes.
  88. ("^[ \t]*\\([[:alnum:]_().|!]+\\)::" 1 font-lock-function-name-face)
  89. ;; Variables.
  90. ("$(\\([[:alnum:]_]+\\))" 1 font-lock-variable-name-face)
  91. ("${\\([[:alnum:]_]+\\)}" 1 font-lock-variable-name-face)
  92. ;; Variable definitions.
  93. ("\\<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1 font-lock-variable-name-face)
  94. ;; File, acl &c in group: { token ... }
  95. ("{[ \t]*\\([^ \t\n]+\\)" 1 font-lock-constant-face)))
  96. (defvar cfengine3-font-lock-keywords
  97. `(
  98. ;; Defuns. This happens early so they don't get caught by looser
  99. ;; patterns.
  100. (,(concat "\\<" cfengine3-defuns-regex "\\>"
  101. "[ \t]+\\<\\([[:alnum:]_]+\\)\\>"
  102. "[ \t]+\\<\\([[:alnum:]_]+\\)"
  103. ;; Optional parentheses with variable names inside.
  104. "\\(?:(\\([^)]*\\))\\)?")
  105. (1 font-lock-builtin-face)
  106. (2 font-lock-constant-face)
  107. (3 font-lock-function-name-face)
  108. (4 font-lock-variable-name-face nil t))
  109. ;; Class selectors.
  110. (,(concat "^[ \t]*" cfengine3-class-selector-regex)
  111. 1 font-lock-keyword-face)
  112. ;; Categories.
  113. (,(concat "^[ \t]*" cfengine3-category-regex)
  114. 1 font-lock-builtin-face)
  115. ;; Variables, including scope, e.g. module.var
  116. ("[@$](\\([[:alnum:]_.]+\\))" 1 font-lock-variable-name-face)
  117. ("[@$]{\\([[:alnum:]_.]+\\)}" 1 font-lock-variable-name-face)
  118. ;; Variable definitions.
  119. ("\\<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1 font-lock-variable-name-face)
  120. ;; Variable types.
  121. (,(concat "\\<" (eval-when-compile (regexp-opt cfengine3-vartypes t)) "\\>")
  122. 1 font-lock-type-face)))
  123. (defvar cfengine2-imenu-expression
  124. `((nil ,(concat "^[ \t]*" (eval-when-compile
  125. (regexp-opt cfengine2-actions t))
  126. ":[^:]")
  127. 1)
  128. ("Variables/classes" "\\<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1)
  129. ("Variables/classes" "\\<define=\\([[:alnum:]_]+\\)" 1)
  130. ("Variables/classes" "\\<DefineClass\\>[ \t]+\\([[:alnum:]_]+\\)" 1))
  131. "`imenu-generic-expression' for CFEngine mode.")
  132. (defun cfengine2-outline-level ()
  133. "`outline-level' function for CFEngine mode."
  134. (if (looking-at "[^:]+\\(?:[:]+\\)$")
  135. (length (match-string 1))))
  136. (defun cfengine2-beginning-of-defun ()
  137. "`beginning-of-defun' function for CFEngine mode.
  138. Treats actions as defuns."
  139. (unless (<= (current-column) (current-indentation))
  140. (end-of-line))
  141. (if (re-search-backward "^[[:alpha:]]+: *$" nil t)
  142. (beginning-of-line)
  143. (goto-char (point-min)))
  144. t)
  145. (defun cfengine2-end-of-defun ()
  146. "`end-of-defun' function for CFEngine mode.
  147. Treats actions as defuns."
  148. (end-of-line)
  149. (if (re-search-forward "^[[:alpha:]]+: *$" nil t)
  150. (beginning-of-line)
  151. (goto-char (point-max)))
  152. t)
  153. ;; Fixme: Should get an extra indent step in editfiles BeginGroup...s.
  154. (defun cfengine2-indent-line ()
  155. "Indent a line in Cfengine mode.
  156. Intended as the value of `indent-line-function'."
  157. (let ((pos (- (point-max) (point))))
  158. (save-restriction
  159. (narrow-to-defun)
  160. (back-to-indentation)
  161. (cond
  162. ;; Action selectors aren't indented; class selectors are
  163. ;; indented one step.
  164. ((looking-at "[[:alnum:]_().|!]+:\\(:\\)?")
  165. (if (match-string 1)
  166. (indent-line-to cfengine-indent)
  167. (indent-line-to 0)))
  168. ;; Outdent leading close brackets one step.
  169. ((or (eq ?\} (char-after))
  170. (eq ?\) (char-after)))
  171. (condition-case ()
  172. (indent-line-to (save-excursion
  173. (forward-char)
  174. (backward-sexp)
  175. (current-column)))
  176. (error nil)))
  177. ;; Inside brackets/parens: indent to start column of non-comment
  178. ;; token on line following open bracket or by one step from open
  179. ;; bracket's column.
  180. ((condition-case ()
  181. (progn (indent-line-to (save-excursion
  182. (backward-up-list)
  183. (forward-char)
  184. (skip-chars-forward " \t")
  185. (if (looking-at "[^\n#]")
  186. (current-column)
  187. (skip-chars-backward " \t")
  188. (+ (current-column) -1
  189. cfengine-indent))))
  190. t)
  191. (error nil)))
  192. ;; Indent by two steps after a class selector.
  193. ((save-excursion
  194. (re-search-backward "^[ \t]*[[:alnum:]_().|!]+::" nil t))
  195. (indent-line-to (* 2 cfengine-indent)))
  196. ;; Indent by one step if we're after an action header.
  197. ((save-excursion
  198. (goto-char (point-min))
  199. (looking-at "[[:alpha:]]+:[ \t]*$"))
  200. (indent-line-to cfengine-indent))
  201. ;; Else don't indent.
  202. (t
  203. (indent-line-to 0))))
  204. ;; If initial point was within line's indentation,
  205. ;; position after the indentation. Else stay at same point in text.
  206. (if (> (- (point-max) pos) (point))
  207. (goto-char (- (point-max) pos)))))
  208. ;; This doesn't work too well in Emacs 21.2. See 22.1 development
  209. ;; code.
  210. (defun cfengine-fill-paragraph (&optional justify)
  211. "Fill `paragraphs' in Cfengine code."
  212. (interactive "P")
  213. (or (if (fboundp 'fill-comment-paragraph)
  214. (fill-comment-paragraph justify) ; post Emacs 21.3
  215. ;; else do nothing in a comment
  216. (nth 4 (parse-partial-sexp (save-excursion
  217. (beginning-of-defun)
  218. (point))
  219. (point))))
  220. (let ((paragraph-start
  221. ;; Include start of parenthesized block.
  222. "\f\\|[ \t]*$\\|.*\(")
  223. (paragraph-separate
  224. ;; Include action and class lines, start and end of
  225. ;; bracketed blocks and end of parenthesized blocks to
  226. ;; avoid including these in fill. This isn't ideal.
  227. "[ \t\f]*$\\|.*#\\|.*[\){}]\\|\\s-*[[:alpha:]_().|!]+:")
  228. fill-paragraph-function)
  229. (fill-paragraph justify))
  230. t))
  231. (defun cfengine3-beginning-of-defun ()
  232. "`beginning-of-defun' function for Cfengine 3 mode.
  233. Treats body/bundle blocks as defuns."
  234. (unless (<= (current-column) (current-indentation))
  235. (end-of-line))
  236. (if (re-search-backward (concat "^[ \t]*" cfengine3-defuns-regex "\\>") nil t)
  237. (beginning-of-line)
  238. (goto-char (point-min)))
  239. t)
  240. (defun cfengine3-end-of-defun ()
  241. "`end-of-defun' function for Cfengine 3 mode.
  242. Treats body/bundle blocks as defuns."
  243. (end-of-line)
  244. (if (re-search-forward (concat "^[ \t]*" cfengine3-defuns-regex "\\>") nil t)
  245. (beginning-of-line)
  246. (goto-char (point-max)))
  247. t)
  248. (defun cfengine3-indent-line ()
  249. "Indent a line in Cfengine 3 mode.
  250. Intended as the value of `indent-line-function'."
  251. (let ((pos (- (point-max) (point)))
  252. parse)
  253. (save-restriction
  254. (narrow-to-defun)
  255. (back-to-indentation)
  256. (setq parse (parse-partial-sexp (point-min) (point)))
  257. (when cfengine-mode-debug
  258. (message "%S" parse))
  259. (cond
  260. ;; Body/bundle blocks start at 0.
  261. ((looking-at (concat cfengine3-defuns-regex "\\>"))
  262. (indent-line-to 0))
  263. ;; Categories are indented one step.
  264. ((looking-at (concat cfengine3-category-regex "[ \t]*$"))
  265. (indent-line-to cfengine-indent))
  266. ;; Class selectors are indented two steps.
  267. ((looking-at (concat cfengine3-class-selector-regex "[ \t]*$"))
  268. (indent-line-to (* 2 cfengine-indent)))
  269. ;; Outdent leading close brackets one step.
  270. ((or (eq ?\} (char-after))
  271. (eq ?\) (char-after)))
  272. (condition-case ()
  273. (indent-line-to (save-excursion
  274. (forward-char)
  275. (backward-sexp)
  276. (current-column)))
  277. (error nil)))
  278. ;; Inside a string and it starts before this line.
  279. ((and (nth 3 parse)
  280. (< (nth 8 parse) (save-excursion (beginning-of-line) (point))))
  281. (indent-line-to 0))
  282. ;; Inside a defun, but not a nested list (depth is 1). This is
  283. ;; a promise, usually.
  284. ;; Indent to cfengine-indent times the nested depth
  285. ;; plus 2. That way, promises indent deeper than class
  286. ;; selectors, which in turn are one deeper than categories.
  287. ((= 1 (nth 0 parse))
  288. (indent-line-to (* (+ 2 (nth 0 parse)) cfengine-indent)))
  289. ;; Inside brackets/parens: indent to start column of non-comment
  290. ;; token on line following open bracket or by one step from open
  291. ;; bracket's column.
  292. ((condition-case ()
  293. (progn (indent-line-to (save-excursion
  294. (backward-up-list)
  295. (forward-char)
  296. (skip-chars-forward " \t")
  297. (cond
  298. ((looking-at "[^\n#]")
  299. (current-column))
  300. ((looking-at "[^\n#]")
  301. (current-column))
  302. (t
  303. (skip-chars-backward " \t")
  304. (+ (current-column) -1
  305. cfengine-indent)))))
  306. t)
  307. (error nil)))
  308. ;; Else don't indent.
  309. (t (indent-line-to 0))))
  310. ;; If initial point was within line's indentation,
  311. ;; position after the indentation. Else stay at same point in text.
  312. (if (> (- (point-max) pos) (point))
  313. (goto-char (- (point-max) pos)))))
  314. ;; CFEngine 3.x grammar
  315. ;; specification: blocks
  316. ;; blocks: block | blocks block;
  317. ;; block: bundle typeid blockid bundlebody
  318. ;; | bundle typeid blockid usearglist bundlebody
  319. ;; | body typeid blockid bodybody
  320. ;; | body typeid blockid usearglist bodybody;
  321. ;; typeid: id
  322. ;; blockid: id
  323. ;; usearglist: '(' aitems ')';
  324. ;; aitems: aitem | aitem ',' aitems |;
  325. ;; aitem: id
  326. ;; bundlebody: '{' statements '}'
  327. ;; statements: statement | statements statement;
  328. ;; statement: category | classpromises;
  329. ;; bodybody: '{' bodyattribs '}'
  330. ;; bodyattribs: bodyattrib | bodyattribs bodyattrib;
  331. ;; bodyattrib: class | selections;
  332. ;; selections: selection | selections selection;
  333. ;; selection: id ASSIGN rval ';' ;
  334. ;; classpromises: classpromise | classpromises classpromise;
  335. ;; classpromise: class | promises;
  336. ;; promises: promise | promises promise;
  337. ;; category: CATEGORY
  338. ;; promise: promiser ARROW rval constraints ';' | promiser constraints ';';
  339. ;; constraints: constraint | constraints ',' constraint |;
  340. ;; constraint: id ASSIGN rval;
  341. ;; class: CLASS
  342. ;; id: ID
  343. ;; rval: ID | QSTRING | NAKEDVAR | list | usefunction
  344. ;; list: '{' litems '}' ;
  345. ;; litems: litem | litem ',' litems |;
  346. ;; litem: ID | QSTRING | NAKEDVAR | list | usefunction
  347. ;; functionid: ID | NAKEDVAR
  348. ;; promiser: QSTRING
  349. ;; usefunction: functionid givearglist
  350. ;; givearglist: '(' gaitems ')'
  351. ;; gaitems: gaitem | gaitems ',' gaitem |;
  352. ;; gaitem: ID | QSTRING | NAKEDVAR | list | usefunction
  353. ;; # from lexer:
  354. ;; bundle: "bundle"
  355. ;; body: "body"
  356. ;; COMMENT #[^\n]*
  357. ;; NAKEDVAR [$@][(][a-zA-Z0-9_\200-\377.]+[)]|[$@][{][a-zA-Z0-9_\200-\377.]+[}]
  358. ;; ID: [a-zA-Z0-9_\200-\377]+
  359. ;; ASSIGN: "=>"
  360. ;; ARROW: "->"
  361. ;; QSTRING: \"((\\\")|[^"])*\"|\'((\\\')|[^'])*\'|`[^`]*`
  362. ;; CLASS: [.|&!()a-zA-Z0-9_\200-\377]+::
  363. ;; CATEGORY: [a-zA-Z_]+:
  364. (defun cfengine-common-settings ()
  365. (set (make-local-variable 'syntax-propertize-function)
  366. ;; In the main syntax-table, \ is marked as a punctuation, because
  367. ;; of its use in DOS-style directory separators. Here we try to
  368. ;; recognize the cases where \ is used as an escape inside strings.
  369. (syntax-propertize-rules ("\\(\\(?:\\\\\\)+\\)\"" (1 "\\"))))
  370. (set (make-local-variable 'parens-require-spaces) nil)
  371. (set (make-local-variable 'comment-start) "# ")
  372. (set (make-local-variable 'comment-start-skip)
  373. "\\(\\(?:^\\|[^\\\\\n]\\)\\(?:\\\\\\\\\\)*\\)#+[ \t]*")
  374. ;; Like Lisp mode. Without this, we lose with, say,
  375. ;; `backward-up-list' when there's an unbalanced quote in a
  376. ;; preceding comment.
  377. (set (make-local-variable 'parse-sexp-ignore-comments) t))
  378. (defun cfengine-common-syntax (table)
  379. ;; The syntax defaults seem OK to give reasonable word movement.
  380. (modify-syntax-entry ?# "<" table)
  381. (modify-syntax-entry ?\n ">#" table)
  382. (modify-syntax-entry ?\" "\"" table)
  383. ;; Variable substitution.
  384. (modify-syntax-entry ?$ "." table)
  385. ;; Doze path separators.
  386. (modify-syntax-entry ?\\ "." table))
  387. ;;;###autoload
  388. (define-derived-mode cfengine3-mode prog-mode "CFE3"
  389. "Major mode for editing CFEngine3 input.
  390. There are no special keybindings by default.
  391. Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves
  392. to the action header."
  393. (cfengine-common-settings)
  394. (cfengine-common-syntax cfengine3-mode-syntax-table)
  395. (set (make-local-variable 'indent-line-function) #'cfengine3-indent-line)
  396. (setq font-lock-defaults
  397. '(cfengine3-font-lock-keywords nil nil nil beginning-of-defun))
  398. ;; Use defuns as the essential syntax block.
  399. (set (make-local-variable 'beginning-of-defun-function)
  400. #'cfengine3-beginning-of-defun)
  401. (set (make-local-variable 'end-of-defun-function)
  402. #'cfengine3-end-of-defun))
  403. ;;;###autoload
  404. (define-derived-mode cfengine2-mode prog-mode "CFE2"
  405. "Major mode for editing CFEngine2 input.
  406. There are no special keybindings by default.
  407. Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves
  408. to the action header."
  409. (cfengine-common-settings)
  410. (cfengine-common-syntax cfengine2-mode-syntax-table)
  411. ;; Shell commands can be quoted by single, double or back quotes.
  412. ;; It's debatable whether we should define string syntax, but it
  413. ;; should avoid potential confusion in some cases.
  414. (modify-syntax-entry ?\' "\"" cfengine2-mode-syntax-table)
  415. (modify-syntax-entry ?\` "\"" cfengine2-mode-syntax-table)
  416. (set (make-local-variable 'indent-line-function) #'cfengine2-indent-line)
  417. (set (make-local-variable 'outline-regexp) "[ \t]*\\(\\sw\\|\\s_\\)+:+")
  418. (set (make-local-variable 'outline-level) #'cfengine2-outline-level)
  419. (set (make-local-variable 'fill-paragraph-function)
  420. #'cfengine-fill-paragraph)
  421. (define-abbrev-table 'cfengine2-mode-abbrev-table cfengine-mode-abbrevs)
  422. (setq font-lock-defaults
  423. '(cfengine2-font-lock-keywords nil nil nil beginning-of-line))
  424. ;; Fixme: set the args of functions in evaluated classes to string
  425. ;; syntax, and then obey syntax properties.
  426. (setq imenu-generic-expression cfengine2-imenu-expression)
  427. (set (make-local-variable 'beginning-of-defun-function)
  428. #'cfengine2-beginning-of-defun)
  429. (set (make-local-variable 'end-of-defun-function) #'cfengine2-end-of-defun))
  430. ;;;###autoload
  431. (defun cfengine-auto-mode ()
  432. "Choose between `cfengine2-mode' and `cfengine3-mode' depending
  433. on the buffer contents"
  434. (let ((v3 nil))
  435. (save-restriction
  436. (goto-char (point-min))
  437. (while (not (or (eobp) v3))
  438. (setq v3 (looking-at (concat cfengine3-defuns-regex "\\>")))
  439. (forward-line)))
  440. (if v3 (cfengine3-mode) (cfengine2-mode))))
  441. (defalias 'cfengine-mode 'cfengine-auto-mode)
  442. (provide 'cfengine3)
  443. (provide 'cfengine)
  444. ;;; cfengine.el ends here