1 ;;; lilypond-font-lock.el --- syntax coloring for LilyPond mode
3 ;; Copyright (C) 1992,1993,1994 Tim Peters
5 ;; Author: 2001-2003: Heikki Junes
6 ;; * Emacs-mode: new keywords, reserved words, identifiers, notenames,
7 ;; some dynamics and brackets are font-lock-keywords
8 ;; * File lilypond.words contains \keywords, \Identifiers and
9 ;; ReservedWords notenames.
10 ;; * context-dependent syntax-tables
11 ;; Author: 1997: Han-Wen Nienhuys
12 ;; Author: 1995-1996 Barry A. Warsaw
13 ;; 1992-1994 Tim Peters
16 ;; Last Modified: 18SEP2003
17 ;; Keywords: lilypond languages music notation
19 ;; This software is provided as-is, without express or implied
20 ;; warranty. Permission to use, copy, modify, distribute or sell this
21 ;; software, without fee, for any purpose and by any individual or
22 ;; organization, is hereby granted, provided that the above copyright
23 ;; notice and this paragraph appear in all copies.
25 ;; This started out as a cannabalised version of python-mode.el, by hwn
26 ;; For changes see the LilyPond ChangeLog
30 ;; - handle lexer modes (\header, \melodic) etc.
32 (defconst LilyPond-font-lock-keywords
33 (let* ((kwregex (mapconcat (lambda (x) (concat "\\" x)) LilyPond-keywords "\\|"))
34 (iregex (mapconcat (lambda (x) (concat "\\" x)) LilyPond-identifiers "\\|"))
35 (ncrwregex (mapconcat (lambda (x) (concat "" x)) LilyPond-non-capitalized-reserved-words "\\|"))
36 (rwregex (mapconcat (lambda (x) (concat "" x)) LilyPond-Capitalized-Reserved-Words "\\|"))
37 (duration "\\([ \t]*\\(\\\\breve\\|128\\|6?4\\|3?2\\|16?\\|8\\)[.]*\\([ \t]*[*][ \t]*[0-9]+\\(/[1-9][0-9]*\\)?\\)?\\)")
38 (longduration "\\([ \t]*\\(\\\\\\(longa\\|breve\\|maxima\\)\\)[.]*\\([ \t]*[*][ \t]*[0-9]+\\(/[1-9][0-9]*\\)?\\)?\\)")
42 ;; Fonts in use (from GNU Emacs Lisp Reference Manual, elisp.ps):
43 ;; font-lock- (c)omment / (s)tring / (k)eyword / (b)uiltin / (f)unction-name /
44 ;; (v)ariable-name / (t)ype / co(n)stant / (w)arning -face
46 ;; The order below is designed so that proofreading would be possible.
49 ;; ... (f) identifiers and (k) keywords.
50 ;; ... (n) user defined indetifiers
51 ;; ... (v) the right and the left side of '='-marks.
52 ;; ... (v) reserved words, e.g., FiguredBass.
53 ;; ... (t) notes and rests
54 ;; "on top", ... (s) lyrics-mode
55 ;; "on top", ... (w) horizontal grouping
56 ;; "on top", ... (f) vertical grouping
57 ;; "on top", ... (b) expressional grouping
58 ;; "on top", ... (s) (multiline-)scheme; urgh. one should count the slurs
59 ;; "on top", ... (s) strings
60 ;; "on top", ... (c) (multiline-)comments
62 ;; One should note 'font-lock-multiline' has been possible since Emacs 21.1.
63 ;; See, e.g., text in "http://emacs.kldp.org/emacs-21.1/etc/NEWS".
65 ;; ... identifiers (defined above, see iregex)
66 (cons (concat "\\(\\([_^-]?\\(" iregex "\\)\\)+\\)\\($\\|[] \t(~{}>\\\\_()^*-]\\)") '(1 font-lock-function-name-face))
68 ;; ... keywords (defined above, see kwregex)
69 (cons (concat "\\(\\([_^-]?\\(" kwregex "\\)\\)+\\)\\($\\|[] \t(~{}>\\\\_()^*-]\\)") '(1 font-lock-keyword-face))
71 ;; ... user defined identifiers \[a-zA-Z]+
72 '("\\([_^-]?\\\\\\([a-zA-Z][a-zA-Z]*\\)\\)" 1 font-lock-constant-face)
74 ;; ... the left side of '=' -mark
75 '("\\([_a-zA-Z.0-9-]+\\)[ \t]*=[ \t]*" 1 font-lock-variable-name-face)
77 ;; ... the right side of '=' -mark
78 '("[ \t]*=[ \t]*\\([_a-zA-Z.0-9-]+\\)" 1 font-lock-variable-name-face)
80 ;; ... reserved words (defined above, see rwregex)
81 (cons (concat "\\(" rwregex "\\)") 'font-lock-variable-name-face)
83 ;; ... note or rest with (an accidental and) a duration, e.g., b,?16.*3/4
84 (cons (concat "\\(^\\|[ <\{[/~(!)\t\\\|]\\)\\(\\(\\(" ncrwregex "\\)[,']*[?!]?\\|[srR]\\)" duration "?\\)") '(2 font-lock-type-face))
86 ;; "on top", ... notes and rests with a long duration
87 (cons (concat longduration) '(0 font-lock-type-face t))
89 ;; "on top", ... lyrics-mode: fontify everything between '<'...'>' or '{'...'}'
90 ; URGH, does not know anything about inner brackets.
91 ; Multiple lines may need refontifying (C-c f).
92 '("\\(\\\\lyrics[^{<]*\\)\\({[^}]*\\|<[^>]*\\)" 2 font-lock-string-face t)
94 ;; "on top", ... horizontal grouping, also as postfix syntax '-*':
98 '("\\(-?[][~}{]\\|\\\\[][]\\)" 0 font-lock-warning-face t)
100 ;; "on top", ... vertical grouping:
101 ;; - '<>'-chord brackets with '\\'-voice sep., not marcato '->'
102 ;; - '<< a b >>8' -chords
103 (cons (concat "\\(\\(-.\\)+\\|[^-^_]\\)\\([<>]+\\(" duration "\\|" longduration "\\)?\\|\\\\\\\\\\)") '(3 font-lock-function-name-face t))
105 ;; "on top", ... expressional grouping, also as postfix syntax '-*':
106 ;; - slurs ( ), \( \), [-^_][()]
107 ;; - hairpins \<, \>, \!
108 '("\\(-?\\\\[(<!>)]\\|[-^_]?[()]\\)" 0 font-lock-builtin-face t)
110 ;; "on top", ... (multiline-)scheme: try find slurs up to 7th
111 '("[_^-]?#\\(#[ft]\\|-?[0-9.]+\\|\"[^\"]*\"\\|['`]?[a-zA-Z:-]+\\|['`]?([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^)]*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*[^)]*)\\)" 0 font-lock-string-face t)
113 ;; "on top", ... strings, match also unending strings at eof:
114 ;; if '\n' was not found, it must be '$' which is eof (?).
115 '("\\([_^-]?\"\\([^\"\\\\]\\|\\\\.\\|\\\\\n\\)*\\(\"\\|$\\)\\)" 0 font-lock-string-face t)
117 ;; "on top", ... (multiline-)comments
118 '("\\(%\\({[^%]*%\\(}\\|\\([^}][^%]*%\\)+}\\)\\|.*\\)\\)" 0 font-lock-comment-face t)
122 "Additional expressions to fontify in LilyPond mode.")
124 ;; define a mode-specific abbrev table for those who use such things
125 (defvar LilyPond-mode-abbrev-table nil
126 "Abbrev table in use in `LilyPond-mode' buffers.")
128 (define-abbrev-table 'LilyPond-mode-abbrev-table nil)
130 (defvar LilyPond-mode-syntax-table nil
131 "Syntax table used in `LilyPond-mode' buffers.")
133 (defun LilyPond-mode-set-syntax-table (&optional not-punct)
134 "Change syntax table according to the argument `not-punct' which contains characters which are given a context dependent non-punctuation syntax: parentheses may be set to parenthesis syntax and characters `-', `^' and `_' may be set to escape syntax."
135 (if (not not-punct) (setq not-punct '()))
136 (setq LilyPond-mode-syntax-table (make-syntax-table))
139 ;; NOTE: Emacs knows only "13"-style (used), XEmacs knows also "1b3b", etc.
140 ( ?\% . "< 13" ) ; comment starter, 1st char in block-comments
141 ( ?\n . ">") ; newline: comment ender
142 ( ?\r . ">") ; formfeed: comment ender
143 ( ?\\ . "\\" ) ; escape characters (as '\n' in strings)
144 ( ?\" . "\"" ) ; string quote characters
145 ;; word constituents (e.g., belonging to a note)
146 ( ?\' . "w") ( ?\, . "w") ; transposing octaves
147 ;; punctuation characters (separate symbols from another)
148 ( ?\$ . "." ) ( ?\& . "." )
149 ( ?\* . "." ) ( ?\+ . "." ) ( ?\/ . "." ) ( ?\= . "." )
150 ( ?\| . "." ) ; bar line
152 ;; all the paren characters are now handled by lily-specific indenting/matching code in lilypond-indent.el
153 (if (or (memq ?\{ not-punct) (memq ?\} not-punct))
154 (setq defaults (cons '( ?\{ . "(} 2" ) (cons '( ?\} . "){ 4" ) defaults))) ; begin and end of a block-comment
155 (setq defaults (cons '( ?\{ . ". 2" ) (cons '( ?\} . ". 4" ) defaults)))) ; begin and end of a block-comment
156 (if (or (memq ?\[ not-punct) (memq ?\] not-punct))
157 (setq defaults (cons '( ?\[ . "(]" ) (cons '( ?\] . ")[" ) defaults)))
158 (setq defaults (cons '( ?\[ . "." ) (cons '( ?\] . "." ) defaults))))
159 (if (or (memq ?\< not-punct) (memq ?\> not-punct))
160 (setq defaults (cons '( ?\< . "(>" ) (cons '( ?\> . ")<" ) defaults)))
161 (setq defaults (cons '( ?\< . "." ) (cons '( ?\> . "." ) defaults))))
162 (if (or (memq ?\( not-punct) (memq ?\) not-punct))
163 (setq defaults (cons '( ?\( . "()" ) (cons '( ?\) . ")(" ) defaults)))
164 (setq defaults (cons '( ?\( . "." ) (cons '( ?\) . "." ) defaults))))
165 ;; In LilyPond the following chars serve as escape chars, e.g., c^> d-) e_( ,
166 ;; but they may be set to punctuation chars, since inside strings they should not act as escape chars
167 (setq defaults (cons (if (memq ?\- not-punct) '( ?\- . "\\" ) '( ?\- . "." ) ) defaults))
168 (setq defaults (cons (if (memq ?\^ not-punct) '( ?\^ . "\\" ) '( ?\^ . "." ) ) defaults))
169 (setq defaults (cons (if (memq ?\_ not-punct) '( ?\_ . "\\" ) '( ?\_ . "." ) ) defaults))
171 (lambda (x) (modify-syntax-entry
172 (car x) (cdr x) LilyPond-mode-syntax-table)))
174 (set-syntax-table LilyPond-mode-syntax-table)))
176 (defun LilyPond-mode-context-set-syntax-table ()
177 "Change syntax table according to current context."
179 ;; default syntax table sets parentheses to punctuation characters
180 (LilyPond-mode-set-syntax-table)
181 ;; find current context
182 (setq context (parse-partial-sexp (point-min) (point)))
183 (cond ((nth 3 context)) ; inside string
184 ((nth 4 context)) ; inside a comment
185 ((eq (char-syntax (char-before (point))) ?\\)) ; found escape-char
186 ((and (eq (char-syntax (char-before (- (point) 1))) ?\\)
187 (memq (char-before (point)) '( ?\) ?\] )))) ; found escape-char
188 ((memq (char-before (point)) '( ?\) ))
189 (LilyPond-mode-set-syntax-table '( ?\( ?\) )))
190 ((memq (char-before (point)) '( ?\] ))
191 (LilyPond-mode-set-syntax-table '( ?\[ ?\] )))
192 ((memq (char-before (point)) '( ?\> ?\} ))
193 (LilyPond-mode-set-syntax-table '( ?\< ?\> ?\{ ?\} ?\^ ?\- ?\_ )))
194 ((memq (char-after (point)) '( ?\( ))
195 (LilyPond-mode-set-syntax-table '( ?\( ?\) )))
196 ((memq (char-after (point)) '( ?\[ ))
197 (LilyPond-mode-set-syntax-table '( ?\[ ?\] )))
198 ((memq (char-after (point)) '( ?\< ?\{ ))
199 (LilyPond-mode-set-syntax-table '( ?\< ?\> ?\{ ?\} ?\^ ?\- ?\_ )))