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