Bump version.
[lilypond.git] / lily / program-option-scheme.cc
blob1e03c5f951bae8e38a4d8d257205d206f60068e4
1 /*
2 program-option-scheme.cc -- implement option setting from Scheme
4 source file of the GNU LilyPond music typesetter
6 (c) 2001--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "program-option.hh"
11 #include <cstdio>
12 #include <cstring>
13 using namespace std;
15 #include "profile.hh"
16 #include "international.hh"
17 #include "main.hh"
18 #include "parse-scm.hh"
19 #include "string-convert.hh"
20 #include "warn.hh"
22 bool debug_skylines;
23 bool debug_property_callbacks;
24 bool debug_page_breaking_scoring;
26 bool relative_includes;
29 Backwards compatibility.
31 bool lily_1_8_relative = false;
32 bool lily_1_8_compatibility_used = false;
33 bool profile_property_accesses = false;
35 crash if internally the wrong type is used for a grob property.
37 bool do_internal_type_checking_global;
38 bool strict_infinity_checking = false;
40 static SCM option_hash;
43 void
44 internal_set_option (SCM var,
45 SCM val)
47 if (0)
49 else if (var == ly_symbol2scm ("profile-property-accesses"))
51 profile_property_accesses = 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 ("protected-scheme-parsing"))
61 parse_protect_global = to_boolean (val);
62 val = scm_from_bool (to_boolean (val));
64 else if (var == ly_symbol2scm ("check-internal-types"))
66 do_internal_type_checking_global = to_boolean (val);
67 val = scm_from_bool (to_boolean (val));
69 else if (var == ly_symbol2scm ("debug-gc-assert-parsed-dead"))
71 parsed_objects_should_be_dead = to_boolean (val);
72 val = scm_from_bool (parsed_objects_should_be_dead);
74 else if (var == ly_symbol2scm ("safe"))
76 be_safe_global = to_boolean (val);
77 val = scm_from_bool (be_safe_global);
79 else if (var == ly_symbol2scm ("old-relative"))
81 lily_1_8_relative = to_boolean (val);
82 /* Needs to be reset for each file that uses this option. */
83 lily_1_8_compatibility_used = to_boolean (val);
84 val = scm_from_bool (to_boolean (val));
86 else if (var == ly_symbol2scm ("strict-infinity-checking"))
88 strict_infinity_checking = to_boolean (val);
89 val = scm_from_bool (to_boolean (val));
91 else if (var == ly_symbol2scm ("debug-skylines"))
93 debug_skylines = to_boolean (val);
94 val = scm_from_bool (to_boolean (val));
96 else if (var == ly_symbol2scm ("debug-property-callbacks"))
98 debug_property_callbacks = to_boolean (val);
99 val = scm_from_bool (to_boolean (val));
101 else if (var == ly_symbol2scm ("debug-page-breaking-scoring"))
103 debug_page_breaking_scoring = to_boolean (val);
104 val = scm_from_bool (to_boolean (val));
106 else if (var == ly_symbol2scm ("datadir"))
108 /* ignore input value. */
109 val = ly_string2scm (lilypond_datadir);
111 else if (var == ly_symbol2scm ("relative-includes"))
113 relative_includes = to_boolean (val);
114 val = scm_from_bool (to_boolean (val));
117 scm_hashq_set_x (option_hash, var, val);
121 ssize const HELP_INDENT = 30;
122 ssize const INDENT = 2;
123 ssize const SEPARATION = 5;
126 Hmmm. should do in SCM / C++ ?
128 static string
129 get_help_string ()
131 SCM alist = ly_hash2alist (option_hash);
132 SCM converter = ly_lily_module_constant ("scm->string");
134 vector<string> opts;
136 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
138 SCM sym = scm_caar (s);
139 SCM val = scm_cdar (s);
140 string opt_spec = String_convert::char_string (' ', INDENT)
141 + ly_symbol2string (sym)
142 + " ("
143 + ly_scm2string (scm_call_1 (converter, val))
144 + ")";
146 if (opt_spec.length () + SEPARATION > HELP_INDENT)
147 opt_spec += "\n" + String_convert::char_string (' ', HELP_INDENT);
148 else
149 opt_spec += String_convert::char_string (' ', HELP_INDENT
150 - opt_spec.length ());
152 SCM opt_help_scm
153 = scm_object_property (sym,
154 ly_symbol2scm ("program-option-documentation"));
155 string opt_help = ly_scm2string (opt_help_scm);
156 replace_all (&opt_help,
157 string ("\n"),
158 string ("\n")
159 + String_convert::char_string (' ', HELP_INDENT));
161 opts.push_back (opt_spec + opt_help + "\n");
164 string help ("Options supported by `ly:set-option':\n\n");
165 vector_sort (opts, less<string> ());
166 for (vsize i = 0; i < opts.size (); i++)
167 help += opts[i];
168 return help;
172 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
173 "Print @code{ly:set-option} usage.")
175 string help = get_help_string ();
176 puts (help.c_str());
178 return SCM_UNSPECIFIED;
182 LY_DEFINE (ly_add_option, "ly:add-option", 3, 0, 0,
183 (SCM sym, SCM val, SCM description),
184 "Add a program option @var{sym} with default @var{val}.")
186 if (!option_hash)
187 option_hash = scm_permanent_object (scm_c_make_hash_table (11));
188 LY_ASSERT_TYPE (ly_is_symbol, sym, 1);
189 LY_ASSERT_TYPE (scm_is_string, description, 3);
191 internal_set_option (sym, val);
193 scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
194 description);
196 return SCM_UNSPECIFIED;
200 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
201 "Set a program option.")
203 LY_ASSERT_TYPE (ly_is_symbol, var, 1);
205 if (val == SCM_UNDEFINED)
206 val = SCM_BOOL_T;
208 string varstr = ly_scm2string (scm_symbol_to_string (var));
209 if (varstr.substr (0, 3) == string ("no-"))
211 var = ly_symbol2scm (varstr.substr (3, varstr.length () - 3).c_str ());
212 val = scm_from_bool (!to_boolean (val));
215 SCM handle = scm_hashq_get_handle (option_hash, var);
216 if (handle == SCM_BOOL_F)
217 warning (_f ("no such internal option: %s", varstr.c_str ()));
219 internal_set_option (var, val);
220 return SCM_UNSPECIFIED;
224 LY_DEFINE (ly_command_line_options, "ly:command-line-options", 0, 0, 0, (),
225 "The Scheme options specified on command-line with @option{-d}.")
227 return ly_string2scm (init_scheme_variables_global);
231 LY_DEFINE (ly_command_line_code, "ly:command-line-code", 0, 0, 0, (),
232 "The Scheme code specified on command-line with @option{-e}.")
234 return ly_string2scm (init_scheme_code_global);
238 LY_DEFINE (ly_command_line_verbose_p, "ly:command-line-verbose?", 0, 0, 0, (),
239 "Was @code{be_verbose_global} set?")
241 return scm_from_bool (be_verbose_global);
245 LY_DEFINE (ly_all_options, "ly:all-options",
246 0, 0, 0, (),
247 "Get all option settings in an alist.")
249 return ly_hash2alist (option_hash);
253 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
254 "Get a global option setting.")
256 LY_ASSERT_TYPE (ly_is_symbol, var, 1);
257 return scm_hashq_ref (option_hash, var, SCM_BOOL_F);