1 ;;; lilypond-font-lock.el --- syntax coloring for LilyPond mode
3 ;; Copyright (C) 1992,1993,1994 Tim Peters
5 ;; Author: 2001-2002: 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
14 ;; Last Modified: 23NOV2002
15 ;; Keywords: lilypond languages music notation
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.
23 ;; This started out as a cannabalised version of python-mode.el, by hwn
24 ;; For changes see the LilyPond ChangeLog
28 ;; - handle lexer modes (\header, \melodic, \lyric) etc.
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 "\\|"))
37 ;; Fonts in use (from GNU Emacs Lisp Reference Manual, elisp.ps):
38 ;; font-lock- comment / string / keyword / builtin / function-name /
39 ;; variable-name / type / constant / warning -face
41 ;; The order below is designed so that proofreading would be possible.
44 ;; ... first identifiers and keywords.
45 ;; ... then other expressions having '\'-mark in the beginning.
46 ;; ... the right and the left side of '='-marks.
47 ;; ... reserved words, e.g., FiguredBass.
48 ;; ... notes and rests
49 ;; "on top", 6) ... '<{[]}>'-brackets
50 ;; "on top", 7) ... ties, slurs and hairpins
51 ;; "on top", 8) ... (multiline-)scheme; urgh. one should count the slurs
52 ;; "on top", 9) ... strings
53 ;; "on top", 10) ... (multiline-)comments
55 ;; One should note 'font-lock-multiline' has been possible since Emacs 21.1.
56 ;; See, e.g., text in "http://emacs.kldp.org/emacs-21.1/etc/NEWS".
58 ;; ... identifiers (defined above, see iregex)
59 (cons (concat "\\(\\([_^-]?\\(" iregex "\\)\\)+\\)\\($\\|[] \t(~{}>\\\\_()^*-]\\)") '(1 font-lock-function-name-face))
61 ;; ... keywords (defined above, see kwregex)
62 (cons (concat "\\(\\([_^-]?\\(" kwregex "\\)\\)+\\)\\($\\|[] \t(~{}>\\\\_()^*-]\\)") '(1 font-lock-keyword-face))
64 ;; ... keyword-type constructs, e.g., ^\abracadabra; not \breve (= a duration)
65 '("\\([_^-]?\\\\\\([^b]\\|b[^r]\\|br[^e]\\|bre[^v]\\|brev[^e]\\|breve[a-zA-Z]\\)[a-zA-Z]*\\)" 1 font-lock-constant-face)
67 ;; ... the left side of '=' -mark
68 '("\\([_a-zA-Z.0-9-]+\\)[ \t]*=[ \t]*" 1 font-lock-variable-name-face)
70 ;; ... the right side of '=' -mark
71 '("[ \t]*=[ \t]*\\([_a-zA-Z.0-9-]+\\)" 1 font-lock-variable-name-face)
73 ;; ... reserved words (defined above, see rwregex)
74 (cons (concat "\\(" rwregex "\\)") 'font-lock-variable-name-face)
76 ;; ... notes and rests, accidentals and duration (multiplied), e.g., b,?16.*3/4
77 '("\\(^\\|[ <\{[~(!)\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\\)[.]*\\([ \t]*[*][ \t]*[0-9]+\\(/[1-9][0-9]*\\)?\\)?\\)?\\)" 2 font-lock-type-face)
79 ;; "on top", ... '{[]}'-brackets
80 '("\\([][}{]\\)" 0 font-lock-warning-face t)
82 ;; "on top", ... '<>'-brackets, not marcato '->'
83 '("\\(\\(-.\\)+\\|[^-^_]\\)\\([[<>]+\\)" 3 font-lock-warning-face t)
85 ;; "on top", ... ties ~, slurs \( () \), hairpins \<, \>, \!
86 '("\\(\\\\[(<!>)]\\|[(~)]\\)" 0 font-lock-builtin-face t)
88 ;; "on top", ... (multiline-)scheme: try find slurs up to 7th
89 '("[_^-]?#\\(#[ft]\\|-?[0-9.]+\\|\"[^\"]*\"\\|['`]?[a-zA-Z-:]+\\|['`]?([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^()]*\\(([^)]*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*)[^()]*\\)*[^)]*)\\)" 0 font-lock-string-face t)
91 ;; "on top", ... strings
92 '("\\([_^-]?\"\\([^\"\\\\]\\|\\\\.\\|\\\\\n\\)*\"\\)" 0 font-lock-string-face t)
94 ;; "on top", ... (multiline-)comments
95 '("\\(%\\({[^%]*%\\(}\\|\\([^}][^%]*%\\)+}\\)\\|.*\\)\\)" 0 font-lock-comment-face t)
99 "Additional expressions to fontify in LilyPond mode.")
101 ;; define a mode-specific abbrev table for those who use such things
102 (defvar LilyPond-mode-abbrev-table nil
103 "Abbrev table in use in `LilyPond-mode' buffers.")
105 (define-abbrev-table 'LilyPond-mode-abbrev-table nil)
107 (defvar LilyPond-mode-syntax-table nil
108 "Syntax table used in `LilyPond-mode' buffers.")
111 (if LilyPond-mode-syntax-table
113 (setq LilyPond-mode-syntax-table (make-syntax-table))
115 (lambda (x) (modify-syntax-entry
116 (car x) (cdr x) LilyPond-mode-syntax-table)))
117 '(( ?\( . "." ) ( ?\) . "." )
118 ( ?\[ . "(]" ) ( ?\] . ")[" ) ;; all the other paren characters are now handled by
119 ( ?\{ . ". 2b" ) ;; lily-specific indenting/matching code in lilypond-indent.el
121 ( ?\< . "." )( ?\> . ".")
122 ( ?\$ . "." ) ( ?\& . "." )
123 ( ?\* . "." ) ( ?\+ . "." )
124 ( ?\/ . "." ) ( ?\= . "." )
125 ( ?\| . "." ) (?\\ . "\\" )
126 ( ?\- . "." ) ( ?\_ . "." ) ( ?\^ . "." ) ; accent positioners: puctuation characters
127 ( ?\' . "w") ( ?\, . "w") ; transposing octaves, parts of words (notes)
128 ( ?\" . "\"" ) ; string quote characters
129 ( ?\% . "< 1b3b" ) ; (block-)comment starter (or ender)
130 ( ?\n . ">") ; newline: comment ender
131 ( ?\r . ">") ; formfeed: comment ender