]> git.donarmstrong.com Git - lilypond.git/blob - lily/program-option.cc
* make/mutopia-rules.make ($(outdir)/%.png $(outdir)/%.pdf $(outdir)/%.ly $(outdir...
[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
24 /*
25   Backwards compatibility.
26 */
27 bool lily_1_8_relative = false;
28 bool lily_1_8_compatibility_used = false;
29
30 /*
31   crash if internally the wrong type is used for a grob property.
32 */
33 bool do_internal_type_checking_global;
34
35 Protected_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 (var == ly_symbol2scm ("midi-debug"))
42     {
43       do_midi_debugging_global = to_boolean (val);
44       val = scm_from_bool (to_boolean (val));
45     }
46   else if (var == ly_symbol2scm ("point-and-click"))
47     {
48       point_and_click_global = to_boolean (val);
49       val = scm_from_bool (to_boolean (val));
50     }
51   else if (var == ly_symbol2scm ("parse-protect"))
52     {
53       parse_protect_global = to_boolean (val);
54       val = scm_from_bool (to_boolean (val));
55     }
56   else if (var == ly_symbol2scm ("internal-type-checking"))
57     {
58       do_internal_type_checking_global = to_boolean (val);
59       val = scm_from_bool (to_boolean (val));
60     }
61   else if (var == ly_symbol2scm ("old-relative"))
62     {
63       lily_1_8_relative = to_boolean (val);
64       /*  Needs to be reset for each file that uses this option.  */
65       lily_1_8_compatibility_used = to_boolean (val);
66       val = scm_from_bool (to_boolean (val));
67     }
68 }
69
70 const int HELP_INDENT = 30; 
71 const int INDENT = 2; 
72 const int SEPARATION = 5; 
73
74 /*
75   Hmmm. should do in SCM / C++  ?
76  */
77 static String
78 get_help_string ()
79 {
80   SCM alist = ly_hash2alist (option_hash_);
81   SCM convertor = ly_lily_module_constant ("scm->string");
82   
83   
84   Array<String> opts;
85   
86   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
87     {
88       SCM sym = scm_caar (s);
89       SCM val = scm_cdar (s);
90       String opt_spec =
91         String_convert::char_string (' ', INDENT)
92         + ly_symbol2string (sym)
93         + " ("
94         + ly_scm2string (scm_call_1 (convertor, val))
95         + ")";
96
97       if (opt_spec.length () + SEPARATION > HELP_INDENT)
98         {
99           opt_spec += "\n"
100             + String_convert::char_string (' ', HELP_INDENT);
101         }
102       else
103         opt_spec += String_convert::char_string (' ', HELP_INDENT - opt_spec.length ());
104
105       SCM opt_help_scm
106         = scm_object_property (sym, ly_symbol2scm ("program-option-documentation")); 
107       String opt_help = ly_scm2string (opt_help_scm);
108       opt_help.substitute (String ("\n"),
109                            String ("\n")
110                            + String_convert::char_string (' ', HELP_INDENT));
111       
112       opts.push (opt_spec + opt_help + "\n");
113     }
114
115   String help ("Options supported by ly:set-option\n\n");
116   opts.sort (String::compare);
117   for (int  i = 0; i < opts.size (); i++)
118     help += opts[i];
119     
120   help += String ("\n");
121   return help;
122 }
123
124 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
125            "Print ly:set-option usage")
126 {
127   String help = get_help_string ();
128   fputs (help.to_str0 (), stdout); 
129
130   exit (0);
131   return SCM_UNSPECIFIED;
132 }
133
134 LY_DEFINE (ly_add_option, "ly:add-option", 3, 0, 0,
135            (SCM sym, SCM val,  SCM description),
136            "Add a program option @var{sym} with default @var{val}.")
137 {
138   if (scm_hash_table_p (option_hash_) == SCM_BOOL_F)
139     option_hash_ = scm_c_make_hash_table (11);
140
141   SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG1, __FUNCTION__, "symbol");
142   SCM_ASSERT_TYPE (scm_is_string (description), description,
143                    SCM_ARG3, __FUNCTION__, "string");
144                   
145   internal_set_option (sym, val);
146
147   scm_set_object_property_x (sym, ly_symbol2scm ("program-option-documentation"),
148                              description);
149
150   return SCM_UNSPECIFIED;
151 }
152
153 LY_DEFINE (ly_set_option, "ly:set-option", 1, 1, 0, (SCM var, SCM val),
154            "Set a program option. Try setting 'help for a help string.")
155 {
156   SCM_ASSERT_TYPE (scm_is_symbol (var), var, SCM_ARG1,
157                    __FUNCTION__,  "symbol");
158
159   if (ly_symbol2scm ("help") == var)
160     {
161       ly_option_usage ();
162     }
163
164   if (val == SCM_UNDEFINED)
165     val = SCM_BOOL_T;
166
167   String  varstr = ly_scm2string (scm_symbol_to_string (var));
168   if (varstr.left_string (3) == String ("no-"))
169     {
170       var = ly_symbol2scm (varstr.nomid_string (0, 3).to_str0 ());
171       val = scm_from_bool (!to_boolean (val));
172     }
173   
174   SCM handle = scm_hashq_get_handle (option_hash_, var);
175   if (handle == SCM_BOOL_F)
176     {
177       warning (_f ("no such internal option: %s", varstr.to_str0 ()));
178     }
179
180   internal_set_option (var, val);
181   return SCM_UNSPECIFIED;
182 }
183
184 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
185            "Get a global option setting.")
186 {
187   SCM_ASSERT_TYPE (scm_is_symbol (var), var,
188                    SCM_ARG1, __FUNCTION__,  "symbol");
189   return scm_hashq_ref (option_hash_, var, SCM_BOOL_F);
190 }