]> git.donarmstrong.com Git - lilypond.git/blob - lily/module-scheme.cc
Doc-es: various updates.
[lilypond.git] / lily / module-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--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 "ly-module.hh" // pulls in lily-guile.hh and guile-compatibility.hh
21 #include "warn.hh"
22 #include "main.hh"
23 #include "std-string.hh"
24
25 /*
26   If a variable is changed in SRC, then DEST doesn't see the
27   definitions.
28 */
29
30 static SCM
31 module_define_closure_func (void *closure,
32                             SCM key,
33                             SCM val,
34                             SCM /* result */)
35 {
36   SCM module = *static_cast<SCM *> (closure);
37   if (to_boolean (scm_variable_bound_p (val)))
38     scm_module_define (module, key, scm_variable_ref (val));
39   return SCM_EOL;
40 }
41
42 LY_DEFINE (ly_module_copy, "ly:module-copy",
43            2, 0, 0, (SCM dest, SCM src),
44            "Copy all bindings from module @var{src} into @var{dest}.")
45 {
46 #define FUNC_NAME __FUNCTION__
47   SCM_VALIDATE_MODULE (1, src);
48   scm_internal_hash_fold ((scm_t_hash_fold_fn) &module_define_closure_func,
49                           static_cast<void *> (&dest),
50                           SCM_EOL, SCM_MODULE_OBARRAY (src));
51   return SCM_UNSPECIFIED;
52 }
53
54
55
56 /* Lookup SYM, but don't give error when it is not defined.
57    N.B. this is only needed when running with Guile versions
58    prior to V2.0.3, when calls to ly_module_lookup can be replaced
59    with direct calls to the Guile API scm_module_variable in the
60    LilyPond codebase.
61 */
62 SCM
63 ly_module_lookup (SCM module, SCM sym)
64 {
65 #define FUNC_NAME __FUNCTION__
66   SCM_VALIDATE_MODULE (1, module);
67 /*
68   Issue 2758:
69     Guile V2 onward has a scm_module_variable API module.
70     Guile V1.8.7 only has a (module-variable) REPL function and we
71     can't import this via Scm_variable since that needs
72     ly_module_lookup itself.
73  */
74 #if GUILEV1
75   return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
76 #else
77   return scm_module_variable (module, sym);
78 #endif
79 #undef FUNC_NAME
80 }
81
82 /* Lookup SYM in a list of modules, which do not have to be related.
83    Return the first instance. */
84 LY_DEFINE (ly_modules_lookup, "ly:modules-lookup",
85            2, 1, 0,
86            (SCM modules, SCM sym, SCM def),
87            "Look up @var{sym} in the list @var{modules},"
88            " returning the first occurence.  If not found, return"
89            " @var{def} or @code{#f} if @var{def} isn't specified.")
90 {
91   for (SCM s = modules; scm_is_pair (s); s = scm_cdr (s))
92     {
93       SCM mod = scm_car (s);
94       SCM v = ly_module_lookup (mod, sym);
95       if (SCM_VARIABLEP (v) && !SCM_UNBNDP (SCM_VARIABLE_REF (v)))
96         return scm_variable_ref (v);
97     }
98
99   if (!SCM_UNBNDP (def))
100     return def;
101   return SCM_BOOL_F;
102 }