2 scm-option.cc -- implement option setting from Scheme
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "program-option.hh"
14 #include "string-convert.hh"
15 #include "protected-scm.hh"
16 #include "parse-scm.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;
32 crash if internally the wrong type is used for a grob property.
34 bool do_internal_type_checking_global;
36 Protected_scm option_hash_;
38 void internal_set_option (SCM var, SCM val)
40 scm_hashq_set_x (option_hash_, var, val);
42 if (var == ly_symbol2scm ("midi-debug"))
44 do_midi_debugging_global = to_boolean (val);
45 val = scm_from_bool (to_boolean (val));
47 else if (var == ly_symbol2scm ("point-and-click"))
49 point_and_click_global = to_boolean (val);
50 val = scm_from_bool (to_boolean (val));
52 else if (var == ly_symbol2scm ("parse-protect"))
54 parse_protect_global = to_boolean (val);
55 val = scm_from_bool (to_boolean (val));
57 else if (var == ly_symbol2scm ("internal-type-checking"))
59 do_internal_type_checking_global = to_boolean (val);
60 val = scm_from_bool (to_boolean (val));
62 else if (var == ly_symbol2scm ("old-relative"))
64 lily_1_8_relative = to_boolean (val);
65 /* Needs to be reset for each file that uses this option. */
66 lily_1_8_compatibility_used = to_boolean (val);
67 val = scm_from_bool (to_boolean (val));
69 else if (var == ly_symbol2scm ("object-keys"))
71 use_object_keys = to_boolean (val);
72 val = scm_from_bool (to_boolean (val));
76 const int HELP_INDENT = 30;
78 const int SEPARATION = 5;
81 Hmmm. should do in SCM / C++ ?
86 SCM alist = ly_hash2alist (option_hash_);
87 SCM convertor = ly_lily_module_constant ("scm->string");
92 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
94 SCM sym = scm_caar (s);
95 SCM val = scm_cdar (s);
97 String_convert::char_string (' ', INDENT)
98 + ly_symbol2string (sym)
100 + ly_scm2string (scm_call_1 (convertor, val))
103 if (opt_spec.length () + SEPARATION > HELP_INDENT)
106 + String_convert::char_string (' ', HELP_INDENT);
109 opt_spec += String_convert::char_string (' ', HELP_INDENT - opt_spec.length ());
112 = scm_object_property (sym, ly_symbol2scm ("program-option-documentation"));
113 String opt_help = ly_scm2string (opt_help_scm);
114 opt_help.substitute (String ("\n"),
116 + String_convert::char_string (' ', HELP_INDENT));
118 opts.push (opt_spec + opt_help + "\n");
121 String help ("Options supported by ly:set-option\n\n");
122 opts.sort (String::compare);
123 for (int i = 0; i < opts.size (); i++)
126 help += String ("\n");
130 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
131 "Print ly:set-option usage")
133 String help = get_help_string ();
134 fputs (help.to_str0 (), stdout);
137 return SCM_UNSPECIFIED;
140 LY_DEFINE (ly_add_option, "ly:add-option", 3, 0, 0,
141 (SCM sym, SCM val, SCM description),
142 "Add a program option @var{sym} with default @var{val}.")
144 if (scm_hash_table_p (option_hash_) == SCM_BOOL_F)
145 option_hash_ = scm_c_make_hash_table (11);
147 SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG1, __FUNCTION__, "symbol");
148 SCM_ASSERT_TYPE (scm_is_string (description), description,
149 SCM_ARG3, __FUNCTION__, "string");
151 internal_set_option (sym, val);
153 scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
156 return SCM_UNSPECIFIED;
159 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
160 "Set a program option. Try setting 'help for a help string.")
162 SCM_ASSERT_TYPE (scm_is_symbol (var), var, SCM_ARG1,
163 __FUNCTION__, "symbol");
165 if (ly_symbol2scm ("help") == var)
170 if (val == SCM_UNDEFINED)
173 String varstr = ly_scm2string (scm_symbol_to_string (var));
174 if (varstr.left_string (3) == String ("no-"))
176 var = ly_symbol2scm (varstr.nomid_string (0, 3).to_str0 ());
177 val = scm_from_bool (!to_boolean (val));
180 SCM handle = scm_hashq_get_handle (option_hash_, var);
181 if (handle == SCM_BOOL_F)
183 warning (_f ("no such internal option: %s", varstr.to_str0 ()));
186 internal_set_option (var, val);
187 return SCM_UNSPECIFIED;
190 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
191 "Get a global option setting.")
193 SCM_ASSERT_TYPE (scm_is_symbol (var), var,
194 SCM_ARG1, __FUNCTION__, "symbol");
195 return scm_hashq_ref (option_hash_, var, SCM_BOOL_F);