1 ;;; display-lily.scm -- Display music expressions using LilyPond notation
5 ;;; (c) 2005--2007 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)
32 #:use-syntax (srfi srfi-39)
33 #:use-syntax (ice-9 optargs))
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;;; Display method definition and call
41 (define-macro (define-display-method music-type vars . body)
42 "Define a display method for a music type and store it in the
43 `display-methods' property of the music type entry found in the
44 `music-name-to-property-table' hash table. Print methods previously
45 defined for that music type are lost.
46 Syntax: (define-display-method MusicType (expression parser)
48 `(let ((type-props (hashq-ref music-name-to-property-table
53 (assoc-set! type-props 'display-methods (list method)))
54 (hashq-set! music-name-to-property-table
59 (define-macro (define-extra-display-method music-type vars . body)
60 "Add a display method for a music type. A primary display method
61 is supposed to have been previously defined with `define-display-method'.
62 This new method should return a string or #f. If #f is returned, the next
63 display method will be called."
64 `(let* ((type-props (hashq-ref music-name-to-property-table
66 (methods (assoc-ref type-props 'display-methods))
67 (new-method (lambda ,vars
70 (assoc-set! type-props
72 (cons new-method methods)))
73 (hashq-set! music-name-to-property-table
78 (define* (tag->lily-string expr #:optional (post-event? #f))
81 (format #f "~a\\tag #'~a" (if post-event? "-" "") tag))
82 (ly:music-property expr 'tags))))
84 (define-public (music->lily-string expr parser)
85 "Print expr, a music expression, in LilyPond syntax"
87 (let* ((music-type (ly:music-property expr 'name))
88 (procs (assoc-ref (hashq-ref music-name-to-property-table
91 (result-string (and procs (any (lambda (proc)
96 (tag->lily-string expr (post-event? expr))
98 (format #f "%{ Print method not implemented for music type ~a %}"
100 (format #f "%{ expecting a music expression: ~a %}" expr)))
102 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
104 ;;; Music pattern matching
108 (and (symbol? x) (char=? #\? (string-ref (symbol->string x) 0))))
111 (and (pair? x) (eqv? (car x) 'music)))
113 (define (music-list? x)
117 (define (music-or-var-list? x)
120 (or (music? e) (var? e)))
123 (define (key-val-list->alist lst)
124 (define (key-val-list->alist-aux lst prev-result)
127 (key-val-list->alist-aux (cddr lst)
128 (cons (cons (first lst) (second lst))
130 (reverse! (key-val-list->alist-aux lst (list))))
132 (define (gen-condition expr pattern)
133 "Helper function for `with-music-match'.
134 Generate an form that checks if the properties of `expr'
135 match thoses described in `pattern'."
136 (let* (;; all (property . value) found at the first depth in pattern,
137 ;; including a (name . <Musictype>) pair.
138 (pat-all-props (cons (cons 'name (second pattern))
139 (key-val-list->alist (cddr pattern))))
140 ;; all (property . value) pairs found in pattern, where value is not
141 ;; a ?var, a music expression or a music list.
142 (prop-vals (remove (lambda (kons)
143 (or (var? (cdr kons))
145 (music-or-var-list? (cdr kons))))
147 ;; list of (property . element) pairs, where element is a music expression
148 (element-list (filter (lambda (kons) (music? (cdr kons)))
150 ;; list of (property . (e1 e2 ..)) pairs, where (e1 e2 ...) is a
151 ;; list a music expressions
152 (elements-list (filter (lambda (kons) (music-or-var-list? (cdr kons)))
155 ;; a form that checks that `expr' is a music expression
156 ;; before actually accessing its properties...
158 ;; a form that checks that `expr' properties have the same
159 ;; values as those given in `pattern'
160 ,@(map (lambda (prop-val)
161 (let ((prop (car prop-val))
162 (val (cdr prop-val)))
163 `(and (not (null? (ly:music-property ,expr ',prop)))
164 (equal? (ly:music-property ,expr ',prop) ,val))))
166 ;; build the test condition for each element found in a (property . element) pair.
167 ;; (typically, property will be 'element)
168 ,@(map (lambda (prop-element)
169 (gen-condition `(ly:music-property ,expr ',(car prop-element)) (cdr prop-element)))
171 ;; build the test conditions for each element found in a (property . (e1 e2 ...)) pair.
172 ;; this requires accessing to an element of a list, hence the index.
173 ;; (typically, property will be 'elements)
174 ,@(map (lambda (prop-elements)
177 `(and ,@(map (lambda (e)
178 (set! index (1+ index))
180 (gen-condition `(and (> (length (ly:music-property ,expr ',(car prop-elements)))
182 (list-ref (ly:music-property ,expr ',(car prop-elements))
186 (cdr prop-elements)))))
189 (define (gen-bindings expr pattern)
190 "Helper function for `with-music-match'.
191 Generate binding forms by looking for ?var symbol in pattern."
192 (let* (;; all (property . value) found at the first depth of pattern,
193 ;; including a (name . <Musictype>) pair.
194 (pat-all-props (cons (cons 'name (second pattern))
195 (key-val-list->alist (cddr pattern))))
196 ;; all (property . ?var) pairs
197 (prop-vars (filter (lambda (kons) (var? (cdr kons)))
199 ;; list of (property . element) pairs, where element is a music expression
200 (element-list (filter (lambda (kons) (music? (cdr kons)))
202 ;; list of (property . (e1 e2 ..)) pairs, where (e1 e2 ...) is a
203 ;; list a music expressions
204 (elements-list (filter (lambda (kons) (music-or-var-list? (cdr kons)))
207 ;; the binding form for the ?var variable found in pattern (first depth).
208 ;; ?var is bound to the value of `expr' property
209 (map (lambda (prop-var)
210 `(,(cdr prop-var) (ly:music-property ,expr ',(car prop-var))))
212 ;; generate bindings for each element found in a (property . element) pair.
213 ;; (typically, property will be 'element)
214 (append-map (lambda (prop-element)
215 (gen-bindings `(ly:music-property ,expr ',(car prop-element))
218 ;; generate bindings for each element found in a (property . (e1 e2 ...)) pair
219 ;; (typically, property will be 'elements)
220 (append-map (lambda (prop-elements)
222 (append-map (lambda (e)
223 (set! index (1+ index))
225 `((,e (list-ref (ly:music-property ,expr ',(car prop-elements)) ,index)))
226 (gen-bindings `(list-ref (ly:music-property ,expr ',(car prop-elements))
229 (cdr prop-elements))))
232 (define-macro (with-music-match music-expr+pattern . body)
233 "If `music-expr' matches `pattern', call `body'. `pattern' should look like:
237 element (music <MusicType> ...)
238 elements ((music <MusicType> ...)
240 (music <MusicType> ...)))
241 The properties of `music-expr' are checked against the values given in the
242 pattern (the name property being the <MusicType> symbol after the `music'
243 keyword), then all music expression found in its properties (such as 'element
245 When ?var is found instead of a property value, ?var is bound that property value,
246 as read inside `music-expr'. ?var may also be used to refere to a whole music
247 expression inside an elements list for instance. These bindings are accessible
249 (let ((music-expr (first music-expr+pattern))
250 (pattern (second music-expr+pattern))
252 `(let ((,expr-sym ,music-expr))
253 (if ,(gen-condition expr-sym pattern)
254 (let ,(gen-bindings expr-sym pattern)
258 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
260 ;;; Special parameters
264 (define-public *indent* (make-parameter 0))
266 ;;; set to #t to force duration printing
267 (define-public *force-duration* (make-parameter #f))
269 ;;; last duration found
270 (define-public *previous-duration* (make-parameter (ly:make-duration 2)))
272 ;;; Set to #t to force a line break with some kinds of expressions (eg sequential music)
273 (define *force-line-break* (make-parameter #t))
274 (define *max-element-number-before-break* (make-parameter 6))
276 ;; \times factor (used in durations)
277 (define *time-factor-denominator* (make-parameter #f))
278 (define *time-factor-numerator* (make-parameter #f))
280 (define *current-context* (make-parameter 'Bottom))
282 (define *explicit-mode* (make-parameter #t))
284 (define (new-line->lily-string)
285 (format #f "~%~v_" (max 0 (1- (*indent*)))))
288 ;;; music type predicate maker
291 (define (make-music-type-predicate . music-types)
292 (define ((make-music-type-predicate-aux mtypes) expr)
295 (or (eqv? (car mtypes) (ly:music-property expr 'name))
296 ((make-music-type-predicate-aux (cdr mtypes)) expr))))
297 (make-music-type-predicate-aux music-types))
299 (load "define-music-display-methods.scm")