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