]> git.donarmstrong.com Git - lilypond.git/commitdiff
Add $current-book and $current-bookpart parser variables
authorReinhold Kainhofer <reinhold@kainhofer.com>
Sun, 30 Aug 2009 00:52:33 +0000 (02:52 +0200)
committerReinhold Kainhofer <reinhold@kainhofer.com>
Mon, 31 Aug 2009 14:10:13 +0000 (16:10 +0200)
They allow us to properly add scores programmatically and mix them
with explicit scores.

Add a sample file to show how to add scores. This is also a proper
regtest for automatically generated scores in scheme

input/regression/scheme-book-scores.ly [new file with mode: 0644]
lily/parser.yy
ly/init.ly
scm/lily-library.scm

diff --git a/input/regression/scheme-book-scores.ly b/input/regression/scheme-book-scores.ly
new file mode 100644 (file)
index 0000000..cd500be
--- /dev/null
@@ -0,0 +1,62 @@
+\version "2.13.4"
+
+\header {
+
+  texidoc = "Scores can be generated with scheme, too, and inserted into the
+current book(part). Generated and explicit scores can be mixed, the header
+informations from top- and booklevel stack correctly."
+
+}
+
+#(use-modules (scm display-lily))
+
+% Sample score, which adds a score (containing just one note) to the current
+% book/bookpart/at toplevel using scheme rather than the parser.
+% That score is supposed to use the global header information, too.
+#(define add-one-note-score 
+   (let ((pitch 0))
+        (lambda (parser)
+          (let* ((scmpitch (ly:make-pitch 0 pitch 0))
+                 (music (make-music 'EventChord
+                          'elements (list (make-music 'NoteEvent
+                                            'duration (ly:make-duration 2 0 1 1)
+                                            'pitch scmpitch))))
+                 (score (scorify-music music parser))
+                 (layout (ly:output-def-clone $defaultlayout))
+                 (desc (markup #:large #:line ((ly:format "Score with a ~a"
+                                (note-name->lily-string scmpitch parser))))))
+            (ly:score-add-output-def! score layout)
+            (add-text parser desc)
+            (add-score parser score))
+            (set! pitch (modulo (1+ pitch) 7)))))
+
+oneNoteScore =
+#(define-music-function (parser location) ()
+   (add-one-note-score parser)
+   (make-music 'Music 'void #t))
+
+%%%
+
+\header {
+  title = "Main Title"
+  subtitle = "Main subtitle"
+  piece = "Piecetitle"
+}
+
+\oneNoteScore
+
+\bookpart {
+  \header { title ="Title 1" subtitle="Sub1"}
+  \oneNoteScore
+  \score { \relative c' c1 }
+  \oneNoteScore
+}
+
+
+\bookpart {
+  \score { \relative c' c1 }
+  \oneNoteScore
+}
+
+\oneNoteScore
+
index 462a58de402ee1f8dac8dabd827919660e015005..813452ef599e814421704a6e3b3a52344dd15f97 100644 (file)
@@ -655,6 +655,7 @@ book_block:
        BOOK '{' book_body '}'  {
                $$ = $3;
                pop_paper (PARSER);
+               PARSER->lexer_->set_identifier (ly_symbol2scm ("$current-book"), SCM_BOOL_F);
        }
        ;
 
@@ -670,11 +671,13 @@ book_body:
                $$->paper_->unprotect ();
                push_paper (PARSER, $$->paper_);
                $$->header_ = PARSER->lexer_->lookup_identifier ("$defaultheader"); 
+               PARSER->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $$->self_scm ());
        }
        | BOOK_IDENTIFIER {
                $$ = unsmob_book ($1);
                $$->protect ();
                $$->origin ()->set_spot (@$);
+               PARSER->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $1);
        }
        | book_body paper_block {
                $$->paper_ = $2;
@@ -722,6 +725,7 @@ book_body:
 bookpart_block:
        BOOKPART '{' bookpart_body '}' {
                $$ = $3;
+               PARSER->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), SCM_BOOL_F);
        }
        ;
 
@@ -729,11 +733,13 @@ bookpart_body:
        {
                $$ = new Book;
                $$->origin ()->set_spot (@$);
+               PARSER->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $$->self_scm ());
        }
        | BOOK_IDENTIFIER {
                $$ = unsmob_book ($1);
                $$->protect ();
                $$->origin ()->set_spot (@$);
+               PARSER->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $1);
        }
        | bookpart_body paper_block {
                $$->paper_ = $2;
index 88b152a9a1d672564ad1850ea9bdc8a9729bf6e3..93048100aea29d1a662b991db8baeaedb4170afe 100644 (file)
@@ -14,6 +14,8 @@
 #(define toplevel-bookparts (list))
 #(define output-count 0) 
 #(define $defaultheader #f)
+#(define $current-book #f)
+#(define $current-bookpart #f)
 #(define version-seen #f)
 #(define expect-error #f) 
 #(define output-empty-score-list #f)
index 7d0cb126020f916d61284dc497b6b721b2647a5b..335c345c3c0e345f6fd7b2b7b493be9f421759d5 100644 (file)
 (define-public (print-book-with-defaults-as-systems parser book)
   (print-book-with parser book ly:book-process-to-systems))
 
+;; Add a score to the current bookpart, book or toplevel
+(define-public (add-score parser score)
+    (cond
+      ((ly:parser-lookup parser '$current-bookpart)
+          ((ly:parser-lookup parser 'bookpart-score-handler)
+               (ly:parser-lookup parser '$current-bookpart) score))
+      ((ly:parser-lookup parser '$current-book)
+          ((ly:parser-lookup parser 'book-score-handler)
+               (ly:parser-lookup parser '$current-book) score))
+      (else
+          ((ly:parser-lookup parser 'toplevel-score-handler) parser score))))
+
+(define-public (add-text parser text)
+  (add-score parser (list text)))
+
+(define-public (add-music parser music)
+  (collect-music-aux (lambda (score)
+                      (add-score parser score))
+                     parser
+                    music))
+
+
 ;;;;;;;;;;;;;;;;
 ;; alist