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