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