lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Alphabetised table of contents?


From: Jean Abou Samra
Subject: Re: Alphabetised table of contents?
Date: Wed, 21 Sep 2022 07:40:48 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.1

Le 21/09/2022 à 07:09, Werner LEMBERG a écrit :
[*] Except Jean has time and interest to produce a solution like pulling
     a rabbit out of a hat :-)


I do have a rabbit to pull out, because I actually already posted a
snippet like this on the lilypond-user-fr list. See

https://lists.gnu.org/archive/html/lilypond-user-fr/2021-01/msg00041.html

This user had needs that went beyond just sorting the TOC (replacing
page number with piece numbers), here is a simpler solution if sorting
is all you need:

\version "2.22.2"

%% Copied from LilyPond's definition and modified
#(define-markup-list-command (ordered-table-of-contents layout props) ()
  #:properties ((baseline-skip))
  (let* ((titleMarkup (ly:output-def-lookup layout 'tocTitleMarkup))
         (indentMarkup (ly:output-def-lookup layout 'tocIndentMarkup))
         (toplevelFormatter (ly:output-def-lookup layout 'tocFormatMarkup))
         (toc-alist (toc-items))
         (sorted-toc-alist
          (sort toc-alist
                ;; Could use comparator-from-key in 2.23
                (lambda (item1 item2)
                  (let ((mkup1 (assoc-get 'toc-markup (cdr item1)))
                        (mkup2 (assoc-get 'toc-markup (cdr item2))))
                     (string<?
                     (markup->string mkup1)
                     (markup->string mkup2)))))))
    (ly:output-def-set-variable! layout 'label-alist-table
                                 (append (ly:output-def-lookup layout 'label-alist-table)
                                         toc-alist))
    (cons (interpret-markup layout props titleMarkup)
          (space-lines baseline-skip
                       (map (lambda (toc-item)
                              (let* ((label (car toc-item))
                                     (alist (cdr toc-item))
                                     (toc-markup (assoc-get 'toc-markup alist))
                                     (text (assoc-get 'text alist))
                                     (level (assoc-get 'level alist)))
                                (interpret-markup
                                 layout
                                 (cons (list
                                        (cons 'toc:page
                                              (markup #:with-link label
                                                      #:page-ref label "XXX" "?"))                                         (cons 'toc:text (markup #:with-link label text))
                                        (cons 'toc:label label)
                                        (cons 'toc:level level)
                                        (cons 'toc:toplevel-formatter toplevelFormatter)
                                        (cons 'toc:indent
                                              (make-line-markup
                                               (make-list level indentMarkup))))
                                       props)
                                 (ly:output-def-lookup layout toc-markup))))
                            sorted-toc-alist)))))

\markuplist \ordered-table-of-contents

\tocItem \markup "A"
{ c' }
\tocItem \markup "C"
{ c' }
\tocItem \markup "E"
{ c' }
\tocItem \markup "F"
{ c' }
\tocItem \markup "H"
{ c' }
\tocItem \markup "G"
{ c' }
\tocItem \markup "B"
{ c' }
\tocItem \markup "D"
{ c' }




Best,
Jean




reply via email to

[Prev in Thread] Current Thread [Next in Thread]