In the attached file, I made a macro \count such that
a^\count a^\count a^\count is understood as a^"1" a^"2" a^"3"...

I also wrote a macro \repeatWithCount such that
\repeatWithCount 3 { a^\acount } also evaluates to a^"1" a^"2" a^"3".

However, (for aesthetic reasons!) I would prefer to use \count in both
situations. Is it possible to modify the \repeatWithCount function so
that it basically says
(set! count count-auto)
(do the \repeatWithCount work)
(set! count count-manual)
 ?

If I directly define
repeatWithCount = #(define-music-function (parser location n music) ...)
then the 'music' parameter is evaluated before repeatWithCount gets
its chance to redefine \count. If this were TeX, I could say something like

\def\repeatWithCount{\def\count\countAuto \RealRepeatWithCount}
\def\RealRepeatWithCount#1#2{ % do the work, then
  \def\count\countManual}

However, trying something like this (i.e. returning
#{ \RealRepeatWithoutCount #}) does not work in Lilypond. Is there a
way to do this?

Thanks,

-- 
        Jérôme
\version "2.18.2"
% Counting notes %<<<
#(begin

; manual counting
(define music-note-count 0)
(define czero (define-music-function (parser location) ()
  (set! music-note-count 0) #{ #}))
(define cset (define-music-function (parser location x) (number?)
  (set! music-note-count x) #{ #} ))

(define (make-count-markup n)
  (markup #:fontsize -1 #:bold #:sans (number->string n)))
(define counter-manual (define-music-function (parser location) ()
  (set! music-note-count (+ 1 music-note-count))
  (make-music 'TextScriptEvent 'direction 1 'text
    (make-count-markup music-note-count))
))

; automatic counting
(define counter-symbol 'Counter)
(define counter-auto (make-music 'TextScriptEvent 'direction 1
   'text counter-symbol))
(define acount counter-auto)
(define count counter-manual)

(define (substitute-count music n)
  (let* (
    (name (ly:music-property music 'name))
    (prop (ly:music-mutable-properties music))
    (out (list name))
    (add-out! (lambda (s v) (set! out (append! out (list s v)))))
  ) (map (lambda (x) (let* (
      (s (car x))
      (v (cdr x))
    ) ; inner let*
    (if (eq? s 'element)
      (set! v (substitute-count v n)))
    (if (member s '(elements articulations))
      (set! v (map (lambda (y) (substitute-count y n)) v)))
    (if (and (eq? name 'TextScriptEvent) (eq? s 'text) (eq? v counter-symbol))
      (set! v (make-count-markup n)))
    (add-out! s v))) ; lambda
  prop)
  (apply make-music out)
))

(define (repeat-with-count n music)
  (make-music 'SequentialMusic 'elements
    (map (lambda (i) (substitute-count music (+ 1 i))) (iota n))
  )
)

(define repeatWithCount (define-music-function (parser location n music)
  (number? ly:music?)
  (make-music 'SequentialMusic 'elements
    (map (lambda (i) (substitute-count music (+ 1 i))) (iota n))
  )
))
) % begin

%>>>
foo = { a4^\count a^\count a^\count }
qux = \repeatWithCount 5 { c'^\acount }

\foo \qux


_______________________________________________
lilypond-user mailing list
lilypond-user@gnu.org
https://lists.gnu.org/mailman/listinfo/lilypond-user

Reply via email to