Hej Brent! Creating multiple indices is not a big thing. The problem is that the code for the toc has to be executed at initialisation (see toc-init.ly), so we cannot simply add more kinds of toc. But we can easily extend this toc code to allow for filtering to specific entries.
The appended file makes use of the optional label toc items might have to filter the toc. You might want to modify the formatting a bit though. Also one thing to keep in mind: Ordering is done lexicographically, so for example "Z" would come before "a". You might want to use a slightly more natural ordering that handles things like cases or typography before the first word-letter (e.g. "Zadok the priest" would come before "”Alas!“, he spoke, ”this does not quite cut it!“) Cheers, Valentin Am Freitag, 23. September 2022, 08:11:20 CEST schrieb Brent Annable: > Thanks for all the tips everyone! I will look into these snippets. > > I do actually need to create multiple indices (alphabetical by first line > and by tune name0, so maybe my dream won't be possible for the time being, > but it's good to know this functionality exists. > > Brent. > > On Thu, 22 Sept 2022 at 04:49, Jean Abou Samra <[email protected]> wrote: > > Le 21/09/2022 à 20:39, Valentin Petzel a écrit : > > > Hello Jean, > > > > > > at least for me this code fails to change the order of the toc. The > > > > reason for > > > > > this is that toc-markup does not in fact contain the markup, but the > > > > symbol > > > > > under which the formatting markup is saved in layout. > > > > > > So I suppose you rather want to use the text property: > > > > > > (sorted-toc-alist > > > > > > (sort toc-alist > > > > > > ;; Could use comparator-from-key in 2.23 > > > (lambda (item1 item2) > > > > > > (let ((mkup1 (assoc-get 'text (cdr item1))) > > > > > > (mkup2 (assoc-get 'text (cdr item2)))) > > > > > > (string<? > > > (markup->string mkup1) > > > (markup->string mkup2)))))) > > > > Whoops, yes. Not sure how I didn't see that they weren't sorted. > > Maybe I did a test and sent the wrong version? Weird. Anyway, > > thanks for the correction.
\version "2.22.2"
#(define (symbol-or-false-or-procedure? x) (or (symbol? x) (not x) (procedure? x)))
% An alist for saving predefined comparator functions. Could just define them and pass a reference, but this way might be a bit cleaner.
#(define orderings
(list
(cons 'lex-by-text
(lambda (item1 item2)
(let ((mkup1 (assoc-get 'text (cdr item1)))
(mkup2 (assoc-get 'text (cdr item2))))
(string<?
(markup->string mkup1)
(markup->string mkup2)))))))
%% Copied from LilyPond's definition and modified
% @param filterfn can be
% A) A symbol (filter on label == filterfn)
% B) A procedure (filter on (filterfn tocitem))
% C) #f (Do not filter)
% @param cmp can be
% A) A symbol (use predefined procedure from orderings alist)
% B) A procedure (sort using (cmp tocitem1 tocitem2))
% C) #f (Do not sort)
#(define-markup-list-command (filtered-ordered-table-of-contents layout props filterfn cmp)
(symbol-or-false-or-procedure? symbol-or-false-or-procedure?)
#:properties ((baseline-skip))
(if (symbol? cmp)
(set! cmp (assoc-get cmp orderings cmp)))
(if (symbol? cmp) (ly:error "Predefined ordering ~a does not exist" cmp))
(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))
(filtered-toc-alist
(if filterfn
(if (symbol? filterfn)
(filter
(lambda (item) (eq? filterfn (assoc-get 'name (cdr item))))
toc-alist)
(filter filterfn toc-alist))
toc-alist))
(sorted-toc-alist
(if cmp
(sort filtered-toc-alist cmp)
filtered-toc-alist)))
(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)))))
\markup \fill-line { " " \fontsize #6 "Index by name" " " }
\markuplist \filtered-ordered-table-of-contents #'title #'lex-by-text
\markup \fill-line { " " \fontsize #6 "Index by lyrics" " " }
\markuplist \filtered-ordered-table-of-contents #'first-lyrics #'lex-by-text
\tocItem #'title \markup "Abraham ate an apple"
\tocItem #'first-lyrics \markup "O behold how marvellous"
{ c' }
\pageBreak
\tocItem #'title \markup "Cesar’s chatting computer"
\tocItem #'first-lyrics \markup "All of this works, splendid!"
{ c' }
\pageBreak
\tocItem #'title \markup "Ernest’s eery emu"
\tocItem #'first-lyrics \markup "I do not want to brag, but this is quite a thing!"
{ c' }
\pageBreak
\tocItem #'title \markup "Flying Fools frolicking funnily"
\tocItem #'first-lyrics \markup "How many pieces are there left, exactly?"
{ c' }
\pageBreak
\tocItem #'title \markup "Hernado held his hands high"
\tocItem #'first-lyrics \markup "Really I’m starting to loose inspiration for these lyrics!"
{ c' }
\pageBreak
\tocItem #'title \markup "Good Geese giggling"
\tocItem #'first-lyrics \markup "Three more to go, come on, you can do it!"
{ c' }
\pageBreak
\tocItem #'title \markup "Beethoven broke Bach’s Biathlon-Record"
\tocItem #'first-lyrics \markup "Um ... Row, row, row your boat?"
{ c' }
\pageBreak
\tocItem #'title \markup "Did not expect this, did you?"
\tocItem #'first-lyrics \markup "I’m so glad I’m FINALLY done with this ..."
{ c' }
signature.asc
Description: This is a digitally signed message part.
