]> git.donarmstrong.com Git - lilypond.git/blob - lily/program-option-scheme.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[lilypond.git] / lily / program-option-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2001--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 "program-option.hh"
21
22 #include <cstdio>
23 #include <cstring>
24 using namespace std;
25
26 #include "profile.hh"
27 #include "international.hh"
28 #include "main.hh"
29 #include "parse-scm.hh"
30 #include "string-convert.hh"
31 #include "warn.hh"
32
33 bool debug_skylines;
34 bool debug_property_callbacks;
35 bool debug_page_breaking_scoring;
36
37 bool music_strings_to_paths;
38 bool relative_includes;
39
40 /*
41   Backwards compatibility.
42 */
43 bool lily_1_8_relative = false;
44 bool lily_1_8_compatibility_used = false;
45 bool profile_property_accesses = false;
46 /*
47   crash if internally the wrong type is used for a grob property.
48 */
49 bool do_internal_type_checking_global;
50 bool strict_infinity_checking = false;
51
52 static SCM option_hash;
53
54
55 void
56 internal_set_option (SCM var,
57                      SCM val)
58 {
59   if (0)
60     ;
61   else if (var == ly_symbol2scm ("profile-property-accesses"))
62     {
63       profile_property_accesses = to_boolean (val);
64       val = scm_from_bool (to_boolean (val));
65     }
66   else if (var == ly_symbol2scm ("point-and-click"))
67     {
68       point_and_click_global = to_boolean (val);
69       val = scm_from_bool (to_boolean (val));
70     }
71   else if (var == ly_symbol2scm ("protected-scheme-parsing"))
72     {
73       parse_protect_global = to_boolean (val);
74       val = scm_from_bool (to_boolean (val));
75     }
76   else if (var == ly_symbol2scm ("check-internal-types"))
77     {
78       do_internal_type_checking_global = to_boolean (val);
79       val = scm_from_bool (to_boolean (val));
80     }
81   else if (var == ly_symbol2scm ("debug-gc-assert-parsed-dead"))
82     {
83       parsed_objects_should_be_dead = to_boolean (val);
84       val = scm_from_bool (parsed_objects_should_be_dead);
85     }
86   else if (var == ly_symbol2scm ("safe"))
87     {
88       be_safe_global = to_boolean (val);
89       val = scm_from_bool (be_safe_global);
90     }
91   else if (var == ly_symbol2scm ("old-relative"))
92     {
93       lily_1_8_relative = to_boolean (val);
94       /* Needs to be reset for each file that uses this option. */
95       lily_1_8_compatibility_used = to_boolean (val);
96       val = scm_from_bool (to_boolean (val));
97     }
98   else if (var == ly_symbol2scm ("strict-infinity-checking"))
99     {
100       strict_infinity_checking = to_boolean (val);
101       val = scm_from_bool (to_boolean (val));
102     }
103   else if (var == ly_symbol2scm ("debug-skylines"))
104     {
105       debug_skylines = to_boolean (val);
106       val = scm_from_bool (to_boolean (val));
107     }
108   else if (var == ly_symbol2scm ("debug-property-callbacks"))
109     {
110       debug_property_callbacks = to_boolean (val);
111       val = scm_from_bool (to_boolean (val));
112     }
113   else if (var == ly_symbol2scm ("debug-page-breaking-scoring"))
114     {
115       debug_page_breaking_scoring = to_boolean (val);
116       val = scm_from_bool (to_boolean (val));
117     }
118   else if (var == ly_symbol2scm ("datadir"))
119     {
120       /* ignore input value. */
121       val = ly_string2scm (lilypond_datadir);
122     }
123   else if (var == ly_symbol2scm ("relative-includes"))
124     {
125       relative_includes = to_boolean (val);
126       val = scm_from_bool (to_boolean (val));
127     }
128   else if (var == ly_symbol2scm ("warning-as-error"))
129     val = scm_from_bool (to_boolean (val));
130   else if (var == ly_symbol2scm ("music-strings-to-paths"))
131     {
132       music_strings_to_paths = to_boolean (val);
133       val = scm_from_bool (to_boolean (val));
134     }
135
136   scm_hashq_set_x (option_hash, var, val);
137 }
138
139
140 ssize const HELP_INDENT = 30;
141 ssize const INDENT = 2;
142 ssize const SEPARATION = 5;
143
144 /*
145   Hmmm. should do in SCM / C++  ?
146 */
147 static string
148 get_help_string ()
149 {
150   SCM alist = ly_hash2alist (option_hash);
151   SCM converter = ly_lily_module_constant ("scm->string");
152
153   vector<string> opts;
154
155   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
156     {
157       SCM sym = scm_caar (s);
158       SCM val = scm_cdar (s);
159       string opt_spec = String_convert::char_string (' ', INDENT)
160                         + ly_symbol2string (sym)
161                         + " ("
162                         + ly_scm2string (scm_call_1 (converter, val))
163                         + ")";
164
165       if (opt_spec.length () + SEPARATION > HELP_INDENT)
166         opt_spec += "\n" + String_convert::char_string (' ', HELP_INDENT);
167       else
168         opt_spec += String_convert::char_string (' ', HELP_INDENT
169                                                       - opt_spec.length ());
170
171       SCM opt_help_scm
172         = scm_object_property (sym,
173                                ly_symbol2scm ("program-option-documentation"));
174       string opt_help = ly_scm2string (opt_help_scm);
175       replace_all (&opt_help,
176                    string ("\n"),
177                    string ("\n")
178                    + String_convert::char_string (' ', HELP_INDENT));
179
180       opts.push_back (opt_spec + opt_help + "\n");
181     }
182
183   string help ("Options supported by `ly:set-option':\n\n");
184   vector_sort (opts, less<string> ());
185   for (vsize i = 0; i < opts.size (); i++)
186     help += opts[i];
187   return help;
188 }
189
190
191 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
192            "Print @code{ly:set-option} usage.")
193 {
194   string help = get_help_string ();
195   puts (help.c_str());
196
197   return SCM_UNSPECIFIED;
198 }
199
200
201 LY_DEFINE (ly_add_option, "ly:add-option", 3, 0, 0,
202            (SCM sym, SCM val, SCM description),
203            "Add a program option @var{sym}.  @var{val} is the default"
204            " value and @var{description} is a string description.")
205 {
206   if (!option_hash)
207     option_hash = scm_permanent_object (scm_c_make_hash_table (11));
208   LY_ASSERT_TYPE (ly_is_symbol, sym, 1);
209   LY_ASSERT_TYPE (scm_is_string, description, 3);
210
211   internal_set_option (sym, val);
212
213   scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
214                              description);
215
216   return SCM_UNSPECIFIED;
217 }
218
219
220 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
221            "Set a program option.")
222 {
223   LY_ASSERT_TYPE (ly_is_symbol, var, 1);
224
225   if (val == SCM_UNDEFINED)
226     val = SCM_BOOL_T;
227
228   string varstr = ly_scm2string (scm_symbol_to_string (var));
229   if (varstr.substr (0, 3) == string ("no-"))
230     {
231       var = ly_symbol2scm (varstr.substr (3, varstr.length () - 3).c_str ());
232       val = scm_from_bool (!to_boolean (val));
233     }
234
235   SCM handle = scm_hashq_get_handle (option_hash, var);
236   if (handle == SCM_BOOL_F)
237     warning (_f ("no such internal option: %s", varstr.c_str ()));
238
239   internal_set_option (var, val);
240   return SCM_UNSPECIFIED;
241 }
242
243
244 LY_DEFINE (ly_command_line_options, "ly:command-line-options", 0, 0, 0, (),
245            "The Scheme options specified on command-line with @option{-d}.")
246 {
247   return ly_string2scm (init_scheme_variables_global);
248 }
249
250
251 LY_DEFINE (ly_command_line_code, "ly:command-line-code", 0, 0, 0, (),
252            "The Scheme code specified on command-line with @option{-e}.")
253 {
254   return ly_string2scm (init_scheme_code_global);
255 }
256
257
258 LY_DEFINE (ly_command_line_verbose_p, "ly:command-line-verbose?", 0, 0, 0, (),
259            "Was @code{be_verbose_global} set?")
260 {
261   return scm_from_bool (be_verbose_global);
262 }
263
264
265 LY_DEFINE (ly_all_options, "ly:all-options",
266            0, 0, 0, (),
267            "Get all option settings in an alist.")
268 {
269   return ly_hash2alist (option_hash);
270 }
271
272
273 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
274            "Get a global option setting.")
275 {
276   LY_ASSERT_TYPE (ly_is_symbol, var, 1);
277   return scm_hashq_ref (option_hash, var, SCM_BOOL_F);
278 }