- (listeners ((measure-counter-event engraver event)
- (set! last-measure-seen (ly:context-property context 'currentBarNumber))
- (set! new-measure? #t)
- (cond
- ((and (= START (ly:event-property event 'span-direction))
- go?)
- (begin
- (set! stop? #t)
- (ly:input-warning
- (ly:event-property event 'origin)
- "count not ended before another begun")))
- ((= START (ly:event-property event 'span-direction))
- (set! go? #t))
- ((= STOP (ly:event-property event 'span-direction))
- (begin
- (set! stop? #t)
- (set! go? #f))))))
+ (listeners
+ ((measure-counter-event engraver event)
+ (cond
+ ((and (= START (ly:event-property event 'span-direction))
+ go?)
+ (set! stop? #t)
+ (ly:input-warning
+ (ly:event-property event 'origin)
+ "count not ended before another begun"))
+ ((= START (ly:event-property event 'span-direction))
+ (set! go? #t)
+ ;; initialize one less so first measure receives a count spanner
+ (set! last-measure-seen
+ (1- (ly:context-property context 'currentBarNumber))))
+ ((= STOP (ly:event-property event 'span-direction))
+ (set! stop? #t)
+ (set! go? #f)))))
+
+ ((process-music trans)
+ (let ((col (ly:context-property context 'currentCommandColumn))
+ (now (ly:context-property context 'measurePosition))
+ (current-bar (ly:context-property context 'currentBarNumber)))
+ ;; Each measure of a count receives a new spanner, which is bounded
+ ;; by the first "command column" of that measure and the following one.
+ ;; The possibility of initial grace notes (negative measure position)
+ ;; is considered.
+ (if (and (> current-bar last-measure-seen)
+ (moment<=? now ZERO-MOMENT))
+ (begin
+ ;; Finish the previous count-spanner if there is one.
+ (if (ly:grob? count-spanner)
+ (begin
+ (ly:spanner-set-bound! count-spanner RIGHT col)
+ (ly:pointer-group-interface::add-grob count-spanner 'columns col)
+ (ly:engraver-announce-end-grob trans count-spanner col)
+ (set! count-spanner '())))
+ ;; If count is over, reset variables.
+ (if stop?
+ (begin
+ (set! elapsed 0)
+ (set! stop? #f)))
+ ;; If count is in progress, begin a count-spanner.
+ (if go?
+ (let* ((c (ly:engraver-make-grob trans 'MeasureCounter col))
+ (counter (ly:grob-property c 'count-from)))
+ (ly:spanner-set-bound! c LEFT col)
+ (ly:pointer-group-interface::add-grob c 'columns col)
+ (set! (ly:grob-property c 'count-from) (+ counter elapsed))
+ (set! count-spanner c)
+ (set! elapsed (1+ elapsed))))))
+ (set! last-measure-seen current-bar)))
+
+ ((finalize trans)
+ (if go?
+ (begin
+ (set! go? #f)
+ (ly:grob-suicide! count-spanner)
+ (set! count-spanner '())
+ (ly:warning "measure count left unfinished")))))))