]> git.donarmstrong.com Git - lilypond.git/blob - scm/display-lily.scm
T1247 - Conditionally do (use-modules (ice-9 curried-definitions)).
[lilypond.git] / scm / display-lily.scm
1 ;;; display-lily.scm -- Display music expressions using LilyPond notation
2 ;;;
3 ;;;
4 ;;;
5 ;;; Copyright (C) 2005--2011 Nicolas Sceaux  <nicolas.sceaux@free.fr>
6 ;;;
7
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
12 ;;; type.
13 ;;;
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
17 ;;; music durations.
18 ;;;
19 ;;; - `with-music-match' can be used to destructure a music expression, extracting
20 ;;; some interesting music properties.
21
22
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)
31   #:use-module (lily))
32
33 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 ;;;
35 ;;; Display method definition and call
36 ;;;
37
38
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)
45           ...body...))"
46   `(let ((type-props (hashq-ref music-name-to-property-table
47                                 ',music-type '()))
48          (method (lambda ,vars
49                    ,@body)))
50      (set! type-props
51            (assoc-set! type-props 'display-methods (list method)))
52      (hashq-set! music-name-to-property-table
53                  ',music-type
54                  type-props)
55      method))
56
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
63                                  ',music-type '()))
64           (methods (assoc-ref type-props 'display-methods))
65           (new-method (lambda ,vars
66                         ,@body)))
67      (set! type-props
68            (assoc-set! type-props
69                        'display-methods
70                        (cons new-method methods)))
71      (hashq-set! music-name-to-property-table
72                  ',music-type
73                  type-props)
74      new-method))
75
76 (define* (tag->lily-string expr #:optional (post-event? #f))
77   (format #f "~{~a ~}"
78           (map (lambda (tag)
79                  (format #f "~a\\tag #'~a" (if post-event? "-" "") tag))
80                (ly:music-property expr 'tags))))
81
82 (define-public (music->lily-string expr parser)
83   "Print @var{expr}, a music expression, in LilyPond syntax."
84   (if (ly:music? expr)
85       (let* ((music-type (ly:music-property expr 'name))
86              (procs (assoc-ref (hashq-ref music-name-to-property-table
87                                           music-type '())
88                                'display-methods))
89              (result-string (and procs (any (lambda (proc)
90                                               (proc expr parser))
91                                             procs))))
92         (if result-string
93             (format #f "~a~a" 
94                     (tag->lily-string expr (post-event? expr))
95                     result-string)
96             (format #f "%{ Print method not implemented for music type ~a %}"
97                     music-type)))
98       (format #f "%{ expecting a music expression: ~a %}" expr)))
99
100 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
101 ;;;
102 ;;; Music pattern matching
103 ;;; 
104
105 (define (var? x)
106   (and (symbol? x) (char=? #\? (string-ref (symbol->string x) 0))))
107
108 (define (music? x)
109   (and (pair? x) (eqv? (car x) 'music)))
110
111 (define (music-list? x)
112   (and (pair? x)
113        (every music? x)))
114
115 (define (music-or-var-list? x)
116   (and (pair? x)
117        (every (lambda (e)
118                 (or (music? e) (var? e)))
119               x)))
120
121 (define (key-val-list->alist lst)
122   (define (key-val-list->alist-aux lst prev-result)
123     (if (null? lst)
124         prev-result
125         (key-val-list->alist-aux (cddr lst)
126                                  (cons (cons (first lst) (second lst))
127                                        prev-result))))
128   (reverse! (key-val-list->alist-aux lst (list))))
129
130 (define (gen-condition expr pattern)
131   "Helper function for `with-music-match'.
132 Generate an form that checks if the properties of `expr'
133 match thoses described in `pattern'."
134   (let* (;; all (property . value) found at the first depth in pattern,
135          ;; including a (name . <Musictype>) pair.
136          (pat-all-props (cons (cons 'name (second pattern))
137                               (key-val-list->alist (cddr pattern))))
138          ;; all (property . value) pairs found in pattern, where value is not
139          ;; a ?var, a music expression or a music list.
140          (prop-vals (remove (lambda (kons)
141                              (or (var? (cdr kons))
142                                  (music? (cdr kons))
143                                  (music-or-var-list? (cdr kons))))
144                             pat-all-props))
145          ;; list of (property . element) pairs, where element is a music expression
146          (element-list (filter (lambda (kons) (music? (cdr kons)))
147                                pat-all-props))
148          ;; list of (property . (e1 e2 ..)) pairs, where (e1 e2 ...) is a 
149          ;; list a music expressions
150          (elements-list (filter (lambda (kons) (music-or-var-list? (cdr kons)))
151                                 pat-all-props)))
152     `(and 
153       ;; a form that checks that `expr' is a music expression
154       ;; before actually accessing its properties...
155       (ly:music? ,expr)
156       ;; a form that checks that `expr' properties have the same
157       ;; values as those given in `pattern'
158       ,@(map (lambda (prop-val)
159                (let ((prop (car prop-val))
160                      (val (cdr prop-val)))
161                  `(and (not (null? (ly:music-property ,expr ',prop)))
162                        (equal? (ly:music-property ,expr ',prop) ,val))))
163              prop-vals)
164       ;; build the test condition for each element found in a (property . element) pair.
165       ;; (typically, property will be 'element)
166       ,@(map (lambda (prop-element)
167                (gen-condition `(ly:music-property ,expr ',(car prop-element)) (cdr prop-element)))
168              element-list)
169       ;; build the test conditions for each element found in a (property . (e1 e2 ...)) pair.
170       ;; this requires accessing to an element of a list, hence the index.
171       ;; (typically, property will be 'elements)
172       ,@(map (lambda (prop-elements)
173                (let ((ges (gensym))
174                      (index -1))
175                  `(and ,@(map (lambda (e)
176                                 (set! index (1+ index))
177                                 (if (music? e)
178                                     (gen-condition `(and (> (length (ly:music-property ,expr ',(car prop-elements)))
179                                                             ,index)
180                                                          (list-ref (ly:music-property ,expr ',(car prop-elements)) 
181                                                                    ,index))
182                                                    e)
183                                     #t))
184                               (cdr prop-elements)))))
185              elements-list))))
186
187 (define (gen-bindings expr pattern)
188   "Helper function for `with-music-match'.
189 Generate binding forms by looking for ?var symbol in pattern."
190   (let* (;; all (property . value) found at the first depth of pattern,
191          ;; including a (name . <Musictype>) pair.
192          (pat-all-props (cons (cons 'name (second pattern))
193                               (key-val-list->alist (cddr pattern))))
194          ;; all (property . ?var) pairs
195          (prop-vars (filter (lambda (kons) (var? (cdr kons)))
196                             pat-all-props))
197          ;; list of (property . element) pairs, where element is a music expression
198          (element-list (filter (lambda (kons) (music? (cdr kons)))
199                                pat-all-props))
200          ;; list of (property . (e1 e2 ..)) pairs, where (e1 e2 ...) is a 
201          ;; list a music expressions
202          (elements-list (filter (lambda (kons) (music-or-var-list? (cdr kons)))
203                                 pat-all-props)))
204     (append 
205      ;; the binding form for the ?var variable found in pattern (first depth).
206      ;; ?var is bound to the value of `expr' property
207      (map (lambda (prop-var)
208             `(,(cdr prop-var) (ly:music-property ,expr ',(car prop-var))))
209           prop-vars)
210      ;; generate bindings for each element found in a (property . element) pair.
211      ;; (typically, property will be 'element)
212      (append-map (lambda (prop-element)
213                    (gen-bindings `(ly:music-property ,expr ',(car prop-element))
214                                  (cdr prop-element)))
215                  element-list)
216      ;; generate bindings for each element found in a (property . (e1 e2 ...)) pair
217      ;; (typically, property will be 'elements)
218             (append-map (lambda (prop-elements)
219                           (let ((index -1))
220                             (append-map (lambda (e)
221                                           (set! index (1+ index))
222                                           (if (var? e)
223                                               `((,e (list-ref (ly:music-property ,expr ',(car prop-elements)) ,index)))
224                                               (gen-bindings `(list-ref (ly:music-property ,expr ',(car prop-elements))
225                                                                        ,index)
226                                                             e)))
227                                         (cdr prop-elements))))
228                         elements-list))))
229
230 (define-macro (with-music-match music-expr+pattern . body)
231   "If `music-expr' matches `pattern', call `body'.  `pattern' should look like:
232   '(music <MusicType>
233      property value
234      property ?var1
235      element (music <MusicType> ...)
236      elements ((music <MusicType> ...)
237                ?var2
238                (music <MusicType> ...)))
239 The properties of `music-expr' are checked against the values given in the
240 pattern (the name property being the <MusicType> symbol after the `music'
241 keyword), then all music expression found in its properties (such as 'element
242 or 'elements).
243 When ?var is found instead of a property value, ?var is bound that property value,
244 as read inside `music-expr'.  ?var may also be used to refere to a whole music 
245 expression inside an elements list for instance.  These bindings are accessible 
246 inside body."
247   (let ((music-expr (first music-expr+pattern))
248         (pattern (second music-expr+pattern))
249         (expr-sym (gensym)))
250     `(let ((,expr-sym ,music-expr))
251        (if ,(gen-condition expr-sym pattern)
252            (let ,(gen-bindings expr-sym pattern)
253              ,@body)
254            #f))))
255
256 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
257 ;;;
258 ;;; Special parameters
259 ;;;
260
261 ;;; indentation
262 (define-public *indent* (make-parameter 0))
263
264 ;;; set to #t to force duration printing
265 (define-public *force-duration* (make-parameter #f))
266
267 ;;; last duration found
268 (define-public *previous-duration* (make-parameter (ly:make-duration 2)))
269
270 ;;; Set to #t to force a line break with some kinds of expressions (eg sequential music)
271 (define *force-line-break* (make-parameter #t))
272 (define *max-element-number-before-break* (make-parameter 6))
273
274 ;; \times factor (used in durations)
275 (define *time-factor-denominator* (make-parameter #f))
276 (define *time-factor-numerator* (make-parameter #f))
277
278 (define *current-context* (make-parameter 'Bottom))
279
280 (define *explicit-mode* (make-parameter #t))
281
282 (define (new-line->lily-string)
283   (format #f "~%~v_" (max 0 (1- (*indent*)))))
284
285 ;;;
286 ;;; music type predicate maker
287 ;;;
288
289 (define (make-music-type-predicate . music-types)
290   (define make-music-type-predicate-aux
291     (lambda (mtypes)
292       (lambda (expr)
293         (if (null? mtypes)
294             #f
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))
298
299 (ly:load "define-music-display-methods.scm")
300