]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/global-context-scheme.cc
Merge with master
[lilypond.git] / lily / global-context-scheme.cc
index 9229a8a0d956d8124cfd83aa671cbfb185f08d7a..c5f4d561f9966487c647527f8d189809ef0a8d6b 100644 (file)
@@ -3,9 +3,8 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  (c) 2005--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
 */
-
 #include "cpu-timer.hh"
 #include "global-context.hh"
 #include "international.hh"
@@ -13,7 +12,6 @@
 #include "music-iterator.hh"
 #include "music-output.hh"
 #include "music.hh"
-#include "object-key.hh"
 #include "output-def.hh"
 #include "translator-group.hh"
 #include "warn.hh"
@@ -28,7 +26,10 @@ LY_DEFINE (ly_format_output, "ly:format-output",
 
   SCM output = g->get_output ();
   progress_indication ("\n");
-  unsmob_music_output (output)->process ();
+
+  if (Music_output *od = unsmob_music_output (output))
+    od->process ();
+  
   return output;
 }
 
@@ -47,47 +48,55 @@ LY_DEFINE (ly_make_global_translator, "ly:make-global-translator",
   return tg->unprotect ();
 }
 
-LY_DEFINE (ly_run_translator, "ly:run-translator",
-          2, 1, 0, (SCM mus, SCM output_def, SCM key),
-          "Process @var{mus} according to @var{output_def}. \n"
-          "An interpretation context is set up,\n"
-          "and @var{mus} is interpreted with it.  \n"
-          "The context is returned in its final state.\n"
-          "\n\n"
-          "Optionally, this routine takes an Object-key to\n"
-          "to uniquely identify the Score block containing it.\n")
+LY_DEFINE (ly_make_global_context, "ly:make-global-context",
+          1, 0, 0, (SCM output_def),
+          "Set up a global interpretation context, using the output\n"
+          "block @var{output_def}.\n"
+          "The context is returned.\n"
+          )
 {
   Output_def *odef = unsmob_output_def (output_def);
-  Music *music = unsmob_music (mus);
 
-  if (!music
-      || !music->get_length ().to_bool ())
+  SCM_ASSERT_TYPE (odef, output_def, SCM_ARG1, __FUNCTION__,
+                  "Output definition");
+
+  Global_context *glob = new Global_context (odef);
+
+  if (!glob)
     {
-      warning (_ ("no music found in score"));
+      programming_error ("no toplevel translator");
       return SCM_BOOL_F;
     }
 
-  SCM_ASSERT_TYPE (music, mus, SCM_ARG1, __FUNCTION__, "Music");
-  SCM_ASSERT_TYPE (odef, output_def, SCM_ARG2, __FUNCTION__,
-                  "Output definition");
+  return glob->unprotect ();
+}
 
-  Cpu_timer timer;
+LY_DEFINE (ly_interpret_music_expression, "ly:interpret-music-expression",
+          2, 0, 0, (SCM mus, SCM ctx),
+          "Interpret the music expression @var{mus} in the\n"
+          "global context @var{ctx}. The context is returned in its\n"
+          "final state.\n")
+{
+  Music *music = unsmob_music (mus);
+  Global_context *g = dynamic_cast<Global_context *> (unsmob_context (ctx));
+  SCM_ASSERT_TYPE (music, mus, SCM_ARG1, __FUNCTION__, "Music");
+  SCM_ASSERT_TYPE (g, ctx, SCM_ARG2, __FUNCTION__, "Global context");
 
-  Global_context *glob = new Global_context (odef, unsmob_key (key));
-  if (!glob)
+  if (!music
+      || !music->get_length ().to_bool ())
     {
-      programming_error ("Couldn't create Global context");
+      warning (_ ("no music found in score"));
       return SCM_BOOL_F;
     }
 
-  SCM tgs = ly_make_global_translator (glob->self_scm ());
-  Translator_group *tg = unsmob_translator_group (tgs);
+  Cpu_timer timer;
 
   message (_ ("Interpreting music... "));
 
   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
   Music_iterator *iter = unsmob_iterator (protected_iter);
-  iter->init_translator (music, glob);
+
+  iter->init_context (music, g);
   iter->construct_children ();
 
   if (!iter->ok ())
@@ -97,13 +106,31 @@ LY_DEFINE (ly_run_translator, "ly:run-translator",
       return SCM_BOOL_F;
     }
 
-  glob->run_iterator_on_me (iter);
+  g->run_iterator_on_me (iter);
+
   iter->quit ();
   scm_remember_upto_here_1 (protected_iter);
-  send_stream_event (glob, "Finish", 0, 0);
+
+  send_stream_event (g, "Finish", 0, 0);
 
   if (be_verbose_global)
     message (_f ("elapsed time: %.2f seconds", timer.read ()));
 
-  return glob->unprotect ();
+  return ctx;
+}
+
+LY_DEFINE (ly_run_translator, "ly:run-translator",
+          2, 1, 0, (SCM mus, SCM output_def),
+          "Process @var{mus} according to @var{output_def}. \n"
+          "An interpretation context is set up,\n"
+          "and @var{mus} is interpreted with it.  \n"
+          "The context is returned in its final state.\n"
+          "\n\n"
+          "Optionally, this routine takes an Object-key to\n"
+          "to uniquely identify the Score block containing it.\n")
+{
+  SCM glob = ly_make_global_context (output_def);
+  ly_make_global_translator (glob);
+  ly_interpret_music_expression (mus, glob);
+  return glob;
 }