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