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