]> git.donarmstrong.com Git - lilypond.git/blob - lily/ly-module.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / ly-module.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2002--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "lily-guile.hh"
21 #include "warn.hh"
22 #include "main.hh"
23 #include "std-string.hh"
24 #include "protected-scm.hh"
25
26
27 SCM
28 ly_make_module (bool safe)
29 {
30   SCM mod = SCM_EOL;
31   if (!safe)
32     {
33       SCM maker = ly_lily_module_constant ("make-module");
34
35       SCM scm_module = ly_lily_module_constant ("the-scm-module");
36
37       mod = scm_call_0 (maker);
38       scm_module_define (mod, ly_symbol2scm ("%module-public-interface"),
39                          mod);
40
41       ly_use_module (mod, scm_module);
42       ly_use_module (mod, global_lily_module);
43     }
44   else
45     {
46       SCM proc = ly_lily_module_constant ("make-safe-lilypond-module");
47       mod = scm_call_0 (proc);
48     }
49
50
51   return mod;
52 }
53
54 SCM
55 ly_use_module (SCM mod, SCM used)
56 {
57   SCM expr
58     = scm_list_3 (ly_symbol2scm ("module-use!"),
59                   mod,
60                   scm_list_2 (ly_symbol2scm ("module-public-interface"),
61                               used));
62
63   return scm_eval (expr, global_lily_module);
64 }
65
66 #define FUNC_NAME __FUNCTION__
67
68
69
70 SCM
71 ly_module_symbols (SCM mod)
72 {
73   SCM_VALIDATE_MODULE (1, mod);
74
75   SCM obarr = SCM_MODULE_OBARRAY (mod);
76   return ly_hash_table_keys (obarr);
77 }
78
79 static SCM
80 entry_to_alist (void * /* closure */,
81                 SCM key,
82                 SCM val,
83                 SCM result)
84 {
85   if (scm_variable_bound_p (val) == SCM_BOOL_T)
86     return scm_cons (scm_cons (key, scm_variable_ref (val)), result);
87   programming_error ("unbound variable in module");
88   return result;
89 }
90
91 LY_DEFINE (ly_module_2_alist, "ly:module->alist",
92            1, 0, 0, (SCM mod),
93            "Dump the contents of module @var{mod} as an alist.")
94 {
95   SCM_VALIDATE_MODULE (1, mod);
96   SCM obarr = SCM_MODULE_OBARRAY (mod);
97
98   return scm_internal_hash_fold ((scm_t_hash_fold_fn) &entry_to_alist,
99                                  NULL, SCM_EOL, obarr);
100 }
101
102 void
103 ly_export (SCM module, SCM namelist)
104 {
105   static SCM export_function;
106   if (!export_function)
107     export_function = scm_permanent_object (scm_c_lookup ("module-export!"));
108
109   scm_call_2 (SCM_VARIABLE_REF (export_function), module, namelist);
110 }
111
112 void
113 ly_reexport_module (SCM mod)
114 {
115   ly_export (mod, ly_module_symbols (mod));
116 }