]> git.donarmstrong.com Git - lilypond.git/blob - lily/ly-module.cc
* lily/main.cc (main_with_guile): call lilypond-main
[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 "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   scm_c_use_module ("lily");
24 }
25
26 SCM
27 ly_make_anonymous_module ()
28 {
29   String s = "*anonymous-ly-" + to_string (module_count++) +  "*";
30   SCM mod = scm_c_define_module (s.to_str0(), ly_init_anonymous_module, 0);
31   return mod;
32 }
33
34 #define FUNC_NAME __FUNCTION__
35
36 static SCM
37 ly_module_define (void *closure, SCM key, SCM val, SCM result)
38 {
39   (void) result;
40   SCM module = (SCM) closure;
41   scm_module_define (module, key, scm_variable_ref (val));
42   return SCM_EOL;
43 }
44
45 /* Ugh signature of scm_internal_hash_fold () is inaccurate.  */
46 typedef SCM (*Hash_cl_func)();
47
48 void
49 ly_import_module (SCM dest, SCM src)
50 {
51   SCM_VALIDATE_MODULE (1, src);
52   scm_internal_hash_fold ((Hash_cl_func) &ly_module_define, (void*) dest,
53                           SCM_EOL, SCM_MODULE_OBARRAY (src));
54 }
55
56 static SCM
57 accumulate_symbol (void *closure, SCM key, SCM val, SCM result)
58 {
59   (void) closure;
60   (void) val;
61   return scm_cons (key, result);
62 }
63
64 SCM
65 ly_module_symbols (SCM mod)
66 {
67   SCM_VALIDATE_MODULE (1, mod);
68   
69   SCM obarr= SCM_MODULE_OBARRAY (mod);
70   return scm_internal_hash_fold ((Hash_cl_func) &accumulate_symbol,
71                                  NULL, SCM_EOL, obarr); 
72 }
73
74 static SCM
75 entry_to_alist (void *closure, SCM key, SCM val, SCM result)
76 {
77   (void) closure;
78   return scm_cons (scm_cons (key, scm_variable_ref (val)), result);
79 }
80
81 SCM
82 ly_module_to_alist (SCM mod)
83 {
84   SCM_VALIDATE_MODULE (1, mod);
85   
86   
87   SCM obarr= SCM_MODULE_OBARRAY (mod);
88
89   return scm_internal_hash_fold ((Hash_cl_func) &entry_to_alist, NULL, SCM_EOL, obarr); 
90 }
91
92 /* Lookup SYM, but don't give error when it is not defined.  */
93 SCM
94 ly_module_lookup (SCM module, SCM sym)
95 {
96 #define FUNC_NAME __FUNCTION__
97   SCM_VALIDATE_MODULE (1, module);
98
99   return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
100 #undef FUNC_NAME
101 }
102
103 SCM
104 ly_modules_lookup (SCM modules, SCM sym)
105 {
106   for (SCM s = ly_car (modules); SCM_MODULEP (s); s = ly_cdr (s))
107     {
108       SCM v = scm_sym2var (sym, scm_module_lookup_closure (s), SCM_UNDEFINED);
109       if (v != SCM_UNDEFINED)
110         return v;
111     }
112   return SCM_UNDEFINED;
113 }
114
115
116 void
117 ly_export (SCM module, SCM namelist)
118 {
119   static SCM export_function;
120   if (!export_function)
121     export_function = scm_permanent_object (scm_c_lookup ("module-export!"));
122   
123   scm_call_2 (SCM_VARIABLE_REF (export_function), module, namelist);
124 }
125
126 void
127 ly_reexport_module (SCM mod)
128 {
129   ly_export (mod, ly_module_symbols (mod));
130 }