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