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