]> git.donarmstrong.com Git - lilypond.git/blob - lily/ly-module.cc
(ly_copy_module_variables): use
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8  */
9
10 #include "string.hh"
11 #include "lily-guile.hh"
12 #include "ly-modules.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   scm_c_use_module ("lily");  
23 }
24
25 Protected_scm anon_modules;
26
27 SCM
28 ly_make_anonymous_module ()
29 {
30   String s = "*anonymous-ly-" + to_string (module_count++) +  "*";
31   SCM mod = scm_c_define_module (s.to_str0(), ly_init_anonymous_module, 0);
32
33   anon_modules = scm_cons (mod, anon_modules);
34   return mod;
35 }
36
37 void
38 ly_clear_anonymous_modules ()
39 {
40   SCM s = anon_modules;
41   anon_modules = SCM_EOL;
42   
43   for (; gh_pair_p (s) ; s = gh_cdr (s))
44     {
45       SCM tab= scm_c_make_hash_table (2);
46       /*
47         UGH.
48       */
49       
50       SCM_STRUCT_DATA(gh_car(s))[scm_module_index_obarray] = (long unsigned int) tab;
51     }
52 }
53
54 #define FUNC_NAME __FUNCTION__
55
56 SCM
57 define_one_var (void * closure, SCM key, SCM val, SCM result)
58 {
59   SCM dest =  (SCM) closure;
60   scm_module_define (dest, key, scm_variable_ref (val));
61   return SCM_EOL;
62 }
63
64 typedef SCM (*Hash_cl_func)();
65
66 void
67 ly_copy_module_variables (SCM dest, SCM src)
68 {
69   SCM_VALIDATE_MODULE (1, src);
70
71   SCM obarr= SCM_MODULE_OBARRAY(src);
72   scm_internal_hash_fold ((Hash_cl_func) &define_one_var, (void*) dest, SCM_EOL, obarr);
73 }
74
75 SCM
76 accumulate_symbol (void * closure, SCM key, SCM val, SCM result)
77 {
78   return scm_cons (key, result);
79 }
80
81 SCM
82 ly_module_symbols (SCM mod)
83 {
84   SCM_VALIDATE_MODULE (1, mod);
85   
86   SCM obarr= SCM_MODULE_OBARRAY(mod);
87   return scm_internal_hash_fold ((Hash_cl_func) &accumulate_symbol, NULL, SCM_EOL, obarr); 
88 }
89
90 SCM
91 entry_to_alist (void * closure, SCM key, SCM val, SCM result)
92 {
93   return scm_cons (scm_cons (key, scm_variable_ref (val)), result);
94 }
95
96 SCM
97 ly_module_to_alist (SCM mod)
98 {
99   SCM_VALIDATE_MODULE (1, mod);
100   
101   
102   SCM obarr= SCM_MODULE_OBARRAY(mod);
103
104   return scm_internal_hash_fold ((Hash_cl_func) &entry_to_alist, NULL, SCM_EOL, obarr); 
105 }
106
107 /*
108   Lookup SYM, but don't give error when it is not defined.
109  */
110 SCM
111 ly_module_lookup (SCM module, SCM sym)
112 {
113 #define FUNC_NAME __FUNCTION__
114   SCM_VALIDATE_MODULE (1, module);
115
116   return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
117 }
118
119 SCM export_function ;
120
121 void
122 ly_export (SCM module, SCM namelist)
123 {
124   if (!export_function)
125     {
126       export_function = scm_permanent_object (scm_c_lookup ("module-export!"));
127     }
128   
129   scm_call_2 (SCM_VARIABLE_REF (export_function),
130               module, namelist);
131 }
132
133 void
134 ly_reexport_module (SCM mod)
135 {
136   ly_export (mod, ly_module_symbols (mod));
137 }