2 lily-guile.cc -- implement assorted Guile bindings
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@cs.uu.nl>
12 #include <math.h> /* isinf */
14 #include <string.h> /* memset */
16 #include "international.hh"
17 #include "libc-extension.hh"
18 #include "lily-guile.hh"
23 #include "dimensions.hh"
25 #include "file-path.hh"
28 source-file.hh includes cmath which undefines isinf and isnan
31 inline int my_isinf (Real r) { return isinf (r); }
32 inline int my_isnan (Real r) { return isnan (r); }
35 LY_DEFINE (ly_find_file, "ly:find-file",
37 "Return the absolute file name of @var{name}, "
38 "or @code{#f} if not found.")
40 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
42 String nm = ly_scm2string (name);
43 String file_name = global_path.find (nm);
44 if (file_name.is_empty ())
47 return scm_makfrom0str (file_name.to_str0 ());
51 Ugh. Gulped file is copied twice. (maybe thrice if you count stdio
54 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
55 1, 1, 0, (SCM name, SCM size),
56 "Read the file @var{name}, and return its contents in a string. "
57 "The file is looked up using the search path. ")
59 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
61 if (size != SCM_UNDEFINED)
63 SCM_ASSERT_TYPE (scm_is_number (size), size, SCM_ARG2, __FUNCTION__, "number");
64 sz = scm_to_int (size);
67 String contents = gulp_file_to_string (ly_scm2string (name), true, sz);
68 return scm_from_locale_stringn (contents.get_str0 (), contents.length ());
71 LY_DEFINE (ly_error, "ly:error",
72 1, 0, 1, (SCM str, SCM rest),
73 "Scheme callable function to issue the error @code{msg}. "
74 "The error is formatted with @code{format} and @code{rest}.")
76 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
77 str = scm_simple_format (SCM_BOOL_F, str, rest);
78 error (ly_scm2string (str));
79 return SCM_UNSPECIFIED;
82 LY_DEFINE (ly_message, "ly:message",
83 1, 0, 1, (SCM str, SCM rest),
84 "Scheme callable function to issue the message @code{msg}. "
85 "The message is formatted with @code{format} and @code{rest}.")
87 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
88 str = scm_simple_format (SCM_BOOL_F, str, rest);
89 message (ly_scm2string (str));
90 return SCM_UNSPECIFIED;
93 LY_DEFINE (ly_progress, "ly:progress",
94 1, 0, 1, (SCM str, SCM rest),
95 "Scheme callable function to print progress @code{str}. "
96 "The message is formatted with @code{format} and @code{rest}.")
98 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
99 str = scm_simple_format (SCM_BOOL_F, str, rest);
100 progress_indication (ly_scm2string (str));
101 return SCM_UNSPECIFIED;
104 LY_DEFINE (ly_programming_error, "ly:programming-error",
105 1, 0, 1, (SCM str, SCM rest),
106 "Scheme callable function to issue the warning @code{msg}. "
107 "The message is formatted with @code{format} and @code{rest}.")
109 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
110 str = scm_simple_format (SCM_BOOL_F, str, rest);
111 programming_error (ly_scm2string (str));
112 return SCM_UNSPECIFIED;
115 LY_DEFINE (ly_warning, "ly:warning",
116 1, 0, 1, (SCM str, SCM rest),
117 "Scheme callable function to issue the warning @code{str}. "
118 "The message is formatted with @code{format} and @code{rest}.")
120 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
121 str = scm_simple_format (SCM_BOOL_F, str, rest);
122 warning (ly_scm2string (str));
123 return SCM_UNSPECIFIED;
126 LY_DEFINE (ly_dir_p, "ly:dir?",
128 "type predicate. A direction is @code{-1}, @code{0} or "
129 "@code{1}, where @code{-1} represents "
130 "left or down and @code{1} represents right or up.")
132 if (scm_is_number (s))
134 int i = scm_to_int (s);
135 return (i >= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
140 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
142 (SCM key, SCM alist, SCM default_value),
143 "Return value if KEY in ALIST, else DEFAULT-VALUE "
144 "(or #f if not specified).")
146 SCM handle = scm_assoc (key, alist);
148 if (default_value == SCM_UNDEFINED)
149 default_value = SCM_BOOL_F;
151 if (scm_is_pair (handle))
152 return scm_cdr (handle);
153 return default_value;
156 LY_DEFINE (ly_number2string, "ly:number->string",
158 "Convert @var{num} to a string without generating many decimals.")
160 SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
162 char str[400]; // ugh.
164 if (scm_exact_p (s) == SCM_BOOL_F)
166 Real r (scm_to_double (s));
168 if (my_isinf (r) || my_isnan (r))
170 if (isinf (r) || isnan (r))
173 programming_error (_ ("infinity or NaN encountered while converting Real number"));
174 programming_error (_ ("setting to zero"));
179 snprintf (str, sizeof (str), "%08.4f", r);
182 snprintf (str, sizeof (str), "%d", scm_to_int (s));
184 return scm_makfrom0str (str);
187 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
188 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
190 char const *vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")";
192 return scm_c_eval_string ((char *)vs);
195 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
196 "Return the unit used for lengths as a string.")
198 return scm_makfrom0str (INTERNAL_UNIT);
201 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
202 "Return @var{d} is a number. Used to distinguish length "
203 "variables from normal numbers.")
205 return scm_number_p (d);
211 LY_DEFINE (ly_protects, "ly:protects",
213 "Return hash of protected objects.")
218 LY_DEFINE (ly_gettext, "ly:gettext",
219 1, 0, 0, (SCM string),
222 SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
223 __FUNCTION__, "string");
224 return scm_makfrom0str (_ (scm_i_string_chars (string)).to_str0 ());
227 LY_DEFINE (ly_output_backend, "ly:output-backend",
229 "Return name of output backend.")
231 return scm_makfrom0str (output_backend_global.to_str0 ());
234 LY_DEFINE (ly_output_formats, "ly:output-formats",
236 "Formats passed to --format as a list of strings, "
237 "used for the output.")
239 Array<String> output_formats = split_string (output_format_global, ',');
242 int output_formats_count = output_formats.size ();
243 for (int i = 0; i < output_formats_count; i++)
244 lst = scm_cons (scm_makfrom0str (output_formats[i].to_str0 ()), lst);
249 LY_DEFINE (ly_wchar_to_utf_8, "ly:wide-char->utf-8",
251 "Encode the Unicode codepoint @var{wc} as UTF-8")
255 SCM_ASSERT_TYPE (scm_is_integer (wc), wc, SCM_ARG1, __FUNCTION__, "integer");
256 unsigned wide_char = (unsigned) scm_to_int (wc);
259 if (wide_char < 0x0080)
260 *p++ = (char)wide_char;
261 else if (wide_char < 0x0800)
263 *p++ = (char) (((wide_char >> 6)) | 0xC0);
264 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
266 else if (wide_char < 0x10000)
268 *p++ = (char) (((wide_char >> 12)) | 0xE0);
269 *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
270 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
274 *p++ = (char) (((wide_char >> 18)) | 0xF0);
275 *p++ = (char) (((wide_char >> 12) & 0x3F) | 0x80);
276 *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
277 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
281 return scm_makfrom0str (buf);
284 LY_DEFINE (ly_effective_prefix, "ly:effective-prefix",
286 "Return effective prefix.")
288 return scm_makfrom0str (prefix_directory.to_str0 ());
291 LY_DEFINE (ly_chain_assoc_get, "ly:chain-assoc-get",
292 2, 1, 0, (SCM key, SCM achain, SCM dfault),
293 "Return value for @var{key} from a list of alists @var{achain}. "
294 "If no if no entry is found, return DFAULT, "
295 "or #f if no DFAULT not specified.")
297 if (scm_is_pair (achain))
299 SCM handle = scm_assoc (key, scm_car (achain));
300 if (scm_is_pair (handle))
301 return scm_cdr (handle);
303 return ly_chain_assoc_get (key, scm_cdr (achain), dfault);
305 return dfault == SCM_UNDEFINED ? SCM_BOOL_F : dfault;
308 LY_DEFINE (ly_stderr_redirect, "ly:stderr-redirect",
309 1, 1, 0, (SCM file_name, SCM mode),
310 "Redirect stderr to FILE-NAME, opened with MODE.")
312 SCM_ASSERT_TYPE (scm_string_p (file_name), file_name, SCM_ARG1,
313 __FUNCTION__, "file_name");
315 if (mode != SCM_UNDEFINED && scm_string_p (mode))
316 m = ly_scm2newstr (mode, 0);
317 /* dup2 and (fileno (current-error-port)) do not work with mingw'c
319 freopen (ly_scm2newstr (file_name, 0), m, stderr);
320 return SCM_UNSPECIFIED;