]> git.donarmstrong.com Git - lilypond.git/blob - lily/program-option.cc
2887a022786cffa481879ab42f4cf78af3a2ac6f
[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
36 static Lilypond_option_init options[] = {
37   {"point-and-click", "#t",
38    "use point & click"},
39   {"midi-debug", "#f",
40    "generate human readable MIDI",},
41   {"internal-type-checking", "#f",
42    "check every property assignment for types"},
43   {"parse-protect", "#t",
44    "continue when finding errors in inline\n" 
45    "scheme are caught in the parser. If off, halt \n"
46    "on errors, and print a stack trace."},
47   {"old-relative", "#f",
48    "relative for simultaneous music works\n"
49    "similar to chord syntax"},
50   {"resolution", "90",
51    "resolution for generating bitmaps"},
52   {"preview-include-book-title", "#t",
53    "include book-titles in preview images."},
54   {"gs-font-load", "#f",
55    "load fonts via Ghostscript."},
56   {"delete-intermediate-files", "#f",
57    "delete unusable PostScript files"},   
58   {"ttf-verbosity", "0",
59    "how much verbosity for TTF font embedding?"},
60   {0,0,0},
61 };
62
63 Protected_scm option_hash_;
64
65 void internal_set_option (SCM var, SCM val)
66 {
67   scm_hashq_set_x (option_hash_, var, val);
68   
69   if (var == ly_symbol2scm ("midi-debug"))
70     {
71       do_midi_debugging_global = to_boolean (val);
72       val = scm_from_bool (to_boolean (val));
73     }
74   else if (var == ly_symbol2scm ("point-and-click"))
75     {
76       point_and_click_global = to_boolean (val);
77       val = scm_from_bool (to_boolean (val));
78     }
79   else if (var == ly_symbol2scm ("parse-protect"))
80     {
81       parse_protect_global = to_boolean (val);
82       val = scm_from_bool (to_boolean (val));
83     }
84   else if (var == ly_symbol2scm ("internal-type-checking"))
85     {
86       do_internal_type_checking_global = to_boolean (val);
87       val = scm_from_bool (to_boolean (val));
88     }
89   else if (var == ly_symbol2scm ("old-relative"))
90     {
91       lily_1_8_relative = to_boolean (val);
92       /*  Needs to be reset for each file that uses this option.  */
93       lily_1_8_compatibility_used = to_boolean (val);
94       val = scm_from_bool (to_boolean (val));
95     }
96 }
97
98 const int HELP_INDENT = 30; 
99 const int INDENT = 2; 
100 const int SEPARATION = 5; 
101
102 static String
103 get_help_string ()
104 {
105   String help ("Options supported by ly:set-option\n\n");
106   for (Lilypond_option_init *p = options; p->name_; p ++)
107     {
108       String opt_spec =
109         String_convert::char_string (' ', INDENT)
110         + String (p->name_)
111         + " ("
112         + String (p->init_)
113         + ")";
114         
115
116       if (opt_spec.length () + SEPARATION > HELP_INDENT)
117         {
118           opt_spec += "\n"
119             + String_convert::char_string (' ', HELP_INDENT);
120         }
121       else
122         opt_spec += String_convert::char_string (' ', HELP_INDENT - opt_spec.length ());
123       
124       String opt_help = p->descr_;
125       opt_help.substitute (String ("\n"),
126                            String ("\n")
127                            + String_convert::char_string (' ', HELP_INDENT));
128       
129       help += opt_spec + opt_help + "\n";
130     }
131   
132   help += String ("\n");
133   return help;
134 }
135
136 static void
137 init_program_options ()
138 {
139   option_hash_ = scm_c_make_hash_table (11);
140
141   for (Lilypond_option_init *p = options; p->name_; p ++)
142     {
143       SCM sym = ly_symbol2scm (p->name_);
144       SCM val = scm_c_eval_string (p->init_);
145
146       internal_set_option (sym, val);
147     }
148
149   String help = get_help_string ();
150
151
152
153   internal_set_option (ly_symbol2scm ("help"),
154                        scm_makfrom0str (help.to_str0 ()));
155 }
156
157 ADD_SCM_INIT_FUNC(scm_option, init_program_options);
158
159
160 /*
161   This interface to option setting is meant for setting options are
162   useful to a limited audience. The reason for this interface is that
163   making command line options clutters up the command-line option name
164   space.
165
166 */
167
168 Protected_scm command_line_settings = SCM_EOL;
169
170 LY_DEFINE (ly_option_usage, "ly:option-usage", 0, 0, 0, (),
171            "Print ly:set-option usage")
172 {
173   SCM stdout = scm_current_output_port();
174   scm_display (ly_get_option (ly_symbol2scm ("help")), stdout);
175   exit (0);
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     {
187       ly_option_usage ();
188     }
189
190   if (val == SCM_UNDEFINED)
191     val = SCM_BOOL_T;
192
193   String  varstr = ly_scm2string (scm_symbol_to_string (var));
194   if (varstr.left_string (3) == String ("no-"))
195     {
196       var = ly_symbol2scm (varstr.nomid_string (0, 3).to_str0 ());
197       val = scm_from_bool (!to_boolean (val));
198     }
199   
200   SCM handle = scm_hashq_get_handle (option_hash_, var);
201   if (handle == SCM_BOOL_F)
202     {
203       warning (_f ("no such internal option: %s", varstr.to_str0 ()));
204     }
205
206   internal_set_option (var, val);
207   return SCM_UNSPECIFIED;
208 }
209
210 LY_DEFINE (ly_get_option, "ly:get-option", 1, 0, 0, (SCM var),
211            "Get a global option setting.")
212 {
213   SCM_ASSERT_TYPE (scm_is_symbol (var), var,
214                    SCM_ARG1, __FUNCTION__,  "symbol");
215   return scm_hashq_ref (option_hash_, var, SCM_BOOL_F);
216 }