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