[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Including only definitions from a Lilypond file
From: |
Jérôme Plût |
Subject: |
Re: Including only definitions from a Lilypond file |
Date: |
Sun, 23 Aug 2020 22:42:42 +0000 |
User-agent: |
NeoMutt/20170113 (1.7.2) |
> On 2020-08-14 4:48 am, David Kastrup wrote:
> > All of the elements in a score are routed through hooks you can
> > redefine. So you can just redefine your various hooks to do nothing and
> > then include the file.
>
> Would something like this work?
>
> %%%%
> \version "2.20.0"
>
> #(begin
> (use-modules (ice-9 regex))
> (let* ((symbols
> (map (lambda (m) (string->symbol (match:substring m 1)))
> (list-matches "define ([a-z-]+-handler)"
> (ly:gulp-file "declarations-init.ly"))))
> (procs (map primitive-eval symbols))
> (null-proc (lambda args #f)))
> (ly:parser-define! 'disableHandlers
> (define-void-function () ()
> (for-each
> (lambda (sym) (primitive-eval `(set! ,sym ,null-proc)))
> symbols)))
> (ly:parser-define! 'restoreHandlers
> (define-void-function () ()
> (for-each
> (lambda (sym proc) (primitive-eval `(set! ,sym ,proc)))
> symbols procs)))))
This mostly works (except that I only needed to replace
"toplevel-.*-handler" functions), thanks!
Now I would like to use this to programmatically include a given file,
e.g. defining a Scheme function #(my-function "filename.ly") that would do
something equivalent to
#(compute something with "filename.ly")
\include "filename.ly"
#(compute something else with "filename.ly")
I tried to call (ly:parser-include-string (string-concatenate
`("\\disableHandlers \\include \"" ,filename "\""))) inside
my-function, but this produces
"fatal error: call-after-session used after session start".
(Some variants using ly:parser-parse-string, current-parser,
ly:parser-clone did not fare better).
Currently I see one (poor man's) way of doing that, which is calling
lilypond -e '(include "myfile.scm")' filename.ly
where "myfile.scm" does the first part of computation and sets up one
of the toplevel-.*-handler functions to do the second part of the
computation. (I've not tried it yet, though!). However this is quite
inelegant and, in particular:
- likely not too robust w.r.t the content of "filename.ly",
- does not allow including more than one file in sequence.
Is there a better way of doing this?
Thanks,
--
Jérôme Plût