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