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@xs4all.nl>
13 #include <cstring> /* memset */
16 #include "international.hh"
17 #include "libc-extension.hh"
18 #include "lily-guile.hh"
23 #include "dimensions.hh"
25 #include "file-path.hh"
27 LY_DEFINE (ly_find_file, "ly:find-file",
29 "Return the absolute file name of @var{name}, "
30 "or @code{#f} if not found.")
32 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
34 String nm = ly_scm2string (name);
35 String file_name = global_path.find (nm);
36 if (file_name.is_empty ())
39 return scm_makfrom0str (file_name.to_str0 ());
43 Ugh. Gulped file is copied twice. (maybe thrice if you count stdio
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. ")
51 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
53 if (size != SCM_UNDEFINED)
55 SCM_ASSERT_TYPE (scm_is_number (size), size, SCM_ARG2, __FUNCTION__, "number");
56 sz = scm_to_int (size);
59 String contents = gulp_file_to_string (ly_scm2string (name), true, sz);
60 return scm_from_locale_stringn (contents.get_str0 (), contents.length ());
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}.")
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;
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}.")
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;
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}.")
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;
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}.")
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;
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}.")
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;
118 LY_DEFINE (ly_dir_p, "ly:dir?",
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.")
124 if (scm_is_number (s))
126 int i = scm_to_int (s);
127 return (i >= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
132 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
134 (SCM key, SCM alist, SCM default_value),
135 "Return value if KEY in ALIST, else DEFAULT-VALUE "
136 "(or #f if not specified).")
138 SCM handle = scm_assoc (key, alist);
140 if (default_value == SCM_UNDEFINED)
141 default_value = SCM_BOOL_F;
143 if (scm_is_pair (handle))
144 return scm_cdr (handle);
145 return default_value;
148 LY_DEFINE (ly_number2string, "ly:number->string",
150 "Convert @var{num} to a string without generating many decimals.")
152 SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
154 char str[400]; // ugh.
156 if (scm_exact_p (s) == SCM_BOOL_F)
158 Real r (scm_to_double (s));
159 if (isinf (r) || isnan (r))
161 programming_error (_ ("infinity or NaN encountered while converting Real number"));
162 programming_error (_ ("setting to zero"));
167 snprintf (str, sizeof (str), "%08.4f", r);
170 snprintf (str, sizeof (str), "%d", scm_to_int (s));
172 return scm_makfrom0str (str);
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)}. ")
178 char const *vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")";
180 return scm_c_eval_string ((char *)vs);
183 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
184 "Return the unit used for lengths as a string.")
186 return scm_makfrom0str (INTERNAL_UNIT);
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.")
193 return scm_number_p (d);
199 LY_DEFINE (ly_protects, "ly:protects",
201 "Return hash of protected objects.")
206 LY_DEFINE (ly_gettext, "ly:gettext",
207 1, 0, 0, (SCM string),
210 SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
211 __FUNCTION__, "string");
212 return scm_makfrom0str (_ (scm_i_string_chars (string)).to_str0 ());
215 LY_DEFINE (ly_output_backend, "ly:output-backend",
217 "Return name of output backend.")
219 return scm_makfrom0str (output_backend_global.to_str0 ());
222 LY_DEFINE (ly_output_formats, "ly:output-formats",
224 "Formats passed to --format as a list of strings, "
225 "used for the output.")
227 Array<String> output_formats = split_string (output_format_global, ',');
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].to_str0 ()), lst);
237 LY_DEFINE (ly_wchar_to_utf_8, "ly:wide-char->utf-8",
239 "Encode the Unicode codepoint @var{wc} as UTF-8")
243 SCM_ASSERT_TYPE (scm_is_integer (wc), wc, SCM_ARG1, __FUNCTION__, "integer");
244 unsigned wide_char = (unsigned) scm_to_int (wc);
247 if (wide_char < 0x0080)
248 *p++ = (char)wide_char;
249 else if (wide_char < 0x0800)
251 *p++ = (char) (((wide_char >> 6)) | 0xC0);
252 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
254 else if (wide_char < 0x10000)
256 *p++ = (char) (((wide_char >> 12)) | 0xE0);
257 *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
258 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
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);
269 return scm_makfrom0str (buf);
272 LY_DEFINE (ly_effective_prefix, "ly:effective-prefix",
274 "Return effective prefix.")
276 return scm_makfrom0str (prefix_directory.to_str0 ());
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.")
285 if (scm_is_pair (achain))
287 SCM handle = scm_assoc (key, scm_car (achain));
288 if (scm_is_pair (handle))
289 return scm_cdr (handle);
291 return ly_chain_assoc_get (key, scm_cdr (achain), dfault);
293 return dfault == SCM_UNDEFINED ? SCM_BOOL_F : dfault;
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.")
300 SCM_ASSERT_TYPE (scm_string_p (file_name), file_name, SCM_ARG1,
301 __FUNCTION__, "file_name");
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
307 freopen (ly_scm2newstr (file_name, 0), m, stderr);
308 return SCM_UNSPECIFIED;