]> git.donarmstrong.com Git - lilypond.git/blob - lily/program-option.cc
* buildscripts/analyse-cxx-log.py: new file. Read compile log to
[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 "string-convert.hh"
16 #include "parse-scm.hh"
17 #include "warn.hh"
18 #include "main.hh"
19
20 /* Write midi as formatted ascii stream? */
21 bool do_midi_debugging_global;
22 bool use_object_keys;
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
35 static SCM option_hash;
36
37 void internal_set_option (SCM var, SCM val)
38 {
39   scm_hashq_set_x (option_hash, var, val);
40
41   if (0)
42     ;
43   else if (var == ly_symbol2scm ("profile-property-accesses"))
44     {
45       profile_property_accesses = to_boolean (val);
46       val = scm_from_bool (to_boolean (val));
47     }
48   else if (var == ly_symbol2scm ("midi-debug"))
49     {
50       do_midi_debugging_global = to_boolean (val);
51       val = scm_from_bool (to_boolean (val));
52     }
53   else if (var == ly_symbol2scm ("point-and-click"))
54     {
55       point_and_click_global = to_boolean (val);
56       val = scm_from_bool (to_boolean (val));
57     }
58   else if (var == ly_symbol2scm ("parse-protect"))
59     {
60       parse_protect_global = to_boolean (val);
61       val = scm_from_bool (to_boolean (val));
62     }
63   else if (var == ly_symbol2scm ("internal-type-checking"))
64     {
65       do_internal_type_checking_global = to_boolean (val);
66       val = scm_from_bool (to_boolean (val));
67     }
68   else if (var == ly_symbol2scm ("old-relative"))
69     {
70       lily_1_8_relative = to_boolean (val);
71       /*  Needs to be reset for each file that uses this option.  */
72       lily_1_8_compatibility_used = to_boolean (val);
73       val = scm_from_bool (to_boolean (val));
74     }
75   else if (var == ly_symbol2scm ("object-keys"))
76     {
77       use_object_keys = to_boolean (val);
78       val = scm_from_bool (to_boolean (val));
79     }
80 }
81
82 const int HELP_INDENT = 30;
83 const int INDENT = 2;
84 const int SEPARATION = 5;
85
86 /*
87   Hmmm. should do in SCM / C++  ?
88 */
89 static String
90 get_help_string ()
91 {
92   SCM alist = ly_hash2alist (option_hash);
93   SCM convertor = ly_lily_module_constant ("scm->string");
94
95   Array<String> opts;
96
97   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
98     {
99       SCM sym = scm_caar (s);
100       SCM val = scm_cdar (s);
101       String opt_spec
102         = String_convert::char_string (' ', INDENT)
103         + ly_symbol2string (sym)
104         + " ("
105         + ly_scm2string (scm_call_1 (convertor, val))
106         + ")";
107
108       if (opt_spec.length () + SEPARATION > HELP_INDENT)
109         {
110           opt_spec += "\n"
111             + String_convert::char_string (' ', HELP_INDENT);
112         }
113       else
114         opt_spec += String_convert::char_string (' ', HELP_INDENT - opt_spec.length ());
115
116       SCM opt_help_scm
117         = scm_object_property (sym, ly_symbol2scm ("program-option-documentation"));
118       String opt_help = ly_scm2string (opt_help_scm);
119       opt_help.substitute (String ("\n"),
120                            String ("\n")
121                            + String_convert::char_string (' ', HELP_INDENT));
122
123       opts.push (opt_spec + opt_help + "\n");
124     }
125
126   String help ("Options supported by ly:set-option\n\n");
127   opts.sort (String::compare);
128   for (int i = 0; i < opts.size (); i++)
129     help += opts[i];
130
131   help += String ("\n");
132   return help;
133 }
134
135 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
136            "Print ly:set-option usage")
137 {
138   String help = get_help_string ();
139   fputs (help.c_str (), stdout);
140
141   exit (0);
142   return SCM_UNSPECIFIED;
143 }
144
145 LY_DEFINE (ly_add_option, "ly:add-option", 3, 0, 0,
146            (SCM sym, SCM val, SCM description),
147            "Add a program option @var{sym} with default @var{val}.")
148 {
149   if (!option_hash)
150     {
151       option_hash = scm_permanent_object (scm_c_make_hash_table (11));
152     }
153   SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG1, __FUNCTION__, "symbol");
154   SCM_ASSERT_TYPE (scm_is_string (description), description,
155                    SCM_ARG3, __FUNCTION__, "string");
156
157   internal_set_option (sym, val);
158
159   scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
160                              description);
161
162   return SCM_UNSPECIFIED;
163 }
164
165 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
166            "Set a program option. Try setting 'help for a help string.")
167 {
168   SCM_ASSERT_TYPE (scm_is_symbol (var), var, SCM_ARG1,
169                    __FUNCTION__, "symbol");
170
171   if (ly_symbol2scm ("help") == var)
172     ly_option_usage ();
173
174   if (val == SCM_UNDEFINED)
175     val = SCM_BOOL_T;
176
177   String varstr = ly_scm2string (scm_symbol_to_string (var));
178   if (varstr.left_string (3) == String ("no-"))
179     {
180       var = ly_symbol2scm (varstr.nomid_string (0, 3).c_str ());
181       val = scm_from_bool (!to_boolean (val));
182     }
183
184   SCM handle = scm_hashq_get_handle (option_hash, var);
185   if (handle == SCM_BOOL_F)
186     warning (_f ("no such internal option: %s", varstr.c_str ()));
187
188   internal_set_option (var, val);
189   return SCM_UNSPECIFIED;
190 }
191
192 LY_DEFINE (ly_command_line_verbose_p, "ly:command-line-verbose?", 0, 0, 0, (),
193            "Was be_verbose_global set?")
194 {
195   return scm_from_bool (be_verbose_global);
196 }
197
198 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
199            "Get a global option setting.")
200 {
201   SCM_ASSERT_TYPE (scm_is_symbol (var), var,
202                    SCM_ARG1, __FUNCTION__, "symbol");
203   return scm_hashq_ref (option_hash, var, SCM_BOOL_F);
204 }