]> git.donarmstrong.com Git - lilypond.git/blob - lily/general-scheme.cc
5beda8355e9f5ec2940e8cbd0e5ecdc59b68bca4
[lilypond.git] / lily / general-scheme.cc
1 /*
2   lily-guile.cc -- implement assorted Guile bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
7   Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
9
10 #include "config.hh"
11
12 #include <cstdio>
13 #include <cstring>  /* memset */
14 using namespace std;
15
16 #include "international.hh"
17 #include "libc-extension.hh"
18 #include "lily-guile.hh"
19 #include "std-string.hh"
20 #include "misc.hh"
21 #include "warn.hh"
22 #include "version.hh"
23 #include "dimensions.hh"
24 #include "main.hh"
25 #include "file-path.hh"
26
27 LY_DEFINE (ly_find_file, "ly:find-file",
28            1, 0, 0, (SCM name),
29            "Return the absolute file name of @var{name}, "
30            "or @code{#f} if not found.")
31 {
32   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
33
34   string nm = ly_scm2string (name);
35   string file_name = global_path.find (nm);
36   if (file_name.empty ())
37     return SCM_BOOL_F;
38
39   return scm_makfrom0str (file_name.c_str ());
40 }
41
42 /*
43   Ugh. Gulped file is copied twice. (maybe thrice if you count stdio
44   buffering.)
45 */
46 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
47            1, 1, 0, (SCM name, SCM size),
48            "Read the file @var{name}, and return its contents in a string.  "
49            "The file is looked up using the search path. ")
50 {
51   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
52   int sz = -1;
53   if (size != SCM_UNDEFINED)
54     {
55       SCM_ASSERT_TYPE (scm_is_number (size), size, SCM_ARG2, __FUNCTION__, "number");
56       sz = scm_to_int (size);
57     }
58   
59   string contents = gulp_file_to_string (ly_scm2string (name), true, sz);
60   return scm_from_locale_stringn (contents.c_str (), contents.length ());
61 }
62
63 LY_DEFINE (ly_error, "ly:error",
64            1, 0, 1, (SCM str, SCM rest),
65            "Scheme callable function to issue the error @code{msg}. "
66            "The error is formatted with @code{format} and @code{rest}.")
67 {
68   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
69   str = scm_simple_format (SCM_BOOL_F, str, rest);
70   error (ly_scm2string (str));
71   return SCM_UNSPECIFIED;
72 }
73
74 LY_DEFINE (ly_message, "ly:message",
75            1, 0, 1, (SCM str, SCM rest),
76            "Scheme callable function to issue the message @code{msg}. "
77            "The message is formatted with @code{format} and @code{rest}.")
78 {
79   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
80   str = scm_simple_format (SCM_BOOL_F, str, rest);
81   message (ly_scm2string (str));
82   return SCM_UNSPECIFIED;
83 }
84
85 LY_DEFINE (ly_progress, "ly:progress",
86            1, 0, 1, (SCM str, SCM rest),
87            "Scheme callable function to print progress @code{str}. "
88            "The message is formatted with @code{format} and @code{rest}.")
89 {
90   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
91   str = scm_simple_format (SCM_BOOL_F, str, rest);
92   progress_indication (ly_scm2string (str));
93   return SCM_UNSPECIFIED;
94 }
95
96 LY_DEFINE (ly_programming_error, "ly:programming-error",
97            1, 0, 1, (SCM str, SCM rest),
98            "Scheme callable function to issue the warning @code{msg}. "
99            "The message is formatted with @code{format} and @code{rest}.")
100 {
101   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
102   str = scm_simple_format (SCM_BOOL_F, str, rest);
103   programming_error (ly_scm2string (str));
104   return SCM_UNSPECIFIED;
105 }
106
107 LY_DEFINE (ly_warning, "ly:warning",
108            1, 0, 1, (SCM str, SCM rest),
109            "Scheme callable function to issue the warning @code{str}. "
110            "The message is formatted with @code{format} and @code{rest}.")
111 {
112   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
113   str = scm_simple_format (SCM_BOOL_F, str, rest);
114   warning (ly_scm2string (str));
115   return SCM_UNSPECIFIED;
116 }
117
118 LY_DEFINE (ly_dir_p, "ly:dir?",
119            1, 0, 0, (SCM s),
120            "type predicate. A direction is @code{-1}, @code{0} or "
121            "@code{1}, where @code{-1} represents "
122            "left or down and @code{1} represents right or up.")
123 {
124   if (scm_is_number (s))
125     {
126       int i = scm_to_int (s);
127       return (i >= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
128     }
129   return SCM_BOOL_F;
130 }
131
132 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
133            2, 1, 0,
134            (SCM key, SCM alist, SCM default_value),
135            "Return value if KEY in ALIST, else DEFAULT-VALUE "
136            "(or #f if not specified).")
137 {
138   SCM handle = scm_assoc (key, alist);
139
140   if (default_value == SCM_UNDEFINED)
141     default_value = SCM_BOOL_F;
142
143   if (scm_is_pair (handle))
144     return scm_cdr (handle);
145   return default_value;
146 }
147
148 LY_DEFINE (ly_number2string, "ly:number->string",
149            1, 0, 0, (SCM s),
150            "Convert @var{num} to a string without generating many decimals.")
151 {
152   SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
153
154   char str[400];                        // ugh.
155
156   if (scm_exact_p (s) == SCM_BOOL_F)
157     {
158       Real r (scm_to_double (s));
159         if (isinf (r) || isnan (r))
160           {
161             programming_error (_ ("infinity or NaN encountered while converting Real number"));
162             programming_error (_ ("setting to zero"));
163
164             r = 0.0;
165           }
166
167       snprintf (str, sizeof (str), "%08.4f", r);
168     }
169   else
170     snprintf (str, sizeof (str), "%d", scm_to_int (s));
171
172   return scm_makfrom0str (str);
173 }
174
175 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
176            "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
177 {
178   char const *vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")";
179
180   return scm_c_eval_string ((char *)vs);
181 }
182
183 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
184            "Return the unit used for lengths as a string.")
185 {
186   return scm_makfrom0str (INTERNAL_UNIT);
187 }
188
189 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
190            "Return @var{d} is a number. Used to distinguish length "
191            "variables from normal numbers.")
192 {
193   return scm_number_p (d);
194 }
195
196 /*
197   Debugging mem leaks:
198 */
199 LY_DEFINE (ly_protects, "ly:protects",
200            0, 0, 0, (),
201            "Return hash of protected objects.")
202 {
203   return scm_protects;
204 }
205
206 LY_DEFINE (ly_gettext, "ly:gettext",
207            1, 0, 0, (SCM string),
208            "Gettext wrapper.")
209 {
210   SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
211                    __FUNCTION__, "string");
212   return scm_makfrom0str (_ (scm_i_string_chars (string)).c_str ());
213 }
214
215 LY_DEFINE (ly_output_backend, "ly:output-backend",
216            0, 0, 0, (),
217            "Return name of output backend.")
218 {
219   return scm_makfrom0str (output_backend_global.c_str ());
220 }
221
222 LY_DEFINE (ly_output_formats, "ly:output-formats",
223            0, 0, 0, (),
224            "Formats passed to --format as a list of strings, "
225            "used for the output.")
226 {
227   vector<string> output_formats = string_split (output_format_global, ',');
228
229   SCM lst = SCM_EOL;
230   int output_formats_count = output_formats.size ();
231   for (int i = 0; i < output_formats_count; i++)
232     lst = scm_cons (scm_makfrom0str (output_formats[i].c_str ()), lst);
233
234   return lst;
235 }
236
237 LY_DEFINE (ly_wchar_to_utf_8, "ly:wide-char->utf-8",
238            1, 0, 0, (SCM wc),
239            "Encode the Unicode codepoint @var{wc} as UTF-8")
240 {
241   char buf[5];
242
243   SCM_ASSERT_TYPE (scm_is_integer (wc), wc, SCM_ARG1, __FUNCTION__, "integer");
244   unsigned wide_char = (unsigned) scm_to_int (wc);
245   char *p = buf;
246
247   if (wide_char < 0x0080)
248     *p++ = (char)wide_char;
249   else if (wide_char < 0x0800)
250     {
251       *p++ = (char) (((wide_char >> 6)) | 0xC0);
252       *p++ = (char) (((wide_char) & 0x3F) | 0x80);
253     }
254   else if (wide_char < 0x10000)
255     {
256       *p++ = (char) (((wide_char >> 12)) | 0xE0);
257       *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
258       *p++ = (char) (((wide_char) & 0x3F) | 0x80);
259     }
260   else
261     {
262       *p++ = (char) (((wide_char >> 18)) | 0xF0);
263       *p++ = (char) (((wide_char >> 12) & 0x3F) | 0x80);
264       *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
265       *p++ = (char) (((wide_char) & 0x3F) | 0x80);
266     }
267   *p = 0;
268
269   return scm_makfrom0str (buf);
270 }
271
272 LY_DEFINE (ly_effective_prefix, "ly:effective-prefix",
273            0, 0, 0, (),
274            "Return effective prefix.")
275 {
276   return scm_makfrom0str (prefix_directory.c_str ());
277 }
278
279 LY_DEFINE (ly_chain_assoc_get, "ly:chain-assoc-get",
280            2, 1, 0, (SCM key, SCM achain, SCM dfault),
281            "Return value for @var{key} from a list of alists @var{achain}.  "
282            "If no if no entry is found, return DFAULT, "
283            "or #f if no DFAULT not specified.")
284 {
285   if (scm_is_pair (achain))
286     {
287       SCM handle = scm_assoc (key, scm_car (achain));
288       if (scm_is_pair (handle))
289         return scm_cdr (handle);
290       else
291         return ly_chain_assoc_get (key, scm_cdr (achain), dfault);
292     }
293   return dfault == SCM_UNDEFINED ? SCM_BOOL_F : dfault;
294 }
295
296 LY_DEFINE (ly_stderr_redirect, "ly:stderr-redirect",
297            1, 1, 0, (SCM file_name, SCM mode),
298            "Redirect stderr to FILE-NAME, opened with MODE.")
299 {
300   SCM_ASSERT_TYPE (scm_string_p (file_name), file_name, SCM_ARG1,
301                    __FUNCTION__, "file_name");
302   char const *m = "w";
303   if (mode != SCM_UNDEFINED && scm_string_p (mode))
304     m = ly_scm2newstr (mode, 0);
305   /* dup2 and (fileno (current-error-port)) do not work with mingw'c
306      gcc -mwindows.  */
307   freopen (ly_scm2newstr (file_name, 0), m, stderr);
308   return SCM_UNSPECIFIED;
309 }
310
311 static SCM
312 accumulate_symbol (void *closure, SCM key, SCM val, SCM result)
313 {
314   (void) closure;
315   (void) val;
316   return scm_cons (key, result);
317 }
318
319 LY_DEFINE(ly_hash_table_keys, "ly:hash-table-keys",
320           1,0,0, (SCM tab),
321           "return a list of keys in @var{tab}")
322 {
323   return scm_internal_hash_fold ((Hash_closure_function) & accumulate_symbol,
324                                  NULL, SCM_EOL, tab);
325 }