]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-font-lock.el
77a508975f54440c22db6433d86020cb56a73db4
[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" "breve" "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       '("\\([_^-]?\\\\[a-zA-Z][a-zA-Z]*\\)" 1 font-lock-constant-face)
121       '("\\([_a-zA-Z.0-9-]+\\)[ \t]*=[ \t]*" 1 font-lock-variable-name-face)
122       '("[ \t]*=[ \t]*\\([_a-zA-Z.0-9-]+\\)" 1 font-lock-variable-name-face)
123
124
125 ;; other reserved words
126       (cons (concat "\\(" rwregex "\\)") 'font-lock-variable-name-face)
127
128 ;; highlight note names; separate notes from (other than ')'-type) brackets
129       '("\\([sR]\\(128\\|64\\|32\\|16\\|8\\|4\\|2\\|1\\)?[.]*[ \t]*[*][ \t]*[0-9]+\\)"1 font-lock-type-face)
130       '("[ <\{[~(!)\t\\\|]\\(\\(\\(\\(do\\|re\\|mi\\|fa\\|sol\\|la\\|si\\)\\(bb?\\|dd?\\|ss?\\)?\\)\\|\\([a-hsrR]\\(flat\\(flat\\)?\\|sharp\\(sharp\\)?\\|ff?\\|ss?\\|is\\(siss\\|s\\|is\\)?\\|es\\(sess\\|s\\|es\\)?\\)?\\)\\|\\(as\\(as\\|es\\)?\\)\\|\\(es\\(es\\)?\\)\\|\\(bb\\)\\)[,']*[?!]?\\(128\\|64\\|32\\|16\\|8\\|4\\|2\\|1\\)?[.]*\\)" 1 font-lock-type-face)
131
132 ;; highlight identifiers
133       (cons (concat "\\([_^-]?\\(" iregex "\\)\\)+\\($\\|[] \t(~{}>\\\\_-()^]\\)") '(0 font-lock-function-name-face t))
134
135 ;; highlight keywords
136       (cons (concat "\\([_^-]?\\(" kwregex "\\)\\)+\\($\\|[] \t(~{}>\\\\_-()^]\\)") '(0 font-lock-keyword-face t))
137
138 ;; highlight bracketing constructs
139       '("\\([][}{]\\)" 0 font-lock-warning-face t)
140       ;; these regexps allow angle-brackets to be highlighted when and only when they delimit simultaneous music
141       ;; fontify open < but leave crescendos \< alone
142       '("\\([^\\]\\|^\\)\\(<\\)" 2 font-lock-warning-face t)
143       ;; fontify the close-brackets in <a b c--> (tenuto) and <a b c-^> (marcato)
144       '("[_^-]\\s-*[-^]\\s-*\\(>\\)" 1 font-lock-warning-face t) 
145       ;; but leave a b c-> (accent) alone, accounting for whitespace
146       '("\\([^\\t\\n _^-]\\|^\\)\\s-*\\(>\\)" 2 font-lock-warning-face t)
147       ;; ties ~, slurs \( () \), hairpins \<,  \>, end-of-hairpin \!, 
148       '("\\(\\\\[(<!>)]\\|[(~)]\\)" 0 font-lock-builtin-face t)
149
150 ;; highlight scheme: handle slurs up to seventh level
151       '("[_^-]?#\\(#[ft]\\|-?[0-9.]+\\|\"[^\"]*\"\\|['`]?[a-zA-Z-:]+\\|['`]?([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^)]*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*[^)]*)\\)" 0 font-lock-string-face t)
152
153 ;; highlight strings: urgh. should hangle strings in strings, i.e., \\\"
154       '("\\([_^-]?\"[^\"]*\"\\)" 0 font-lock-string-face t)
155
156 ;; highlight (block) comments; urgh. block comments should be updatable
157       '("\\(%\\({[^%]*%\\(}\\|\\([^}][^%]*%\\)+}\\)\\|.*\\)\\)" 0 font-lock-comment-face t)
158
159       )
160     )
161   "Additional expressions to highlight in LilyPond mode.")
162
163 ;; define a mode-specific abbrev table for those who use such things
164 (defvar LilyPond-mode-abbrev-table nil
165   "Abbrev table in use in `LilyPond-mode' buffers.")
166
167 (define-abbrev-table 'LilyPond-mode-abbrev-table nil)
168
169 (defvar LilyPond-mode-syntax-table nil
170   "Syntax table used in `LilyPond-mode' buffers.")
171
172 ;;
173 (if LilyPond-mode-syntax-table
174     ()
175   (setq LilyPond-mode-syntax-table (make-syntax-table))
176   (mapcar (function
177            (lambda (x) (modify-syntax-entry
178                         (car x) (cdr x) LilyPond-mode-syntax-table)))
179           '(( ?\( . "." ) ( ?\) . "." ) 
180             ( ?\[ . "(]" ) ( ?\] . ")[" ) ;; all the other paren characters are now handled by          
181             ( ?\{  .  ".2b" )             ;; lily-specific indenting/matching code in lilypond-indent.el 
182             ( ?\}  .  ".4b" )              
183             ( ?\< . "." )( ?\> . ".") 
184             ( ?\$ . "." ) ( ?\% . "." ) ( ?\& . "." )
185             ( ?\* . "." ) ( ?\+ . "." )
186             ( ?\/ . "." )  ( ?\= . "." )
187             ( ?\| . "." ) (?\\ . "\\" )
188             ( ?\- . "." ) ( ?\_ . "." ) ( ?\^ . "." )
189             ( ?\' . "w")        
190             ( ?\" . "\"" )
191             ( ?\%  .  ". 1b3b" )
192             ( ?\n . ">")
193             ( ?\r . ">")
194             ))
195   )
196