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