*** empty log message ***
[lilypond.git] / lily / translator-scheme.cc
blob9a7bf5c065fe67a04a2c614ae5d620fb92118c19
1 /*
2 translator-scheme.cc -- implement Scheme context functions
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "translator.hh"
11 #include "translator-def.hh"
13 #include "translator-group.hh"
14 #include "lily-guile.hh"
16 LY_DEFINE(ly_get_context_property,
17 "ly:get-context-property", 2, 0, 0,
18 (SCM context, SCM name),
19 "retrieve the value of @var{name} from context @var{context}")
21 Translator *t = unsmob_translator (context);
22 Translator_group* tr= dynamic_cast<Translator_group*> (t);
23 SCM_ASSERT_TYPE(tr, context, SCM_ARG1, __FUNCTION__, "Translator group");
24 SCM_ASSERT_TYPE(gh_symbol_p (name), name, SCM_ARG2, __FUNCTION__, "symbol");
26 return tr->internal_get_property (name);
30 LY_DEFINE(ly_set_context_property,
31 "ly:set-context-property!", 3, 0, 0,
32 (SCM context, SCM name, SCM val),
33 "set value of property @var{name} in context @var{context} to @var{val}.")
35 Translator *t = unsmob_translator (context);
36 Translator_group* tr= dynamic_cast<Translator_group*> (t);
38 SCM_ASSERT_TYPE(tr, context, SCM_ARG1, __FUNCTION__, "Context");
39 SCM_ASSERT_TYPE(gh_symbol_p (name), name, SCM_ARG2, __FUNCTION__, "symbol");
41 tr->internal_set_property (name, val);
43 return SCM_UNSPECIFIED;
47 LY_DEFINE(ly_context_property_where_defined,
48 "ly:context-property-where-defined", 2, 0, 0,
49 (SCM context, SCM name),
50 "Return the context above @var{context} where @var{name} is defined.")
52 Translator *t = unsmob_translator (context);
53 Translator_group* tr = dynamic_cast<Translator_group*> (t);
54 SCM_ASSERT_TYPE(tr, context, SCM_ARG1, __FUNCTION__, "Context");
55 SCM_ASSERT_TYPE(gh_symbol_p (name), name, SCM_ARG2, __FUNCTION__, "symbol");
58 tr = tr->where_defined (name);
60 if (tr)
61 return tr->self_scm();
63 return SCM_EOL;
66 LY_DEFINE(ly_unset_context_property,
67 "ly:unset-context-property", 2, 0, 0,
68 (SCM context, SCM name),
69 "Unset value of property @var{name} in context @var{context}.")
71 Translator *t = unsmob_translator (context);
72 Translator_group* tr = dynamic_cast<Translator_group*> (t);
73 SCM_ASSERT_TYPE(tr, context, SCM_ARG1, __FUNCTION__, "Context");
74 SCM_ASSERT_TYPE(gh_symbol_p (name), name, SCM_ARG2, __FUNCTION__, "symbol");
76 tr->unset_property (name);
78 return SCM_UNSPECIFIED;
83 LY_DEFINE(ly_context_parent,
84 "ly:context-parent", 1, 0, 0,
85 (SCM context),
86 "Return the parent of @var{context}, #f if none.")
88 Translator *t = unsmob_translator (context);
89 Translator_group* tr= dynamic_cast<Translator_group*> (t);
91 SCM_ASSERT_TYPE(tr, context, SCM_ARG1, __FUNCTION__, "Context");
93 tr = tr->daddy_trans_ ;
94 if (tr)
95 return tr->self_scm();
96 else
97 return SCM_BOOL_F;
101 Todo: should support translator IDs, and creation?
103 LY_DEFINE(ly_translator_find,
104 "ly:translator-find", 2, 0,0,
105 (SCM context, SCM name),
106 "Find a parent of @var{context} that has name or alias @var{name}. "
107 "Return @code{#f} if not found." )
109 Translator_group* tr= dynamic_cast<Translator_group*> ( unsmob_translator (context));
111 SCM_ASSERT_TYPE(tr, context, SCM_ARG1, __FUNCTION__, "context");
112 SCM_ASSERT_TYPE(gh_symbol_p (name), name, SCM_ARG2, __FUNCTION__, "symbol");
114 while (tr)
116 if (tr->is_alias_b (name))
117 return tr->self_scm();
118 tr = tr->daddy_trans_ ;
121 return SCM_BOOL_F;
125 LY_DEFINE(ly_context_properties,
126 "ly:context-properties", 1, 0, 0,
127 (SCM context),
128 "Return all properties of @var{context} in an alist.")
130 Translator *t = unsmob_translator (context);
131 Translator_group* tr= dynamic_cast<Translator_group*> (t);
133 SCM_ASSERT_TYPE(tr, context, SCM_ARG1, __FUNCTION__, "Context");
135 return tr->properties_as_alist ();
140 LY_DEFINE(ly_translator_name,
141 "ly:translator-name", 1,0,0, (SCM trans),
142 "Return the type name of the translator @var{trans}.")
144 Translator* tr = unsmob_translator (trans);
145 SCM_ASSERT_TYPE(tr, trans, SCM_ARG1, __FUNCTION__, "Context");
147 char const* nm = classname (tr);
148 return scm_makfrom0str (nm);
151 LY_DEFINE(ly_translator_description,
152 "ly:translator-description",
153 1,0,0, (SCM me),
154 "Return an alist of properties of translator @var{me}.")
156 Translator *tr =unsmob_translator (me);
157 SCM_ASSERT_TYPE (tr, me, SCM_ARG1, __FUNCTION__, "Context");
159 return tr->translator_description ();
163 LY_DEFINE(ly_context_pushpop_property,
164 "ly:context-pushpop-property", 3, 1, 0,
165 (SCM context, SCM grob, SCM eltprop, SCM val),
166 "Do a single @code{\\override} or @code{\\revert} operation "
167 "in @var{context}. The grob definition @code{grob} is extended with "
168 "@code{eltprop} (if @var{val} is specified) "
169 "or reverted (if unspecified).")
171 Translator_group *tg = dynamic_cast<Translator_group*> (unsmob_translator (context));
173 SCM_ASSERT_TYPE(tg, context, SCM_ARG1, __FUNCTION__, "context");
174 SCM_ASSERT_TYPE(gh_symbol_p (grob), grob, SCM_ARG2, __FUNCTION__, "symbol");
175 SCM_ASSERT_TYPE(gh_symbol_p (eltprop), eltprop, SCM_ARG3, __FUNCTION__, "symbol");
177 tg->execute_pushpop_property (grob, eltprop, val);
179 return SCM_UNDEFINED;
182 LY_DEFINE(ly_context_p,
183 "ly:context?", 1, 0, 0,
184 (SCM x),
185 "Type predicate: is @var{x} a context?")
187 Translator_group *tg = dynamic_cast<Translator_group*> (unsmob_translator (x));
189 return SCM_BOOL (tg);
194 Translator::print_smob (SCM s, SCM port, scm_print_state *)
196 Translator *sc = (Translator *) ly_cdr (s);
198 scm_puts ("#<Translator ", port);
199 if (Translator_def *d=unsmob_translator_def (sc->definition_))
201 scm_display (d->type_name_, port);
203 else
204 scm_display (ly_translator_name (s), port);
206 scm_display (sc->simple_trans_list_, port);
209 don't try to print properties, that is too much hassle.
211 scm_puts (" >", port);
213 return 1;