]> git.donarmstrong.com Git - lilypond.git/blob - lily/ly-module.cc
(ly_module_define): only define variable if
[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 SCM
108 ly_module_to_alist (SCM mod)
109 {
110   SCM_VALIDATE_MODULE (1, mod);
111   
112   
113   SCM obarr= SCM_MODULE_OBARRAY (mod);
114
115   return scm_internal_hash_fold ((Hash_cl_func) &entry_to_alist, NULL, SCM_EOL, obarr); 
116 }
117
118 /* Lookup SYM, but don't give error when it is not defined.  */
119 SCM
120 ly_module_lookup (SCM module, SCM sym)
121 {
122 #define FUNC_NAME __FUNCTION__
123   SCM_VALIDATE_MODULE (1, module);
124
125   return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
126 #undef FUNC_NAME
127 }
128
129 /*
130   Lookup SYM in a list of modules, which do not have to be related.
131   Return the first instance.
132  */
133 SCM
134 ly_modules_lookup (SCM modules, SCM sym)
135 {
136   for (SCM s = ly_car (modules); SCM_MODULEP (s); s = ly_cdr (s))
137     {
138       SCM v = scm_sym2var (sym, scm_module_lookup_closure (s), SCM_UNDEFINED);
139       if (v != SCM_UNDEFINED)
140         return v;
141     }
142   return SCM_UNDEFINED;
143 }
144
145
146 void
147 ly_export (SCM module, SCM namelist)
148 {
149   static SCM export_function;
150   if (!export_function)
151     export_function = scm_permanent_object (scm_c_lookup ("module-export!"));
152   
153   scm_call_2 (SCM_VARIABLE_REF (export_function), module, namelist);
154 }
155
156 void
157 ly_reexport_module (SCM mod)
158 {
159   ly_export (mod, ly_module_symbols (mod));
160 }