]> git.donarmstrong.com Git - lilypond.git/blob - lily/module-scheme.cc
(LY_DEFINE): new file.
[lilypond.git] / lily / module-scheme.cc
1 /*
2   lily/module-scheme.cc -- implement module bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "lily-guile.hh"
11 #include "warn.hh"
12 #include "main.hh"
13 #include "string.hh"
14
15 /*
16   If a variable in changed in SRC, we DEST doesn't see the
17   definitions.
18 */
19
20 static SCM
21 module_define_closure_func (void *closure, SCM key, SCM val, SCM result)
22 {
23   (void) result;
24   SCM module = (SCM) closure;
25   if (scm_variable_bound_p (val) == SCM_BOOL_T)
26     scm_module_define (module, key, scm_variable_ref (val));
27   return SCM_EOL;
28 }
29
30 LY_DEFINE (ly_module_copy, "ly:module-copy",
31            2, 0, 0, (SCM dest, SCM src),
32            "Copy all bindings from module SRC into DEST.")
33 {
34 #define FUNC_NAME __FUNCTION__
35   SCM_VALIDATE_MODULE (1, src);
36   scm_internal_hash_fold ((Hash_closure_function) & module_define_closure_func,
37                           (void *) dest,
38                           SCM_EOL, SCM_MODULE_OBARRAY (src));
39   return SCM_UNSPECIFIED;
40 }
41
42 LY_DEFINE(ly_clear_anonymous_modules, "ly:clear-anonymous-modules",
43           0, 0, 0, (),
44           "Plug a GUILE 1.6 and 1.7 memory leak by breaking a weak reference "
45           "pointer cycle explicitly."
46           )
47 {
48 #ifdef MODULE_GC_KLUDGE
49   clear_anonymous_modules();
50 #endif
51
52   return SCM_UNSPECIFIED;
53 }
54
55
56 /* Lookup SYM, but don't give error when it is not defined.  */
57 SCM
58 ly_module_lookup (SCM module, SCM sym)
59 {
60 #define FUNC_NAME __FUNCTION__
61   SCM_VALIDATE_MODULE (1, module);
62
63   return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
64 #undef FUNC_NAME
65 }
66
67 /* Lookup SYM in a list of modules, which do not have to be related.
68    Return the first instance. */
69 LY_DEFINE (ly_modules_lookup, "ly:modules-lookup",
70            2, 1, 0,
71            (SCM modules, SCM sym, SCM def),
72            "Lookup @var{sym} in the list @var{modules}, "
73            "returning the first occurence.  "
74            "If not found, return @var{default}, or @code{#f}.")
75 {
76   for (SCM s = modules; scm_is_pair (s); s = scm_cdr (s))
77     {
78       SCM mod = scm_car (s);
79       SCM v = ly_module_lookup (mod, sym);
80       if (SCM_VARIABLEP (v) && SCM_VARIABLE_REF (v) != SCM_UNDEFINED)
81         return scm_variable_ref (v);
82     }
83
84   if (def != SCM_UNDEFINED)
85     return def;
86   return SCM_BOOL_F;
87 }