]> git.donarmstrong.com Git - lilypond.git/commitdiff
Issue 4858: Let define-session-public place variables natively into parser
authorDavid Kastrup <dak@gnu.org>
Sun, 22 May 2016 11:54:40 +0000 (13:54 +0200)
committerDavid Kastrup <dak@gnu.org>
Tue, 31 May 2016 19:50:18 +0000 (21:50 +0200)
Putting them as native variables in the parser module (rather than
using export/import) makes `set!' and `define' equivalent rather than
having `define' create a shadowing definition of the session variable.
That is important in order to avoid the values of the variable
diverging between parser module and `lily'.

scm/lily.scm

index 5ead4e6c9cf9da25e8f12bcf08cdfa145692f613..ce3486cf3f4e70f9479fa6b4ac91e8a596c5a490 100644 (file)
@@ -86,6 +86,7 @@
 ;;
 
 (define lilypond-declarations '())
+(define lilypond-exports '())
 (define after-session-hook (make-hook))
 
 (define-public (call-after-session thunk)
@@ -115,10 +116,14 @@ session has started."
   `(,add-session-variable ',name ,value))
 
 (defmacro-public define-session-public (name value)
-  "Like @code{define-session}, but also exports @var{name}."
-  `(begin
-     (define-session ,name ,value)
-     (export ,name)))
+  "Like @code{define-session}, but also exports @var{name} into parser modules."
+  (define (add-session-variable name value)
+    (if (ly:undead? lilypond-declarations)
+        (ly:error (_ "define-session-public used after session start")))
+    (let ((var (make-variable value)))
+      (module-add! (current-module) name var)
+      (set! lilypond-exports (acons name var lilypond-exports))))
+  `(,add-session-variable ',name ,value))
 
 (define (session-terminate)
   (if (ly:undead? lilypond-declarations)
@@ -158,7 +163,16 @@ variables to their value after the initial call of @var{thunk}."
                  (module-add! (current-module) (car p) var))))
          (ly:get-undead lilypond-declarations)))
       (begin
+        ;; import all public session variables natively into parser
+        ;; module.  That makes them behave identically under define/set!
+        (for-each (lambda (v)
+                    (module-add! (current-module) (car v) (cdr v)))
+                  lilypond-exports)
+        ;; Initialize first session
         (thunk)
+        ;; lilypond-exports is no longer needed since we will grab its
+        ;; values from (current-module).
+        (set! lilypond-exports #f)
         (set! lilypond-interfaces
               (filter (lambda (m) (eq? 'interface (module-kind m)))
                       (module-uses (current-module))))