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