2 scm-option.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"
22 /* Write midi as formatted ascii stream? */
23 bool do_midi_debugging_global;
28 Backwards compatibility.
30 bool lily_1_8_relative = false;
31 bool lily_1_8_compatibility_used = false;
32 bool profile_property_accesses = false;
34 crash if internally the wrong type is used for a grob property.
36 bool do_internal_type_checking_global;
37 bool strict_infinity_checking = false;
39 static SCM option_hash;
41 void internal_set_option (SCM var, SCM val)
43 scm_hashq_set_x (option_hash, var, val);
47 else if (var == ly_symbol2scm ("profile-property-accesses"))
49 profile_property_accesses = to_boolean (val);
50 val = scm_from_bool (to_boolean (val));
52 else if (var == ly_symbol2scm ("debug-midi"))
54 do_midi_debugging_global = to_boolean (val);
55 val = scm_from_bool (to_boolean (val));
57 else if (var == ly_symbol2scm ("point-and-click"))
59 point_and_click_global = to_boolean (val);
60 val = scm_from_bool (to_boolean (val));
62 else if (var == ly_symbol2scm ("protected-scheme-parsing"))
64 parse_protect_global = to_boolean (val);
65 val = scm_from_bool (to_boolean (val));
67 else if (var == ly_symbol2scm ("check-internal-types"))
69 do_internal_type_checking_global = to_boolean (val);
70 val = scm_from_bool (to_boolean (val));
72 else if (var == ly_symbol2scm ("debug-gc-assert-parsed-dead"))
74 parsed_objects_should_be_dead = to_boolean (val);
75 val = scm_from_bool (parsed_objects_should_be_dead);
77 else if (var == ly_symbol2scm ("safe"))
79 be_safe_global = to_boolean (val);
80 val = scm_from_bool (be_safe_global);
82 else if (var == ly_symbol2scm ("old-relative"))
84 lily_1_8_relative = to_boolean (val);
85 /* Needs to be reset for each file that uses this option. */
86 lily_1_8_compatibility_used = to_boolean (val);
87 val = scm_from_bool (to_boolean (val));
89 else if (var == ly_symbol2scm ("object-keys"))
91 use_object_keys = to_boolean (val);
92 val = scm_from_bool (to_boolean (val));
94 else if (var == ly_symbol2scm ("strict-infinity-checking"))
96 strict_infinity_checking = to_boolean (val);
97 val = scm_from_bool (to_boolean (val));
99 else if (var == ly_symbol2scm ("debug-skylines"))
101 debug_skylines = to_boolean (val);
102 val = scm_from_bool (to_boolean (val));
106 ssize const HELP_INDENT = 30;
107 ssize const INDENT = 2;
108 ssize const SEPARATION = 5;
111 Hmmm. should do in SCM / C++ ?
116 SCM alist = ly_hash2alist (option_hash);
117 SCM convertor = ly_lily_module_constant ("scm->string");
121 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
123 SCM sym = scm_caar (s);
124 SCM val = scm_cdar (s);
126 = String_convert::char_string (' ', INDENT)
127 + ly_symbol2string (sym)
129 + ly_scm2string (scm_call_1 (convertor, val))
132 if (opt_spec.length () + SEPARATION > HELP_INDENT)
135 + String_convert::char_string (' ', HELP_INDENT);
138 opt_spec += String_convert::char_string (' ', HELP_INDENT - opt_spec.length ());
141 = scm_object_property (sym, ly_symbol2scm ("program-option-documentation"));
142 string opt_help = ly_scm2string (opt_help_scm);
143 replace_all (opt_help,
146 + String_convert::char_string (' ', HELP_INDENT));
148 opts.push_back (opt_spec + opt_help + "\n");
151 string help ("Options supported by ly:set-option\n\n");
152 vector_sort (opts, less<string> ());
153 for (vsize i = 0; i < opts.size (); i++)
156 help += string ("\n");
160 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
161 "Print @code{ly:set-option} usage")
163 string help = get_help_string ();
164 progress_indication (help);
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)
201 if (val == SCM_UNDEFINED)
204 string varstr = ly_scm2string (scm_symbol_to_string (var));
205 if (varstr.substr (0, 3) == string ("no-"))
207 var = ly_symbol2scm (varstr.substr (3, varstr.length () -3).c_str ());
208 val = scm_from_bool (!to_boolean (val));
211 SCM handle = scm_hashq_get_handle (option_hash, var);
212 if (handle == SCM_BOOL_F)
213 warning (_f ("no such internal option: %s", varstr.c_str ()));
215 internal_set_option (var, val);
216 return SCM_UNSPECIFIED;
219 LY_DEFINE (ly_command_line_verbose_p, "ly:command-line-verbose?", 0, 0, 0, (),
220 "Was be_verbose_global set?")
222 return scm_from_bool (be_verbose_global);
227 LY_DEFINE (ly_all_option, "ly:all-options",
229 "Get all option settings in an alist.")
231 return ly_hash2alist (option_hash);
235 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
236 "Get a global option setting.")
238 SCM_ASSERT_TYPE (scm_is_symbol (var), var,
239 SCM_ARG1, __FUNCTION__, "symbol");
240 return scm_hashq_ref (option_hash, var, SCM_BOOL_F);
246 get_program_option (const char *s)
248 SCM sym = ly_symbol2scm (s);
250 return to_boolean (ly_get_option (sym));