]> git.donarmstrong.com Git - lilypond.git/blob - lily/program-option.cc
Coverage fixes: remove tweak registration/application, gnome backend,
[lilypond.git] / lily / program-option.cc
1 /*
2   scm-option.cc -- implement option setting from Scheme
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2001--2007  Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "program-option.hh"
10
11 #include <cstdio>
12 #include <cstring>
13 using namespace std;
14
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"
21
22 /* Write midi as formatted ascii stream? */
23 bool do_midi_debugging_global;
24 bool debug_skylines;
25
26 /*
27   Backwards compatibility.
28 */
29 bool lily_1_8_relative = false;
30 bool lily_1_8_compatibility_used = false;
31 bool profile_property_accesses = false;
32 /*
33   crash if internally the wrong type is used for a grob property.
34 */
35 bool do_internal_type_checking_global;
36 bool strict_infinity_checking = false; 
37
38 static SCM option_hash;
39
40 void internal_set_option (SCM var, SCM val)
41 {
42   scm_hashq_set_x (option_hash, var, val);
43
44   if (0)
45     ;
46   else if (var == ly_symbol2scm ("profile-property-accesses"))
47     {
48       profile_property_accesses = to_boolean (val);
49       val = scm_from_bool (to_boolean (val));
50     }
51   else if (var == ly_symbol2scm ("debug-midi"))
52     {
53       do_midi_debugging_global = to_boolean (val);
54       val = scm_from_bool (to_boolean (val));
55     }
56   else if (var == ly_symbol2scm ("point-and-click"))
57     {
58       point_and_click_global = to_boolean (val);
59       val = scm_from_bool (to_boolean (val));
60     }
61   else if (var == ly_symbol2scm ("protected-scheme-parsing"))
62     {
63       parse_protect_global = to_boolean (val);
64       val = scm_from_bool (to_boolean (val));
65     }
66   else if (var == ly_symbol2scm ("check-internal-types"))
67     {
68       do_internal_type_checking_global = to_boolean (val);
69       val = scm_from_bool (to_boolean (val));
70     }
71   else if (var == ly_symbol2scm ("debug-gc-assert-parsed-dead"))
72     {
73       parsed_objects_should_be_dead = to_boolean (val);
74       val = scm_from_bool (parsed_objects_should_be_dead);
75     }
76   else if (var == ly_symbol2scm ("safe"))
77     {
78       be_safe_global = to_boolean (val);
79       val = scm_from_bool (be_safe_global);
80     }
81   else if (var == ly_symbol2scm ("old-relative"))
82     {
83       lily_1_8_relative = to_boolean (val);
84       /*  Needs to be reset for each file that uses this option.  */
85       lily_1_8_compatibility_used = to_boolean (val);
86       val = scm_from_bool (to_boolean (val));
87     }
88   else if (var == ly_symbol2scm ("strict-infinity-checking"))
89     {
90       strict_infinity_checking = to_boolean (val);
91       val = scm_from_bool (to_boolean (val));
92     }
93   else if (var == ly_symbol2scm ("debug-skylines"))
94     {
95       debug_skylines = to_boolean (val);
96       val = scm_from_bool (to_boolean (val));
97     }
98 }
99
100 ssize const HELP_INDENT = 30;
101 ssize const INDENT = 2;
102 ssize const SEPARATION = 5;
103
104 /*
105   Hmmm. should do in SCM / C++  ?
106 */
107 static string
108 get_help_string ()
109 {
110   SCM alist = ly_hash2alist (option_hash);
111   SCM convertor = ly_lily_module_constant ("scm->string");
112
113   vector<string> opts;
114
115   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
116     {
117       SCM sym = scm_caar (s);
118       SCM val = scm_cdar (s);
119       string opt_spec
120         = String_convert::char_string (' ', INDENT)
121         + ly_symbol2string (sym)
122         + " ("
123         + ly_scm2string (scm_call_1 (convertor, val))
124         + ")";
125
126       if (opt_spec.length () + SEPARATION > HELP_INDENT)
127         {
128           opt_spec += "\n"
129             + String_convert::char_string (' ', HELP_INDENT);
130         }
131       else
132         opt_spec += String_convert::char_string (' ', HELP_INDENT - opt_spec.length ());
133
134       SCM opt_help_scm
135         = scm_object_property (sym, ly_symbol2scm ("program-option-documentation"));
136       string opt_help = ly_scm2string (opt_help_scm);
137       replace_all (opt_help,
138                    string ("\n"),
139                    string ("\n")
140                    + String_convert::char_string (' ', HELP_INDENT));
141
142       opts.push_back (opt_spec + opt_help + "\n");
143     }
144
145   string help ("Options supported by ly:set-option\n\n");
146   vector_sort (opts, less<string> ());
147   for (vsize i = 0; i < opts.size (); i++)
148     help += opts[i];
149
150   help += string ("\n");
151   return help;
152 }
153
154 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
155            "Print @code{ly:set-option} usage")
156 {
157   string help = get_help_string ();
158   progress_indication (help);
159
160   return SCM_UNSPECIFIED;
161 }
162
163 LY_DEFINE (ly_add_option, "ly:add-option", 3, 0, 0,
164            (SCM sym, SCM val, SCM description),
165            "Add a program option @var{sym} with default @var{val}.")
166 {
167   if (!option_hash)
168     {
169       option_hash = scm_permanent_object (scm_c_make_hash_table (11));
170     }
171   SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG1, __FUNCTION__, "symbol");
172   SCM_ASSERT_TYPE (scm_is_string (description), description,
173                    SCM_ARG3, __FUNCTION__, "string");
174
175   internal_set_option (sym, val);
176
177   scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
178                              description);
179
180   return SCM_UNSPECIFIED;
181 }
182
183 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
184            "Set a program option. Try setting 'help for a help string.")
185 {
186   SCM_ASSERT_TYPE (scm_is_symbol (var), var, SCM_ARG1,
187                    __FUNCTION__, "symbol");
188
189   if (ly_symbol2scm ("help") == var)
190     {
191       ly_option_usage ();
192       exit (0);
193     }
194
195   if (val == SCM_UNDEFINED)
196     val = SCM_BOOL_T;
197
198   string varstr = ly_scm2string (scm_symbol_to_string (var));
199   if (varstr.substr (0, 3) == string ("no-"))
200     {
201       var = ly_symbol2scm (varstr.substr (3, varstr.length () -3).c_str ());
202       val = scm_from_bool (!to_boolean (val));
203     }
204
205   SCM handle = scm_hashq_get_handle (option_hash, var);
206   if (handle == SCM_BOOL_F)
207     warning (_f ("no such internal option: %s", varstr.c_str ()));
208
209   internal_set_option (var, val);
210   return SCM_UNSPECIFIED;
211 }
212
213 LY_DEFINE (ly_command_line_verbose_p, "ly:command-line-verbose?", 0, 0, 0, (),
214            "Was be_verbose_global set?")
215 {
216   return scm_from_bool (be_verbose_global);
217 }
218
219
220
221 LY_DEFINE (ly_all_option, "ly:all-options",
222            0, 0, 0, (),
223            "Get all option settings in an alist.")
224 {
225   return ly_hash2alist (option_hash);
226 }
227
228
229 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
230            "Get a global option setting.")
231 {
232   SCM_ASSERT_TYPE (scm_is_symbol (var), var,
233                    SCM_ARG1, __FUNCTION__, "symbol");
234   return scm_hashq_ref (option_hash, var, SCM_BOOL_F);
235 }
236
237
238
239 bool
240 get_program_option (const char *s)
241 {
242   SCM sym = ly_symbol2scm (s);
243
244   return to_boolean (ly_get_option (sym));
245 }