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