]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cfengine.el
Merge from emacs-24; up to 2013-01-03T02:37:57Z!rgm@gnu.org
[gnu-emacs] / lisp / progmodes / cfengine.el
1 ;;; cfengine.el --- mode for editing Cfengine files
2
3 ;; Copyright (C) 2001-2013 Free Software Foundation, Inc.
4
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Maintainer: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: languages
8 ;; Version: 1.3
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Provides support for editing GNU Cfengine files, including
28 ;; font-locking, Imenu and indentation, but with no special keybindings.
29
30 ;; The CFEngine 3.x support doesn't have Imenu support but patches are
31 ;; welcome.
32
33 ;; By default, CFEngine 3.x syntax is used.
34
35 ;; You can set it up so either `cfengine2-mode' (2.x and earlier) or
36 ;; `cfengine3-mode' (3.x) will be picked, depending on the buffer
37 ;; contents:
38
39 ;; (add-to-list 'auto-mode-alist '("\\.cf\\'" . cfengine-auto-mode))
40
41 ;; OR you can choose to always use a specific version, if you prefer
42 ;; it:
43
44 ;; (add-to-list 'auto-mode-alist '("\\.cf\\'" . cfengine3-mode))
45 ;; (add-to-list 'auto-mode-alist '("^cf\\." . cfengine2-mode))
46 ;; (add-to-list 'auto-mode-alist '("^cfagent.conf\\'" . cfengine2-mode))
47
48 ;; It's *highly* recommended that you enable the eldoc minor mode:
49
50 ;; (add-hook 'cfengine-mode-hook 'turn-on-eldoc-mode)
51
52 ;; This is not the same as the mode written by Rolf Ebert
53 ;; <ebert@waporo.muc.de>, distributed with cfengine-2.0.5. It does
54 ;; better fontification and indentation, inter alia.
55
56 ;;; Code:
57
58 (defgroup cfengine ()
59 "Editing CFEngine files."
60 :group 'languages)
61
62 (defcustom cfengine-indent 2
63 "Size of a CFEngine indentation step in columns."
64 :group 'cfengine
65 :type 'integer)
66
67 (defcustom cfengine-cf-promises
68 (or (executable-find "cf-promises")
69 (executable-find "/var/cfengine/bin/cf-promises")
70 (executable-find "/usr/bin/cf-promises")
71 (executable-find "/usr/local/bin/cf-promises")
72 (executable-find "~/bin/cf-promises"))
73 "The location of the cf-promises executable.
74 Used for syntax discovery and checking. Set to nil to disable
75 the `compile-command' override and the ElDoc support."
76 :group 'cfengine
77 :type 'file)
78
79 (defcustom cfengine-parameters-indent '(promise pname 0)
80 "*Indentation of CFEngine3 promise parameters (hanging indent).
81
82 For example, say you have this code:
83
84 bundle x y
85 {
86 section:
87 class::
88 promise ...
89 promiseparameter => ...
90 }
91
92 You can choose to indent promiseparameter from the beginning of
93 the line (absolutely) or from the word \"promise\" (relatively).
94
95 You can also choose to indent the start of the word
96 \"promiseparameter\" or the arrow that follows it.
97
98 Finally, you can choose the amount of the indent.
99
100 The default is to anchor at promise, indent parameter name, and offset 0:
101
102 bundle agent rcfiles
103 {
104 files:
105 any::
106 \"/tmp/netrc\"
107 comment => \"my netrc\",
108 perms => mog(\"600\", \"tzz\", \"tzz\");
109 }
110
111 Here we anchor at beginning of line, indent arrow, and offset 10:
112
113 bundle agent rcfiles
114 {
115 files:
116 any::
117 \"/tmp/netrc\"
118 comment => \"my netrc\",
119 perms => mog(\"600\", \"tzz\", \"tzz\");
120 }
121
122 Some, including cfengine_stdlib.cf, like to anchor at promise, indent
123 arrow, and offset 16 or so:
124
125 bundle agent rcfiles
126 {
127 files:
128 any::
129 \"/tmp/netrc\"
130 comment => \"my netrc\",
131 perms => mog(\"600\", \"tzz\", \"tzz\");
132 }
133 "
134
135 :group 'cfengine
136 :type '(list
137 (choice (const :tag "Anchor at beginning of promise" promise)
138 (const :tag "Anchor at beginning of line" bol))
139 (choice (const :tag "Indent parameter name" pname)
140 (const :tag "Indent arrow" arrow))
141 (integer :tag "Indentation amount from anchor")))
142
143 (defvar cfengine-mode-debug nil
144 "Whether `cfengine-mode' should print debugging info.")
145
146 (defvar cfengine-mode-syntax-cache nil
147 "Cache for `cfengine-mode' syntax trees obtained from 'cf-promises -s json'.")
148
149 (defcustom cfengine-mode-abbrevs nil
150 "Abbrevs for CFEngine2 mode."
151 :group 'cfengine
152 :type '(repeat (list (string :tag "Name")
153 (string :tag "Expansion")
154 (choice :tag "Hook" (const nil) function))))
155
156 (make-obsolete-variable 'cfengine-mode-abbrevs 'edit-abbrevs "24.1")
157
158 ;; Taken from the doc for pre-release 2.1.
159 (eval-and-compile
160 (defconst cfengine2-actions
161 '("acl" "alerts" "binservers" "broadcast" "control" "classes" "copy"
162 "defaultroute" "disks" "directories" "disable" "editfiles" "files"
163 "filters" "groups" "homeservers" "ignore" "import" "interfaces"
164 "links" "mailserver" "methods" "miscmounts" "mountables"
165 "processes" "packages" "rename" "required" "resolve"
166 "shellcommands" "tidy" "unmount"
167 ;; Keywords for cfservd.
168 "admit" "grant" "deny")
169 "List of the action keywords supported by Cfengine.
170 This includes those for cfservd as well as cfagent.")
171
172 (defconst cfengine3-defuns
173 (mapcar
174 'symbol-name
175 '(bundle body))
176 "List of the CFEngine 3.x defun headings.")
177
178 (defconst cfengine3-defuns-regex
179 (regexp-opt cfengine3-defuns t)
180 "Regex to match the CFEngine 3.x defuns.")
181
182 (defconst cfengine3-class-selector-regex "\\([[:alnum:]_().&|!:]+\\)::")
183
184 (defconst cfengine3-category-regex "\\([[:alnum:]_]+\\):")
185
186 (defconst cfengine3-vartypes
187 (mapcar
188 'symbol-name
189 '(string int real slist ilist rlist irange rrange counter data))
190 "List of the CFEngine 3.x variable types."))
191
192 (defvar cfengine2-font-lock-keywords
193 `(;; Actions.
194 ;; List the allowed actions explicitly, so that errors are more obvious.
195 (,(concat "^[ \t]*" (eval-when-compile
196 (regexp-opt cfengine2-actions t))
197 ":")
198 1 font-lock-keyword-face)
199 ;; Classes.
200 ("^[ \t]*\\([[:alnum:]_().|!]+\\)::" 1 font-lock-function-name-face)
201 ;; Variables.
202 ("$(\\([[:alnum:]_]+\\))" 1 font-lock-variable-name-face)
203 ("${\\([[:alnum:]_]+\\)}" 1 font-lock-variable-name-face)
204 ;; Variable definitions.
205 ("\\_<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1 font-lock-variable-name-face)
206 ;; File, acl &c in group: { token ... }
207 ("{[ \t]*\\([^ \t\n]+\\)" 1 font-lock-constant-face)))
208
209 (defvar cfengine3-font-lock-keywords
210 `(
211 ;; Defuns. This happens early so they don't get caught by looser
212 ;; patterns.
213 (,(concat "\\_<" cfengine3-defuns-regex "\\_>"
214 "[ \t]+\\_<\\([[:alnum:]_.:]+\\)\\_>"
215 "[ \t]+\\_<\\([[:alnum:]_.:]+\\)"
216 ;; Optional parentheses with variable names inside.
217 "\\(?:(\\([^)]*\\))\\)?")
218 (1 font-lock-builtin-face)
219 (2 font-lock-constant-face)
220 (3 font-lock-function-name-face)
221 (4 font-lock-variable-name-face nil t))
222
223 ;; Class selectors.
224 (,(concat "^[ \t]*" cfengine3-class-selector-regex)
225 1 font-lock-keyword-face)
226
227 ;; Categories.
228 (,(concat "^[ \t]*" cfengine3-category-regex)
229 1 font-lock-builtin-face)
230
231 ;; Variables, including scope, e.g. module.var
232 ("[@$](\\([[:alnum:]_.:]+\\))" 1 font-lock-variable-name-face)
233 ("[@$]{\\([[:alnum:]_.:]+\\)}" 1 font-lock-variable-name-face)
234
235 ;; Variable definitions.
236 ("\\_<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1 font-lock-variable-name-face)
237
238 ;; Variable types.
239 (,(concat "\\_<" (eval-when-compile (regexp-opt cfengine3-vartypes t)) "\\_>")
240 1 font-lock-type-face)))
241
242 (defvar cfengine2-imenu-expression
243 `((nil ,(concat "^[ \t]*" (eval-when-compile
244 (regexp-opt cfengine2-actions t))
245 ":[^:]")
246 1)
247 ("Variables/classes" "\\_<\\([[:alnum:]_]+\\)[ \t]*=[ \t]*(" 1)
248 ("Variables/classes" "\\_<define=\\([[:alnum:]_]+\\)" 1)
249 ("Variables/classes" "\\_<DefineClass\\>[ \t]+\\([[:alnum:]_]+\\)" 1))
250 "`imenu-generic-expression' for CFEngine mode.")
251
252 (defun cfengine2-outline-level ()
253 "`outline-level' function for CFEngine mode."
254 (if (looking-at "[^:]+\\(?:[:]+\\)$")
255 (length (match-string 1))))
256
257 (defun cfengine2-beginning-of-defun ()
258 "`beginning-of-defun' function for CFEngine mode.
259 Treats actions as defuns."
260 (unless (<= (current-column) (current-indentation))
261 (end-of-line))
262 (if (re-search-backward "^[[:alpha:]]+: *$" nil t)
263 (beginning-of-line)
264 (goto-char (point-min)))
265 t)
266
267 (defun cfengine2-end-of-defun ()
268 "`end-of-defun' function for CFEngine mode.
269 Treats actions as defuns."
270 (end-of-line)
271 (if (re-search-forward "^[[:alpha:]]+: *$" nil t)
272 (beginning-of-line)
273 (goto-char (point-max)))
274 t)
275
276 ;; Fixme: Should get an extra indent step in editfiles BeginGroup...s.
277
278 (defun cfengine2-indent-line ()
279 "Indent a line in Cfengine mode.
280 Intended as the value of `indent-line-function'."
281 (let ((pos (- (point-max) (point))))
282 (save-restriction
283 (narrow-to-defun)
284 (back-to-indentation)
285 (cond
286 ;; Action selectors aren't indented; class selectors are
287 ;; indented one step.
288 ((looking-at "[[:alnum:]_().|!]+:\\(:\\)?")
289 (if (match-string 1)
290 (indent-line-to cfengine-indent)
291 (indent-line-to 0)))
292 ;; Outdent leading close brackets one step.
293 ((or (eq ?\} (char-after))
294 (eq ?\) (char-after)))
295 (condition-case ()
296 (indent-line-to (save-excursion
297 (forward-char)
298 (backward-sexp)
299 (current-column)))
300 (error nil)))
301 ;; Inside brackets/parens: indent to start column of non-comment
302 ;; token on line following open bracket or by one step from open
303 ;; bracket's column.
304 ((condition-case ()
305 (progn (indent-line-to (save-excursion
306 (backward-up-list)
307 (forward-char)
308 (skip-chars-forward " \t")
309 (if (looking-at "[^\n#]")
310 (current-column)
311 (skip-chars-backward " \t")
312 (+ (current-column) -1
313 cfengine-indent))))
314 t)
315 (error nil)))
316 ;; Indent by two steps after a class selector.
317 ((save-excursion
318 (re-search-backward "^[ \t]*[[:alnum:]_().|!]+::" nil t))
319 (indent-line-to (* 2 cfengine-indent)))
320 ;; Indent by one step if we're after an action header.
321 ((save-excursion
322 (goto-char (point-min))
323 (looking-at "[[:alpha:]]+:[ \t]*$"))
324 (indent-line-to cfengine-indent))
325 ;; Else don't indent.
326 (t
327 (indent-line-to 0))))
328 ;; If initial point was within line's indentation,
329 ;; position after the indentation. Else stay at same point in text.
330 (if (> (- (point-max) pos) (point))
331 (goto-char (- (point-max) pos)))))
332
333 ;; This doesn't work too well in Emacs 21.2. See 22.1 development
334 ;; code.
335 (defun cfengine-fill-paragraph (&optional justify)
336 "Fill `paragraphs' in Cfengine code."
337 (interactive "P")
338 (or (if (fboundp 'fill-comment-paragraph)
339 (fill-comment-paragraph justify) ; post Emacs 21.3
340 ;; else do nothing in a comment
341 (nth 4 (parse-partial-sexp (save-excursion
342 (beginning-of-defun)
343 (point))
344 (point))))
345 (let ((paragraph-start
346 ;; Include start of parenthesized block.
347 "\f\\|[ \t]*$\\|.*\(")
348 (paragraph-separate
349 ;; Include action and class lines, start and end of
350 ;; bracketed blocks and end of parenthesized blocks to
351 ;; avoid including these in fill. This isn't ideal.
352 "[ \t\f]*$\\|.*#\\|.*[\){}]\\|\\s-*[[:alpha:]_().|!]+:")
353 fill-paragraph-function)
354 (fill-paragraph justify))
355 t))
356
357 (defun cfengine3-beginning-of-defun ()
358 "`beginning-of-defun' function for Cfengine 3 mode.
359 Treats body/bundle blocks as defuns."
360 (unless (<= (current-column) (current-indentation))
361 (end-of-line))
362 (if (re-search-backward (concat "^[ \t]*" cfengine3-defuns-regex "\\_>") nil t)
363 (beginning-of-line)
364 (goto-char (point-min)))
365 t)
366
367 (defun cfengine3-end-of-defun ()
368 "`end-of-defun' function for Cfengine 3 mode.
369 Treats body/bundle blocks as defuns."
370 (end-of-line)
371 (if (re-search-forward (concat "^[ \t]*" cfengine3-defuns-regex "\\_>") nil t)
372 (beginning-of-line)
373 (goto-char (point-max)))
374 t)
375
376 (defun cfengine3-indent-line ()
377 "Indent a line in Cfengine 3 mode.
378 Intended as the value of `indent-line-function'."
379 (let ((pos (- (point-max) (point)))
380 parse)
381 (save-restriction
382 (narrow-to-defun)
383 (back-to-indentation)
384 (setq parse (parse-partial-sexp (point-min) (point)))
385 (when cfengine-mode-debug
386 (message "%S" parse))
387
388 (cond
389 ;; Body/bundle blocks start at 0.
390 ((looking-at (concat cfengine3-defuns-regex "\\_>"))
391 (indent-line-to 0))
392 ;; Categories are indented one step.
393 ((looking-at (concat cfengine3-category-regex "[ \t]*\\(#.*\\)*$"))
394 (indent-line-to cfengine-indent))
395 ;; Class selectors are indented two steps.
396 ((looking-at (concat cfengine3-class-selector-regex "[ \t]*\\(#.*\\)*$"))
397 (indent-line-to (* 2 cfengine-indent)))
398 ;; Outdent leading close brackets one step.
399 ((or (eq ?\} (char-after))
400 (eq ?\) (char-after)))
401 (condition-case ()
402 (indent-line-to (save-excursion
403 (forward-char)
404 (backward-sexp)
405 (move-beginning-of-line nil)
406 (skip-chars-forward " \t")
407 (current-column)))
408 (error nil)))
409 ;; Inside a string and it starts before this line: do nothing.
410 ((and (nth 3 parse)
411 (< (nth 8 parse) (save-excursion (beginning-of-line) (point))))
412 )
413
414 ;; Inside a defun, but not a nested list (depth is 1). This is
415 ;; a promise, usually.
416
417 ;; Indent to cfengine-indent times the nested depth
418 ;; plus 2. That way, promises indent deeper than class
419 ;; selectors, which in turn are one deeper than categories.
420 ((= 1 (nth 0 parse))
421 (let ((p-anchor (nth 0 cfengine-parameters-indent))
422 (p-what (nth 1 cfengine-parameters-indent))
423 (p-indent (nth 2 cfengine-parameters-indent)))
424 ;; Do we have the parameter anchor and location and indent
425 ;; defined, and are we looking at a promise parameter?
426 (if (and p-anchor p-what p-indent
427 (looking-at "\\([[:alnum:]_]+[ \t]*\\)=>"))
428 (let* ((arrow-offset (* -1 (length (match-string 1))))
429 (extra-offset (if (eq p-what 'arrow) arrow-offset 0))
430 (base-offset (if (eq p-anchor 'promise)
431 (* (+ 2 (nth 0 parse)) cfengine-indent)
432 0)))
433 (indent-line-to (max 0 (+ p-indent base-offset extra-offset))))
434 ;; Else, indent to cfengine-indent times the nested depth
435 ;; plus 2. That way, promises indent deeper than class
436 ;; selectors, which in turn are one deeper than categories.
437 (indent-line-to (* (+ 2 (nth 0 parse)) cfengine-indent)))))
438 ;; Inside brackets/parens: indent to start column of non-comment
439 ;; token on line following open bracket or by one step from open
440 ;; bracket's column.
441 ((condition-case ()
442 (progn (indent-line-to (save-excursion
443 (backward-up-list)
444 (forward-char)
445 (skip-chars-forward " \t")
446 (cond
447 ((looking-at "[^\n#]")
448 (current-column))
449 ((looking-at "[^\n#]")
450 (current-column))
451 (t
452 (skip-chars-backward " \t")
453 (+ (current-column) -1
454 cfengine-indent)))))
455 t)
456 (error nil)))
457 ;; Else don't indent.
458 (t (indent-line-to 0))))
459 ;; If initial point was within line's indentation,
460 ;; position after the indentation. Else stay at same point in text.
461 (if (> (- (point-max) pos) (point))
462 (goto-char (- (point-max) pos)))))
463
464 ;; CFEngine 3.x grammar
465
466 ;; specification: blocks
467 ;; blocks: block | blocks block;
468 ;; block: bundle typeid blockid bundlebody
469 ;; | bundle typeid blockid usearglist bundlebody
470 ;; | body typeid blockid bodybody
471 ;; | body typeid blockid usearglist bodybody;
472
473 ;; typeid: id
474 ;; blockid: id
475 ;; usearglist: '(' aitems ')';
476 ;; aitems: aitem | aitem ',' aitems |;
477 ;; aitem: id
478
479 ;; bundlebody: '{' statements '}'
480 ;; statements: statement | statements statement;
481 ;; statement: category | classpromises;
482
483 ;; bodybody: '{' bodyattribs '}'
484 ;; bodyattribs: bodyattrib | bodyattribs bodyattrib;
485 ;; bodyattrib: class | selections;
486 ;; selections: selection | selections selection;
487 ;; selection: id ASSIGN rval ';' ;
488
489 ;; classpromises: classpromise | classpromises classpromise;
490 ;; classpromise: class | promises;
491 ;; promises: promise | promises promise;
492 ;; category: CATEGORY
493 ;; promise: promiser ARROW rval constraints ';' | promiser constraints ';';
494 ;; constraints: constraint | constraints ',' constraint |;
495 ;; constraint: id ASSIGN rval;
496 ;; class: CLASS
497 ;; id: ID
498 ;; rval: ID | QSTRING | NAKEDVAR | list | usefunction
499 ;; list: '{' litems '}' ;
500 ;; litems: litem | litem ',' litems |;
501 ;; litem: ID | QSTRING | NAKEDVAR | list | usefunction
502
503 ;; functionid: ID | NAKEDVAR
504 ;; promiser: QSTRING
505 ;; usefunction: functionid givearglist
506 ;; givearglist: '(' gaitems ')'
507 ;; gaitems: gaitem | gaitems ',' gaitem |;
508 ;; gaitem: ID | QSTRING | NAKEDVAR | list | usefunction
509
510 ;; # from lexer:
511
512 ;; bundle: "bundle"
513 ;; body: "body"
514 ;; COMMENT #[^\n]*
515 ;; NAKEDVAR [$@][(][a-zA-Z0-9_\200-\377.]+[)]|[$@][{][a-zA-Z0-9_\200-\377.]+[}]
516 ;; ID: [a-zA-Z0-9_\200-\377]+
517 ;; ASSIGN: "=>"
518 ;; ARROW: "->"
519 ;; QSTRING: \"((\\\")|[^"])*\"|\'((\\\')|[^'])*\'|`[^`]*`
520 ;; CLASS: [.|&!()a-zA-Z0-9_\200-\377]+::
521 ;; CATEGORY: [a-zA-Z_]+:
522
523 (defun cfengine3--current-word (&optional bounds)
524 "Propose a word around point in the current CFEngine 3 buffer."
525 (let ((c (char-after (point)))
526 (s (syntax-ppss)))
527 (when (not (nth 3 s)) ; not inside a string
528 (if bounds
529 (save-excursion
530 (let ((oldpoint (point))
531 start end)
532 (skip-syntax-backward "w_") (setq start (point))
533 (goto-char oldpoint)
534 (skip-syntax-forward "w_") (setq end (point))
535 (when (not (and (eq start oldpoint)
536 (eq end oldpoint)))
537 (list start (point)))))
538 (and c
539 (memq (char-syntax c) '(?_ ?w))
540 (current-word))))))
541
542 (defun cfengine3--current-function ()
543 "Look up current CFEngine 3 function"
544 (let* ((syntax (assoc cfengine-cf-promises cfengine-mode-syntax-cache))
545 (flist (assoc 'functions syntax)))
546 (when flist
547 (let ((w (cfengine3--current-word)))
548 (and w (assq (intern w) flist))))))
549
550 ;; format from "cf-promises -s json", e.g. "sort" function:
551 ;; ((category . "data")
552 ;; (variadic . :json-false)
553 ;; (parameters . [((range . "[a-zA-Z0-9_$(){}\\[\\].:]+") (type . "string"))
554 ;; ((range . "lex,int,real,IP,ip,MAC,mac") (type . "option"))])
555 ;; (returnType . "slist")
556 ;; (status . "normal"))
557
558 (defun cfengine3-format-function-docstring (fdef)
559 (let* ((f (format "%s" (car-safe fdef)))
560 (def (cdr fdef))
561 (rtype (cdr (assq 'returnType def)))
562 (plist (cdr (assq 'parameters def)))
563 (has-some-parameters (> (length plist) 0))
564 (variadic (eq t (cdr (assq 'variadic def)))))
565
566 ;; (format "[%S]%s %s(%s%s)" def
567 (format "%s %s(%s%s)"
568 (if rtype
569 (propertize rtype 'face 'font-lock-variable-name-face)
570 "???")
571 (propertize f 'face 'font-lock-function-name-face)
572 (mapconcat (lambda (p)
573 (let ((type (cdr (assq 'type p)))
574 (range (cdr (assq 'range p))))
575 (cond
576 ((not (stringp type)) "???type???")
577 ((not (stringp range)) "???range???")
578 ;; options are lists of possible keywords
579 ((equal type "option")
580 (propertize (concat "[" range "]")
581 'face
582 'font-lock-keyword-face))
583 ;; anything else is a type name as a variable
584 (t (propertize type
585 'face
586 'font-lock-variable-name-face)))))
587 plist
588 ", ")
589 (if variadic
590 (if has-some-parameters ", ..." "...")
591 ""))))
592
593 (defun cfengine3-make-syntax-cache ()
594 "Build the CFEngine 3 syntax cache.
595 Calls `cfengine-cf-promises' with \"-s json\""
596 (when cfengine-cf-promises
597 (let ((loaded-json-lib (require 'json nil t))
598 (syntax (assoc cfengine-cf-promises cfengine-mode-syntax-cache)))
599 (if (not loaded-json-lib)
600 (message "JSON library could not be loaded!")
601 (unless syntax
602 (with-demoted-errors
603 (with-temp-buffer
604 (call-process-shell-command cfengine-cf-promises
605 nil ; no input
606 t ; current buffer
607 nil ; no redisplay
608 "-s" "json")
609 (goto-char (point-min))
610 (setq syntax (json-read))
611 (setq cfengine-mode-syntax-cache
612 (cons (cons cfengine-cf-promises syntax)
613 cfengine-mode-syntax-cache)))))))))
614
615 (defun cfengine3-documentation-function ()
616 "Document CFengine 3 functions around point.
617 Intended as the value of `eldoc-documentation-function', which
618 see. Use it by executing `turn-on-eldoc-mode'."
619 (cfengine3-make-syntax-cache)
620 (let ((fdef (cfengine3--current-function)))
621 (when fdef
622 (cfengine3-format-function-docstring fdef))))
623
624 (defun cfengine3-completion-function ()
625 "Return completions for function name around or before point."
626 (cfengine3-make-syntax-cache)
627 (let* ((bounds (cfengine3--current-word t))
628 (syntax (assoc cfengine-cf-promises cfengine-mode-syntax-cache))
629 (flist (assoc 'functions syntax)))
630 (when bounds
631 (append bounds (list (cdr flist))))))
632
633 (defun cfengine-common-settings ()
634 (set (make-local-variable 'syntax-propertize-function)
635 ;; In the main syntax-table, \ is marked as a punctuation, because
636 ;; of its use in DOS-style directory separators. Here we try to
637 ;; recognize the cases where \ is used as an escape inside strings.
638 (syntax-propertize-rules ("\\(\\(?:\\\\\\)+\\)\"" (1 "\\"))))
639 (set (make-local-variable 'parens-require-spaces) nil)
640 (set (make-local-variable 'comment-start) "# ")
641 (set (make-local-variable 'comment-start-skip)
642 "\\(\\(?:^\\|[^\\\\\n]\\)\\(?:\\\\\\\\\\)*\\)#+[ \t]*")
643 ;; Like Lisp mode. Without this, we lose with, say,
644 ;; `backward-up-list' when there's an unbalanced quote in a
645 ;; preceding comment.
646 (set (make-local-variable 'parse-sexp-ignore-comments) t))
647
648 (defun cfengine-common-syntax (table)
649 ;; The syntax defaults seem OK to give reasonable word movement.
650 (modify-syntax-entry ?# "<" table)
651 (modify-syntax-entry ?\n ">#" table)
652 (modify-syntax-entry ?\" "\"" table) ; "string"
653 (modify-syntax-entry ?\' "\"" table) ; 'string'
654 ;; Variable substitution.
655 (modify-syntax-entry ?$ "." table)
656 ;; Doze path separators.
657 (modify-syntax-entry ?\\ "." table))
658
659 (defconst cfengine3--prettify-symbols-alist
660 '(("->" . ?→)
661 ("=>" . ?⇒)
662 ("::" . ?∷)))
663
664 ;;;###autoload
665 (define-derived-mode cfengine3-mode prog-mode "CFE3"
666 "Major mode for editing CFEngine3 input.
667 There are no special keybindings by default.
668
669 Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves
670 to the action header."
671 (cfengine-common-settings)
672 (cfengine-common-syntax cfengine3-mode-syntax-table)
673
674 (set (make-local-variable 'indent-line-function) #'cfengine3-indent-line)
675
676 (setq font-lock-defaults
677 '(cfengine3-font-lock-keywords
678 nil nil nil beginning-of-defun))
679 (setq-local prettify-symbols-alist cfengine3--prettify-symbols-alist)
680
681 ;; `compile-command' is almost never a `make' call with CFEngine so
682 ;; we override it
683 (when cfengine-cf-promises
684 (set (make-local-variable 'compile-command)
685 (concat cfengine-cf-promises
686 " -f "
687 (when buffer-file-name
688 (shell-quote-argument buffer-file-name)))))
689
690 (set (make-local-variable 'eldoc-documentation-function)
691 #'cfengine3-documentation-function)
692
693 (add-hook 'completion-at-point-functions
694 #'cfengine3-completion-function nil t)
695
696 ;; Use defuns as the essential syntax block.
697 (set (make-local-variable 'beginning-of-defun-function)
698 #'cfengine3-beginning-of-defun)
699 (set (make-local-variable 'end-of-defun-function)
700 #'cfengine3-end-of-defun))
701
702 ;;;###autoload
703 (define-derived-mode cfengine2-mode prog-mode "CFE2"
704 "Major mode for editing CFEngine2 input.
705 There are no special keybindings by default.
706
707 Action blocks are treated as defuns, i.e. \\[beginning-of-defun] moves
708 to the action header."
709 (cfengine-common-settings)
710 (cfengine-common-syntax cfengine2-mode-syntax-table)
711
712 ;; Shell commands can be quoted by single, double or back quotes.
713 ;; It's debatable whether we should define string syntax, but it
714 ;; should avoid potential confusion in some cases.
715 (modify-syntax-entry ?\` "\"" cfengine2-mode-syntax-table)
716
717 (set (make-local-variable 'indent-line-function) #'cfengine2-indent-line)
718 (set (make-local-variable 'outline-regexp) "[ \t]*\\(\\sw\\|\\s_\\)+:+")
719 (set (make-local-variable 'outline-level) #'cfengine2-outline-level)
720 (set (make-local-variable 'fill-paragraph-function)
721 #'cfengine-fill-paragraph)
722 (define-abbrev-table 'cfengine2-mode-abbrev-table cfengine-mode-abbrevs)
723 (setq font-lock-defaults
724 '(cfengine2-font-lock-keywords nil nil nil beginning-of-line))
725 ;; Fixme: set the args of functions in evaluated classes to string
726 ;; syntax, and then obey syntax properties.
727 (setq imenu-generic-expression cfengine2-imenu-expression)
728 (set (make-local-variable 'beginning-of-defun-function)
729 #'cfengine2-beginning-of-defun)
730 (set (make-local-variable 'end-of-defun-function) #'cfengine2-end-of-defun))
731
732 ;;;###autoload
733 (defun cfengine-auto-mode ()
734 "Choose between `cfengine2-mode' and `cfengine3-mode' depending
735 on the buffer contents"
736 (let ((v3 nil))
737 (save-restriction
738 (goto-char (point-min))
739 (while (not (or (eobp) v3))
740 (setq v3 (looking-at (concat cfengine3-defuns-regex "\\_>")))
741 (forward-line)))
742 (if v3 (cfengine3-mode) (cfengine2-mode))))
743
744 (defalias 'cfengine-mode 'cfengine3-mode)
745
746 (provide 'cfengine3)
747 (provide 'cfengine)
748
749 ;;; cfengine.el ends here