2 program-option-scheme.cc -- implement option setting from Scheme
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "program-option.hh"
16 #include "international.hh"
18 #include "parse-scm.hh"
19 #include "string-convert.hh"
23 bool debug_property_callbacks;
24 bool debug_page_breaking_scoring;
27 Backwards compatibility.
29 bool lily_1_8_relative = false;
30 bool lily_1_8_compatibility_used = false;
31 bool profile_property_accesses = false;
33 crash if internally the wrong type is used for a grob property.
35 bool do_internal_type_checking_global;
36 bool strict_infinity_checking = false;
38 static SCM option_hash;
40 void internal_set_option (SCM var, SCM val)
42 scm_hashq_set_x (option_hash, var, val);
46 else if (var == ly_symbol2scm ("profile-property-accesses"))
48 profile_property_accesses = to_boolean (val);
49 val = scm_from_bool (to_boolean (val));
51 else if (var == ly_symbol2scm ("point-and-click"))
53 point_and_click_global = to_boolean (val);
54 val = scm_from_bool (to_boolean (val));
56 else if (var == ly_symbol2scm ("protected-scheme-parsing"))
58 parse_protect_global = to_boolean (val);
59 val = scm_from_bool (to_boolean (val));
61 else if (var == ly_symbol2scm ("check-internal-types"))
63 do_internal_type_checking_global = to_boolean (val);
64 val = scm_from_bool (to_boolean (val));
66 else if (var == ly_symbol2scm ("debug-gc-assert-parsed-dead"))
68 parsed_objects_should_be_dead = to_boolean (val);
69 val = scm_from_bool (parsed_objects_should_be_dead);
71 else if (var == ly_symbol2scm ("safe"))
73 be_safe_global = to_boolean (val);
74 val = scm_from_bool (be_safe_global);
76 else if (var == ly_symbol2scm ("old-relative"))
78 lily_1_8_relative = to_boolean (val);
79 /* Needs to be reset for each file that uses this option. */
80 lily_1_8_compatibility_used = to_boolean (val);
81 val = scm_from_bool (to_boolean (val));
83 else if (var == ly_symbol2scm ("strict-infinity-checking"))
85 strict_infinity_checking = to_boolean (val);
86 val = scm_from_bool (to_boolean (val));
88 else if (var == ly_symbol2scm ("debug-skylines"))
90 debug_skylines = to_boolean (val);
91 val = scm_from_bool (to_boolean (val));
93 else if (var == ly_symbol2scm ("debug-property-callbacks"))
95 debug_property_callbacks = to_boolean (val);
96 val = scm_from_bool (to_boolean (val));
98 else if (var == ly_symbol2scm ("debug-page-breaking-scoring"))
100 debug_page_breaking_scoring = to_boolean (val);
101 val = scm_from_bool (to_boolean (val));
107 ssize const HELP_INDENT = 30;
108 ssize const INDENT = 2;
109 ssize const SEPARATION = 5;
112 Hmmm. should do in SCM / C++ ?
117 SCM alist = ly_hash2alist (option_hash);
118 SCM convertor = ly_lily_module_constant ("scm->string");
122 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
124 SCM sym = scm_caar (s);
125 SCM val = scm_cdar (s);
127 = String_convert::char_string (' ', INDENT)
128 + ly_symbol2string (sym)
130 + ly_scm2string (scm_call_1 (convertor, val))
133 if (opt_spec.length () + SEPARATION > HELP_INDENT)
136 + String_convert::char_string (' ', HELP_INDENT);
139 opt_spec += String_convert::char_string (' ', HELP_INDENT - opt_spec.length ());
142 = scm_object_property (sym, ly_symbol2scm ("program-option-documentation"));
143 string opt_help = ly_scm2string (opt_help_scm);
144 replace_all (opt_help,
147 + String_convert::char_string (' ', HELP_INDENT));
149 opts.push_back (opt_spec + opt_help + "\n");
152 string help ("Options supported by ly:set-option\n\n");
153 vector_sort (opts, less<string> ());
154 for (vsize i = 0; i < opts.size (); i++)
157 help += string ("\n");
162 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
163 "Print @code{ly:set-option} usage")
165 string help = get_help_string ();
166 progress_indication (help);
168 return SCM_UNSPECIFIED;
171 LY_DEFINE (ly_add_option, "ly:add-option", 3, 0, 0,
172 (SCM sym, SCM val, SCM description),
173 "Add a program option @var{sym} with default @var{val}.")
177 option_hash = scm_permanent_object (scm_c_make_hash_table (11));
179 LY_ASSERT_TYPE (ly_is_symbol, sym, 1);
180 LY_ASSERT_TYPE (scm_is_string, description, 3);
182 internal_set_option (sym, val);
184 scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
187 return SCM_UNSPECIFIED;
190 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
191 "Set a program option.")
193 LY_ASSERT_TYPE (ly_is_symbol, var, 1);
195 if (val == SCM_UNDEFINED)
198 string varstr = ly_scm2string (scm_symbol_to_string (var));
199 if (varstr.substr (0, 3) == string ("no-"))
201 var = ly_symbol2scm (varstr.substr (3, varstr.length () -3).c_str ());
202 val = scm_from_bool (!to_boolean (val));
205 SCM handle = scm_hashq_get_handle (option_hash, var);
206 if (handle == SCM_BOOL_F)
207 warning (_f ("no such internal option: %s", varstr.c_str ()));
209 internal_set_option (var, val);
210 return SCM_UNSPECIFIED;
213 LY_DEFINE (ly_command_line_options, "ly:command-line-options", 0, 0, 0, (),
214 "The Scheme specified on command-line with @samp{-d}.")
216 return ly_string2scm (init_scheme_variables_global);
219 LY_DEFINE (ly_command_line_code, "ly:command-line-code", 0, 0, 0, (),
220 "The Scheme specified on command-line with @samp{-e}.")
222 return ly_string2scm (init_scheme_code_global);
225 LY_DEFINE (ly_command_line_verbose_p, "ly:command-line-verbose?", 0, 0, 0, (),
226 "Was be_verbose_global set?")
228 return scm_from_bool (be_verbose_global);
233 LY_DEFINE (ly_all_options, "ly:all-options",
235 "Get all option settings in an alist.")
237 return ly_hash2alist (option_hash);
241 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
242 "Get a global option setting.")
244 LY_ASSERT_TYPE (ly_is_symbol, var, 1);
245 return scm_hashq_ref (option_hash, var, SCM_BOOL_F);