]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-font-lock.el
* lily/separation-item.cc: add padding property.
[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 (defun LilyPond-mode-set-syntax-table (&optional not-punct)
128   "Change syntax table which can be customized according to a context."
129   (interactive)
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 which can be customized according to a context."
173   ;; make a syntax table without parentheses
174   (interactive)
175   ;; default map sets parentheses to punctuation characters
176   (LilyPond-mode-set-syntax-table) 
177   ;; find the context
178   (setq context (parse-partial-sexp (point-min) (point)))
179   (cond ((nth 3 context)) ; inside string
180         ((nth 4 context)) ; inside a comment
181         ((memq (char-before (point)) '( ?\) ))
182          (LilyPond-mode-set-syntax-table '( ?\( ?\) )))
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-after (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         ))