]> git.donarmstrong.com Git - lilypond.git/blob - lily/program-option-scheme.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / program-option-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2001--2011  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 void
55 internal_set_option (SCM var,
56                      SCM val)
57 {
58   string varstr = robust_symbol2string (var, "");
59   bool valbool = to_boolean (val);
60   SCM val_scm_bool = scm_from_bool (valbool);
61   if (0)
62     ;
63   else if (varstr == "profile-property-accesses")
64     {
65       profile_property_accesses = valbool;
66       val = val_scm_bool;
67     }
68   else if (varstr == "point-and-click")
69     {
70       point_and_click_global = valbool;
71       val = val_scm_bool;
72     }
73   else if (varstr == "protected-scheme-parsing")
74     {
75       parse_protect_global = valbool;
76       val = val_scm_bool;
77     }
78   else if (varstr == "check-internal-types")
79     {
80       do_internal_type_checking_global = valbool;
81       val = val_scm_bool;
82     }
83   else if (varstr == "debug-gc-assert-parsed-dead")
84     {
85       parsed_objects_should_be_dead = valbool;
86       val = val_scm_bool;
87     }
88   else if (varstr == "safe")
89     {
90       be_safe_global = valbool;
91       val = val_scm_bool;
92     }
93   else if (varstr == "old-relative")
94     {
95       lily_1_8_relative = valbool;
96       /* Needs to be reset for each file that uses this option. */
97       lily_1_8_compatibility_used = valbool;
98       val = val_scm_bool;
99     }
100   else if (varstr == "strict-infinity-checking")
101     {
102       strict_infinity_checking = valbool;
103       val = val_scm_bool;
104     }
105   else if (varstr == "debug-skylines")
106     {
107       debug_skylines = valbool;
108       val = val_scm_bool;
109     }
110   else if (varstr == "debug-property-callbacks")
111     {
112       debug_property_callbacks = valbool;
113       val = val_scm_bool;
114     }
115   else if (varstr == "debug-page-breaking-scoring")
116     {
117       debug_page_breaking_scoring = valbool;
118       val = val_scm_bool;
119     }
120   else if (varstr == "datadir")
121     {
122       /* ignore input value. */
123       val = ly_string2scm (lilypond_datadir);
124     }
125   else if (varstr == "relative-includes")
126     {
127       relative_includes = valbool;
128       val = val_scm_bool;
129     }
130   else if (varstr == "warning-as-error")
131     val = val_scm_bool;
132   else if (varstr == "music-strings-to-paths")
133     {
134       music_strings_to_paths = valbool;
135       val = val_scm_bool;
136     }
137
138   scm_hashq_set_x (option_hash, var, val);
139 }
140
141 ssize const HELP_INDENT = 30;
142 ssize const INDENT = 2;
143 ssize const SEPARATION = 5;
144
145 /*
146   Hmmm. should do in SCM / C++  ?
147 */
148 static string
149 get_help_string ()
150 {
151   SCM alist = ly_hash2alist (option_hash);
152   SCM converter = ly_lily_module_constant ("scm->string");
153
154   vector<string> opts;
155
156   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
157     {
158       SCM sym = scm_caar (s);
159       SCM val = scm_cdar (s);
160       string opt_spec = String_convert::char_string (' ', INDENT)
161                         + ly_symbol2string (sym)
162                         + " ("
163                         + ly_scm2string (scm_call_1 (converter, val))
164                         + ")";
165
166       if (opt_spec.length () + SEPARATION > HELP_INDENT)
167         opt_spec += "\n" + String_convert::char_string (' ', HELP_INDENT);
168       else
169         opt_spec += String_convert::char_string (' ', HELP_INDENT
170                                                  - opt_spec.length ());
171
172       SCM opt_help_scm
173         = scm_object_property (sym,
174                                ly_symbol2scm ("program-option-documentation"));
175       string opt_help = ly_scm2string (opt_help_scm);
176       replace_all (&opt_help,
177                    string ("\n"),
178                    string ("\n")
179                    + String_convert::char_string (' ', HELP_INDENT));
180
181       opts.push_back (opt_spec + opt_help + "\n");
182     }
183
184   string help ("Options supported by `ly:set-option':\n\n");
185   vector_sort (opts, less<string> ());
186   for (vsize i = 0; i < opts.size (); i++)
187     help += opts[i];
188   return help;
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   fflush (stdout);
197
198   return SCM_UNSPECIFIED;
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 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
220            "Set a program option.")
221 {
222   LY_ASSERT_TYPE (ly_is_symbol, var, 1);
223
224   if (val == SCM_UNDEFINED)
225     val = SCM_BOOL_T;
226
227   string varstr = robust_symbol2string (var, "");
228   if (varstr.substr (0, 3) == string ("no-"))
229     {
230       var = ly_symbol2scm (varstr.substr (3, varstr.length () - 3).c_str ());
231       val = scm_from_bool (!to_boolean (val));
232     }
233
234   SCM handle = scm_hashq_get_handle (option_hash, var);
235   if (handle == SCM_BOOL_F)
236     warning (_f ("no such internal option: %s", varstr.c_str ()));
237
238   internal_set_option (var, val);
239   return SCM_UNSPECIFIED;
240 }
241
242 LY_DEFINE (ly_command_line_options, "ly:command-line-options", 0, 0, 0, (),
243            "The Scheme options specified on command-line with @option{-d}.")
244 {
245   return ly_string2scm (init_scheme_variables_global);
246 }
247
248 LY_DEFINE (ly_command_line_code, "ly:command-line-code", 0, 0, 0, (),
249            "The Scheme code specified on command-line with @option{-e}.")
250 {
251   return ly_string2scm (init_scheme_code_global);
252 }
253
254 LY_DEFINE (ly_verbose_output_p, "ly:verbose-output?", 0, 0, 0, (),
255            "Was verbose output requested, i.e. loglevel at least @code{DEBUG}?")
256 {
257   return scm_from_bool (is_loglevel (LOG_DEBUG));
258 }
259
260 LY_DEFINE (ly_all_options, "ly:all-options",
261            0, 0, 0, (),
262            "Get all option settings in an alist.")
263 {
264   return ly_hash2alist (option_hash);
265 }
266
267 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
268            "Get a global option setting.")
269 {
270   LY_ASSERT_TYPE (ly_is_symbol, var, 1);
271   return scm_hashq_ref (option_hash, var, SCM_BOOL_F);
272 }