]> git.donarmstrong.com Git - lilypond.git/blob - lily/ly-module.cc
*** empty log message ***
[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_vector_fill_x (SCM_MODULE_OBARRAY(gh_car(s)), SCM_EOL);
46     }
47 }
48
49 #define FUNC_NAME __FUNCTION__
50
51 void
52 ly_copy_module_variables (SCM dest, SCM src)
53 {
54   SCM_VALIDATE_MODULE (1, src);
55
56   SCM obarr= SCM_MODULE_OBARRAY(src);
57   SCM syms = SCM_EOL;
58
59   for (int i = 0;  i < SCM_VECTOR_LENGTH (obarr); i++)
60     {
61       for( SCM s = scm_vector_ref(obarr, SCM_MAKINUM (i));
62            gh_pair_p (s); s = gh_cdr (s))
63         {
64           scm_module_define (dest, gh_caar (s), scm_variable_ref (gh_cdar(s)));
65         }
66     }
67 }
68
69 SCM
70 ly_module_symbols (SCM mod)
71 {
72   SCM_VALIDATE_MODULE (1, mod);
73   
74   SCM obarr= SCM_MODULE_OBARRAY(mod);
75   SCM syms = SCM_EOL;
76
77   for (int i = 0;  i < SCM_VECTOR_LENGTH (obarr); i++)
78     {
79       for( SCM s = scm_vector_ref(obarr, SCM_MAKINUM (i));
80            gh_pair_p (s); s = gh_cdr (s))
81         {
82           syms = scm_cons (gh_caar (s), syms);
83         }
84     }
85   return syms;
86 }
87
88
89
90 SCM
91 ly_module_to_alist (SCM mod)
92 {
93   SCM_VALIDATE_MODULE (1, mod);
94   
95   
96   SCM obarr= SCM_MODULE_OBARRAY(mod);
97   SCM alist = SCM_EOL;
98
99   for (int i = 0;  i < SCM_VECTOR_LENGTH (obarr); i++)
100     {
101       for( SCM s = scm_vector_ref(obarr, SCM_MAKINUM (i));
102            gh_pair_p (s); s = gh_cdr (s))
103         {
104           alist = scm_acons (gh_caar (s), scm_variable_ref (gh_cdar (s)),
105                                                             alist);
106         }
107     }
108   return alist;
109 }
110
111 /*
112   Lookup SYM, but don't give error when it is not defined.
113  */
114 SCM
115 ly_module_lookup (SCM module, SCM sym)
116 {
117 #define FUNC_NAME __FUNCTION__
118   SCM_VALIDATE_MODULE (1, module);
119
120   return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
121 }
122
123 SCM export_function ;
124
125 void
126 ly_export (SCM module, SCM namelist)
127 {
128   if (!export_function)
129     {
130       export_function = scm_permanent_object (scm_c_lookup ("module-export!"));
131     }
132   
133   scm_call_2 (SCM_VARIABLE_REF (export_function),
134               module, namelist);
135 }
136
137 void
138 ly_reexport_module (SCM mod)
139 {
140   ly_export (mod, ly_module_symbols (mod));
141 }