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