]> git.donarmstrong.com Git - lilypond.git/blob - lily/module-scheme.cc
Run grand-replace for 2010.
[lilypond.git] / lily / module-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--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 "ly-module.hh"
21
22 #include "warn.hh"
23 #include "main.hh"
24 #include "std-string.hh"
25
26
27 /*
28   If a variable is changed in SRC, then DEST doesn't see the
29   definitions.
30 */
31
32 static SCM
33 module_define_closure_func (void *closure,
34                             SCM key,
35                             SCM val,
36                             SCM /* result */)
37 {
38   SCM module = (SCM) closure;
39   if (scm_variable_bound_p (val) == SCM_BOOL_T)
40     scm_module_define (module, key, scm_variable_ref (val));
41   return SCM_EOL;
42 }
43
44 LY_DEFINE (ly_module_copy, "ly:module-copy",
45            2, 0, 0, (SCM dest, SCM src),
46            "Copy all bindings from module @var{src} into @var{dest}.")
47 {
48 #define FUNC_NAME __FUNCTION__
49   SCM_VALIDATE_MODULE (1, src);
50   scm_internal_hash_fold ((Hash_closure_function) & module_define_closure_func,
51                           (void *) dest,
52                           SCM_EOL, SCM_MODULE_OBARRAY (src));
53   return SCM_UNSPECIFIED;
54 }
55
56 LY_DEFINE (ly_clear_anonymous_modules, "ly:clear-anonymous-modules",
57            0, 0, 0, (),
58            "Plug a GUILE 1.6 and 1.7 memory leak by breaking a weak"
59            " reference pointer cycle explicitly.")
60 {
61 #ifdef MODULE_GC_KLUDGE
62   clear_anonymous_modules ();
63 #endif
64
65   return SCM_UNSPECIFIED;
66 }
67
68 /* Lookup SYM, but don't give error when it is not defined.  */
69 SCM
70 ly_module_lookup (SCM module, SCM sym)
71 {
72 #define FUNC_NAME __FUNCTION__
73   SCM_VALIDATE_MODULE (1, module);
74
75   return scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F);
76 #undef FUNC_NAME
77 }
78
79 /* Lookup SYM in a list of modules, which do not have to be related.
80    Return the first instance. */
81 LY_DEFINE (ly_modules_lookup, "ly:modules-lookup",
82            2, 1, 0,
83            (SCM modules, SCM sym, SCM def),
84            "Look up @var{sym} in the list @var{modules},"
85            " returning the first occurence.  If not found, return"
86            " @var{def} or @code{#f} if @var{def} isn't specified.")
87 {
88   for (SCM s = modules; scm_is_pair (s); s = scm_cdr (s))
89     {
90       SCM mod = scm_car (s);
91       SCM v = ly_module_lookup (mod, sym);
92       if (SCM_VARIABLEP (v) && SCM_VARIABLE_REF (v) != SCM_UNDEFINED)
93         return scm_variable_ref (v);
94     }
95
96   if (def != SCM_UNDEFINED)
97     return def;
98   return SCM_BOOL_F;
99 }