2 ly-module.cc -- implement guile module stuff.
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "lily-guile.hh"
14 #define FUNC_NAME __FUNCTION__
16 static int module_count;
19 ly_init_anonymous_module (void *data)
25 ly_make_anonymous_module (bool safe)
30 String s = "*anonymous-ly-" + to_string (module_count++) + "*";
31 mod = scm_c_define_module (s.to_str0 (), ly_init_anonymous_module, 0);
32 ly_use_module (mod, global_lily_module);
36 SCM proc = ly_lily_module_constant ("make-safe-lilypond-module");
37 mod = scm_call_0 (proc);
43 ly_use_module (SCM mod, SCM used)
46 = scm_list_3 (ly_symbol2scm ("module-use!"),
48 scm_list_2 (ly_symbol2scm ("module-public-interface"),
51 return scm_eval (expr, global_lily_module);
54 #define FUNC_NAME __FUNCTION__
57 module_define_closure_func (void *closure, SCM key, SCM val, SCM result)
60 SCM module = (SCM) closure;
61 if (scm_variable_bound_p (val) == SCM_BOOL_T)
62 scm_module_define (module, key, scm_variable_ref (val));
66 /* Ugh signature of scm_internal_hash_fold () is inaccurate. */
67 typedef SCM (*Hash_cl_func)();
70 If a variable in changed in SRC, we DEST doesn't see the
73 LY_DEFINE (ly_module_copy, "ly:module-copy",
74 2, 0, 0, (SCM dest, SCM src),
75 "Copy all bindings from module SRC into DEST.")
77 SCM_VALIDATE_MODULE (1, src);
78 scm_internal_hash_fold ((Hash_cl_func) &module_define_closure_func,
80 SCM_EOL, SCM_MODULE_OBARRAY (src));
81 return SCM_UNSPECIFIED;
85 accumulate_symbol (void *closure, SCM key, SCM val, SCM result)
89 return scm_cons (key, result);
93 ly_module_symbols (SCM mod)
95 SCM_VALIDATE_MODULE (1, mod);
97 SCM obarr = SCM_MODULE_OBARRAY (mod);
98 return scm_internal_hash_fold ((Hash_cl_func) &accumulate_symbol,
99 NULL, SCM_EOL, obarr);
103 entry_to_alist (void *closure, SCM key, SCM val, SCM result)
106 if (scm_variable_bound_p (val) == SCM_BOOL_T)
108 return scm_cons (scm_cons (key, scm_variable_ref (val)), result);
112 programming_error ("Unbound variable in module.");
117 LY_DEFINE (ly_module2alist, "ly:module->alist",
119 "Dump the contents of module @var{mod} as an alist.")
121 SCM_VALIDATE_MODULE (1, mod);
122 SCM obarr = SCM_MODULE_OBARRAY (mod);
124 return scm_internal_hash_fold ((Hash_cl_func) &entry_to_alist, NULL, SCM_EOL, obarr);
127 /* Lookup SYM, but don't give error when it is not defined. */
129 ly_module_lookup (SCM module, SCM sym)
131 #define FUNC_NAME __FUNCTION__
132 SCM_VALIDATE_MODULE (1, module);
134 return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
138 /* Lookup SYM in a list of modules, which do not have to be related.
139 Return the first instance. */
140 LY_DEFINE (ly_modules_lookup, "ly:modules-lookup",
142 (SCM modules, SCM sym, SCM def),
143 "Lookup @var{sym} in the list @var{modules}, "
144 "returning the first occurence. "
145 "If not found, return @var{default}, or @code{#f}.")
147 for (SCM s = modules; scm_is_pair (s); s = scm_cdr (s))
149 SCM mod = scm_car (s);
150 SCM v = ly_module_lookup (mod, sym);
151 if (SCM_VARIABLEP (v) && SCM_VARIABLE_REF (v) != SCM_UNDEFINED)
152 return scm_variable_ref(v);
155 if (def != SCM_UNDEFINED)
161 ly_export (SCM module, SCM namelist)
163 static SCM export_function;
164 if (!export_function)
165 export_function = scm_permanent_object (scm_c_lookup ("module-export!"));
167 scm_call_2 (SCM_VARIABLE_REF (export_function), module, namelist);
171 ly_reexport_module (SCM mod)
173 ly_export (mod, ly_module_symbols (mod));