2 scm-option.cc -- implement option setting from Scheme
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "program-option.hh"
15 #include "international.hh"
17 #include "parse-scm.hh"
18 #include "string-convert.hh"
21 /* Write midi as formatted ascii stream? */
22 bool do_midi_debugging_global;
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 ("debug-midi"))
53 do_midi_debugging_global = to_boolean (val);
54 val = scm_from_bool (to_boolean (val));
56 else if (var == ly_symbol2scm ("point-and-click"))
58 point_and_click_global = to_boolean (val);
59 val = scm_from_bool (to_boolean (val));
61 else if (var == ly_symbol2scm ("protected-scheme-parsing"))
63 parse_protect_global = to_boolean (val);
64 val = scm_from_bool (to_boolean (val));
66 else if (var == ly_symbol2scm ("check-internal-types"))
68 do_internal_type_checking_global = to_boolean (val);
69 val = scm_from_bool (to_boolean (val));
71 else if (var == ly_symbol2scm ("debug-gc-assert-parsed-dead"))
73 parsed_objects_should_be_dead = to_boolean (val);
74 val = scm_from_bool (parsed_objects_should_be_dead);
76 else if (var == ly_symbol2scm ("safe"))
78 be_safe_global = to_boolean (val);
79 val = scm_from_bool (be_safe_global);
81 else if (var == ly_symbol2scm ("old-relative"))
83 lily_1_8_relative = to_boolean (val);
84 /* Needs to be reset for each file that uses this option. */
85 lily_1_8_compatibility_used = to_boolean (val);
86 val = scm_from_bool (to_boolean (val));
88 else if (var == ly_symbol2scm ("object-keys"))
90 use_object_keys = to_boolean (val);
91 val = scm_from_bool (to_boolean (val));
93 else if (var == ly_symbol2scm ("strict-infinity-checking"))
95 strict_infinity_checking = to_boolean (val);
96 val = scm_from_bool (to_boolean (val));
98 else if (var == ly_symbol2scm ("debug-skylines"))
100 debug_skylines = to_boolean (val);
101 val = scm_from_bool (to_boolean (val));
105 ssize const HELP_INDENT = 30;
106 ssize const INDENT = 2;
107 ssize const SEPARATION = 5;
110 Hmmm. should do in SCM / C++ ?
115 SCM alist = ly_hash2alist (option_hash);
116 SCM convertor = ly_lily_module_constant ("scm->string");
120 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
122 SCM sym = scm_caar (s);
123 SCM val = scm_cdar (s);
125 = String_convert::char_string (' ', INDENT)
126 + ly_symbol2string (sym)
128 + ly_scm2string (scm_call_1 (convertor, val))
131 if (opt_spec.length () + SEPARATION > HELP_INDENT)
134 + String_convert::char_string (' ', HELP_INDENT);
137 opt_spec += String_convert::char_string (' ', HELP_INDENT - opt_spec.length ());
140 = scm_object_property (sym, ly_symbol2scm ("program-option-documentation"));
141 string opt_help = ly_scm2string (opt_help_scm);
142 replace_all (opt_help,
145 + String_convert::char_string (' ', HELP_INDENT));
147 opts.push_back (opt_spec + opt_help + "\n");
150 string help ("Options supported by ly:set-option\n\n");
151 vector_sort (opts, less<string> ());
152 for (vsize i = 0; i < opts.size (); i++)
155 help += string ("\n");
159 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
160 "Print ly:set-option usage")
162 string help = get_help_string ();
163 fputs (help.c_str (), stdout);
166 return SCM_UNSPECIFIED;
169 LY_DEFINE (ly_add_option, "ly:add-option", 3, 0, 0,
170 (SCM sym, SCM val, SCM description),
171 "Add a program option @var{sym} with default @var{val}.")
175 option_hash = scm_permanent_object (scm_c_make_hash_table (11));
177 SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG1, __FUNCTION__, "symbol");
178 SCM_ASSERT_TYPE (scm_is_string (description), description,
179 SCM_ARG3, __FUNCTION__, "string");
181 internal_set_option (sym, val);
183 scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
186 return SCM_UNSPECIFIED;
189 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
190 "Set a program option. Try setting 'help for a help string.")
192 SCM_ASSERT_TYPE (scm_is_symbol (var), var, SCM_ARG1,
193 __FUNCTION__, "symbol");
195 if (ly_symbol2scm ("help") == var)
198 if (val == SCM_UNDEFINED)
201 string varstr = ly_scm2string (scm_symbol_to_string (var));
202 if (varstr.substr (0, 3) == string ("no-"))
204 var = ly_symbol2scm (varstr.substr (3, varstr.length () -3).c_str ());
205 val = scm_from_bool (!to_boolean (val));
208 SCM handle = scm_hashq_get_handle (option_hash, var);
209 if (handle == SCM_BOOL_F)
210 warning (_f ("no such internal option: %s", varstr.c_str ()));
212 internal_set_option (var, val);
213 return SCM_UNSPECIFIED;
216 LY_DEFINE (ly_command_line_verbose_p, "ly:command-line-verbose?", 0, 0, 0, (),
217 "Was be_verbose_global set?")
219 return scm_from_bool (be_verbose_global);
224 LY_DEFINE (ly_all_option, "ly:all-options",
226 "Get all option settings in an alist.")
228 return ly_hash2alist (option_hash);
232 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
233 "Get a global option setting.")
235 SCM_ASSERT_TYPE (scm_is_symbol (var), var,
236 SCM_ARG1, __FUNCTION__, "symbol");
237 return scm_hashq_ref (option_hash, var, SCM_BOOL_F);
243 get_program_option (const char *s)
245 SCM sym = ly_symbol2scm (s);
247 return to_boolean (ly_get_option (sym));