]> git.donarmstrong.com Git - lilypond.git/blob - lily/ly-module.cc
small cleanups.
[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 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 #define FUNC_NAME __FUNCTION__
14
15 static int module_count;
16
17 void
18 ly_init_anonymous_module (void * data)
19 {
20   scm_c_use_module ("lily");  
21 }
22
23 SCM
24 ly_make_anonymous_module ()
25 {
26   String s = "*anonymous-ly-" + to_string (module_count++) +  "*";
27   SCM mod = scm_c_define_module (s.to_str0(), ly_init_anonymous_module, 0);
28   return mod;
29 }
30
31 void
32 ly_copy_module_variables (SCM dest, SCM src)
33 {
34  #define FUNC_NAME __FUNCTION__
35   SCM_VALIDATE_MODULE (1, src);
36
37   SCM obarr= SCM_MODULE_OBARRAY(src);
38   SCM syms = SCM_EOL;
39
40   for (int i = 0;  i < SCM_VECTOR_LENGTH (obarr); i++)
41     {
42       for( SCM s = scm_vector_ref(obarr, SCM_MAKINUM (i));
43            gh_pair_p (s); s = gh_cdr (s))
44         {
45           scm_module_define (dest, gh_caar (s), scm_variable_ref (gh_cdar(s)));
46         }
47     }
48 }
49
50 SCM
51 ly_module_symbols (SCM mod)
52 {
53   SCM_VALIDATE_MODULE (1, mod);
54   
55   SCM obarr= SCM_MODULE_OBARRAY(mod);
56   SCM syms = SCM_EOL;
57
58   for (int i = 0;  i < SCM_VECTOR_LENGTH (obarr); i++)
59     {
60       for( SCM s = scm_vector_ref(obarr, SCM_MAKINUM (i));
61            gh_pair_p (s); s = gh_cdr (s))
62         {
63           syms = scm_cons (gh_caar (s), syms);
64         }
65     }
66   return syms;
67 }
68
69
70
71 SCM
72 ly_module_to_alist (SCM mod)
73 {
74   SCM_VALIDATE_MODULE (1, mod);
75   
76   
77   SCM obarr= SCM_MODULE_OBARRAY(mod);
78   SCM alist = SCM_EOL;
79
80   for (int i = 0;  i < SCM_VECTOR_LENGTH (obarr); i++)
81     {
82       for( SCM s = scm_vector_ref(obarr, SCM_MAKINUM (i));
83            gh_pair_p (s); s = gh_cdr (s))
84         {
85           alist = scm_acons (gh_caar (s), scm_variable_ref (gh_cdar (s)),
86                                                             alist);
87         }
88     }
89   return alist;
90 }
91
92 /*
93   Lookup SYM, but don't give error when it is not defined.
94  */
95 SCM
96 ly_module_lookup (SCM module, SCM sym)
97 {
98 #define FUNC_NAME __FUNCTION__
99   SCM_VALIDATE_MODULE (1, module);
100
101   return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
102 }
103
104 SCM export_function ;
105
106 void
107 ly_export (SCM module, SCM namelist)
108 {
109   if (!export_function)
110     {
111       export_function = scm_permanent_object (scm_c_lookup ("module-export!"));
112     }
113   
114   scm_call_2 (SCM_VARIABLE_REF (export_function),
115               module, namelist);
116 }
117
118 void
119 ly_reexport_module (SCM mod)
120 {
121   ly_export (mod, ly_module_symbols (mod));
122 }