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