From ed8b69df5758e1206c6f178de63c396931519f2a Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer Date: Sun, 30 Aug 2009 02:52:33 +0200 Subject: [PATCH] Add $current-book and $current-bookpart parser variables 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 | 62 ++++++++++++++++++++++++++ lily/parser.yy | 6 +++ ly/init.ly | 2 + scm/lily-library.scm | 22 +++++++++ 4 files changed, 92 insertions(+) create mode 100644 input/regression/scheme-book-scores.ly diff --git a/input/regression/scheme-book-scores.ly b/input/regression/scheme-book-scores.ly new file mode 100644 index 0000000000..cd500bea6c --- /dev/null +++ b/input/regression/scheme-book-scores.ly @@ -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 + diff --git a/lily/parser.yy b/lily/parser.yy index 462a58de40..813452ef59 100644 --- a/lily/parser.yy +++ b/lily/parser.yy @@ -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; diff --git a/ly/init.ly b/ly/init.ly index 88b152a9a1..93048100ae 100644 --- a/ly/init.ly +++ b/ly/init.ly @@ -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) diff --git a/scm/lily-library.scm b/scm/lily-library.scm index 7d0cb12602..335c345c3c 100644 --- a/scm/lily-library.scm +++ b/scm/lily-library.scm @@ -170,6 +170,28 @@ (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 -- 2.39.2