2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 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/>.
21 #include "translator.hh"
22 #include "lily-imports.hh"
23 #include "international.hh"
24 #include "scm-hash.hh"
26 #include "protected-scm.hh"
29 Translator_creator::call (SCM ctx)
31 return (allocate_ (LY_ASSERT_SMOB (Context, ctx, 1)))->unprotect ();
34 Protected_scm global_translator_dict;
35 Protected_scm global_translator_dict_rev;
37 LY_DEFINE (get_all_translators, "ly:get-all-translators", 0, 0, 0, (),
38 "Return a list of all translator objects that may be"
41 Scheme_hash_table *dict = unsmob<Scheme_hash_table> (global_translator_dict);
42 SCM l = dict ? dict->to_alist () : SCM_EOL;
44 for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
45 scm_set_car_x (s, scm_cdar (s));
51 add_translator_creator (SCM creator, SCM name, SCM description)
53 Scheme_hash_table *dict = unsmob<Scheme_hash_table> (global_translator_dict);
56 global_translator_dict = Scheme_hash_table::make_smob ();
57 global_translator_dict_rev =
58 scm_make_weak_key_hash_table (scm_from_int (119));
59 dict = unsmob<Scheme_hash_table> (global_translator_dict);
61 dict->set (name, creator);
62 scm_hashq_set_x (global_translator_dict_rev, creator, scm_cons (name, description));
65 LY_DEFINE (ly_translator_name, "ly:translator-name",
66 1, 0, 0, (SCM creator),
67 "Return the type name of the translator definition @var{creator}."
68 " The name is a symbol.")
70 SCM res = global_translator_dict_rev.is_bound ()
71 ? scm_hashq_ref (global_translator_dict_rev, creator, SCM_BOOL_F)
73 SCM_ASSERT_TYPE (scm_is_pair (res),
74 creator, SCM_ARG1, __FUNCTION__, "translator definition");
78 LY_DEFINE (ly_translator_description, "ly:translator-description",
79 1, 0, 0, (SCM creator),
80 "Return an alist of properties of translator definition @var{creator}.")
82 SCM res = global_translator_dict_rev.is_bound ()
83 ? scm_hashq_ref (global_translator_dict_rev, creator, SCM_BOOL_F)
85 SCM_ASSERT_TYPE (scm_is_pair (res),
86 creator, SCM_ARG1, __FUNCTION__, "translator definition");
90 LY_DEFINE (ly_register_translator, "ly:register-translator",
91 2, 1, 0, (SCM creator, SCM name, SCM description),
92 "Register a translator @var{creator} (usually a descriptive"
93 " alist or a function/closure returning one when given a"
94 " context argument) with the given symbol @var{name} and"
95 " the given @var{description} alist.")
97 SCM_ASSERT_TYPE (ly_is_procedure (creator) || scm_is_pair (creator),
98 creator, SCM_ARG1, __FUNCTION__, "translator creator");
99 LY_ASSERT_TYPE (ly_is_symbol, name, 2);
100 if (SCM_UNBNDP (description))
101 description = SCM_EOL;
103 LY_ASSERT_TYPE (ly_is_list, description, 3);
104 add_translator_creator (creator, name, description);
105 return SCM_UNSPECIFIED;
109 get_translator_creator (SCM sym)
112 Scheme_hash_table *dict = unsmob<Scheme_hash_table> (global_translator_dict);
114 dict->try_retrieve (sym, &v);
116 if (scm_is_false (v))
118 warning (_f ("unknown translator: `%s'", ly_symbol2string (sym).c_str ()));