]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-font-lock.el
* scm/music-functions.scm (make-ottava-set):
[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-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 gives keywords, identifiers and reserved words
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.7.20
14 ;; Last Modified: 9JUN2003
15 ;; Keywords: lilypond languages music notation
16
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.
22
23 ;; This started out as a cannabalised version of python-mode.el, by hwn
24 ;; For changes see the LilyPond ChangeLog
25 ;;
26
27 ;; TODO:
28 ;;   - handle lexer modes (\header, \melodic) etc.
29
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          (rwregex (mapconcat (lambda (x) (concat "" x))  LilyPond-reserved-words "\\|"))
34 )
35
36     (list 
37 ;; Fonts in use (from GNU Emacs Lisp Reference Manual, elisp.ps):
38 ;; font-lock- (c)omment / (s)tring / (k)eyword / (b)uiltin / (f)unction-name / 
39 ;;            (v)ariable-name / (t)ype / co(n)stant / (w)arning -face
40
41 ;; The order below is designed so that proofreading would be possible.
42
43 ;; Fontify...
44 ;; ... (f) identifiers and (k) keywords.
45 ;; ... (n) user defined indetifiers
46 ;; ... (v) the right and the left side of '='-marks.
47 ;; ... (v) reserved words, e.g., FiguredBass.
48 ;; ... (t) notes and rests
49 ;; "on top", ... (s) lyrics-mode
50 ;; "on top", ... (w) horizontal grouping
51 ;; "on top", ... (f) vertical grouping
52 ;; "on top", ... (b) expressional grouping
53 ;; "on top", ... (s) (multiline-)scheme; urgh. one should count the slurs
54 ;; "on top", ... (s) strings
55 ;; "on top", ... (c) (multiline-)comments
56
57 ;; One should note 'font-lock-multiline' has been possible since Emacs 21.1.
58 ;; See, e.g., text in "http://emacs.kldp.org/emacs-21.1/etc/NEWS".
59
60 ;; ... identifiers (defined above, see iregex)
61       (cons (concat "\\(\\([_^-]?\\(" iregex "\\)\\)+\\)\\($\\|[] \t(~{}>\\\\_()^*-]\\)") '(1 font-lock-function-name-face))
62
63 ;; ... keywords (defined above, see kwregex)
64       (cons (concat "\\(\\([_^-]?\\(" kwregex "\\)\\)+\\)\\($\\|[] \t(~{}>\\\\_()^*-]\\)") '(1 font-lock-keyword-face))
65
66 ;; ... user defined identifiers \[a-zA-Z]+, but not \breve or \longa (durations)
67       '("\\([_^-]?\\\\\\([ac-km-zA-Z]\\|l[a-np-zA-Z]\\|b[a-qs-zA-Z]\\|lo[a-mo-zA-Z]\\|br[a-df-zA-Z]\\|lon[a-fh-zA-Z]\\|bre[a-uw-zA-Z]\\|long[b-zA-Z]\\|brev[a-df-zA-Z]\\|\\(longa\\|breve\\)[a-zA-Z]\\)[a-zA-Z]*\\)" 1 font-lock-constant-face)
68
69 ;; ... the left side of '=' -mark
70       '("\\([_a-zA-Z.0-9-]+\\)[ \t]*=[ \t]*" 1 font-lock-variable-name-face)
71
72 ;; ... the right side of '=' -mark
73       '("[ \t]*=[ \t]*\\([_a-zA-Z.0-9-]+\\)" 1 font-lock-variable-name-face)
74
75 ;; ... reserved words (defined above, see rwregex)
76       (cons (concat "\\(" rwregex "\\)") 'font-lock-variable-name-face)
77
78 ;; ... note or rest with (an accidental and) a duration (multiplied), e.g., b,?16.*3/4
79       '("\\(^\\|[ <\{[/~(!)\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\\|longa\\)\\)[.]*\\([ \t]*[*][ \t]*[0-9]+\\(/[1-9][0-9]*\\)?\\)?\\)\\)" 2 font-lock-type-face)
80 ;; ... note or rest (with an accidental), e.g., b,? -- allows cis\longaX
81       '("\\(^\\|[ <\{[/~(!)\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]\\)" 2 font-lock-type-face)
82
83 ;; "on top", ... lyrics-mode: fontify everything between '<'...'>' or '{'...'}'
84 ;            URGH, does not know anything about inner brackets.
85 ;            Multiple lines may need refontifying (C-c f).
86       '("\\(\\\\lyrics[^{<]*\\)\\({[^}]*\\|<[^>]*\\)" 2 font-lock-string-face t)
87
88 ;; "on top", ... horizontal grouping, also as postfix syntax '-*':
89 ;;               - brackets '{[]}'
90 ;;               - ties '~'
91 ;;               - ligatures \[, \]
92       '("\\(-?[][~}{]\\|\\\\[][]\\)" 0 font-lock-warning-face t)
93
94 ;; "on top", ... vertical grouping:
95 ;;               - '<>'-chord brackets with '\\'-voice sep., not marcato '->'
96 ;;               - '<< a b >>8' -chords
97       '("\\(\\(-.\\)+\\|[^-^_]\\)\\([<>]+\\(\\(128\\|6?4\\|3?2\\|16?\\|8\\|\\\\\\(breve\\|longa\\)\\)[.]*\\([ \t]*[*][ \t]*[0-9]+\\(/[1-9][0-9]*\\)?\\)?\\)?\\|\\\\\\\\\\)" 3 font-lock-function-name-face t)
98
99 ;; "on top", ... expressional grouping, also as postfix syntax '-*':
100 ;;               - slurs ( ), \( \), [-^_][()]
101 ;;               - hairpins \<, \>, \! 
102       '("\\(-?\\\\[(<!>)]\\|[-^_]?[()]\\)" 0 font-lock-builtin-face t)
103
104 ;; "on top", ... (multiline-)scheme: try find slurs up to 7th
105       '("[_^-]?#\\(#[ft]\\|-?[0-9.]+\\|\"[^\"]*\"\\|['`]?[a-zA-Z:-]+\\|['`]?([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^)]*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*[^)]*)\\)" 0 font-lock-string-face t)
106
107 ;; "on top", ... strings, match also unending strings at eof:
108 ;;               if '\n' was not found, it must be '$' which is eof (?).
109       '("\\([_^-]?\"\\([^\"\\\\]\\|\\\\.\\|\\\\\n\\)*\\(\"\\|$\\)\\)" 0 font-lock-string-face t)
110
111 ;; "on top", ... (multiline-)comments
112       '("\\(%\\({[^%]*%\\(}\\|\\([^}][^%]*%\\)+}\\)\\|.*\\)\\)" 0 font-lock-comment-face t)
113
114       )
115     )
116   "Additional expressions to fontify in LilyPond mode.")
117
118 ;; define a mode-specific abbrev table for those who use such things
119 (defvar LilyPond-mode-abbrev-table nil
120   "Abbrev table in use in `LilyPond-mode' buffers.")
121
122 (define-abbrev-table 'LilyPond-mode-abbrev-table nil)
123
124 (defvar LilyPond-mode-syntax-table nil
125   "Syntax table used in `LilyPond-mode' buffers.")
126
127 (if LilyPond-mode-syntax-table
128     ()
129   (setq LilyPond-mode-syntax-table (make-syntax-table))
130   ;; NOTE: Emacs knows only "13"-style (used), XEmacs knows also "1b3b", etc.
131   (mapcar (function
132            (lambda (x) (modify-syntax-entry
133                         (car x) (cdr x) LilyPond-mode-syntax-table)))
134           '(
135             ;; all the paren characters are now handled by   
136             ;; lily-specific indenting/matching code in lilypond-indent.el
137             ;; Emacs' show-paren-function and XEmacs' paren-highlight use
138             ;; these slur-definitions through Lilypond specific scan-sexps.
139             ( ?\[ . "(]" ) ( ?\] . ")[" )
140             ( ?\( . "()" ) ( ?\) . ")(" ) 
141             ( ?\< . "(>" ) ( ?\> . ")<") 
142             ( ?\{  .  "(} 2" )  ; also 2nd char in begin of block-comment
143             ( ?\}  .  "){ 4" )  ; also 2nd char in end of block-comment
144             ( ?\%  .  "< 13" ) ; comment starter, 1st char in block-comments
145             ( ?\n . ">")       ; newline: comment ender
146             ( ?\r . ">")       ; formfeed: comment ender
147             ( ?\\ . "\\" )     ; escape characters (as '\n' in strings)
148             ( ?\" . "\"" )     ; string quote characters
149             ;; word constituents (e.g., belonging to a note)
150             ( ?\' . "w") ( ?\, . "w") ; transposing octaves
151             ;; punctuation characters (separate symbols from another)
152             ( ?\$ . "." ) ( ?\& . "." )
153             ( ?\* . "." ) ( ?\+ . "." ) ( ?\/ . "." )  ( ?\= . "." )
154             ( ?\| . "." )      ; bar line
155             ( ?\- . "." ) ( ?\_ . "." ) ( ?\^ . "." ) ; accent positioners
156             ))
157   )
158