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