1 ;;; display-lily.scm -- Display music expressions using LilyPond notation
5 ;;; Copyright (C) 2005--2012 Nicolas Sceaux <nicolas.sceaux@free.fr>
8 ;;; - This file defines the procedures used to define display methods for each
9 ;;; music type: define-display-method and define-extra-display-method.
10 ;;; See scm/define-music-display-methods.scm
11 ;;; Display methods are stored in the `display-methods' property of each music
14 ;;; - `music->lily-string' return a string describing a music expression using
15 ;;; LilyPond notation. The special variables *indent*, *previous-duration*,
16 ;;; and *force-duration* influence the indentation level and the display of
19 ;;; - `with-music-match' can be used to destructure a music expression, extracting
20 ;;; some interesting music properties.
23 (define-module (scm display-lily)
24 #:use-module (ice-9 optargs)
25 #:use-module (ice-9 format)
26 #:use-module (ice-9 regex)
27 #:use-module (ice-9 pretty-print)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-13)
30 #:use-module (srfi srfi-39)
33 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 ;;; Display method definition and call
39 (define-macro (define-display-method music-type vars . body)
40 "Define a display method for a music type and store it in the
41 `display-methods' property of the music type entry found in the
42 `music-name-to-property-table' hash table. Print methods previously
43 defined for that music type are lost.
44 Syntax: (define-display-method MusicType (expression parser)
46 `(let ((type-props (hashq-ref music-name-to-property-table
51 (assoc-set! type-props 'display-methods (list method)))
52 (hashq-set! music-name-to-property-table
57 (define-macro (define-extra-display-method music-type vars . body)
58 "Add a display method for a music type. A primary display method
59 is supposed to have been previously defined with `define-display-method'.
60 This new method should return a string or #f. If #f is returned, the next
61 display method will be called."
62 `(let* ((type-props (hashq-ref music-name-to-property-table
64 (methods (assoc-ref type-props 'display-methods))
65 (new-method (lambda ,vars
68 (assoc-set! type-props
70 (cons new-method methods)))
71 (hashq-set! music-name-to-property-table
76 (define* (tag->lily-string expr #:optional (post-event? #f))
79 (format #f "~a\\tag #'~a" (if post-event? "-" "") tag))
80 (ly:music-property expr 'tags))))
82 (define* (tweaks->lily-string expr #:optional (post-event? #f))
85 (format #f "~a\\tweak ~a #~a"
86 (if post-event? "-" "")
87 (if (pair? (car tweak))
89 (caar tweak) (cdar tweak))
90 (format #f "#'~a" (car tweak)))
91 (scheme-expr->lily-string (cdr tweak))))
92 (ly:music-property expr 'tweaks))))
94 (define-public (music->lily-string expr parser)
95 "Print @var{expr}, a music expression, in LilyPond syntax."
97 (let* ((music-type (ly:music-property expr 'name))
98 (procs (assoc-ref (hashq-ref music-name-to-property-table
101 (result-string (and procs (any (lambda (proc)
106 (tag->lily-string expr (post-event? expr))
107 (tweaks->lily-string expr (post-event? expr))
109 (format #f "%{ Print method not implemented for music type ~a %}"
111 (format #f "%{ expecting a music expression: ~a %}" expr)))
113 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
115 ;;; Music pattern matching
119 (and (symbol? x) (char=? #\? (string-ref (symbol->string x) 0))))
122 (and (pair? x) (eqv? (car x) 'music)))
124 (define (music-list? x)
128 (define (music-or-var-list? x)
131 (or (music? e) (var? e)))
134 (define (key-val-list->alist lst)
135 (define (key-val-list->alist-aux lst prev-result)
138 (key-val-list->alist-aux (cddr lst)
139 (cons (cons (first lst) (second lst))
141 (reverse! (key-val-list->alist-aux lst (list))))
143 (define (gen-condition expr pattern)
144 "Helper function for `with-music-match'.
145 Generate an form that checks if the properties of `expr'
146 match thoses described in `pattern'."
147 (let* (;; all (property . value) found at the first depth in pattern,
148 ;; including a (name . <Musictype>) pair.
149 (pat-all-props (cons (cons 'name (second pattern))
150 (key-val-list->alist (cddr pattern))))
151 ;; all (property . value) pairs found in pattern, where value is not
152 ;; a ?var, a music expression or a music list.
153 (prop-vals (remove (lambda (kons)
154 (or (var? (cdr kons))
156 (music-or-var-list? (cdr kons))))
158 ;; list of (property . element) pairs, where element is a music expression
159 (element-list (filter (lambda (kons) (music? (cdr kons)))
161 ;; list of (property . (e1 e2 ..)) pairs, where (e1 e2 ...) is a
162 ;; list a music expressions
163 (elements-list (filter (lambda (kons) (music-or-var-list? (cdr kons)))
166 ;; a form that checks that `expr' is a music expression
167 ;; before actually accessing its properties...
169 ;; a form that checks that `expr' properties have the same
170 ;; values as those given in `pattern'
171 ,@(map (lambda (prop-val)
172 (let ((prop (car prop-val))
173 (val (cdr prop-val)))
174 `(and (not (null? (ly:music-property ,expr ',prop)))
175 (equal? (ly:music-property ,expr ',prop) ,val))))
177 ;; build the test condition for each element found in a (property . element) pair.
178 ;; (typically, property will be 'element)
179 ,@(map (lambda (prop-element)
180 (gen-condition `(ly:music-property ,expr ',(car prop-element)) (cdr prop-element)))
182 ;; build the test conditions for each element found in a (property . (e1 e2 ...)) pair.
183 ;; this requires accessing to an element of a list, hence the index.
184 ;; (typically, property will be 'elements)
185 ,@(map (lambda (prop-elements)
188 `(and ,@(map (lambda (e)
189 (set! index (1+ index))
191 (gen-condition `(and (> (length (ly:music-property ,expr ',(car prop-elements)))
193 (list-ref (ly:music-property ,expr ',(car prop-elements))
197 (cdr prop-elements)))))
200 (define (gen-bindings expr pattern)
201 "Helper function for `with-music-match'.
202 Generate binding forms by looking for ?var symbol in pattern."
203 (let* (;; all (property . value) found at the first depth of pattern,
204 ;; including a (name . <Musictype>) pair.
205 (pat-all-props (cons (cons 'name (second pattern))
206 (key-val-list->alist (cddr pattern))))
207 ;; all (property . ?var) pairs
208 (prop-vars (filter (lambda (kons) (var? (cdr kons)))
210 ;; list of (property . element) pairs, where element is a music expression
211 (element-list (filter (lambda (kons) (music? (cdr kons)))
213 ;; list of (property . (e1 e2 ..)) pairs, where (e1 e2 ...) is a
214 ;; list a music expressions
215 (elements-list (filter (lambda (kons) (music-or-var-list? (cdr kons)))
218 ;; the binding form for the ?var variable found in pattern (first depth).
219 ;; ?var is bound to the value of `expr' property
220 (map (lambda (prop-var)
221 `(,(cdr prop-var) (ly:music-property ,expr ',(car prop-var))))
223 ;; generate bindings for each element found in a (property . element) pair.
224 ;; (typically, property will be 'element)
225 (append-map (lambda (prop-element)
226 (gen-bindings `(ly:music-property ,expr ',(car prop-element))
229 ;; generate bindings for each element found in a (property . (e1 e2 ...)) pair
230 ;; (typically, property will be 'elements)
231 (append-map (lambda (prop-elements)
233 (append-map (lambda (e)
234 (set! index (1+ index))
236 `((,e (list-ref (ly:music-property ,expr ',(car prop-elements)) ,index)))
237 (gen-bindings `(list-ref (ly:music-property ,expr ',(car prop-elements))
240 (cdr prop-elements))))
243 (define-macro (with-music-match music-expr+pattern . body)
244 "If `music-expr' matches `pattern', call `body'. `pattern' should look like:
248 element (music <MusicType> ...)
249 elements ((music <MusicType> ...)
251 (music <MusicType> ...)))
252 The properties of `music-expr' are checked against the values given in the
253 pattern (the name property being the <MusicType> symbol after the `music'
254 keyword), then all music expression found in its properties (such as 'element
256 When ?var is found instead of a property value, ?var is bound that property value,
257 as read inside `music-expr'. ?var may also be used to refere to a whole music
258 expression inside an elements list for instance. These bindings are accessible
260 (let ((music-expr (first music-expr+pattern))
261 (pattern (second music-expr+pattern))
263 `(let ((,expr-sym ,music-expr))
264 (if ,(gen-condition expr-sym pattern)
265 (let ,(gen-bindings expr-sym pattern)
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
271 ;;; Special parameters
275 (define-public *indent* (make-parameter 0))
277 ;;; set to #t to force duration printing
278 (define-public *force-duration* (make-parameter #f))
280 ;;; last duration found
281 (define-public *previous-duration* (make-parameter (ly:make-duration 2)))
283 ;;; Set to #t to force a line break with some kinds of expressions (eg sequential music)
284 (define *force-line-break* (make-parameter #t))
285 (define *max-element-number-before-break* (make-parameter 6))
287 ;; \times factor (used in durations)
288 (define *time-scale* (make-parameter 1))
290 (define *current-context* (make-parameter 'Bottom))
292 (define *explicit-mode* (make-parameter #t))
294 (define (new-line->lily-string)
295 (format #f "~%~v_" (max 0 (1- (*indent*)))))
298 ;;; music type predicate maker
301 (define (make-music-type-predicate . music-types)
302 (define make-music-type-predicate-aux
307 (or (eqv? (car mtypes) (ly:music-property expr 'name))
308 ((make-music-type-predicate-aux (cdr mtypes)) expr))))))
309 (make-music-type-predicate-aux music-types))
311 (ly:load "define-music-display-methods.scm")