Debian Codegraph @

cylc/flow/etc/syntax/cylc-mode.el

raw
1 ;; ____________________________________________________________________________
2 ;;
3 ;; = cylc-mode.el =
4 ;; Emacs syntax highlighting mode for Cylc workflow definition (flow.cylc)
5 ;; files
6 ;; ____________________________________________________________________________
7 ;;
8 ;; = Instructions =
9 ;; Place this file in $HOME/.emacs.d/lisp/ (create this directory if it
10 ;; doesn't exist) and add the following lines in your $HOME/.emacs file:
11 ;;
12 ;; (add-to-list 'load-path "~/.emacs.d/lisp/")
13 ;; (require 'cylc-mode)
14 ;;
15 ;; ____________________________________________________________________________
16
17 (defvar cylc-mode-hook nil)
18
19 ;; Extend region hook to ensure correct re-fontifying of the multi-line region
20 (defun cylc-font-lock-extend-region ()
21 "Extend the search region to include an entire block of text."
22 ;; Avoid compiler warnings about these global variables from font-lock.el.
23 ;; See the documentation for variable `font-lock-extend-region-functions'.
24 (eval-when-compile (defvar font-lock-beg) (defvar font-lock-end))
25 (save-excursion
26 (goto-char font-lock-beg)
27 (let ((found (or (re-search-backward "\n\n" nil t) (point-min))))
28 (goto-char font-lock-end)
29 (when (re-search-forward "\n\n" nil t)
30 (beginning-of-line)
31 (setq font-lock-end (point)))
32 (setq font-lock-beg found))))
33
34 ;; Define the mode and the syntax highlighting for it
35 (define-derived-mode cylc-mode fundamental-mode
36 "flow.cylc" "Major mode for editing Cylc workflow definition files"
37
38 ;; Note: ordered according to reverse application precedence, where
39 ;; specification order for faces changes resultant highlighting
40
41 ;; Assignment and dependency characters, but only outside of Jinja2
42 (font-lock-add-keywords nil '(("=+" . font-lock-preprocessor-face)))
43 (font-lock-add-keywords nil '(("=>" . font-lock-keyword-face)))
44
45 ;; Cylc setting keys/names
46 (font-lock-add-keywords nil
47 '(("^\\( *[a-zA-Z0-9\-]+ *\\)=+[^>]" 1 font-lock-variable-name-face t)))
48
49 ;; Account for section headings (see below) with internal patterns, e.g. a
50 ;; Jinja2 statement, inside by matching start and end heading groups. Note:
51 ;; must be applied here to get correct 'pure' header & ICD highlighting.
52 (font-lock-add-keywords nil
53 '(("\\[\\( *[ a-zA-Z0-9\-\_,.]*\\)" . font-lock-warning-face)))
54 (font-lock-add-keywords nil
55 '(("\\( *[ a-zA-Z0-9\-\_,.]*\\)\\]" . font-lock-warning-face)))
56 (font-lock-add-keywords nil
57 '(("\\[\\[\\( *[ a-zA-Z0-9\-\_,.]*\\)" . font-lock-function-name-face)))
58 (font-lock-add-keywords nil
59 '(("\\( *[ a-zA-Z0-9\-\_,.]*\\)\\]\\]" . font-lock-function-name-face)))
60 (font-lock-add-keywords nil
61 '(("\\[\\[\\[\\( *[ a-zA-Z0-9\-\_,.]*\\)" . font-lock-type-face)))
62 (font-lock-add-keywords nil
63 '(("\\( *[ a-zA-Z0-9\-\_,.]*\\)\\]\\]\\]" . font-lock-type-face)))
64
65 ;; Inter-cycle dependencies (distinguish from top-level section headings)
66 (font-lock-add-keywords nil '(("\\[.*\\]" . font-lock-string-face)))
67
68 ;; All 'pure' Cylc section (of any level e.g. sub-, sub-sub-) headings:
69 ;; ... Top-level headings, enclosed in single square brackets
70 (font-lock-add-keywords nil '(("^ *\\[.*\\]$" . font-lock-warning-face)))
71 ;; ... Second-level (sub-) section headings, enclosed in double brackets
72 (font-lock-add-keywords nil
73 '(("^ *\\[\\[.*\\]\\]$" . font-lock-function-name-face)))
74 ;; ... Third-level (sub-sub-) section headings, enclosed in triple brackets
75 (font-lock-add-keywords nil
76 '(("^ *\\[\\[\\[.*\\]\\]\\]$" . font-lock-type-face)))
77
78 ;; All comments: standard ('# ... ') and Jinja2 ('{# ... #}')
79 (font-lock-add-keywords nil
80 '(("#.*$" . font-lock-comment-face))) ;; in-line only, by precedence
81 ;; Stop interference. No regex lookarounds in Emacs Lisp; ugly workaround
82 (font-lock-add-keywords nil
83 '(("^#\\(\\([^{].{2}\\|.[^#]\\).*\\|.{0,1}\\)$"
84 . font-lock-comment-face)))
85 (font-lock-add-keywords nil
86 '(("{#\\(.\\|\n\\)*?#}" . font-lock-comment-face))) ;; not in-line
87
88 ;; All Jinja2 excl. comments: '{% ... %}' and '{{ ... }}' incl. multiline
89 (font-lock-add-keywords nil
90 '(("{%\\(.\\|\n\\)*?%}" . font-lock-constant-face)))
91 (font-lock-add-keywords nil '(("{{.*?}}" . font-lock-constant-face)))
92
93 ;; Highlight triple quotes for a multi-line setting value
94 (font-lock-add-keywords nil '(("\"\"\"" . 'font-lock-builtin-face)))
95
96 ;; Add the extend region hook to deal with the multiline matching above
97 (add-hook 'font-lock-extend-region-functions 'cylc-font-lock-extend-region)
98
99 ;; Make sure jit-lock scans larger multiline regions correctly
100 (set (make-local-variable 'jit-lock-contextually) t)
101
102 ;; Force any other fundamental mode inherit font-locking to be ignored, this
103 ;; previously caused double-quotes to break the multiline highlighting
104 (set (make-local-variable 'font-lock-keywords-only) t)
105
106 ;; We need multiline mode
107 (set (make-local-variable 'font-lock-multiline) t)
108
109 ;; Run the mode hooks to allow a user to execute mode-specific actions
110 (run-hooks 'cylc-mode-hook))
111
112 ;;;###autoload
113 (add-to-list 'auto-mode-alist '("suite.*\\.rc\\'" . cylc-mode))
114 (add-to-list 'auto-mode-alist '("\\.cylc\\'" . cylc-mode))
115
116 (provide 'cylc-mode)