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