1 ;;; define-music-display-methods.scm -- data for displaying music
2 ;;; expressions using LilyPond notation.
4 ;;; Copyright (C) 2005--2015 Nicolas Sceaux <nicolas.sceaux@free.fr>
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 ;;; Display method implementation
12 (define-module (scm display-lily))
17 (define (scheme-expr->lily-string scm-arg)
18 (cond ((or (number? scm-arg)
21 (format #f "~s" scm-arg))
22 ((or (symbol? scm-arg)
24 (format #f "'~s" scm-arg))
27 (or (procedure-name scm-arg)
28 (with-output-to-string
30 (pretty-print (procedure-source scm-arg)))))))
33 (with-output-to-string
35 (display-scheme-music scm-arg)))))))
40 (define-public (markup->lily-string markup-expr)
41 "Return a string describing, in LilyPond syntax, the given markup
43 (define (proc->command proc)
44 (let ((cmd-markup (symbol->string (procedure-name proc))))
45 (substring cmd-markup 0 (- (string-length cmd-markup)
46 (string-length "-markup")))))
47 (define (arg->string arg)
50 ((markup? arg) ;; a markup
51 (markup->lily-string-aux arg))
52 ((and (pair? arg) (every markup? arg)) ;; a markup list
53 (format #f "{~{ ~a~}}" (map-in-order markup->lily-string-aux arg)))
54 (else ;; a scheme argument
55 (format #f "#~a" (scheme-expr->lily-string arg)))))
56 (define (markup->lily-string-aux expr)
59 (let ((cmd (car expr))
61 (if (eqv? cmd simple-markup) ;; a simple markup
62 (format #f "~s" (car args))
63 (format #f "\\~a~{ ~a~}"
65 (map-in-order arg->string args))))))
66 (cond ((string? markup-expr)
67 (format #f "~s" markup-expr))
68 ((eqv? (car markup-expr) simple-markup)
69 (format #f "~s" (second markup-expr)))
71 (format #f "\\markup ~a"
72 (markup->lily-string-aux markup-expr)))))
78 ;; It is a pity that there is no rassoc in Scheme.
79 (define* (rassoc item alist #:optional (test equal?))
80 (do ((alist alist (cdr alist))
82 ((or result (null? alist)) result)
83 (if (and (car alist) (test item (cdar alist)))
84 (set! result (car alist)))))
86 (define-public (note-name->lily-string ly-pitch)
87 ;; here we define a custom pitch= function, since we do not want to
88 ;; test whether octaves are also equal. (otherwise, we would be using equal?)
89 (define (pitch= pitch1 pitch2)
90 (and (= (ly:pitch-notename pitch1) (ly:pitch-notename pitch2))
91 (= (ly:pitch-alteration pitch1) (ly:pitch-alteration pitch2))))
92 (let* ((pitches (ly:parser-lookup 'pitchnames))
93 (result (rassoc ly-pitch pitches pitch=)))
94 (and result (car result))))
96 (define-public (octave->lily-string pitch)
97 (let ((octave (ly:pitch-octave pitch)))
99 (make-string (1+ octave) #\'))
101 (make-string (1- (* -1 octave)) #\,))
107 (define*-public (duration->lily-string ly-duration #:key
109 (time-scale (*time-scale*)))
110 (let ((log2 (ly:duration-log ly-duration))
111 (dots (ly:duration-dot-count ly-duration))
112 (scale (ly:duration-scale ly-duration)))
113 (if (or force-duration (not (*omit-duration*)))
114 (string-append (case log2
118 (else (number->string (expt 2 log2))))
119 (make-string dots #\.)
120 (let ((end-scale (/ scale time-scale)))
121 (if (= end-scale 1) ""
122 (format #f "*~a" end-scale))))
129 (define post-event? (music-type-predicate 'post-event))
131 (define* (event-direction->lily-string event #:optional (required #t))
132 (let ((direction (ly:music-property event 'direction)))
133 (cond ((or (not direction) (null? direction) (= CENTER direction))
134 (if required "-" ""))
135 ((= UP direction) "^")
136 ((= DOWN direction) "_")
139 (define-macro (define-post-event-display-method type vars direction-required str)
140 `(define-display-method ,type ,vars
142 (event-direction->lily-string ,(car vars) ,direction-required)
145 (define-macro (define-span-event-display-method type vars direction-required str-start str-stop)
146 `(define-display-method ,type ,vars
148 (event-direction->lily-string ,(car vars) ,direction-required)
149 (if (= START (ly:music-property ,(car vars) 'span-direction))
153 (define-display-method HyphenEvent (event)
155 (define-display-method ExtenderEvent (event)
157 (define-display-method TieEvent (event)
159 (define-display-method BeamForbidEvent (event)
161 (define-display-method StringNumberEvent (event)
162 (format #f "\\~a" (ly:music-property event 'string-number)))
165 (define-display-method TremoloEvent (event)
166 (let ((tremolo-type (ly:music-property event 'tremolo-type 8)))
167 (format #f ":~a" tremolo-type)))
169 (define-display-method ArticulationEvent (event) #t
170 (let* ((articulation (ly:music-property event 'articulation-type))
172 (case (string->symbol articulation)
176 ((staccatissimo) "!")
181 (format #f "~a~:[\\~;~]~a"
182 (event-direction->lily-string event shorthand)
184 (or shorthand articulation))))
186 (define-post-event-display-method FingeringEvent (event) #t
187 (ly:music-property event 'digit))
189 (define-post-event-display-method TextScriptEvent (event) #t
190 (markup->lily-string (ly:music-property event 'text)))
192 (define-post-event-display-method MultiMeasureTextEvent (event) #t
193 (markup->lily-string (ly:music-property event 'text)))
195 (define-post-event-display-method BendAfterEvent (event) #f
196 (format #f "\\bendAfter #~a " (ly:music-property event 'delta-step)))
198 (define-post-event-display-method HarmonicEvent (event) #f "\\harmonic")
199 (define-post-event-display-method GlissandoEvent (event) #f "\\glissando")
200 (define-post-event-display-method ArpeggioEvent (event) #f "\\arpeggio")
201 (define-post-event-display-method AbsoluteDynamicEvent (event) #f
202 (format #f "\\~a" (ly:music-property event 'text)))
204 (define-post-event-display-method StrokeFingerEvent (event) #f
205 (format #f "\\rightHandFinger #~a " (ly:music-property event 'digit)))
207 (define-span-event-display-method BeamEvent (event) #f "[" "]")
208 (define-span-event-display-method SlurEvent (event) #f "(" ")")
209 (define-span-event-display-method CrescendoEvent (event) #f "\\<" "\\!")
210 (define-span-event-display-method DecrescendoEvent (event) #f "\\>" "\\!")
211 (define-span-event-display-method EpisemaEvent (event) #f "\\episemInitium" "\\episemFinis")
212 (define-span-event-display-method PhrasingSlurEvent (event) #f "\\(" "\\)")
213 (define-span-event-display-method SustainEvent (event) #f "\\sustainOn" "\\sustainOff")
214 (define-span-event-display-method SostenutoEvent (event) #f "\\sostenutoOn" "\\sostenutoOff")
215 (define-span-event-display-method TextSpanEvent (event) #f "\\startTextSpan" "\\stopTextSpan")
216 (define-span-event-display-method TrillSpanEvent (event) #f "\\startTrillSpan" "\\stopTrillSpan")
217 (define-span-event-display-method StaffSpanEvent (event) #f "\\startStaff" "\\stopStaff")
218 (define-span-event-display-method NoteGroupingEvent (event) #f "\\startGroup" "\\stopGroup")
219 (define-span-event-display-method UnaCordaEvent (event) #f "\\unaCorda" "\\treCorde")
225 (define-display-method GraceMusic (expr)
226 (format #f "\\grace ~a"
227 (music->lily-string (ly:music-property expr 'element))))
229 ;; \acciaccatura \appoggiatura \grace
230 ;; TODO: it would be better to compare ?start and ?stop
231 ;; with startAppoggiaturaMusic and stopAppoggiaturaMusic,
232 ;; using a custom music equality predicate.
233 (define-extra-display-method GraceMusic (expr)
234 "Display method for appoggiatura."
235 (with-music-match (expr (music
242 ;; we check whether ?start and ?stop look like
243 ;; startAppoggiaturaMusic stopAppoggiaturaMusic
244 (and (with-music-match (?start (music
251 span-direction START))))))
253 (with-music-match (?stop (music
260 span-direction STOP))))))
261 (format #f "\\appoggiatura ~a" (music->lily-string ?music))))))
264 (define-extra-display-method GraceMusic (expr)
265 "Display method for acciaccatura."
266 (with-music-match (expr (music
273 ;; we check whether ?start and ?stop look like
274 ;; startAcciaccaturaMusic stopAcciaccaturaMusic
275 (and (with-music-match (?start (music
282 span-direction START)))
287 grob-property-path '(stroke-style)
291 (with-music-match (?stop (music
297 grob-property-path '(stroke-style)
305 span-direction STOP))))))
306 (format #f "\\acciaccatura ~a" (music->lily-string ?music))))))
308 (define-extra-display-method GraceMusic (expr)
309 "Display method for grace."
310 (with-music-match (expr (music
317 ;; we check whether ?start and ?stop look like
318 ;; startGraceMusic stopGraceMusic
319 (and (null? (ly:music-property ?start 'elements))
320 (null? (ly:music-property ?stop 'elements))
321 (format #f "\\grace ~a" (music->lily-string ?music)))))
327 (define-display-method SequentialMusic (seq)
328 (let ((force-line-break (and (*force-line-break*)
330 (> (length (ly:music-property seq 'elements))
331 (*max-element-number-before-break*))))
332 (elements (ly:music-property seq 'elements))
333 (chord? (make-music-type-predicate 'EventChord))
334 (note-or-chord? (make-music-type-predicate 'EventChord 'NoteEvent
335 'LyricEvent 'RestEvent
337 (cluster? (make-music-type-predicate 'ClusterNoteEvent))
338 (note? (make-music-type-predicate 'NoteEvent)))
339 (format #f "~a~a{~v%~v_~{~a~^ ~}~v%~v_}"
343 (any cluster? (ly:music-property e 'elements)))))
347 (if (*explicit-mode*)
348 ;; if the sequence contains EventChord which contains figures ==> figuremode
349 ;; if the sequence contains EventChord which contains lyrics ==> lyricmode
350 ;; if the sequence contains EventChord which contains drum notes ==> drummode
351 (cond ((any (lambda (chord)
352 (any (make-music-type-predicate 'BassFigureEvent)
353 (ly:music-property chord 'elements)))
354 (filter chord? elements))
356 ((any (lambda (chord)
357 (any (make-music-type-predicate 'LyricEvent)
359 (ly:music-property chord 'elements))))
360 (filter note-or-chord? elements))
362 ((any (lambda (chord)
365 (not (null? (ly:music-property event 'drum-type)))))
367 (ly:music-property chord 'elements))))
368 (filter note-or-chord? elements))
370 (else ;; TODO: other modes?
373 (if force-line-break 1 0)
374 (if force-line-break (+ 2 (*indent*)) 1)
375 (parameterize ((*indent* (+ 2 (*indent*))))
376 (map-in-order (lambda (music)
377 (music->lily-string music))
379 (if force-line-break 1 0)
380 (if force-line-break (*indent*) 1))))
382 (define-display-method SimultaneousMusic (sim)
383 (parameterize ((*indent* (+ 3 (*indent*))))
384 (format #f "<< ~{~a ~}>>"
385 (map-in-order (lambda (music)
386 (music->lily-string music))
387 (ly:music-property sim 'elements)))))
389 (define-extra-display-method SimultaneousMusic (expr)
390 "If `sim' is an \afterGrace expression, return \"\\afterGrace ...\".
391 Otherwise, return #f."
392 ;; TODO: do something with afterGraceFraction?
393 (with-music-match (expr (music 'SimultaneousMusic
394 elements (?before-grace
395 (music 'SequentialMusic
396 elements ((music 'SkipMusic)
399 (format #f "\\afterGrace ~a ~a"
400 (music->lily-string ?before-grace)
401 (music->lily-string ?grace))))
407 (define-display-method EventChord (chord)
408 ;; event_chord : command_element
409 ;; | note_chord_element
411 ;; TODO : tagged post_events
412 ;; post_events : ( post_event | tagged_post_event )*
413 ;; tagged_post_event: '-' \tag embedded_scm post_event
415 (let* ((elements (append (ly:music-property chord 'elements)
416 (ly:music-property chord 'articulations)))
417 (chord-repeat (ly:music-property chord 'duration)))
420 (partition (music-type-predicate 'rhythmic-event)
422 (lambda (chord-elements other-elements)
423 (cond ((pair? chord-elements)
424 ;; note_chord_element :
425 ;; '<' (notepitch | drumpitch)* '>" duration post_events
426 (let ((duration (duration->lily-string (ly:music-property
429 ;; Format duration first so that it does not appear on
431 (format #f "< ~{~a ~}>~a~:{~:[-~;~]~a~^ ~}"
432 (parameterize ((*omit-duration* #t))
435 (music->lily-string music))
438 (map-in-order (lambda (music)
441 (music->lily-string music)))
443 ((ly:duration? chord-repeat)
444 (let ((duration (duration->lily-string chord-repeat)))
445 (format #f "q~a~:{~:[-~;~]~a~^ ~}"
447 (map-in-order (lambda (music)
450 (music->lily-string music)))
453 ((and (= 1 (length other-elements))
454 (not (post-event? (car other-elements))))
455 (format #f (music->lily-string (car other-elements))))
457 (format #f "< >~:{~:[-~;~]~a~^ ~}"
458 (map-in-order (lambda (music)
461 (music->lily-string music)))
462 other-elements))))))))
464 (define-display-method MultiMeasureRestMusic (mmrest)
465 (format #f "R~a~{~a~^ ~}"
466 (duration->lily-string (ly:music-property mmrest 'duration))
467 (map-in-order (lambda (music)
468 (music->lily-string music))
469 (ly:music-property mmrest 'articulations))))
471 (define-display-method SkipMusic (skip)
472 (format #f "\\skip ~a" (duration->lily-string (ly:music-property skip 'duration) #:force-duration #t)))
474 (define-display-method OttavaMusic (ottava)
475 (format #f "\\ottava #~a" (ly:music-property ottava 'ottava-number)))
478 ;;; Notes, rests, skips...
481 (define (simple-note->lily-string event)
482 (format #f "~a~a~a~a~a~a~:{~:[-~;~]~a~}" ; pitchname octave !? octave-check duration optional_rest articulations
483 (note-name->lily-string (ly:music-property event 'pitch))
484 (octave->lily-string (ly:music-property event 'pitch))
485 (let ((forced (ly:music-property event 'force-accidental))
486 (cautionary (ly:music-property event 'cautionary)))
487 (cond ((and (not (null? forced))
489 (not (null? cautionary))
492 ((and (not (null? forced)) forced) "!")
494 (let ((octave-check (ly:music-property event 'absolute-octave)))
495 (if (not (null? octave-check))
496 (format #f "=~a" (cond ((>= octave-check 0)
497 (make-string (1+ octave-check) #\'))
499 (make-string (1- (* -1 octave-check)) #\,))
502 (duration->lily-string (ly:music-property event 'duration))
503 (if ((make-music-type-predicate 'RestEvent) event)
505 (map-in-order (lambda (event)
508 (music->lily-string event)))
509 (ly:music-property event 'articulations))))
511 (define-display-method NoteEvent (note)
512 (cond ((not (null? (ly:music-property note 'pitch))) ;; note
513 (simple-note->lily-string note))
514 ((not (null? (ly:music-property note 'drum-type))) ;; drum
515 (format #f "~a~a~{~a~}" (ly:music-property note 'drum-type)
516 (duration->lily-string (ly:music-property note 'duration))
517 (map-in-order (lambda (event)
518 (music->lily-string event))
519 (ly:music-property note 'articulations))))
522 (format #f "~a~{~a~}"
523 (duration->lily-string (ly:music-property note 'duration)
525 (map-in-order (lambda (event)
526 (music->lily-string event))
527 (ly:music-property note 'articulations))))))
529 (define-display-method ClusterNoteEvent (note)
530 (simple-note->lily-string note))
532 (define-display-method RestEvent (rest)
533 (if (not (null? (ly:music-property rest 'pitch)))
534 (simple-note->lily-string rest)
535 (format #f "r~a~{~a~}"
536 (duration->lily-string (ly:music-property rest 'duration))
537 (map-in-order (lambda (event)
538 (music->lily-string event))
539 (ly:music-property rest 'articulations)))))
541 (define-display-method MultiMeasureRestEvent (rest)
542 (string-append "R" (duration->lily-string (ly:music-property rest 'duration))))
544 (define-display-method SkipEvent (rest)
545 (format #f "s~a~{~a~}"
546 (duration->lily-string (ly:music-property rest 'duration))
547 (map-in-order (lambda (event)
548 (music->lily-string event))
549 (ly:music-property rest 'articulations))))
551 (define-display-method RepeatedChord (chord)
552 (music->lily-string (ly:music-property chord 'element)))
554 (define-display-method MarkEvent (mark)
555 (let ((label (ly:music-property mark 'label)))
558 (format #f "\\mark ~a" (markup->lily-string label)))))
560 (define-display-method KeyChangeEvent (key)
561 (let ((pitch-alist (ly:music-property key 'pitch-alist))
562 (tonic (ly:music-property key 'tonic)))
563 (if (or (null? pitch-alist)
566 (let ((c-pitch-alist (ly:transpose-key-alist pitch-alist
567 (ly:pitch-diff (ly:make-pitch 0 0 0) tonic))))
568 (format #f "\\key ~a \\~a~a"
569 (note-name->lily-string (ly:music-property key 'tonic))
571 (and (equal? (ly:parser-lookup mode) c-pitch-alist)
572 (symbol->string mode)))
573 '(major minor ionian locrian aeolian mixolydian lydian phrygian dorian))
574 (new-line->lily-string))))))
576 (define-display-method RelativeOctaveCheck (octave)
577 (let ((pitch (ly:music-property octave 'pitch)))
578 (format #f "\\octaveCheck ~a~a"
579 (note-name->lily-string pitch)
580 (octave->lily-string pitch))))
582 (define-display-method VoiceSeparator (sep)
585 (define-display-method LigatureEvent (ligature)
586 (if (= START (ly:music-property ligature 'span-direction))
590 (define-display-method BarCheck (check)
591 (format #f "|~a" (new-line->lily-string)))
593 (define-display-method PesOrFlexaEvent (expr)
596 (define-display-method BassFigureEvent (figure)
597 (let ((alteration (ly:music-property figure 'alteration))
598 (fig (ly:music-property figure 'figure))
599 (bracket-start (ly:music-property figure 'bracket-start))
600 (bracket-stop (ly:music-property figure 'bracket-stop)))
602 (format #f "~a~a~a~a"
603 (if (null? bracket-start) "" "[")
604 (cond ((null? fig) "_")
605 ((markup? fig) (second fig)) ;; fig: (<number-markup> "number")
607 (if (null? alteration)
610 ((= alteration DOUBLE-FLAT) "--")
611 ((= alteration FLAT) "-")
612 ((= alteration NATURAL) "!")
613 ((= alteration SHARP) "+")
614 ((= alteration DOUBLE-SHARP) "++")
616 (if (null? bracket-stop) "" "]"))))
618 (define-display-method LyricEvent (lyric)
619 (format #f "~a~{~a~^ ~}"
620 (let ((text (ly:music-property lyric 'text)))
621 (if (or (string? text)
622 (eqv? (first text) simple-markup))
623 ;; a string or a simple markup
624 (let ((string (if (string? text)
627 (if (string-match "(\"| |[0-9])" string)
628 ;; TODO check exactly in which cases double quotes should be used
629 (format #f "~s" string)
631 (markup->lily-string text)))
632 (map-in-order (lambda (m) (music->lily-string m))
633 (ly:music-property lyric 'articulations))))
635 (define-display-method BreathingEvent (event)
642 (define-display-method AutoChangeMusic (m)
643 (format #f "\\autochange ~a"
645 (ly:music-property (ly:music-property m 'element) 'element))))
647 (define-display-method ContextChange (m)
648 (format #f "\\change ~a = \"~a\""
649 (ly:music-property m 'change-to-type)
650 (ly:music-property m 'change-to-id)))
654 (define-display-method TimeScaledMusic (times)
655 (let* ((num (ly:music-property times 'numerator))
656 (den (ly:music-property times 'denominator))
657 (span (ly:music-property times 'duration #f))
658 ;; need to format before changing time scale
660 (and span (duration->lily-string span #:force-duration #t)))
662 (time-scale (*time-scale*)))
664 (parameterize ((*force-line-break* #f)
665 (*time-scale* (* time-scale scale)))
666 (format #f "\\tuplet ~a/~a ~@[~a ~]~a"
670 (music->lily-string (ly:music-property times 'element))))))
673 (define-display-method RelativeOctaveMusic (m)
674 (music->lily-string (ly:music-property m 'element)))
676 (define-display-method TransposedMusic (m)
677 (music->lily-string (ly:music-property m 'element)))
683 (define-display-method AlternativeEvent (alternative) "")
685 (define (repeat->lily-string expr repeat-type)
686 (let* ((main (music->lily-string (ly:music-property expr 'element))))
687 (format #f "\\repeat ~a ~a ~a ~a"
689 (ly:music-property expr 'repeat-count)
691 (let ((alternatives (ly:music-property expr 'elements)))
692 (if (null? alternatives)
694 (format #f "\\alternative { ~{~a ~}}"
695 (map-in-order (lambda (music)
696 (music->lily-string music))
699 (define-display-method VoltaRepeatedMusic (expr)
700 (repeat->lily-string expr "volta"))
702 (define-display-method UnfoldedRepeatedMusic (expr)
703 (repeat->lily-string expr "unfold"))
705 (define-display-method PercentRepeatedMusic (expr)
706 (repeat->lily-string expr "percent"))
708 (define-display-method TremoloRepeatedMusic (expr)
709 (repeat->lily-string expr "tremolo"))
715 (define-display-method ContextSpeccedMusic (expr)
716 (let ((id (ly:music-property expr 'context-id))
717 (create-new (ly:music-property expr 'create-new))
718 (music (ly:music-property expr 'element))
719 (operations (ly:music-property expr 'property-operations))
720 (ctype (ly:music-property expr 'context-type)))
721 (format #f "~a ~a~a~a ~a"
722 (if (and (not (null? create-new)) create-new)
728 (format #f " = ~s" id))
729 (if (null? operations)
731 (format #f " \\with {~{~a~}~%~v_}"
732 (parameterize ((*indent* (+ (*indent*) 2)))
734 (format #f "~%~v_\\~a ~s"
740 (parameterize ((*current-context* ctype))
741 (music->lily-string music)))))
743 ;; special cases: \figures \lyrics \drums
744 (define-extra-display-method ContextSpeccedMusic (expr)
745 (with-music-match (expr (music 'ContextSpeccedMusic
747 property-operations ?op
748 context-type ?context-type
751 (parameterize ((*explicit-mode* #f))
754 (format #f "\\figures ~a" (music->lily-string ?sequence)))
756 (format #f "\\lyrics ~a" (music->lily-string ?sequence)))
758 (format #f "\\drums ~a" (music->lily-string ?sequence)))
763 ;;; Context properties
765 (define-extra-display-method ContextSpeccedMusic (expr)
766 (let ((element (ly:music-property expr 'element))
767 (property-tuning? (make-music-type-predicate 'PropertySet
771 (sequence? (make-music-type-predicate 'SequentialMusic)))
772 (if (and (ly:music? element)
773 (or (property-tuning? element)
774 (and (sequence? element)
775 (every property-tuning? (ly:music-property element 'elements)))))
776 (parameterize ((*current-context* (ly:music-property expr 'context-type)))
777 (music->lily-string element))
780 (define-public (value->lily-string arg)
781 (cond ((ly:music? arg)
782 (music->lily-string arg))
784 (format #f "#~s" arg))
786 (markup->lily-string arg))
788 (format #f "##{ ~a #}" (duration->lily-string arg #:force-duration #t)))
791 (note-name->lily-string arg)
792 (octave->lily-string arg)))
794 (format #f "#~a" (scheme-expr->lily-string arg)))))
796 (define-display-method PropertySet (expr)
797 (let ((property (ly:music-property expr 'symbol))
798 (value (ly:music-property expr 'value))
799 (once (ly:music-property expr 'once)))
800 (format #f "~a\\set ~a~a = ~a~a"
801 (if (and (not (null? once)))
804 (if (eqv? (*current-context*) 'Bottom)
806 (format #f "~a . " (*current-context*)))
808 (value->lily-string value)
809 (new-line->lily-string))))
811 (define-display-method PropertyUnset (expr)
812 (format #f "~a\\unset ~a~a~a"
813 (if (ly:music-property expr 'once #f) "\\once " "")
814 (if (eqv? (*current-context*) 'Bottom)
816 (format #f "~a . " (*current-context*)))
817 (ly:music-property expr 'symbol)
818 (new-line->lily-string)))
820 ;;; Layout properties
822 (define-display-method OverrideProperty (expr)
823 (let* ((symbol (ly:music-property expr 'symbol))
824 (properties (ly:music-property expr 'grob-property-path
825 (list (ly:music-property expr 'grob-property))))
826 (value (ly:music-property expr 'grob-value))
827 (once (ly:music-property expr 'once)))
829 (format #f "~a\\override ~{~a~^.~} = ~a~a"
834 (if (eqv? (*current-context*) 'Bottom)
835 (cons symbol properties)
836 (cons* (*current-context*) symbol properties))
837 (value->lily-string value)
838 (new-line->lily-string))))
840 (define-display-method RevertProperty (expr)
841 (let* ((symbol (ly:music-property expr 'symbol))
842 (properties (ly:music-property expr 'grob-property-path
843 (list (ly:music-property expr
845 (once (ly:music-property expr 'once #f)))
846 (format #f "~a\\revert ~{~a~^.~}~a"
847 (if once "\\once " "")
848 (if (eqv? (*current-context*) 'Bottom)
849 (cons symbol properties)
850 (cons* (*current-context*) symbol properties))
851 (new-line->lily-string))))
853 (define-display-method TimeSignatureMusic (expr)
854 (let* ((num (ly:music-property expr 'numerator))
855 (den (ly:music-property expr 'denominator))
856 (structure (ly:music-property expr 'beat-structure)))
857 (if (null? structure)
861 (new-line->lily-string))
863 "\\time #'~a ~a/~a~a"
865 (new-line->lily-string)))))
867 ;;; \melisma and \melismaEnd
868 (define-extra-display-method ContextSpeccedMusic (expr)
869 "If expr is a melisma, return \"\\melisma\", otherwise, return #f."
870 (with-music-match (expr (music 'ContextSpeccedMusic
871 element (music 'PropertySet
873 symbol 'melismaBusy)))
876 (define-extra-display-method ContextSpeccedMusic (expr)
877 "If expr is a melisma end, return \"\\melismaEnd\", otherwise, return #f."
878 (with-music-match (expr (music 'ContextSpeccedMusic
879 element (music 'PropertyUnset
880 symbol 'melismaBusy)))
884 (define-extra-display-method SequentialMusic (expr)
885 (with-music-match (expr (music 'SequentialMusic
886 elements ((music 'TempoChangeEvent
889 metronome-count ?count)
890 (music 'ContextSpeccedMusic
891 element (music 'PropertySet
892 symbol 'tempoWholesPerMinute)))))
893 (format #f "\\tempo ~{~a~a~}~a = ~a~a"
895 (list (markup->lily-string ?text) " ")
897 (duration->lily-string ?unit #:force-duration #t)
899 (format #f "~a - ~a" (car ?count) (cdr ?count))
901 (new-line->lily-string))))
903 (define-display-method TempoChangeEvent (expr)
904 (let ((text (ly:music-property expr 'text)))
905 (format #f "\\tempo ~a~a"
906 (markup->lily-string text)
907 (new-line->lily-string))))
910 (define clef-name-alist #f)
911 (define-public (memoize-clef-names clefs)
912 "Initialize @code{clef-name-alist}, if not already set."
913 (if (not clef-name-alist)
914 (set! clef-name-alist
915 (map (lambda (name+vals)
916 (cons (cdr name+vals)
920 (define-extra-display-method ContextSpeccedMusic (expr)
921 "If @var{expr} is a clef change, return \"\\clef ...\".
922 Otherwise, return @code{#f}."
923 (with-music-match (expr (music 'ContextSpeccedMusic
925 element (music 'SequentialMusic
926 elements ((music 'PropertySet
930 symbol 'middleCClefPosition)
933 symbol 'clefPosition)
935 value ?clef-transposition
936 symbol 'clefTransposition)
938 value ?clef-transposition-style
939 symbol 'clefTranspositionStyle)
941 procedure ly:set-middle-C!)))))
942 (let ((clef-name (assoc-get (list ?clef-glyph ?clef-position 0)
945 (format #f "\\clef \"~a~?\"~a"
947 (case ?clef-transposition-style
948 ((parenthesized) "~a(~a)")
949 ((bracketed) "~a[~a]")
951 (cond ((zero? ?clef-transposition)
953 ((positive? ?clef-transposition)
954 (list "^" (1+ ?clef-transposition)))
955 (else (list "_" (- 1 ?clef-transposition))))
956 (new-line->lily-string))))))
959 (define-extra-display-method ContextSpeccedMusic (expr)
960 "If `expr' is a bar, return \"\\bar ...\".
961 Otherwise, return #f."
962 (with-music-match (expr (music 'ContextSpeccedMusic
964 element (music 'PropertySet
967 (format #f "\\bar \"~a\"~a" ?bar-type (new-line->lily-string))))
970 (define-extra-display-method ContextSpeccedMusic (expr)
971 "If `expr' is a partial measure, return \"\\partial ...\".
972 Otherwise, return #f."
973 (with-music-match (expr (music
980 duration ?duration))))
983 (format #f "\\partial ~a"
984 (duration->lily-string ?duration #:force-duration #t)))))
989 (define-display-method ApplyOutputEvent (applyoutput)
990 (let ((proc (ly:music-property applyoutput 'procedure))
991 (ctx (ly:music-property applyoutput 'context-type))
992 (grob (ly:music-property applyoutput 'symbol)))
993 (format #f "\\applyOutput ~a~@[.~a~] #~a"
995 (and (symbol? grob) grob)
996 (or (procedure-name proc)
997 (with-output-to-string
999 (pretty-print (procedure-source proc))))))))
1001 (define-display-method ApplyContext (applycontext)
1002 (let ((proc (ly:music-property applycontext 'procedure)))
1003 (format #f "\\applyContext #~a"
1004 (or (procedure-name proc)
1005 (with-output-to-string
1007 (pretty-print (procedure-source proc))))))))
1010 (define-display-method PartCombineMusic (expr)
1011 (let ((dir (ly:music-property expr 'direction)))
1012 (format #f "\\partcombine~a ~a~a~a"
1013 (cond ((equal? dir UP) "Up")
1014 ((equal? dir DOWN) "Down")
1016 (music->lily-string (car (ly:music-property expr 'elements)))
1017 (new-line->lily-string)
1018 (music->lily-string (cadr (ly:music-property expr 'elements))))))
1020 (define-display-method PartCombinePartMusic (expr)
1021 (with-music-match ((ly:music-property expr 'element)
1022 (music 'ContextSpeccedMusic element ?part))
1023 (format #f "~a" (music->lily-string ?part))))
1025 (define-extra-display-method ContextSpeccedMusic (expr)
1026 "If `expr' is a \\partcombine expression, return \"\\partcombine ...\".
1027 Otherwise, return #f."
1029 (expr (music 'ContextSpeccedMusic
1031 element (music 'SimultaneousMusic
1032 elements ((music 'ContextSpeccedMusic
1034 context-type 'Voice)
1035 (music 'ContextSpeccedMusic
1037 context-type 'Voice)
1038 (music 'ContextSpeccedMusic
1040 context-type 'Voice)
1041 (music 'ContextSpeccedMusic
1043 context-type 'Voice)
1044 (music 'ContextSpeccedMusic
1046 context-type 'NullVoice)
1049 (?pc-music (music 'PartCombineMusic))
1050 (format #f "~a" (music->lily-string ?pc-music)))))
1052 (define-display-method UnrelativableMusic (expr)
1053 (music->lily-string (ly:music-property expr 'element)))
1056 (define-display-method QuoteMusic (expr)
1057 (or (with-music-match (expr (music
1059 quoted-voice-direction ?quoted-voice-direction
1060 quoted-music-name ?quoted-music-name
1061 quoted-context-id "cue"
1062 quoted-context-type 'CueVoice
1064 (format #f "\\cueDuring #~s #~a ~a"
1066 ?quoted-voice-direction
1067 (music->lily-string ?music)))
1068 (format #f "\\quoteDuring #~s ~a"
1069 (ly:music-property expr 'quoted-music-name)
1070 (music->lily-string (ly:music-property expr 'element)))))
1075 (define-display-method LineBreakEvent (expr)
1076 (if (null? (ly:music-property expr 'break-permission))
1080 (define-display-method PageBreakEvent (expr)
1081 (if (null? (ly:music-property expr 'break-permission))
1085 (define-display-method PageTurnEvent (expr)
1086 (if (null? (ly:music-property expr 'break-permission))
1090 (define-extra-display-method EventChord (expr)
1091 (with-music-match (expr (music 'EventChord
1092 elements ((music 'LineBreakEvent
1093 break-permission 'force)
1094 (music 'PageBreakEvent
1095 break-permission 'force))))
1098 (define-extra-display-method EventChord (expr)
1099 (with-music-match (expr (music 'EventChord
1100 elements ((music 'LineBreakEvent
1101 break-permission 'force)
1102 (music 'PageBreakEvent
1103 break-permission 'force)
1104 (music 'PageTurnEvent
1105 break-permission 'force))))
1113 (define-display-method LyricCombineMusic (expr)
1114 (format #f "\\lyricsto ~s ~a"
1115 (ly:music-property expr 'associated-context)
1116 (parameterize ((*explicit-mode* #f)
1117 (*omit-duration* #t))
1118 (music->lily-string (ly:music-property expr 'element)))))
1121 (define-extra-display-method SimultaneousMusic (expr)
1122 (with-music-match (expr (music 'SimultaneousMusic
1123 elements ((music 'ContextSpeccedMusic
1127 (music 'ContextSpeccedMusic
1129 context-type 'Staff)
1130 (music 'ContextSpeccedMusic
1132 context-type 'Staff))))
1133 (with-music-match (?ac-music (music 'AutoChangeMusic))
1135 (music->lily-string ?ac-music)))))
1138 (define-extra-display-method SimultaneousMusic (expr)
1139 (with-music-match (expr (music 'SimultaneousMusic
1140 elements ((music 'ContextSpeccedMusic
1143 element ?note-sequence)
1144 (music 'ContextSpeccedMusic
1145 context-type 'Lyrics
1147 element (music 'LyricCombineMusic
1148 associated-context ?associated-id
1149 element ?lyric-sequence)))))
1150 (if (string=? ?id ?associated-id)
1151 (format #f "~a~a \\addlyrics ~a"
1152 (music->lily-string ?note-sequence)
1153 (new-line->lily-string)
1154 (parameterize ((*explicit-mode* #f)
1155 (*omit-duration* #t))
1156 (music->lily-string ?lyric-sequence)))
1159 ;; Silence internal event sent at end of each lyrics block
1160 (define-display-method CompletizeExtenderEvent (expr)