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"
25 Backwards compatibility.
27 bool lily_1_8_relative = false;
28 bool lily_1_8_compatibility_used = false;
29 bool profile_property_accesses = false;
31 crash if internally the wrong type is used for a grob property.
33 bool do_internal_type_checking_global;
34 bool strict_infinity_checking = false;
36 static SCM option_hash;
38 void internal_set_option (SCM var, SCM val)
40 scm_hashq_set_x (option_hash, var, val);
44 else if (var == ly_symbol2scm ("profile-property-accesses"))
46 profile_property_accesses = to_boolean (val);
47 val = scm_from_bool (to_boolean (val));
49 else if (var == ly_symbol2scm ("point-and-click"))
51 point_and_click_global = to_boolean (val);
52 val = scm_from_bool (to_boolean (val));
54 else if (var == ly_symbol2scm ("protected-scheme-parsing"))
56 parse_protect_global = to_boolean (val);
57 val = scm_from_bool (to_boolean (val));
59 else if (var == ly_symbol2scm ("check-internal-types"))
61 do_internal_type_checking_global = to_boolean (val);
62 val = scm_from_bool (to_boolean (val));
64 else if (var == ly_symbol2scm ("debug-gc-assert-parsed-dead"))
66 parsed_objects_should_be_dead = to_boolean (val);
67 val = scm_from_bool (parsed_objects_should_be_dead);
69 else if (var == ly_symbol2scm ("safe"))
71 be_safe_global = to_boolean (val);
72 val = scm_from_bool (be_safe_global);
74 else if (var == ly_symbol2scm ("old-relative"))
76 lily_1_8_relative = to_boolean (val);
77 /* Needs to be reset for each file that uses this option. */
78 lily_1_8_compatibility_used = to_boolean (val);
79 val = scm_from_bool (to_boolean (val));
81 else if (var == ly_symbol2scm ("strict-infinity-checking"))
83 strict_infinity_checking = to_boolean (val);
84 val = scm_from_bool (to_boolean (val));
86 else if (var == ly_symbol2scm ("debug-skylines"))
88 debug_skylines = to_boolean (val);
89 val = scm_from_bool (to_boolean (val));
97 get_program_option (const char *s)
99 SCM sym = ly_symbol2scm (s);
101 return to_boolean (ly_get_option (sym));
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");
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 LY_ASSERT_TYPE (ly_is_symbol, sym, 1);
178 LY_ASSERT_TYPE (scm_is_string, description, 3);
180 internal_set_option (sym, val);
182 scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
185 return SCM_UNSPECIFIED;
188 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
189 "Set a program option.")
191 LY_ASSERT_TYPE (ly_is_symbol, var, 1);
193 if (val == SCM_UNDEFINED)
196 string varstr = ly_scm2string (scm_symbol_to_string (var));
197 if (varstr.substr (0, 3) == string ("no-"))
199 var = ly_symbol2scm (varstr.substr (3, varstr.length () -3).c_str ());
200 val = scm_from_bool (!to_boolean (val));
203 SCM handle = scm_hashq_get_handle (option_hash, var);
204 if (handle == SCM_BOOL_F)
205 warning (_f ("no such internal option: %s", varstr.c_str ()));
207 internal_set_option (var, val);
208 return SCM_UNSPECIFIED;
211 LY_DEFINE (ly_command_line_options, "ly:command-line-options", 0, 0, 0, (),
212 "The Scheme specified on command-line with @samp{-d}.")
214 return ly_string2scm (init_scheme_variables_global);
217 LY_DEFINE (ly_command_line_code, "ly:command-line-code", 0, 0, 0, (),
218 "The Scheme specified on command-line with @samp{-e}.")
220 return ly_string2scm (init_scheme_code_global);
223 LY_DEFINE (ly_command_line_verbose_p, "ly:command-line-verbose?", 0, 0, 0, (),
224 "Was be_verbose_global set?")
226 return scm_from_bool (be_verbose_global);
231 LY_DEFINE (ly_all_options, "ly:all-options",
233 "Get all option settings in an alist.")
235 return ly_hash2alist (option_hash);
239 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
240 "Get a global option setting.")
242 LY_ASSERT_TYPE (ly_is_symbol, var, 1);
243 return scm_hashq_ref (option_hash, var, SCM_BOOL_F);