]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator-scheme.cc
* lily/stem.cc (thickness): new function.
[lilypond.git] / lily / translator-scheme.cc
1 /*   
2   translator-scheme.cc --  implement Scheme context functions
3  
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2002--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8  */
9
10 #include "translator.hh"
11 #include "translator-def.hh"
12
13 #include "translator-group.hh"
14 #include "lily-guile.hh"
15
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}")
20 {
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");
25
26   return tr->internal_get_property (name);
27   
28 }
29
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}.")
34 {
35   Translator *t = unsmob_translator (context);
36   Translator_group* tr=   dynamic_cast<Translator_group*> (t);
37
38   SCM_ASSERT_TYPE(tr, context, SCM_ARG1, __FUNCTION__, "Context");
39   SCM_ASSERT_TYPE(gh_symbol_p (name), name, SCM_ARG2, __FUNCTION__, "symbol");
40
41   tr->internal_set_property (name, val);
42
43   return SCM_UNSPECIFIED;
44 }
45
46
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.")
51 {
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");
56
57
58   tr = tr->where_defined (name);
59
60   if (tr)
61     return tr->self_scm();
62
63   return SCM_EOL;
64 }
65
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}.")
70 {
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");
75
76   tr->unset_property (name);
77
78   return SCM_UNSPECIFIED;
79 }
80
81
82
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.")
87 {
88   Translator *t = unsmob_translator (context);
89   Translator_group* tr=   dynamic_cast<Translator_group*> (t);
90
91   SCM_ASSERT_TYPE(tr, context, SCM_ARG1, __FUNCTION__, "Context");
92
93   tr =  tr->daddy_trans_ ;
94   if (tr)
95     return tr->self_scm();
96   else
97     return SCM_BOOL_F;
98 }
99
100 /*
101   Todo: should support translator IDs, and creation?
102  */
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." )
108 {
109   Translator_group* tr=   dynamic_cast<Translator_group*> ( unsmob_translator (context));
110
111   SCM_ASSERT_TYPE(tr, context, SCM_ARG1, __FUNCTION__, "context");
112   SCM_ASSERT_TYPE(gh_symbol_p (name), name, SCM_ARG2, __FUNCTION__, "symbol");
113   
114   while (tr)
115     {
116       if (tr->is_alias (name))
117         return tr->self_scm();
118       tr =  tr->daddy_trans_ ;
119     }
120   
121   return SCM_BOOL_F;
122 }
123
124
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.")
129 {
130   Translator *t = unsmob_translator (context);
131   Translator_group* tr=   dynamic_cast<Translator_group*> (t);
132
133   SCM_ASSERT_TYPE(tr, context, SCM_ARG1, __FUNCTION__, "Context");
134
135   return tr->properties_as_alist ();
136 }
137
138
139
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}. The name is a symbol.")
143 {
144   Translator* tr =  unsmob_translator (trans);
145   SCM_ASSERT_TYPE(tr, trans, SCM_ARG1, __FUNCTION__, "Context");
146
147   char const* nm = classname (tr);
148   return ly_symbol2scm (nm);
149 }
150
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}.")
155 {
156   Translator *tr =unsmob_translator (me);
157   SCM_ASSERT_TYPE (tr, me, SCM_ARG1, __FUNCTION__, "Context");
158
159   return tr->translator_description ();
160 }
161
162
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).")
170 {
171   Translator_group *tg = dynamic_cast<Translator_group*> (unsmob_translator (context));
172
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");
176
177   tg->execute_pushpop_property (grob, eltprop, val);
178
179   return SCM_UNDEFINED;
180 }
181
182 LY_DEFINE(ly_context_p,
183           "ly:context?", 1, 0, 0,
184           (SCM x),
185           "Type predicate: is @var{x} a context?")
186 {
187   Translator_group *tg = dynamic_cast<Translator_group*> (unsmob_translator (x));
188
189   return SCM_BOOL (tg);
190 }
191           
192
193 int
194 Translator::print_smob (SCM s, SCM port, scm_print_state *)
195 {
196   Translator *sc = (Translator *) ly_cdr (s);
197      
198   scm_puts ("#<Translator ", port);
199   if (Context_def *d=unsmob_context_def (sc->definition_))
200     {
201       scm_display (d->get_context_name (), port);
202     }
203   else
204     scm_display (ly_translator_name (s), port);
205
206   if (Translator_group *td=dynamic_cast<Translator_group*> (sc))
207     {
208       scm_puts ("=", port);
209       scm_puts (td->id_string_.to_str0 (), port);
210     }
211   
212   scm_display (sc->simple_trans_list_, port);
213
214   /*
215     don't try to print properties, that is too much hassle.
216    */
217   scm_puts (" >", port);
218   
219   return 1;
220 }
221