]> git.donarmstrong.com Git - lilypond.git/blob - lily/program-option.cc
dist emacs patches too.
[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--2006  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 "international.hh"
16 #include "main.hh"
17 #include "parse-scm.hh"
18 #include "string-convert.hh"
19 #include "warn.hh"
20
21 /* Write midi as formatted ascii stream? */
22 bool do_midi_debugging_global;
23 bool use_object_keys;
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 ("debug-midi"))
51     {
52       do_midi_debugging_global = to_boolean (val);
53       val = scm_from_bool (to_boolean (val));
54     }
55   else if (var == ly_symbol2scm ("point-and-click"))
56     {
57       point_and_click_global = to_boolean (val);
58       val = scm_from_bool (to_boolean (val));
59     }
60   else if (var == ly_symbol2scm ("protected-scheme-parsing"))
61     {
62       parse_protect_global = to_boolean (val);
63       val = scm_from_bool (to_boolean (val));
64     }
65   else if (var == ly_symbol2scm ("check-internal-types"))
66     {
67       do_internal_type_checking_global = to_boolean (val);
68       val = scm_from_bool (to_boolean (val));
69     }
70   else if (var == ly_symbol2scm ("debug-gc-assert-parsed-dead"))
71     {
72       parsed_objects_should_be_dead = to_boolean (val);
73       val = scm_from_bool (parsed_objects_should_be_dead);
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 ("object-keys"))
83     {
84       use_object_keys = to_boolean (val);
85       val = scm_from_bool (to_boolean (val));
86     }
87   else if (var == ly_symbol2scm ("strict-infinity-checking"))
88     {
89       strict_infinity_checking = to_boolean (val);
90       val = scm_from_bool (to_boolean (val));
91     }
92 }
93
94 ssize const HELP_INDENT = 30;
95 ssize const INDENT = 2;
96 ssize const SEPARATION = 5;
97
98 /*
99   Hmmm. should do in SCM / C++  ?
100 */
101 static string
102 get_help_string ()
103 {
104   SCM alist = ly_hash2alist (option_hash);
105   SCM convertor = ly_lily_module_constant ("scm->string");
106
107   vector<string> opts;
108
109   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
110     {
111       SCM sym = scm_caar (s);
112       SCM val = scm_cdar (s);
113       string opt_spec
114         = String_convert::char_string (' ', INDENT)
115         + ly_symbol2string (sym)
116         + " ("
117         + ly_scm2string (scm_call_1 (convertor, val))
118         + ")";
119
120       if (opt_spec.length () + SEPARATION > HELP_INDENT)
121         {
122           opt_spec += "\n"
123             + String_convert::char_string (' ', HELP_INDENT);
124         }
125       else
126         opt_spec += String_convert::char_string (' ', HELP_INDENT - opt_spec.length ());
127
128       SCM opt_help_scm
129         = scm_object_property (sym, ly_symbol2scm ("program-option-documentation"));
130       string opt_help = ly_scm2string (opt_help_scm);
131       replace_all (opt_help,
132                    string ("\n"),
133                    string ("\n")
134                    + String_convert::char_string (' ', HELP_INDENT));
135
136       opts.push_back (opt_spec + opt_help + "\n");
137     }
138
139   string help ("Options supported by ly:set-option\n\n");
140   vector_sort (opts, less<string> ());
141   for (vsize i = 0; i < opts.size (); i++)
142     help += opts[i];
143
144   help += string ("\n");
145   return help;
146 }
147
148 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
149            "Print ly:set-option usage")
150 {
151   string help = get_help_string ();
152   fputs (help.c_str (), stdout);
153
154   exit (0);
155   return SCM_UNSPECIFIED;
156 }
157
158 LY_DEFINE (ly_add_option, "ly:add-option", 3, 0, 0,
159            (SCM sym, SCM val, SCM description),
160            "Add a program option @var{sym} with default @var{val}.")
161 {
162   if (!option_hash)
163     {
164       option_hash = scm_permanent_object (scm_c_make_hash_table (11));
165     }
166   SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG1, __FUNCTION__, "symbol");
167   SCM_ASSERT_TYPE (scm_is_string (description), description,
168                    SCM_ARG3, __FUNCTION__, "string");
169
170   internal_set_option (sym, val);
171
172   scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
173                              description);
174
175   return SCM_UNSPECIFIED;
176 }
177
178 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
179            "Set a program option. Try setting 'help for a help string.")
180 {
181   SCM_ASSERT_TYPE (scm_is_symbol (var), var, SCM_ARG1,
182                    __FUNCTION__, "symbol");
183
184   if (ly_symbol2scm ("help") == var)
185     ly_option_usage ();
186
187   if (val == SCM_UNDEFINED)
188     val = SCM_BOOL_T;
189
190   string varstr = ly_scm2string (scm_symbol_to_string (var));
191   if (varstr.substr (0, 3) == string ("no-"))
192     {
193       var = ly_symbol2scm (varstr.substr (3, varstr.length () -3).c_str ());
194       val = scm_from_bool (!to_boolean (val));
195     }
196
197   SCM handle = scm_hashq_get_handle (option_hash, var);
198   if (handle == SCM_BOOL_F)
199     warning (_f ("no such internal option: %s", varstr.c_str ()));
200
201   internal_set_option (var, val);
202   return SCM_UNSPECIFIED;
203 }
204
205 LY_DEFINE (ly_command_line_verbose_p, "ly:command-line-verbose?", 0, 0, 0, (),
206            "Was be_verbose_global set?")
207 {
208   return scm_from_bool (be_verbose_global);
209 }
210
211
212
213
214
215
216
217 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
218            "Get a global option setting.")
219 {
220   SCM_ASSERT_TYPE (scm_is_symbol (var), var,
221                    SCM_ARG1, __FUNCTION__, "symbol");
222   return scm_hashq_ref (option_hash, var, SCM_BOOL_F);
223 }
224
225
226 bool
227 get_program_option (const char *s)
228 {
229   SCM sym = ly_symbol2scm (s);
230
231   return to_boolean (ly_get_option (sym));
232 }
233
234
235 bool
236 get_program_option (const char *s)
237 {
238   SCM sym = ly_symbol2scm (s);
239
240   return to_boolean (ly_get_option (sym));
241 }