From d15756b9b2817a578ad38cf4c5a5e58db52cbe09 Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Thu, 26 Jul 2012 22:04:42 +0200
Subject: [PATCH] Make the toc file work with bookparts, too

---
 orchestrallily.ily | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/orchestrallily.ily b/orchestrallily.ily
index 5804d28..b7faa07 100644
--- a/orchestrallily.ily
+++ b/orchestrallily.ily
@@ -830,12 +830,17 @@ keepcuefilter = #(define-music-function (parser location music) (ly:music?)
 removecuefilter = #(define-music-function (parser location music) (ly:music?)
   ((ly:music-function-extract removeWithTag) parser location 'cued ((ly:music-function-extract killCues) parser location music)))
 
-
+%% The page numbers are pages counts in the pdf file, not visible page number!
+%% So we have to offset them if the first page is not page #1
+%% unfortunately this means we have to store the first-page-number of the first
+%% bookpart in a global variable, because in later layouts we don't have that
+%% information available any more (first-page-number will be the first page
+%% number of the currently processed bookpart!)
+#(define oly:first-page-offset #f)
 #(define (oly:create-toc-file layout pages)
-  (let* ((label-table (ly:output-def-lookup layout 'label-page-table))
-	 ;; The page numbers are pages counts in the pdf file, not visible page number!
-	 ;; So we have to offset them if the first page is not page #1
-	 (first-page-offset (1- (ly:output-def-lookup layout 'first-page-number))))
+  (if (not oly:first-page-offset)
+      (set! oly:first-page-offset (1- (ly:output-def-lookup layout 'first-page-number))))
+  (let* ((label-table (ly:output-def-lookup layout 'label-page-table)))
     (if (not (null? label-table))
       (let* ((format-line (lambda (toc-item)
              (let* ((label (car toc-item))
@@ -843,9 +848,12 @@ removecuefilter = #(define-music-function (parser location music) (ly:music?)
                     (label-page (and (list? label-table)
                                      (assoc label label-table)))
                     (page (and label-page (cdr label-page))))
-               (format #f "~a, section, 1, {~a}, ~a" (- page first-page-offset) text label))))
+               (if page
+                   (format #f "~a, section, 1, {~a}, ~a" (- page oly:first-page-offset) text label)
+                   ;; label came from a different bookpart, so ignore it!
+                   #f))))
              (formatted-toc-items (map format-line (toc-items)))
-             (whole-string (string-join formatted-toc-items ",\n"))
+             (whole-string (string-join (filter (lambda (i) i) formatted-toc-items) ",\n"))
              (output-name (ly:parser-output-name parser))
              (outfilename (format "~a.toc" output-name))
              (outfile (open-output-file outfilename)))
-- 
GitLab