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