]> git.donarmstrong.com Git - lilypond.git/blob - lily/ly-module.cc
(ly_make_anonymous_module): call make-module
[lilypond.git] / lily / ly-module.cc
1 /*
2   ly-module.cc -- implement guile module stuff.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2002--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "lily-guile.hh"
10 #include "warn.hh"
11 #include "main.hh"
12 #include "string.hh"
13
14 #define FUNC_NAME __FUNCTION__
15
16 SCM
17 ly_make_anonymous_module (bool safe)
18 {
19   SCM mod = SCM_EOL;
20   if (!safe)
21     {
22       SCM maker = ly_lily_module_constant ("make-module");
23       SCM scm_module = ly_lily_module_constant ("the-scm-module");
24       
25       mod = scm_call_0 (maker);
26       ly_use_module (mod, scm_module);
27       ly_use_module (mod, global_lily_module);
28     }
29   else
30     {
31       SCM proc = ly_lily_module_constant ("make-safe-lilypond-module");
32       mod = scm_call_0 (proc);
33     }
34   return mod;
35 }
36
37 SCM
38 ly_use_module (SCM mod, SCM used)
39 {
40   SCM expr
41     = scm_list_3 (ly_symbol2scm ("module-use!"),
42                   mod,
43                   scm_list_2 (ly_symbol2scm ("module-public-interface"),
44                               used));
45
46   return scm_eval (expr, global_lily_module);
47 }
48
49 #define FUNC_NAME __FUNCTION__
50
51 static SCM
52 module_define_closure_func (void *closure, SCM key, SCM val, SCM result)
53 {
54   (void) result;
55   SCM module = (SCM) closure;
56   if (scm_variable_bound_p (val) == SCM_BOOL_T)
57     scm_module_define (module, key, scm_variable_ref (val));
58   return SCM_EOL;
59 }
60
61 /* Ugh signature of scm_internal_hash_fold () is inaccurate.  */
62 typedef SCM (*Hash_cl_func) ();
63
64 /*
65   If a variable in changed in SRC, we DEST doesn't see the
66   definitions.
67 */
68 LY_DEFINE (ly_module_copy, "ly:module-copy",
69            2, 0, 0, (SCM dest, SCM src),
70            "Copy all bindings from module SRC into DEST.")
71 {
72   SCM_VALIDATE_MODULE (1, src);
73   scm_internal_hash_fold ((Hash_cl_func) & module_define_closure_func,
74                           (void *) dest,
75                           SCM_EOL, SCM_MODULE_OBARRAY (src));
76   return SCM_UNSPECIFIED;
77 }
78
79 static SCM
80 accumulate_symbol (void *closure, SCM key, SCM val, SCM result)
81 {
82   (void) closure;
83   (void) val;
84   return scm_cons (key, result);
85 }
86
87 SCM
88 ly_module_symbols (SCM mod)
89 {
90   SCM_VALIDATE_MODULE (1, mod);
91
92   SCM obarr = SCM_MODULE_OBARRAY (mod);
93   return scm_internal_hash_fold ((Hash_cl_func) & accumulate_symbol,
94                                  NULL, SCM_EOL, obarr);
95 }
96
97 static SCM
98 entry_to_alist (void *closure, SCM key, SCM val, SCM result)
99 {
100   (void) closure;
101   if (scm_variable_bound_p (val) == SCM_BOOL_T)
102     return scm_cons (scm_cons (key, scm_variable_ref (val)), result);
103   programming_error ("unbound variable in module");
104   return result;
105 }
106
107 LY_DEFINE (ly_module2alist, "ly:module->alist",
108            1, 0, 0, (SCM mod),
109            "Dump the contents of  module @var{mod} as an alist.")
110 {
111   SCM_VALIDATE_MODULE (1, mod);
112   SCM obarr = SCM_MODULE_OBARRAY (mod);
113
114   return scm_internal_hash_fold ((Hash_cl_func) & entry_to_alist, NULL, SCM_EOL, obarr);
115 }
116
117 /* Lookup SYM, but don't give error when it is not defined.  */
118 SCM
119 ly_module_lookup (SCM module, SCM sym)
120 {
121 #define FUNC_NAME __FUNCTION__
122   SCM_VALIDATE_MODULE (1, module);
123
124   return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
125 #undef FUNC_NAME
126 }
127
128 /* Lookup SYM in a list of modules, which do not have to be related.
129    Return the first instance. */
130 LY_DEFINE (ly_modules_lookup, "ly:modules-lookup",
131            2, 1, 0,
132            (SCM modules, SCM sym, SCM def),
133            "Lookup @var{sym} in the list @var{modules}, "
134            "returning the first occurence.  "
135            "If not found, return @var{default}, or @code{#f}.")
136 {
137   for (SCM s = modules; scm_is_pair (s); s = scm_cdr (s))
138     {
139       SCM mod = scm_car (s);
140       SCM v = ly_module_lookup (mod, sym);
141       if (SCM_VARIABLEP (v) && SCM_VARIABLE_REF (v) != SCM_UNDEFINED)
142         return scm_variable_ref (v);
143     }
144
145   if (def != SCM_UNDEFINED)
146     return def;
147   return SCM_BOOL_F;
148 }
149
150 void
151 ly_export (SCM module, SCM namelist)
152 {
153   static SCM export_function;
154   if (!export_function)
155     export_function = scm_permanent_object (scm_c_lookup ("module-export!"));
156
157   scm_call_2 (SCM_VARIABLE_REF (export_function), module, namelist);
158 }
159
160 void
161 ly_reexport_module (SCM mod)
162 {
163   ly_export (mod, ly_module_symbols (mod));
164 }