2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "ly-module.hh"
24 #include "std-string.hh"
27 If a variable is changed in SRC, then DEST doesn't see the
32 module_define_closure_func (void *closure,
37 SCM module = (SCM) closure;
38 if (scm_variable_bound_p (val) == SCM_BOOL_T)
39 scm_module_define (module, key, scm_variable_ref (val));
43 LY_DEFINE (ly_module_copy, "ly:module-copy",
44 2, 0, 0, (SCM dest, SCM src),
45 "Copy all bindings from module @var{src} into @var{dest}.")
47 #define FUNC_NAME __FUNCTION__
48 SCM_VALIDATE_MODULE (1, src);
49 scm_internal_hash_fold ((scm_t_hash_fold_fn) &module_define_closure_func,
51 SCM_EOL, SCM_MODULE_OBARRAY (src));
52 return SCM_UNSPECIFIED;
55 /* Lookup SYM, but don't give error when it is not defined. */
57 ly_module_lookup (SCM module, SCM sym)
59 #define FUNC_NAME __FUNCTION__
60 SCM_VALIDATE_MODULE (1, module);
62 return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
66 /* Lookup SYM in a list of modules, which do not have to be related.
67 Return the first instance. */
68 LY_DEFINE (ly_modules_lookup, "ly:modules-lookup",
70 (SCM modules, SCM sym, SCM def),
71 "Look up @var{sym} in the list @var{modules},"
72 " returning the first occurence. If not found, return"
73 " @var{def} or @code{#f} if @var{def} isn't specified.")
75 for (SCM s = modules; scm_is_pair (s); s = scm_cdr (s))
77 SCM mod = scm_car (s);
78 SCM v = ly_module_lookup (mod, sym);
79 if (SCM_VARIABLEP (v) && SCM_VARIABLE_REF (v) != SCM_UNDEFINED)
80 return scm_variable_ref (v);
83 if (def != SCM_UNDEFINED)