]> git.donarmstrong.com Git - lilypond.git/blob - lily/ly-module.cc
Web-ja: update introduction
[lilypond.git] / lily / ly-module.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2002--2015 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 "ly-module.hh"
22 #include "warn.hh"
23 #include "main.hh"
24 #include "protected-scm.hh"
25 #include "lily-imports.hh"
26
27 SCM
28 ly_make_module (bool safe)
29 {
30   SCM mod = SCM_EOL;
31   if (!safe)
32     {
33       /* Look up (evaluate) Scheme make-module function and call it */
34
35       mod = Guile_user::make_module ();
36       /*
37         Look up and call Guile module-export-all! or, when using
38         Guile V1.8, the compatible shim defined in lily.scm.
39       */
40 #if GUILEV2
41       Guile_user::module_export_all_x (mod);
42 #else
43       Lily::module_export_all_x (mod);
44 #endif
45
46       /*
47         Evaluate Guile module "the-root-module",
48         and ensure we inherit definitions from it and the "lily" module
49         N.B. this used to be "the-scm-module" and is deprecated in
50         Guile V1.9/2.0
51       */
52
53       ly_use_module (mod, Guile_user::the_root_module);
54       ly_use_module (mod, Lily::module);
55     }
56   else
57     {
58       /* Evaluate and call make-safe-lilypond-module */
59       mod = Lily::make_safe_lilypond_module ();
60     }
61
62   return mod;
63 }
64
65 SCM
66 ly_use_module (SCM mod, SCM used)
67 {
68   /*
69     Pick up the module's interface definition.
70     TODO - Replace inline evaluations (interpreted)
71     with guile API calls if these become available.
72   */
73   /*
74     Set up to interpret
75     '(module_use! <mod> (module-public-interface <used>))'
76   */
77   return Guile_user::module_use_x (mod,
78                                    Guile_user::module_public_interface (used));
79 }
80
81 #define FUNC_NAME __FUNCTION__
82
83 SCM
84 ly_module_symbols (SCM mod)
85 {
86   SCM_VALIDATE_MODULE (1, mod);
87
88   SCM obarr = SCM_MODULE_OBARRAY (mod);
89   return ly_hash_table_keys (obarr);
90 }
91
92 static SCM
93 entry_to_alist (void * /* closure */,
94                 SCM key,
95                 SCM val,
96                 SCM result)
97 {
98   if (to_boolean (scm_variable_bound_p (val)))
99     return scm_cons (scm_cons (key, scm_variable_ref (val)), result);
100   programming_error ("unbound variable in module");
101   return result;
102 }
103
104 LY_DEFINE (ly_module_2_alist, "ly:module->alist",
105            1, 0, 0, (SCM mod),
106            "Dump the contents of module @var{mod} as an alist.")
107 {
108   SCM_VALIDATE_MODULE (1, mod);
109   SCM obarr = SCM_MODULE_OBARRAY (mod);
110
111   return scm_internal_hash_fold ((scm_t_hash_fold_fn) &entry_to_alist,
112                                  NULL, SCM_EOL, obarr);
113 }
114
115 void
116 ly_export (SCM module, SCM namelist)
117 {
118   Guile_user::module_export_x (module, namelist);
119 }
120
121 void
122 ly_reexport_module (SCM mod)
123 {
124   ly_export (mod, ly_module_symbols (mod));
125 }