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;
25 Backwards compatibility.
27 bool lily_1_8_relative = false;
28 bool lily_1_8_compatibility_used = false;
31 crash if internally the wrong type is used for a grob property.
33 bool do_internal_type_checking_global;
35 Protected_scm option_hash_;
37 void internal_set_option (SCM var, SCM val)
39 scm_hashq_set_x (option_hash_, var, val);
41 if (var == ly_symbol2scm ("midi-debug"))
43 do_midi_debugging_global = to_boolean (val);
44 val = scm_from_bool (to_boolean (val));
46 else if (var == ly_symbol2scm ("point-and-click"))
48 point_and_click_global = to_boolean (val);
49 val = scm_from_bool (to_boolean (val));
51 else if (var == ly_symbol2scm ("parse-protect"))
53 parse_protect_global = to_boolean (val);
54 val = scm_from_bool (to_boolean (val));
56 else if (var == ly_symbol2scm ("internal-type-checking"))
58 do_internal_type_checking_global = to_boolean (val);
59 val = scm_from_bool (to_boolean (val));
61 else if (var == ly_symbol2scm ("old-relative"))
63 lily_1_8_relative = to_boolean (val);
64 /* Needs to be reset for each file that uses this option. */
65 lily_1_8_compatibility_used = to_boolean (val);
66 val = scm_from_bool (to_boolean (val));
70 const int HELP_INDENT = 30;
72 const int SEPARATION = 5;
77 SCM alist = ly_hash2alist (option_hash_);
78 SCM convertor = ly_lily_module_constant ("scm->string");
81 String help ("Options supported by ly:set-option\n\n");
82 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
84 SCM sym = scm_caar (s);
85 SCM val = scm_cdar (s);
87 String_convert::char_string (' ', INDENT)
88 + ly_symbol2string (sym)
90 + ly_scm2string (scm_call_1 (convertor, val))
93 if (opt_spec.length () + SEPARATION > HELP_INDENT)
96 + String_convert::char_string (' ', HELP_INDENT);
99 opt_spec += String_convert::char_string (' ', HELP_INDENT - opt_spec.length ());
102 = scm_object_property (sym, ly_symbol2scm ("program-option-documentation"));
103 String opt_help = ly_scm2string (opt_help_scm);
104 opt_help.substitute (String ("\n"),
106 + String_convert::char_string (' ', HELP_INDENT));
108 help += opt_spec + opt_help + "\n";
111 help += String ("\n");
115 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
116 "Print ly:set-option usage")
118 String help = get_help_string ();
119 fputs (help.to_str0 (), stdout);
122 return SCM_UNSPECIFIED;
125 LY_DEFINE (ly_add_option, "ly:add-option", 3, 0, 0,
126 (SCM sym, SCM val, SCM description),
127 "Add a program option @var{sym} with default @var{val}.")
129 if (scm_hash_table_p (option_hash_) == SCM_BOOL_F)
130 option_hash_ = scm_c_make_hash_table (11);
132 SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG1, __FUNCTION__, "symbol");
133 SCM_ASSERT_TYPE (scm_is_string (description), description,
134 SCM_ARG3, __FUNCTION__, "string");
136 internal_set_option (sym, val);
138 scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
141 return SCM_UNSPECIFIED;
144 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
145 "Set a program option. Try setting 'help for a help string.")
147 SCM_ASSERT_TYPE (scm_is_symbol (var), var, SCM_ARG1,
148 __FUNCTION__, "symbol");
150 if (ly_symbol2scm ("help") == var)
155 if (val == SCM_UNDEFINED)
158 String varstr = ly_scm2string (scm_symbol_to_string (var));
159 if (varstr.left_string (3) == String ("no-"))
161 var = ly_symbol2scm (varstr.nomid_string (0, 3).to_str0 ());
162 val = scm_from_bool (!to_boolean (val));
165 SCM handle = scm_hashq_get_handle (option_hash_, var);
166 if (handle == SCM_BOOL_F)
168 warning (_f ("no such internal option: %s", varstr.to_str0 ()));
171 internal_set_option (var, val);
172 return SCM_UNSPECIFIED;
175 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
176 "Get a global option setting.")
178 SCM_ASSERT_TYPE (scm_is_symbol (var), var,
179 SCM_ARG1, __FUNCTION__, "symbol");
180 return scm_hashq_ref (option_hash_, var, SCM_BOOL_F);