2 ly-module.cc -- implement guile module stuff.
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
12 #include "lily-guile.hh"
13 #include "ly-module.hh"
14 #include "protected-scm.hh"
16 #define FUNC_NAME __FUNCTION__
18 static int module_count;
21 ly_init_anonymous_module (void *data)
27 ly_make_anonymous_module (bool safe)
33 String s = "*anonymous-ly-" + to_string (module_count++) + "*";
34 mod = scm_c_define_module (s.to_str0 (), ly_init_anonymous_module, 0);
36 ly_use_module (mod, global_lily_module);
40 SCM proc = ly_scheme_function ("make-safe-lilypond-module");
42 mod = scm_call_0 (proc);
48 ly_use_module (SCM mod, SCM used)
51 = scm_list_3 (ly_symbol2scm ("module-use!"),
53 scm_list_2 (ly_symbol2scm ("module-public-interface"),
56 return scm_eval (expr, global_lily_module);
59 #define FUNC_NAME __FUNCTION__
62 ly_module_define (void *closure, SCM key, SCM val, SCM result)
65 SCM module = (SCM) closure;
66 if (scm_variable_bound_p (val) == SCM_BOOL_T)
67 scm_module_define (module, key, scm_variable_ref (val));
71 /* Ugh signature of scm_internal_hash_fold () is inaccurate. */
72 typedef SCM (*Hash_cl_func)();
75 ly_import_module (SCM dest, SCM src)
77 SCM_VALIDATE_MODULE (1, src);
78 scm_internal_hash_fold ((Hash_cl_func) &ly_module_define, (void*) dest,
79 SCM_EOL, SCM_MODULE_OBARRAY (src));
83 accumulate_symbol (void *closure, SCM key, SCM val, SCM result)
87 return scm_cons (key, result);
91 ly_module_symbols (SCM mod)
93 SCM_VALIDATE_MODULE (1, mod);
95 SCM obarr= SCM_MODULE_OBARRAY (mod);
96 return scm_internal_hash_fold ((Hash_cl_func) &accumulate_symbol,
97 NULL, SCM_EOL, obarr);
101 entry_to_alist (void *closure, SCM key, SCM val, SCM result)
104 return scm_cons (scm_cons (key, scm_variable_ref (val)), result);
107 LY_DEFINE(ly_module_to_alist, "ly:module->alist",
109 "Dump the contents of module @var{mod} as an alist.")
111 SCM_VALIDATE_MODULE (1, mod);
112 SCM obarr= SCM_MODULE_OBARRAY (mod);
114 return scm_internal_hash_fold ((Hash_cl_func) &entry_to_alist, NULL, SCM_EOL, obarr);
117 /* Lookup SYM, but don't give error when it is not defined. */
119 ly_module_lookup (SCM module, SCM sym)
121 #define FUNC_NAME __FUNCTION__
122 SCM_VALIDATE_MODULE (1, module);
124 return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
129 Lookup SYM in a list of modules, which do not have to be related.
130 Return the first instance.
132 LY_DEFINE(ly_modules_lookup, "ly:modules-lookup",
134 (SCM modules, SCM sym, SCM def),
135 "Lookup @var{sym} in the list @var{modules}, returning the "
136 "first occurence. If not found, return @var{default}, or @code{#f}.")
138 for (SCM s = modules; ly_c_pair_p (s); s = ly_cdr (s))
140 SCM mod = ly_car (s);
141 SCM v = scm_sym2var (sym, scm_module_lookup_closure (mod), SCM_UNDEFINED);
142 if (SCM_VARIABLEP(v) && SCM_VARIABLE_REF(v) != SCM_UNDEFINED)
143 return SCM_VARIABLE_REF(v);
146 if (def != SCM_UNDEFINED)
153 ly_export (SCM module, SCM namelist)
155 static SCM export_function;
156 if (!export_function)
157 export_function = scm_permanent_object (scm_c_lookup ("module-export!"));
159 scm_call_2 (SCM_VARIABLE_REF (export_function), module, namelist);
163 ly_reexport_module (SCM mod)
165 ly_export (mod, ly_module_symbols (mod));