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