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;
26 Backwards compatibility.
28 bool lily_1_8_relative = false;
29 bool lily_1_8_compatibility_used = false;
30 bool profile_property_accesses = false;
32 crash if internally the wrong type is used for a grob property.
34 bool do_internal_type_checking_global;
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 ("midi-debug"))
51 do_midi_debugging_global = to_boolean (val);
52 val = scm_from_bool (to_boolean (val));
54 else if (var == ly_symbol2scm ("point-and-click"))
56 point_and_click_global = to_boolean (val);
57 val = scm_from_bool (to_boolean (val));
59 else if (var == ly_symbol2scm ("parse-protect"))
61 parse_protect_global = to_boolean (val);
62 val = scm_from_bool (to_boolean (val));
64 else if (var == ly_symbol2scm ("internal-type-checking"))
66 do_internal_type_checking_global = to_boolean (val);
67 val = scm_from_bool (to_boolean (val));
69 else if (var == ly_symbol2scm ("old-relative"))
71 lily_1_8_relative = to_boolean (val);
72 /* Needs to be reset for each file that uses this option. */
73 lily_1_8_compatibility_used = to_boolean (val);
74 val = scm_from_bool (to_boolean (val));
76 else if (var == ly_symbol2scm ("object-keys"))
78 use_object_keys = to_boolean (val);
79 val = scm_from_bool (to_boolean (val));
83 ssize const HELP_INDENT = 30;
84 ssize const INDENT = 2;
85 ssize const SEPARATION = 5;
88 Hmmm. should do in SCM / C++ ?
93 SCM alist = ly_hash2alist (option_hash);
94 SCM convertor = ly_lily_module_constant ("scm->string");
96 Array<std::string> opts;
98 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
100 SCM sym = scm_caar (s);
101 SCM val = scm_cdar (s);
103 = String_convert::char_string (' ', INDENT)
104 + ly_symbol2string (sym)
106 + ly_scm2string (scm_call_1 (convertor, val))
109 if (opt_spec.length () + SEPARATION > HELP_INDENT)
112 + String_convert::char_string (' ', HELP_INDENT);
115 opt_spec += String_convert::char_string (' ', HELP_INDENT - opt_spec.length ());
118 = scm_object_property (sym, ly_symbol2scm ("program-option-documentation"));
119 std::string opt_help = ly_scm2string (opt_help_scm);
120 replace_all (opt_help,
123 + String_convert::char_string (' ', HELP_INDENT));
125 opts.push (opt_spec + opt_help + "\n");
128 std::string help ("Options supported by ly:set-option\n\n");
129 opts.sort (string_compare);
130 for (int i = 0; i < opts.size (); i++)
133 help += std::string ("\n");
137 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
138 "Print ly:set-option usage")
140 std::string help = get_help_string ();
141 fputs (help.c_str (), stdout);
144 return SCM_UNSPECIFIED;
147 LY_DEFINE (ly_add_option, "ly:add-option", 3, 0, 0,
148 (SCM sym, SCM val, SCM description),
149 "Add a program option @var{sym} with default @var{val}.")
153 option_hash = scm_permanent_object (scm_c_make_hash_table (11));
155 SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG1, __FUNCTION__, "symbol");
156 SCM_ASSERT_TYPE (scm_is_string (description), description,
157 SCM_ARG3, __FUNCTION__, "string");
159 internal_set_option (sym, val);
161 scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
164 return SCM_UNSPECIFIED;
167 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
168 "Set a program option. Try setting 'help for a help string.")
170 SCM_ASSERT_TYPE (scm_is_symbol (var), var, SCM_ARG1,
171 __FUNCTION__, "symbol");
173 if (ly_symbol2scm ("help") == var)
176 if (val == SCM_UNDEFINED)
179 std::string varstr = ly_scm2string (scm_symbol_to_string (var));
180 if (varstr.substr (0, 3) == std::string ("no-"))
182 var = ly_symbol2scm (varstr.substr (3, varstr.length () -3).c_str ());
183 val = scm_from_bool (!to_boolean (val));
186 SCM handle = scm_hashq_get_handle (option_hash, var);
187 if (handle == SCM_BOOL_F)
188 warning (_f ("no such internal option: %s", varstr.c_str ()));
190 internal_set_option (var, val);
191 return SCM_UNSPECIFIED;
194 LY_DEFINE (ly_command_line_verbose_p, "ly:command-line-verbose?", 0, 0, 0, (),
195 "Was be_verbose_global set?")
197 return scm_from_bool (be_verbose_global);
200 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
201 "Get a global option setting.")
203 SCM_ASSERT_TYPE (scm_is_symbol (var), var,
204 SCM_ARG1, __FUNCTION__, "symbol");
205 return scm_hashq_ref (option_hash, var, SCM_BOOL_F);