]> git.donarmstrong.com Git - lilypond.git/blob - lily/program-option-scheme.cc
Merge branch 'lilypond/translation' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / program-option-scheme.cc
1 /*
2   program-option-scheme.cc -- implement option setting from Scheme
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2001--2009  Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "program-option.hh"
10
11 #include <cstdio>
12 #include <cstring>
13 using namespace std;
14
15 #include "profile.hh"
16 #include "international.hh"
17 #include "main.hh"
18 #include "parse-scm.hh"
19 #include "string-convert.hh"
20 #include "warn.hh"
21
22 bool debug_skylines;
23 bool debug_property_callbacks;
24 bool debug_page_breaking_scoring;
25
26 bool relative_includes;
27
28 /*
29   Backwards compatibility.
30 */
31 bool lily_1_8_relative = false;
32 bool lily_1_8_compatibility_used = false;
33 bool profile_property_accesses = false;
34 /*
35   crash if internally the wrong type is used for a grob property.
36 */
37 bool do_internal_type_checking_global;
38 bool strict_infinity_checking = false;
39
40 static SCM option_hash;
41
42
43 void
44 internal_set_option (SCM var,
45                      SCM val)
46 {
47   if (0)
48     ;
49   else if (var == ly_symbol2scm ("profile-property-accesses"))
50     {
51       profile_property_accesses = to_boolean (val);
52       val = scm_from_bool (to_boolean (val));
53     }
54   else if (var == ly_symbol2scm ("point-and-click"))
55     {
56       point_and_click_global = to_boolean (val);
57       val = scm_from_bool (to_boolean (val));
58     }
59   else if (var == ly_symbol2scm ("protected-scheme-parsing"))
60     {
61       parse_protect_global = to_boolean (val);
62       val = scm_from_bool (to_boolean (val));
63     }
64   else if (var == ly_symbol2scm ("check-internal-types"))
65     {
66       do_internal_type_checking_global = to_boolean (val);
67       val = scm_from_bool (to_boolean (val));
68     }
69   else if (var == ly_symbol2scm ("debug-gc-assert-parsed-dead"))
70     {
71       parsed_objects_should_be_dead = to_boolean (val);
72       val = scm_from_bool (parsed_objects_should_be_dead);
73     }
74   else if (var == ly_symbol2scm ("safe"))
75     {
76       be_safe_global = to_boolean (val);
77       val = scm_from_bool (be_safe_global);
78     }
79   else if (var == ly_symbol2scm ("old-relative"))
80     {
81       lily_1_8_relative = to_boolean (val);
82       /* Needs to be reset for each file that uses this option. */
83       lily_1_8_compatibility_used = to_boolean (val);
84       val = scm_from_bool (to_boolean (val));
85     }
86   else if (var == ly_symbol2scm ("strict-infinity-checking"))
87     {
88       strict_infinity_checking = to_boolean (val);
89       val = scm_from_bool (to_boolean (val));
90     }
91   else if (var == ly_symbol2scm ("debug-skylines"))
92     {
93       debug_skylines = to_boolean (val);
94       val = scm_from_bool (to_boolean (val));
95     }
96   else if (var == ly_symbol2scm ("debug-property-callbacks"))
97     {
98       debug_property_callbacks = to_boolean (val);
99       val = scm_from_bool (to_boolean (val));
100     }
101   else if (var == ly_symbol2scm ("debug-page-breaking-scoring"))
102     {
103       debug_page_breaking_scoring = to_boolean (val);
104       val = scm_from_bool (to_boolean (val));
105     }
106   else if (var == ly_symbol2scm ("datadir"))
107     {
108       /* ignore input value. */
109       val = ly_string2scm (lilypond_datadir);
110     }
111   else if (var == ly_symbol2scm ("relative-includes"))
112     {
113       relative_includes = to_boolean (val);
114       val = scm_from_bool (to_boolean (val));
115     }
116   else if (var == ly_symbol2scm ("warning-as-error"))
117     val = scm_from_bool (to_boolean (val));
118
119   scm_hashq_set_x (option_hash, var, val);
120 }
121
122
123 ssize const HELP_INDENT = 30;
124 ssize const INDENT = 2;
125 ssize const SEPARATION = 5;
126
127 /*
128   Hmmm. should do in SCM / C++  ?
129 */
130 static string
131 get_help_string ()
132 {
133   SCM alist = ly_hash2alist (option_hash);
134   SCM converter = ly_lily_module_constant ("scm->string");
135
136   vector<string> opts;
137
138   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
139     {
140       SCM sym = scm_caar (s);
141       SCM val = scm_cdar (s);
142       string opt_spec = String_convert::char_string (' ', INDENT)
143                         + ly_symbol2string (sym)
144                         + " ("
145                         + ly_scm2string (scm_call_1 (converter, val))
146                         + ")";
147
148       if (opt_spec.length () + SEPARATION > HELP_INDENT)
149         opt_spec += "\n" + String_convert::char_string (' ', HELP_INDENT);
150       else
151         opt_spec += String_convert::char_string (' ', HELP_INDENT
152                                                       - opt_spec.length ());
153
154       SCM opt_help_scm
155         = scm_object_property (sym,
156                                ly_symbol2scm ("program-option-documentation"));
157       string opt_help = ly_scm2string (opt_help_scm);
158       replace_all (&opt_help,
159                    string ("\n"),
160                    string ("\n")
161                    + String_convert::char_string (' ', HELP_INDENT));
162
163       opts.push_back (opt_spec + opt_help + "\n");
164     }
165
166   string help ("Options supported by `ly:set-option':\n\n");
167   vector_sort (opts, less<string> ());
168   for (vsize i = 0; i < opts.size (); i++)
169     help += opts[i];
170   return help;
171 }
172
173
174 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
175            "Print @code{ly:set-option} usage.")
176 {
177   string help = get_help_string ();
178   puts (help.c_str());
179
180   return SCM_UNSPECIFIED;
181 }
182
183
184 LY_DEFINE (ly_add_option, "ly:add-option", 3, 0, 0,
185            (SCM sym, SCM val, SCM description),
186            "Add a program option @var{sym}.  @var{val} is the default"
187            " value and @var{description} is a string description.")
188 {
189   if (!option_hash)
190     option_hash = scm_permanent_object (scm_c_make_hash_table (11));
191   LY_ASSERT_TYPE (ly_is_symbol, sym, 1);
192   LY_ASSERT_TYPE (scm_is_string, description, 3);
193
194   internal_set_option (sym, val);
195
196   scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
197                              description);
198
199   return SCM_UNSPECIFIED;
200 }
201
202
203 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
204            "Set a program option.")
205 {
206   LY_ASSERT_TYPE (ly_is_symbol, var, 1);
207
208   if (val == SCM_UNDEFINED)
209     val = SCM_BOOL_T;
210
211   string varstr = ly_scm2string (scm_symbol_to_string (var));
212   if (varstr.substr (0, 3) == string ("no-"))
213     {
214       var = ly_symbol2scm (varstr.substr (3, varstr.length () - 3).c_str ());
215       val = scm_from_bool (!to_boolean (val));
216     }
217
218   SCM handle = scm_hashq_get_handle (option_hash, var);
219   if (handle == SCM_BOOL_F)
220     warning (_f ("no such internal option: %s", varstr.c_str ()));
221
222   internal_set_option (var, val);
223   return SCM_UNSPECIFIED;
224 }
225
226
227 LY_DEFINE (ly_command_line_options, "ly:command-line-options", 0, 0, 0, (),
228            "The Scheme options specified on command-line with @option{-d}.")
229 {
230   return ly_string2scm (init_scheme_variables_global);
231 }
232
233
234 LY_DEFINE (ly_command_line_code, "ly:command-line-code", 0, 0, 0, (),
235            "The Scheme code specified on command-line with @option{-e}.")
236 {
237   return ly_string2scm (init_scheme_code_global);
238 }
239
240
241 LY_DEFINE (ly_command_line_verbose_p, "ly:command-line-verbose?", 0, 0, 0, (),
242            "Was @code{be_verbose_global} set?")
243 {
244   return scm_from_bool (be_verbose_global);
245 }
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
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 }