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