]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-font-lock.el
* scripts/lilypond-book.py: make URL for printfilename option.
[lilypond.git] / lilypond-font-lock.el
1 ;;; lilypond-font-lock.el --- syntax coloring for LilyPond mode
2
3 ;; Copyright (C) 1992,1993,1994  Tim Peters
4
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
11 ;; Created:       Feb 1992
12 ;; Version:       1.5.52
13 ;; Last Modified: 13APR2002
14 ;; Keywords: lilypond languages music notation
15
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.
21
22 ;; This started out as a cannabalised version of python-mode.el, by hwn
23 ;; For changes see the LilyPond ChangeLog
24 ;;
25
26 ;; TODO:
27 ;;   - handle lexer modes (\header, \melodic, \lyric) etc.
28
29 (defconst LilyPond-font-lock-keywords
30   (let* ((keywords '( ; need special order due to over[lapping] of words
31
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" 
62 "stop\\(ped\\)?"
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"
69
70                       ))
71
72   (identifiers '( 
73
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*
81 "EasyNotation"
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\\)Accidentals" ; *Accidentals
93 "noResetKey" "modern\\(Voice\\)?Cautionaries" "unaCorda" "treCorde"
94
95                       ))
96
97   (reservedwords '(
98
99 ;; Other words which look nicer when colored
100 "Accidentals" "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"
107
108                       ))
109
110        (kwregex (mapconcat (lambda (x) (concat "\\\\" x))  keywords "\\|"))
111        (iregex (mapconcat (lambda (x) (concat "\\\\" x))  identifiers "\\|"))
112        (rwregex (mapconcat (lambda (x) (concat "" x))  reservedwords "\\|"))
113 )
114
115     (list 
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
119
120 ;; The order below is designed so that proofreading would be possible.
121
122 ;; Fontify...
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
133
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".
136
137 ;; ... identifiers (defined above, see iregex)
138       (cons (concat "\\(\\([_^-]?\\(" iregex "\\)\\)+\\)\\($\\|[] \t(~{}>\\\\_()^*-]\\)") '(1 font-lock-function-name-face))
139
140 ;; ... keywords (defined above, see kwregex)
141       (cons (concat "\\(\\([_^-]?\\(" kwregex "\\)\\)+\\)\\($\\|[] \t(~{}>\\\\_()^*-]\\)") '(1 font-lock-keyword-face))
142
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)
145
146 ;; ... the left side of '=' -mark
147       '("\\([_a-zA-Z.0-9-]+\\)[ \t]*=[ \t]*" 1 font-lock-variable-name-face)
148
149 ;; ... the right side of '=' -mark
150       '("[ \t]*=[ \t]*\\([_a-zA-Z.0-9-]+\\)" 1 font-lock-variable-name-face)
151
152 ;; ... reserved words (defined above, see rwregex)
153       (cons (concat "\\(" rwregex "\\)") 'font-lock-variable-name-face)
154
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)
157
158 ;; "on top", ... '{[]}'-brackets
159       '("\\([][}{]\\)" 0 font-lock-warning-face t)
160
161 ;; "on top", ... '<>'-brackets, not marcato '->'
162       '("\\(\\(-.\\)+\\|[^-^_]\\)\\([[<>]+\\)" 3 font-lock-warning-face t) 
163
164 ;; "on top", ... ties ~, slurs \( () \), hairpins \<, \>, \! 
165       '("\\(\\\\[(<!>)]\\|[(~)]\\)" 0 font-lock-builtin-face t)
166
167 ;; "on top", ... (multiline-)scheme: try find slurs up to 7th
168       '("[_^-]?#\\(#[ft]\\|-?[0-9.]+\\|\"[^\"]*\"\\|['`]?[a-zA-Z-:]+\\|['`]?([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^)]*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*[^)]*)\\)" 0 font-lock-string-face t)
169
170 ;; "on top", ... strings
171       '("\\([_^-]?\"\\([^\"\\\\]\\|\\\\.\\|\\\\\n\\)*\"\\)" 0 font-lock-string-face t)
172
173 ;; "on top", ... (multiline-)comments
174       '("\\(%\\({[^%]*%\\(}\\|\\([^}][^%]*%\\)+}\\)\\|.*\\)\\)" 0 font-lock-comment-face t)
175
176       )
177     )
178   "Additional expressions to fontify in LilyPond mode.")
179
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.")
183
184 (define-abbrev-table 'LilyPond-mode-abbrev-table nil)
185
186 (defvar LilyPond-mode-syntax-table nil
187   "Syntax table used in `LilyPond-mode' buffers.")
188
189 ;;
190 (if LilyPond-mode-syntax-table
191     ()
192   (setq LilyPond-mode-syntax-table (make-syntax-table))
193   (mapcar (function
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 
199             ( ?\}  .  ". 4b" )              
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
211             ))
212   )
213