+(define-markup-command (auto-footnote layout props mkup note)
+ (markup? markup?)
+ #:category other
+ #:properties ((raise 0.5)
+ (padding 0.0))
+ "Have footnote @var{note} act as an annotation to the markup @var{mkup}.
+
+@lilypond[verbatim,quote]
+\\markup {
+ \\auto-footnote a b
+ \\override #'(padding . 0.2)
+ \\auto-footnote c d
+}
+@end lilypond
+The footnote will be annotated automatically."
+ (let* ((markup-stencil (interpret-markup layout props mkup))
+ (footnote-hash (gensym "footnote"))
+ (stencil-seed 0)
+ (gauge-stencil (interpret-markup
+ layout
+ props
+ ((ly:output-def-lookup
+ layout
+ 'footnote-numbering-function)
+ stencil-seed)))
+ (x-ext (ly:stencil-extent gauge-stencil X))
+ (y-ext (ly:stencil-extent gauge-stencil Y))
+ (footnote-number
+ `(delay-stencil-evaluation
+ ,(delay
+ (ly:stencil-expr
+ (let* ((table
+ (ly:output-def-lookup layout
+ 'number-footnote-table))
+ (footnote-stencil (if (list? table)
+ (assoc-get footnote-hash
+ table)
+ empty-stencil))
+ (footnote-stencil (if (ly:stencil? footnote-stencil)
+ footnote-stencil
+ (begin
+ (ly:programming-error
+"Cannot find correct footnote for a markup object.")
+ empty-stencil)))
+ (gap (- (interval-length x-ext)
+ (interval-length
+ (ly:stencil-extent footnote-stencil X))))
+ (y-trans (- (+ (cdr y-ext)
+ raise)
+ (cdr (ly:stencil-extent footnote-stencil
+ Y)))))
+ (ly:stencil-translate footnote-stencil
+ (cons gap y-trans)))))))
+ (main-stencil (ly:stencil-combine-at-edge
+ markup-stencil
+ X
+ RIGHT
+ (ly:make-stencil footnote-number x-ext y-ext)
+ padding)))
+ (ly:stencil-add
+ main-stencil
+ (ly:make-stencil
+ `(footnote ,footnote-hash #t ,(interpret-markup layout props note))
+ '(0 . 0)
+ '(0 . 0)))))
+