]> git.donarmstrong.com Git - lilypond.git/blob - lily/context-scheme.cc
f58a793ed4ef805fcbcf4cd47435e88de47edf82
[lilypond.git] / lily / context-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2012 Jan Nieuwenhuizen <janneke@gnu.org>
5   Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "context.hh"
22 #include "context-def.hh"
23 #include "dispatcher.hh"
24
25 LY_DEFINE (ly_context_current_moment,
26            "ly:context-current-moment",
27            1, 0, 0, (SCM context),
28            "Return the current moment of @var{context}.")
29 {
30   Context *tr = unsmob_context (context);
31
32   LY_ASSERT_SMOB (Context, context, 1);
33
34   return tr->now_mom ().smobbed_copy ();
35 }
36
37 LY_DEFINE (ly_context_id, "ly:context-id",
38            1, 0, 0, (SCM context),
39            "Return the ID string of @var{context},"
40            " i.e., for @code{\\context Voice = \"one\" @dots{}}"
41            " return the string @code{one}.")
42 {
43   Context *tr = unsmob_context (context);
44
45   LY_ASSERT_SMOB (Context, context, 1);
46
47   return ly_string2scm (tr->id_string ());
48 }
49
50 LY_DEFINE (ly_context_name, "ly:context-name",
51            1, 0, 0, (SCM context),
52            "Return the name of @var{context},"
53            " i.e., for @code{\\context Voice = \"one\" @dots{}}"
54            " return the symbol @code{Voice}.")
55 {
56   LY_ASSERT_SMOB (Context, context, 1);
57
58   Context *tr = unsmob_context (context);
59
60   return ly_symbol2scm (tr->context_name ().c_str ());
61 }
62
63 LY_DEFINE (ly_context_grob_definition, "ly:context-grob-definition",
64            2, 0, 0, (SCM context, SCM name),
65            "Return the definition of @var{name} (a symbol) within"
66            " @var{context} as an alist.")
67 {
68   Context *tr = unsmob_context (context);
69
70   LY_ASSERT_SMOB (Context, context, 1);
71   LY_ASSERT_TYPE (ly_is_symbol, name, 2);
72
73   return updated_grob_properties (tr, name);
74 }
75
76 LY_DEFINE (ly_context_pushpop_property, "ly:context-pushpop-property",
77            3, 1, 0, (SCM context, SCM grob, SCM eltprop, SCM val),
78            "Do a single @code{\\override} or @code{\\revert} operation"
79            " in @var{context}.  The grob definition @var{grob} is extended"
80            " with @var{eltprop} (if @var{val} is specified) or reverted"
81            " (if unspecified).")
82 {
83   Context *tg = unsmob_context (context);
84
85   LY_ASSERT_SMOB (Context, context, 1);
86   LY_ASSERT_TYPE (ly_is_symbol, grob, 2);
87   LY_ASSERT_TYPE (ly_is_symbol, eltprop, 3);
88
89   execute_pushpop_property (tg, grob, eltprop, val);
90
91   return SCM_UNSPECIFIED;
92 }
93
94 LY_DEFINE (ly_context_property, "ly:context-property",
95            2, 1, 0, (SCM context, SCM sym, SCM def),
96            "Return the value for property @var{sym} in @var{context}."
97            " If @var{def} is given, and property value is @code{'()},"
98            " return @var{def}.")
99 {
100   LY_ASSERT_SMOB (Context, context, 1);
101   LY_ASSERT_TYPE (ly_is_symbol, sym, 2);
102
103   Context *t = unsmob_context (context);
104   SCM result = t->internal_get_property (sym);
105   return def != SCM_UNDEFINED && scm_is_null (result) ? def : result;
106 }
107
108 LY_DEFINE (ly_context_set_property_x, "ly:context-set-property!",
109            3, 0, 0, (SCM context, SCM name, SCM val),
110            "Set value of property @var{name} in context @var{context}"
111            " to @var{val}.")
112 {
113   LY_ASSERT_SMOB (Context, context, 1);
114   LY_ASSERT_TYPE (ly_is_symbol, name, 2);
115
116   Context *tr = unsmob_context (context);
117
118   tr->set_property (name, val);
119
120   return SCM_UNSPECIFIED;
121 }
122
123 LY_DEFINE (ly_context_property_where_defined, "ly:context-property-where-defined",
124            2, 0, 0, (SCM context, SCM name),
125            "Return the context above @var{context}"
126            " where @var{name} is defined.")
127 {
128   LY_ASSERT_SMOB (Context, context, 1);
129   LY_ASSERT_TYPE (ly_is_symbol, name, 2);
130
131   Context *tr = unsmob_context (context);
132
133   SCM val;
134   tr = tr->where_defined (name, &val);
135   if (tr)
136     return tr->self_scm ();
137
138   return SCM_EOL;
139 }
140
141 LY_DEFINE (ly_context_unset_property, "ly:context-unset-property", 2, 0, 0,
142            (SCM context, SCM name),
143            "Unset value of property @var{name} in context @var{context}.")
144 {
145   LY_ASSERT_SMOB (Context, context, 1);
146   LY_ASSERT_TYPE (ly_is_symbol, name, 2);
147   Context *tr = unsmob_context (context);
148
149   tr->unset_property (name);
150   return SCM_UNSPECIFIED;
151 }
152
153 LY_DEFINE (ly_context_parent, "ly:context-parent",
154            1, 0, 0, (SCM context),
155            "Return the parent of @var{context}, @code{#f} if none.")
156 {
157   LY_ASSERT_SMOB (Context, context, 1);
158   Context *tr = unsmob_context (context);
159
160   tr = tr->get_parent_context ();
161   if (tr)
162     return tr->self_scm ();
163   else
164     return SCM_BOOL_F;
165 }
166
167 /* FIXME: todo: should support translator IDs, and creation? */
168 LY_DEFINE (ly_context_find, "ly:context-find",
169            2, 0, 0, (SCM context, SCM name),
170            "Find a parent of @var{context} that has name or alias @var{name}."
171            "  Return @code{#f} if not found.")
172 {
173   LY_ASSERT_SMOB (Context, context, 1);
174   LY_ASSERT_TYPE (ly_is_symbol, name, 2);
175   Context *tr = unsmob_context (context);
176
177   while (tr)
178     {
179       if (tr->is_alias (name))
180         return tr->self_scm ();
181       tr = tr->get_parent_context ();
182     }
183
184   return SCM_BOOL_F;
185 }
186
187 LY_DEFINE (ly_context_now, "ly:context-now",
188            1, 0, 0, (SCM context),
189            "Return @code{now-moment} of context @var{context}.")
190 {
191   LY_ASSERT_SMOB (Context, context, 1);
192   Context *ctx = unsmob_context (context);
193   return ctx->now_mom ().smobbed_copy ();
194 }
195
196 LY_DEFINE (ly_context_event_source, "ly:context-event-source",
197            1, 0, 0, (SCM context),
198            "Return @code{event-source} of context @var{context}.")
199 {
200   LY_ASSERT_SMOB (Context, context, 1);
201   Context *ctx = unsmob_context (context);
202   return ctx->event_source ()->self_scm ();
203 }
204
205 LY_DEFINE (ly_context_events_below, "ly:context-events-below",
206            1, 0, 0, (SCM context),
207            "Return a @code{stream-distributor} that distributes all events"
208            " from @var{context} and all its subcontexts.")
209 {
210   LY_ASSERT_SMOB (Context, context, 1);
211   Context *ctx = unsmob_context (context);
212   return ctx->events_below ()->self_scm ();
213 }