]> git.donarmstrong.com Git - lilypond.git/blob - lily/program-option.cc
b12a246f311f8ae023d0a4b8aa31f0a82afe3877
[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
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 ("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 ("object-keys"))
84     {
85       use_object_keys = 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 }
94
95 ssize const HELP_INDENT = 30;
96 ssize const INDENT = 2;
97 ssize const SEPARATION = 5;
98
99 /*
100   Hmmm. should do in SCM / C++  ?
101 */
102 static string
103 get_help_string ()
104 {
105   SCM alist = ly_hash2alist (option_hash);
106   SCM convertor = ly_lily_module_constant ("scm->string");
107
108   vector<string> opts;
109
110   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
111     {
112       SCM sym = scm_caar (s);
113       SCM val = scm_cdar (s);
114       string opt_spec
115         = String_convert::char_string (' ', INDENT)
116         + ly_symbol2string (sym)
117         + " ("
118         + ly_scm2string (scm_call_1 (convertor, val))
119         + ")";
120
121       if (opt_spec.length () + SEPARATION > HELP_INDENT)
122         {
123           opt_spec += "\n"
124             + String_convert::char_string (' ', HELP_INDENT);
125         }
126       else
127         opt_spec += String_convert::char_string (' ', HELP_INDENT - opt_spec.length ());
128
129       SCM opt_help_scm
130         = scm_object_property (sym, ly_symbol2scm ("program-option-documentation"));
131       string opt_help = ly_scm2string (opt_help_scm);
132       replace_all (opt_help,
133                    string ("\n"),
134                    string ("\n")
135                    + String_convert::char_string (' ', HELP_INDENT));
136
137       opts.push_back (opt_spec + opt_help + "\n");
138     }
139
140   string help ("Options supported by ly:set-option\n\n");
141   vector_sort (opts, less<string> ());
142   for (vsize i = 0; i < opts.size (); i++)
143     help += opts[i];
144
145   help += string ("\n");
146   return help;
147 }
148
149 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
150            "Print ly:set-option usage")
151 {
152   string help = get_help_string ();
153   fputs (help.c_str (), stdout);
154
155   exit (0);
156   return SCM_UNSPECIFIED;
157 }
158
159 LY_DEFINE (ly_add_option, "ly:add-option", 3, 0, 0,
160            (SCM sym, SCM val, SCM description),
161            "Add a program option @var{sym} with default @var{val}.")
162 {
163   if (!option_hash)
164     {
165       option_hash = scm_permanent_object (scm_c_make_hash_table (11));
166     }
167   SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG1, __FUNCTION__, "symbol");
168   SCM_ASSERT_TYPE (scm_is_string (description), description,
169                    SCM_ARG3, __FUNCTION__, "string");
170
171   internal_set_option (sym, val);
172
173   scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
174                              description);
175
176   return SCM_UNSPECIFIED;
177 }
178
179 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
180            "Set a program option. Try setting 'help for a help string.")
181 {
182   SCM_ASSERT_TYPE (scm_is_symbol (var), var, SCM_ARG1,
183                    __FUNCTION__, "symbol");
184
185   if (ly_symbol2scm ("help") == var)
186     ly_option_usage ();
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_verbose_p, "ly:command-line-verbose?", 0, 0, 0, (),
207            "Was be_verbose_global set?")
208 {
209   return scm_from_bool (be_verbose_global);
210 }
211
212 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
213            "Get a global option setting.")
214 {
215   SCM_ASSERT_TYPE (scm_is_symbol (var), var,
216                    SCM_ARG1, __FUNCTION__, "symbol");
217   return scm_hashq_ref (option_hash, var, SCM_BOOL_F);
218 }