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