1 ;;; lilypond-font-lock.el --- syntax coloring for LilyPond mode
3 ;; Copyright (C) 1992,1993,1994 Tim Peters
5 ;; Author: 2001: Heikki Junes
6 ;; * Emacs-mode: new keywords, reserved words, identifiers, notenames,
7 ;; some dynamics and brackets are font-lock-keywords
8 ;; Author: 1997: Han-Wen Nienhuys
9 ;; Author: 1995-1996 Barry A. Warsaw
10 ;; 1992-1994 Tim Peters
13 ;; Last Modified: 13APR2002
14 ;; Keywords: lilypond languages music notation
16 ;; This software is provided as-is, without express or implied
17 ;; warranty. Permission to use, copy, modify, distribute or sell this
18 ;; software, without fee, for any purpose and by any individual or
19 ;; organization, is hereby granted, provided that the above copyright
20 ;; notice and this paragraph appear in all copies.
22 ;; This started out as a cannabalised version of python-mode.el, by hwn
23 ;; For changes see the LilyPond ChangeLog
27 ;; - handle lexer modes (\header, \melodic, \lyric) etc.
29 (defconst LilyPond-font-lock-keywords
30 (let* ((keywords '( ; need special order due to over[lapping] of words
32 ;; all letters are lowercase
33 "accent" "accepts" "accompany" "\\(add\\)?lyrics"
34 "\\(aeol\\|dor\\|ion\\|locr\\|\\(mixo\\)?lyd\\|phryg\\)ian"
35 "alias" "\\(altern\\|rel\\)ative" "apply" "arpeggio" "autochange" "bar" "break"
36 "breathe" "beamintervals" "broken" "blend" "\\(bc\\|end\\)incipit"
37 "ch\\(ar\\)?" "cg" "chord\\(s\\|stest\\|\\(chord\\)?modifiers\\)?"
38 "clef[ \t]*\"?\\(F\\|G\\|alto\\|baritone\\|bass\\|\\(mezzo\\)?soprano\\|treble\\|violin\\|tenor\\)?\"?"
39 "clipping" "[cm]m" "coda" "complex"
40 "\\(command\\)?spanrequest" "consists\\(end\\)?"
41 "context" "contrabasso" "\\(de\\)?cr" "default" "denies" "different" "dirs"
42 "down\\(bow\\|prall\\)?" "duration" "\\(dynamic\\|text\\)?script"
43 "eccentric" "eg" "embeddedps" "elementdescriptions" "\\(end\\)?cresc"
44 "ex\\(treme\\)?" "fermata" "f+" "figures" "font" "flageolet" "fp" "fragment"
45 "s?fz" "gliss\\(ando\\)?" "gg" "gmsus" "grace" "gr\\(and\\)?staff"
46 "header" "\\(h\\|v\\)size" "in\\(clude\\|versions\\|visible\\)?"
47 "key\\(s\\(ignature\\)?\\)?" "lag" "\\(l\\|r\\)heel" "line\\(break\\|prall\\)"
48 "longa" "lower" "\\(l\\|r\\)toe"
49 "mark" "marcato" "maxima" "mel\\(isma\\|ody\\)?" "midi" "m\\(aj\\|in\\)or"
50 "\\(up\\|down\\)?mordent" "monstrous" "multipart" "music"
51 "\\(musical\\)?pitch" "m\\(p\\|f\\|m\\)?" "name" "newpage" "noise\\(beat\\)?"
52 "normal\\(key\\|size\\)" "\\(note\\|pitch\\)?names" "notes" "nt?"
53 "one\\(staff\\)?" "open" "\\(output\\)?property" "over\\(ride\\)?"
54 "part\\(combine\\|ial\\)" "penalty" "p+" "pt"
55 "prall\\(down\\|mordent\\|prall\\|up\\)?" "quickmeasure" "rc\\(ed\\)?" "remove"
56 "repeat[ \t]*\\(\\(un\\)?fold\\|percent\\|\\|tremolo\\|volta\\)?" "rest"
57 "revert" "\\(reverse\\)?turn" "rfz?" "rhythm"
58 "right" "scales?" "scheme" "\\(sc\\)?paper" "\\(sc\\)?score" "sd"
59 "segno" "sequential" "set\\(tings\\)?" "shortlong"
60 "simultaneous" "singlepart" "skip" "small" "\\(smart\\)?transpose"
61 "s[pf]+" "staccat\\(issim\\)?o" "staff\\(height\\|space\\)" "start"
63 "st\\(paper\\|score\\)" "stuff" "stylesheet" "su" "tab" "tempo" "tenuto"
64 "thenotes" "thrd" "threevoice" "thumb" "tilt\\(down\\|up\\)"
65 "timb" "times?" "tiny" "toeters" "touch" "translator"
66 "trill" "type" "t\\(wo\\(voice\\(steminvert\\)?\\)?\\)?"
67 "un\\(der\\|set\\)" "up\\(bow\\|per\\|prall\\)?" "version"
68 "visible" "voicedefault" "x"
74 ;; in principle, have one or more uppercase letters
75 "\\(\\(BarNumbering\\|\\(Inner\\)?Choir\\|Grand\\|HaraKiri\\|OrchestralPart\\|Piano\\|Rhythmic\\)?Staff\\|\\(Cue\\|Lyrics\\)?Voice\\|\\(Orchestral\\)?Score\\|ChordNames\\|FiguredBass\\|Grace\\|Lyrics\\|NoteNames\\|\\(Inner\\)?Staff\\(Group\\|Container\\)?\\|Thread\\)Context" ; *Context
76 "\\(script\\|dots\\|dynamic\\|slur\\|stem\\|sustain\\|sostenuto\\|unaCorda\\|treCorde\\|tie\\|tuplet\\)\\(Both\\|Down\\|Up\\)" ; *(Both/Down/Up)
77 "\\(slur\\|tie\\)\\(Dotted\\|Solid\\)" ; *(Dotted/Solid)
78 "\\(autoBeam\\|cadenza\\|impro\\|turn\\)\\(Off\\|On\\)" ; *(On/Off)
79 "\\(empty\\|fat\\)Text" ; *Text
80 "shift\\(On+\\|Off\\|I\\|II\\|III\\|IV\\|V\\)" ; shift*
82 "\\(hide\\|show\\)StaffSwitch"
83 "\\(lower\\|upper\\)Voice"
84 "voice\\(One\\|Two\\|Three\\|Four\\|B\\|C\\|D\\|E\\)" ; voice*
85 "paper\\(Eleven\\|Sixteen\\|Thirteen\\|TwentySix\\)" ; paper*
86 "\\(lower\\|upper\\)\\(Octave\\|One\\)" ; (lower/upper)*
87 "hairyChord" "\\(Piano\\|Rhythmic\\)\\(Staff\\)?"
88 "\\(clarinetti\\|fagotti\\|flauti\\|melodic\\|oboi\\|\\(quite\\|rather\\|somewhat\\)LongLyrics\\|violinoII?\\)?\\(Staff\\)?" ; *Staff
89 "\\(archi\\|bassi\\|legni\\|ottoni\\|timpani\\|viole\\|violini\\)\\(Group\\)" ; *Group
90 "melisma\\(End\\)?" "staff\\(One\\|Two\\)?" "rests\\(II\\)?" "specialKey"
91 "noBreak" "paperTwentysix" "endHorizScript" "FontBody" "text(I)+"
92 "\\(modern\\|forget\\)Accidental" ; *Accidentals
93 "noResetKey" "modern\\(Voice\\)?Cautionaries" "unaCorda" "treCorde"
99 ;; Other words which look nicer when colored
100 "Accidental" "autoBeamSettings" "BarLine" "Beam"
101 "ChordName\\([s]?\\|s.[a-zA-Z]*\\)" "Dots" "DynamicText"
102 "FiguredBass" "Hairpin" "\\(\\(Inner\\)?Choir\\|Grand\\|Piano\\|Tab\\)Staff"
103 "Slur" "Stem" "SpacingSpanner" "System\\(StartDelimiter\\)?"
104 "\\(Grace\\|Lyrics\\|Note\\(Head\\|Names\\)\\|Score\\|\\(Rhythmic\\)?Staff\\(Symbol\\)?\\|Thread\\|Voice\\)\\(.[a-zA-Z]*\\)?" ; combine below, if possible
105 "\\(Grace\\|Lyrics\\|Note\\(Head\\|Names\\)\\|Score\\|\\(Rhythmic\\)?Staff\\(Symbol\\)?\\|Thread\\|Voice\\)[ \t]*\\(.[ \t]*[a-zA-Z]*\\)?"
106 "TextScript" "TimeSignature" "VerticalAlignment"
110 (kwregex (mapconcat (lambda (x) (concat "\\\\" x
)) keywords
"\\|"))
111 (iregex (mapconcat (lambda (x) (concat "\\\\" x
)) identifiers
"\\|"))
112 (rwregex (mapconcat (lambda (x) (concat "" x
)) reservedwords
"\\|"))
116 ;; Fonts in use (from GNU Emacs Lisp Reference Manual, elisp.ps):
117 ;; font-lock- comment / string / keyword / builtin / function-name /
118 ;; variable-name / type / constant / warning -face
120 ;; The order below is designed so that proofreading would be possible.
123 ;; ... first identifiers and keywords.
124 ;; ... then other expressions having '\'-mark in the beginning.
125 ;; ... the right and the left side of '='-marks.
126 ;; ... reserved words, e.g., FiguredBass.
127 ;; ... notes and rests
128 ;; "on top", 6) ... '<{[]}>'-brackets
129 ;; "on top", 7) ... ties, slurs and hairpins
130 ;; "on top", 8) ... (multiline-)scheme; urgh. one should count the slurs
131 ;; "on top", 9) ... strings
132 ;; "on top", 10) ... (multiline-)comments
134 ;; One should note 'font-lock-multiline' has been possible since Emacs 21.1.
135 ;; See, e.g., text in "http://emacs.kldp.org/emacs-21.1/etc/NEWS".
137 ;; ... identifiers (defined above, see iregex)
138 (cons (concat "\\(\\([_^-]?\\(" iregex
"\\)\\)+\\)\\($\\|[] \t(~{}>\\\\_()^*-]\\)") '(1 font-lock-function-name-face
))
140 ;; ... keywords (defined above, see kwregex)
141 (cons (concat "\\(\\([_^-]?\\(" kwregex
"\\)\\)+\\)\\($\\|[] \t(~{}>\\\\_()^*-]\\)") '(1 font-lock-keyword-face
))
143 ;; ... keyword-type constructs, e.g., ^\abracadabra; not \breve (= a duration)
144 '("\\([_^-]?\\\\\\([^b]\\|b[^r]\\|br[^e]\\|bre[^v]\\|brev[^e]\\|breve[a-zA-Z]\\)[a-zA-Z]*\\)" 1 font-lock-constant-face
)
146 ;; ... the left side of '=' -mark
147 '("\\([_a-zA-Z.0-9-]+\\)[ \t]*=[ \t]*" 1 font-lock-variable-name-face
)
149 ;; ... the right side of '=' -mark
150 '("[ \t]*=[ \t]*\\([_a-zA-Z.0-9-]+\\)" 1 font-lock-variable-name-face
)
152 ;; ... reserved words (defined above, see rwregex)
153 (cons (concat "\\(" rwregex
"\\)") 'font-lock-variable-name-face
)
155 ;; ... notes and rests, accidentals and duration (multiplied), e.g., b,?16.*3/4
156 '("\\(^\\|[ <\{[~(!)\t\\\|]\\)\\(\\(\\(\\(bb\\|as[ae]s\\|eses\\|\\(do\\|re\\|[ms]i\\|[fl]a\\|sol\\)\\(bb?\\|dd?\\|ss?\\)?\\)\\|\\([a-h]\\(\\(flat\\)+\\|\\(sharp\\)+\\|is\\(siss\\|i?s\\)?\\|es\\(sess\\|e?s\\)?\\|ff?\\|ss?\\)?\\)\\)[,']*[?!]?\\|[srR]\\)\\([ \t]*\\(128\\|6?4\\|3?2\\|16?\\|8\\|\\\\breve\\)[.]*\\([ \t]*[*][ \t]*[0-9]+\\(/[1-9][0-9]*\\)?\\)?\\)?\\)" 2 font-lock-type-face
)
158 ;; "on top", ... '{[]}'-brackets
159 '("\\([][}{]\\)" 0 font-lock-warning-face t
)
161 ;; "on top", ... '<>'-brackets, not marcato '->'
162 '("\\(\\(-.\\)+\\|[^-^_]\\)\\([[<>]+\\)" 3 font-lock-warning-face t
)
164 ;; "on top", ... ties ~, slurs \( () \), hairpins \<, \>, \!
165 '("\\(\\\\[(<!>)]\\|[(~)]\\)" 0 font-lock-builtin-face t
)
167 ;; "on top", ... (multiline-)scheme: try find slurs up to 7th
168 '("[_^-]?#\\(#[ft]\\|-?[0-9.]+\\|\"[^\"]*\"\\|['`]?[a-zA-Z-:]+\\|['`]?([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^)]*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*[^)]*)\\)" 0 font-lock-string-face t
)
170 ;; "on top", ... strings
171 '("\\([_^-]?\"\\([^\"\\\\]\\|\\\\.\\|\\\\\n\\)*\"\\)" 0 font-lock-string-face t
)
173 ;; "on top", ... (multiline-)comments
174 '("\\(%\\({[^%]*%\\(}\\|\\([^}][^%]*%\\)+}\\)\\|.*\\)\\)" 0 font-lock-comment-face t
)
178 "Additional expressions to fontify in LilyPond mode.")
180 ;; define a mode-specific abbrev table for those who use such things
181 (defvar LilyPond-mode-abbrev-table nil
182 "Abbrev table in use in `LilyPond-mode' buffers.")
184 (define-abbrev-table 'LilyPond-mode-abbrev-table nil
)
186 (defvar LilyPond-mode-syntax-table nil
187 "Syntax table used in `LilyPond-mode' buffers.")
190 (if LilyPond-mode-syntax-table
192 (setq LilyPond-mode-syntax-table
(make-syntax-table))
194 (lambda (x) (modify-syntax-entry
195 (car x
) (cdr x
) LilyPond-mode-syntax-table
)))
196 '(( ?\
( .
"." ) ( ?\
) .
"." )
197 ( ?\
[ .
"(]" ) ( ?\
] .
")[" ) ;; all the other paren characters are now handled by
198 ( ?\
{ .
". 2b" ) ;; lily-specific indenting/matching code in lilypond-indent.el
200 ( ?\
< .
"." )( ?\
> .
".")
201 ( ?\$ .
"." ) ( ?\
& .
"." )
202 ( ?\
* .
"." ) ( ?\
+ .
"." )
203 ( ?\
/ .
"." ) ( ?\
= .
"." )
204 ( ?\| .
"." ) (?
\\ .
"\\" )
205 ( ?\- .
"." ) ( ?\_ .
"." ) ( ?\^ .
"." ) ; accent positioners: puctuation characters
206 ( ?
\' .
"w") ( ?\
, .
"w") ; transposing octaves, parts of words (notes)
207 ( ?
\" .
"\"" ) ; string quote characters
208 ( ?\% .
"< 1b3b" ) ; (block-)comment starter (or ender)
209 ( ?
\n .
">") ; newline: comment ender
210 ( ?
\r .
">") ; formfeed: comment ender