]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-scheme.cc
d4289d7bcc69550d018517d618e256ef1030863e
[lilypond.git] / lily / context-scheme.cc
1 /*
2   context-scheme.cc -- Context bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2007 Jan Nieuwenhuizen <janneke@gnu.org>
7   Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
9
10 #include "context.hh"
11 #include "context-def.hh"
12 #include "dispatcher.hh"
13
14 LY_DEFINE (ly_context_id, "ly:context-id",
15            1, 0, 0, (SCM context),
16            "Return the ID string of @var{context},"
17            " i.e., for @code{\\context Voice = one @dots{}}"
18            " return the string @code{one}.")
19 {
20   Context *tr = unsmob_context (context);
21
22   LY_ASSERT_SMOB (Context, context, 1);
23
24   return ly_string2scm (tr->id_string ());
25 }
26
27 LY_DEFINE (ly_context_name, "ly:context-name",
28            1, 0, 0, (SCM context),
29            "Return the name of @var{context},"
30            " i.e., for @code{\\context Voice = one @dots{}}"
31            " return the symbol @code{Voice}.")
32 {
33   LY_ASSERT_SMOB (Context, context, 1);
34
35   Context *tr = unsmob_context (context);
36
37   return ly_symbol2scm (tr->context_name ().c_str ());
38 }
39
40 LY_DEFINE (ly_context_grob_definition, "ly:context-grob-definition",
41            2, 0, 0, (SCM context, SCM name),
42            "Return the definition of @var{name} (a symbol) within"
43            " @var{context} as an alist.")
44 {
45   Context *tr = unsmob_context (context);
46   
47   LY_ASSERT_SMOB (Context, context, 1);
48   LY_ASSERT_TYPE (ly_is_symbol, name, 2);
49
50   return updated_grob_properties (tr, name);
51 }
52
53 LY_DEFINE (ly_context_pushpop_property, "ly:context-pushpop-property",
54            3, 1, 0, (SCM context, SCM grob, SCM eltprop, SCM val),
55            "Do a single @code{\\override} or @code{\\revert} operation"
56            " in @var{context}.  The grob definition @var{grob} is extended"
57            " with @var{eltprop} (if @var{val} is specified) or reverted"
58            " (if unspecified).")
59 {
60   Context *tg = unsmob_context (context);
61
62   LY_ASSERT_SMOB (Context, context, 1);
63   LY_ASSERT_TYPE (ly_is_symbol, grob, 2);
64   LY_ASSERT_TYPE (ly_is_symbol, eltprop, 3);
65
66   execute_pushpop_property (tg, grob, eltprop, val);
67
68   return SCM_UNSPECIFIED;
69 }
70
71 LY_DEFINE (ly_context_property, "ly:context-property",
72            2, 0, 0, (SCM c, SCM name),
73            "Return the value of @var{name} from context @var{c}.")
74 {
75   LY_ASSERT_SMOB (Context, c, 1);
76   LY_ASSERT_TYPE (ly_is_symbol, name, 2);
77
78   Context *t = unsmob_context (c);
79   return t->internal_get_property (name);
80 }
81
82 LY_DEFINE (ly_context_set_property_x, "ly:context-set-property!",
83            3, 0, 0, (SCM context, SCM name, SCM val),
84            "Set value of property @var{name} in context @var{context}"
85            " to @var{val}.")
86 {
87   LY_ASSERT_SMOB (Context, context, 1);
88   LY_ASSERT_TYPE (ly_is_symbol, name, 2);
89
90   Context *tr = unsmob_context (context);
91
92   tr->set_property (name, val);
93
94   return SCM_UNSPECIFIED;
95 }
96
97 LY_DEFINE (ly_context_property_where_defined, "ly:context-property-where-defined",
98            2, 0, 0, (SCM context, SCM name),
99            "Return the context above @var{context}"
100            " where @var{name} is defined.")
101 {
102   LY_ASSERT_SMOB (Context, context, 1);
103   LY_ASSERT_TYPE (ly_is_symbol, name, 2);
104   
105   Context *tr = unsmob_context (context);
106
107   SCM val;
108   tr = tr->where_defined (name, &val);
109   if (tr)
110     return tr->self_scm ();
111
112   return SCM_EOL;
113 }
114
115 LY_DEFINE (ly_context_unset_property, "ly:context-unset-property", 2, 0, 0,
116            (SCM context, SCM name),
117            "Unset value of property @var{name} in context @var{context}.")
118 {
119   LY_ASSERT_SMOB (Context, context, 1);
120   LY_ASSERT_TYPE (ly_is_symbol, name, 2);
121   Context *tr = unsmob_context (context);
122   
123   tr->unset_property (name);
124   return SCM_UNSPECIFIED;
125 }
126
127 LY_DEFINE (ly_context_parent, "ly:context-parent",
128            1, 0, 0, (SCM context),
129            "Return the parent of @var{context}, @code{#f} if none.")
130 {
131   LY_ASSERT_SMOB (Context, context, 1);
132   Context *tr = unsmob_context (context);
133
134   tr = tr->get_parent_context ();
135   if (tr)
136     return tr->self_scm ();
137   else
138     return SCM_BOOL_F;
139 }
140
141 /* FIXME: todo: should support translator IDs, and creation? */
142 LY_DEFINE (ly_context_find, "ly:context-find",
143            2, 0, 0, (SCM context, SCM name),
144            "Find a parent of @var{context} that has name or alias @var{name}."
145            "  Return @code{#f} if not found.")
146 {
147   LY_ASSERT_SMOB (Context, context, 1);
148   LY_ASSERT_TYPE (ly_is_symbol, name, 2);
149   Context *tr = unsmob_context (context);
150
151   while (tr)
152     {
153       if (tr->is_alias (name))
154         return tr->self_scm ();
155       tr = tr->get_parent_context ();
156     }
157
158   return SCM_BOOL_F;
159 }
160
161 LY_DEFINE (ly_context_now, "ly:context-now",
162            1, 0, 0, (SCM context),
163            "Return @code{now-moment} of context @var{context}.")
164 {
165   LY_ASSERT_SMOB (Context, context, 1);
166   Context *ctx = unsmob_context (context);
167   return ctx->now_mom ().smobbed_copy ();
168 }
169
170 LY_DEFINE (ly_context_event_source, "ly:context-event-source",
171            1, 0, 0, (SCM context),
172            "Return @code{event-source} of context @var{context}.")
173 {
174   LY_ASSERT_SMOB (Context, context, 1);
175   Context *ctx = unsmob_context (context);
176   return ctx->event_source ()->self_scm ();
177 }
178
179 LY_DEFINE (ly_context_events_below, "ly:context-events-below",
180            1, 0, 0, (SCM context),
181            "Return a @code{stream-distributor} that distributes all events"
182            " from @var{context} and all its subcontexts.")
183 {
184   LY_ASSERT_SMOB (Context, context, 1);
185   Context *ctx = unsmob_context (context);
186   return ctx->events_below ()->self_scm ();
187 }