]> git.donarmstrong.com Git - lilypond.git/blob - lily/program-option.cc
* lily/context.cc (where_defined): also assign value in
[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--2005  Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "program-option.hh"
10
11 #include <cstdio>
12 #include <string.h>
13
14 #include "string-convert.hh"
15 #include "protected-scm.hh"
16 #include "parse-scm.hh"
17 #include "warn.hh"
18 #include "main.hh"
19
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 Protected_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 const int HELP_INDENT = 30; 
84 const int INDENT = 2; 
85 const int SEPARATION = 5; 
86
87 /*
88   Hmmm. should do in SCM / C++  ?
89  */
90 static String
91 get_help_string ()
92 {
93   SCM alist = ly_hash2alist (option_hash_);
94   SCM convertor = ly_lily_module_constant ("scm->string");
95   
96   
97   Array<String> opts;
98   
99   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
100     {
101       SCM sym = scm_caar (s);
102       SCM val = scm_cdar (s);
103       String opt_spec =
104         String_convert::char_string (' ', INDENT)
105         + ly_symbol2string (sym)
106         + " ("
107         + ly_scm2string (scm_call_1 (convertor, val))
108         + ")";
109
110       if (opt_spec.length () + SEPARATION > HELP_INDENT)
111         {
112           opt_spec += "\n"
113             + String_convert::char_string (' ', HELP_INDENT);
114         }
115       else
116         opt_spec += String_convert::char_string (' ', HELP_INDENT - opt_spec.length ());
117
118       SCM opt_help_scm
119         = scm_object_property (sym, ly_symbol2scm ("program-option-documentation")); 
120       String opt_help = ly_scm2string (opt_help_scm);
121       opt_help.substitute (String ("\n"),
122                            String ("\n")
123                            + String_convert::char_string (' ', HELP_INDENT));
124       
125       opts.push (opt_spec + opt_help + "\n");
126     }
127
128   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 += 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   String help = get_help_string ();
141   fputs (help.to_str0 (), 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 (scm_hash_table_p (option_hash_) == SCM_BOOL_F)
152     option_hash_ = scm_c_make_hash_table (11);
153
154   SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG1, __FUNCTION__, "symbol");
155   SCM_ASSERT_TYPE (scm_is_string (description), description,
156                    SCM_ARG3, __FUNCTION__, "string");
157                   
158   internal_set_option (sym, val);
159
160   scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
161                              description);
162
163   return SCM_UNSPECIFIED;
164 }
165
166 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
167            "Set a program option. Try setting 'help for a help string.")
168 {
169   SCM_ASSERT_TYPE (scm_is_symbol (var), var, SCM_ARG1,
170                    __FUNCTION__,  "symbol");
171
172   if (ly_symbol2scm ("help") == var)
173     {
174       ly_option_usage ();
175     }
176
177   if (val == SCM_UNDEFINED)
178     val = SCM_BOOL_T;
179
180   String  varstr = ly_scm2string (scm_symbol_to_string (var));
181   if (varstr.left_string (3) == String ("no-"))
182     {
183       var = ly_symbol2scm (varstr.nomid_string (0, 3).to_str0 ());
184       val = scm_from_bool (!to_boolean (val));
185     }
186   
187   SCM handle = scm_hashq_get_handle (option_hash_, var);
188   if (handle == SCM_BOOL_F)
189     {
190       warning (_f ("no such internal option: %s", varstr.to_str0 ()));
191     }
192
193   internal_set_option (var, val);
194   return SCM_UNSPECIFIED;
195 }
196
197 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
198            "Get a global option setting.")
199 {
200   SCM_ASSERT_TYPE (scm_is_symbol (var), var,
201                    SCM_ARG1, __FUNCTION__,  "symbol");
202   return scm_hashq_ref (option_hash_, var, SCM_BOOL_F);
203 }