/*
- music-output-def.cc -- implement Music_output_def
+ music-output-def.cc -- implement Output_def
source file of the GNU LilyPond music typesetter
- (c) 1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+ (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
*/
-#include "scope.hh"
-#include "debug.hh"
-#include "music-output-def.hh"
-#include "global-translator.hh"
-#include "dictionary-iter.hh"
-#include "identifier.hh"
+#include "book-paper-def.hh"
+#include "context-def.hh"
+#include "file-path.hh"
+#include "global-context.hh"
+#include "lily-guile.hh"
+#include "ly-module.hh"
+#include "main.hh"
+#include "output-def.hh"
+#include "output-def.hh"
+#include "scm-hash.hh"
+#include "warn.hh"
+
+#include "ly-smobs.icc"
+
+Output_def::Output_def ()
+{
+ scope_ = SCM_EOL;
+ parent_ = 0;
+ smobify_self ();
-int
-Music_output_def::get_next_default_count () const
+ scope_ = ly_make_anonymous_module (false);
+}
+
+Output_def::~Output_def ()
+{
+}
+
+Output_def::Output_def (Output_def const &s)
{
- return 0;
+ scope_ = SCM_EOL;
+ parent_ = 0;
+ smobify_self ();
+
+ scope_= ly_make_anonymous_module (false);
+ if (is_module (s.scope_))
+ ly_import_module (scope_, s.scope_);
}
-IMPLEMENT_IS_TYPE_B(Music_output_def);
-Music_output_def::Music_output_def ()
+IMPLEMENT_SMOBS (Output_def);
+IMPLEMENT_DEFAULT_EQUAL_P (Output_def);
+
+
+SCM
+Output_def::mark_smob (SCM m)
{
- scope_p_ = new Scope;
- translator_p_dict_p_ = new Scope;
+ Output_def * mo = (Output_def*) SCM_CELL_WORD_1 (m);
+
+ /*
+ FIXME: why is this necessary?
+ all bookpaper_ should be protected by themselves.
+ */
+ if (mo->parent_)
+ scm_gc_mark (mo->parent_->self_scm ());
+
+
+ mo->derived_mark ();
+ return mo->scope_;
}
-Music_output_def::~Music_output_def ()
+void
+Output_def::derived_mark () {}
+
+void
+assign_context_def (Output_def * m, SCM transdef)
{
- delete scope_p_;
- delete translator_p_dict_p_;
+ Context_def *tp = unsmob_context_def (transdef);
+ assert (tp);
+
+ if (tp)
+ {
+ SCM sym = tp->get_context_name ();
+ scm_module_define (m->scope_, sym, transdef);
+ }
+}
+
+/*
+ find the translator for NAME. NAME must be a symbol.
+*/
+SCM
+find_context_def (Output_def const *m, SCM name)
+{
+ Context_def *cd = unsmob_context_def (m->lookup_variable (name));
+ return cd ? cd->self_scm () : SCM_EOL;
}
-Music_output_def::Music_output_def (Music_output_def const &s)
+int
+Output_def::print_smob (SCM s, SCM p, scm_print_state *)
{
- scope_p_ = new Scope (*s.scope_p_);
- translator_p_dict_p_ = new Scope (*s.translator_p_dict_p_);
+ Output_def * def = unsmob_output_def (s);
+ scm_puts ("#< ", p);
+ scm_puts (classname (def), p);
- for (Dictionary_iter<Identifier*> i (*translator_p_dict_p_); i.ok (); i++)
- {
- Translator * t = i.val ()->access_Translator (false);
- t-> output_def_l_ = this;
- }
+ (void)def;
+ scm_puts (">", p);
+ return 1;
}
-void
-Music_output_def::assign_translator (Translator*tp)
+Real
+Output_def::get_dimension (SCM s) const
{
- String s =tp->type_str_;
- if (translator_p_dict_p_->elem_b (s))
- delete translator_p_dict_p_->elem (s);
+ SCM val = lookup_variable (s);
+ return ly_scm2double (val);
+}
+
+
+SCM
+Output_def::lookup_variable (SCM sym) const
+{
+ SCM var = ly_module_lookup (scope_, sym);
+ if (SCM_VARIABLEP (var))
+ return SCM_VARIABLE_REF (var);
+
+ if (parent_)
+ return parent_->lookup_variable (sym);
- (*translator_p_dict_p_)[s] = new Translator_identifier (tp, 0);
- tp ->output_def_l_ = this;
+ return SCM_EOL;
+}
+
+SCM
+Output_def::c_variable (String s) const
+{
+ return lookup_variable (ly_symbol2scm (s.to_str0 ()));
+}
+
+void
+Output_def::set_variable (SCM sym, SCM val)
+{
+ scm_module_define (scope_, sym, val);
}
-Translator*
-Music_output_def::find_translator_l (String name) const
+LY_DEFINE (ly_paper_lookup, "ly:output-def-lookup",
+ 2, 0, 0, (SCM pap, SCM sym),
+ "Lookup @var{sym} in @var{pap}. "
+ "Return the value or @code{'()} if undefined.")
{
- if (translator_p_dict_p_->elem_b (name))
- return (*translator_p_dict_p_)[name]->access_Translator ();
+ Output_def *op = unsmob_output_def (pap);
+ SCM_ASSERT_TYPE (op, pap, SCM_ARG1, __FUNCTION__, "Output_def");
+ SCM_ASSERT_TYPE (ly_c_symbol_p (sym), sym, SCM_ARG2, __FUNCTION__, "symbol");
- if (global_translator_dict_p->elem_b (name))
- return (*global_translator_dict_p)[name];
+ return op->lookup_variable (sym);
+}
- return 0;
+LY_DEFINE (ly_output_def_scope, "ly:output-def-scope",
+ 1, 0,0, (SCM def),
+ "Get the variable scope inside @var{def}.")
+{
+ Output_def *op = unsmob_output_def (def);
+ SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
+ return op->scope_;
}
-Global_translator *
-Music_output_def::get_global_translator_p ()
+LY_DEFINE (ly_output_def_parent, "ly:output-def-parent",
+ 1, 0, 0, (SCM def),
+ "Get the parent output-def of @var{def}.")
{
- Translator * t = find_translator_l ("Score");
- if (!t)
- error (_("Can't find Score context"));
- t = t->clone ();
- Global_translator *g = dynamic_cast <Translator_group *> (t)->global_l ();
- t->add_processing ();
-
- return g;
+ Output_def *op = unsmob_output_def (def);
+ SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
+ return op->parent_ ? op->parent_->self_scm () : SCM_EOL;
}
-void
-Music_output_def::print () const
+
+LY_DEFINE (ly_output_def_clone, "ly:output-def-clone",
+ 1, 0, 0, (SCM def),
+ "Clone @var{def}.")
{
-#ifndef NPRINT
- translator_p_dict_p_->print ();
- scope_p_->print( );
-#endif
+ Output_def *op = unsmob_output_def (def);
+ SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
+ SCM s = op->clone ()->self_scm ();
+ scm_gc_unprotect_object (s);
+ return s;
}
-String
-Music_output_def::get_default_output () const
+LY_DEFINE(ly_output_description, "ly:output-description",
+ 1,0,0,
+ (SCM output_def),
+ "Return the description of translators in @var{output-def}.")
{
- if (!scope_p_->elem_b ("output"))
- return "";
- Identifier * id = (*scope_p_) ["output"];
+ Output_def *id = unsmob_output_def (output_def);
+
+ SCM al =ly_module_to_alist (id->scope_);
+
+ SCM l = SCM_EOL;
+ for (SCM s = al ; ly_c_pair_p (s); s = ly_cdr (s))
+ {
+ Context_def * td = unsmob_context_def (ly_cdar (s));
+ SCM key = ly_caar (s);
+ if (td && key == td->get_context_name ())
+ {
+
+ l = scm_cons (scm_cons (key, td->to_alist ()), l);
+ }
+ }
+ return l;
+}
+
+
- String *p = dynamic_cast <String *> (id);
- return p ? *p : String ("");
+#include "interval.hh"
+
+/* FIXME. This is broken until we have a generic way of
+ putting lists inside the \paper block. */
+Interval
+line_dimensions_int (Output_def *def, int n)
+{
+ Real lw = def->get_dimension (ly_symbol2scm ("linewidth"));
+ Real ind = n ? 0.0 : def->get_dimension (ly_symbol2scm ("indent"));
+
+ return Interval (ind, lw);
+}
+
+LY_DEFINE (ly_paper_def_p, "ly:paper-def?",
+ 1, 0, 0, (SCM def),
+ "Is @var{def} a paper definition?")
+{
+ return ly_bool2scm (unsmob_output_def (def));
}