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