]> git.donarmstrong.com Git - lilypond.git/blob - lily/ly-module.cc
* lily/ly-module.cc (LY_DEFINE): bugfix.
[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
10 #include "main.hh"
11 #include "string.hh"
12 #include "lily-guile.hh"
13 #include "ly-module.hh"
14 #include "protected-scm.hh"
15
16 #define FUNC_NAME __FUNCTION__
17
18 static int module_count;
19
20 void
21 ly_init_anonymous_module (void *data)
22 {
23   (void) data;
24 }
25
26 SCM
27 ly_make_anonymous_module (bool safe)
28 {
29   SCM mod = SCM_EOL;
30   if (!safe)
31     {
32       
33       String s = "*anonymous-ly-" + to_string (module_count++) +  "*";
34       mod = scm_c_define_module (s.to_str0 (), ly_init_anonymous_module, 0);
35
36       ly_use_module (mod, global_lily_module);
37     }
38   else
39     {
40       SCM proc = ly_scheme_function ("make-safe-lilypond-module");
41
42       mod = scm_call_0 (proc);
43     }
44   return mod;
45 }
46
47 SCM
48 ly_use_module (SCM mod, SCM used)
49 {
50   SCM expr
51     = scm_list_3 (ly_symbol2scm ("module-use!"),
52                   mod,
53                   scm_list_2 (ly_symbol2scm ("module-public-interface"),
54                               used));
55   
56   return scm_eval (expr, global_lily_module);
57 }
58
59 #define FUNC_NAME __FUNCTION__
60
61 static SCM
62 ly_module_define (void *closure, SCM key, SCM val, SCM result)
63 {
64   (void) 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));
68   return SCM_EOL;
69 }
70
71 /* Ugh signature of scm_internal_hash_fold () is inaccurate.  */
72 typedef SCM (*Hash_cl_func)();
73
74 void
75 ly_import_module (SCM dest, SCM src)
76 {
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));
80 }
81
82 static SCM
83 accumulate_symbol (void *closure, SCM key, SCM val, SCM result)
84 {
85   (void) closure;
86   (void) val;
87   return scm_cons (key, result);
88 }
89
90 SCM
91 ly_module_symbols (SCM mod)
92 {
93   SCM_VALIDATE_MODULE (1, mod);
94   
95   SCM obarr= SCM_MODULE_OBARRAY (mod);
96   return scm_internal_hash_fold ((Hash_cl_func) &accumulate_symbol,
97                                  NULL, SCM_EOL, obarr); 
98 }
99
100 static SCM
101 entry_to_alist (void *closure, SCM key, SCM val, SCM result)
102 {
103   (void) closure;
104   return scm_cons (scm_cons (key, scm_variable_ref (val)), result);
105 }
106
107 LY_DEFINE(ly_module_to_alist, "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 /*
129   Lookup SYM in a list of modules, which do not have to be related.
130   Return the first instance.
131  */
132 LY_DEFINE(ly_modules_lookup, "ly:modules-lookup",
133           2, 1, 0,
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}.")
137 {
138   for (SCM s = modules; ly_c_pair_p (s); s = ly_cdr (s))
139     {
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);
144     }
145
146   if (def != SCM_UNDEFINED)
147     return def;
148   else
149     return SCM_BOOL_F;
150 }
151
152 void
153 ly_export (SCM module, SCM namelist)
154 {
155   static SCM export_function;
156   if (!export_function)
157     export_function = scm_permanent_object (scm_c_lookup ("module-export!"));
158   
159   scm_call_2 (SCM_VARIABLE_REF (export_function), module, namelist);
160 }
161
162 void
163 ly_reexport_module (SCM mod)
164 {
165   ly_export (mod, ly_module_symbols (mod));
166 }