2 lily-guile.cc -- implement assorted Guile bindings
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2007 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@xs4all.nl>
14 #include <cstring> /* memset */
17 #include "international.hh"
18 #include "libc-extension.hh"
19 #include "lily-guile.hh"
23 #include "dimensions.hh"
25 #include "file-path.hh"
26 #include "relocate.hh"
27 #include "file-name.hh"
28 #include "string-convert.hh"
30 LY_DEFINE (ly_find_file, "ly:find-file",
32 "Return the absolute file name of @var{name}, "
33 "or @code{#f} if not found.")
35 LY_ASSERT_TYPE (scm_is_string, name, 1);
37 string nm = ly_scm2string (name);
38 string file_name = global_path.find (nm);
39 if (file_name.empty ())
42 return ly_string2scm (file_name);
46 Ugh. Gulped file is copied twice. (maybe thrice if you count stdio
49 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
50 1, 1, 0, (SCM name, SCM size),
51 "Read the file @var{name}, and return its contents in a string. "
52 "The file is looked up using the search path. ")
54 LY_ASSERT_TYPE (scm_is_string, name, 1);
56 if (size != SCM_UNDEFINED)
58 LY_ASSERT_TYPE (scm_is_number, size, 2);
59 sz = scm_to_int (size);
62 string contents = gulp_file_to_string (ly_scm2string (name), true, sz);
63 return scm_from_locale_stringn (contents.c_str (), contents.length ());
66 LY_DEFINE (ly_error, "ly:error",
67 1, 0, 1, (SCM str, SCM rest),
68 "Scheme callable function to issue the error @code{msg}. "
69 "The error is formatted with @code{format} and @code{rest}.")
71 LY_ASSERT_TYPE (scm_is_string, str, 1);
72 str = scm_simple_format (SCM_BOOL_F, str, rest);
73 error (ly_scm2string (str));
74 return SCM_UNSPECIFIED;
77 LY_DEFINE (ly_message, "ly:message",
78 1, 0, 1, (SCM str, SCM rest),
79 "Scheme callable function to issue the message @code{msg}. "
80 "The message is formatted with @code{format} and @code{rest}.")
82 LY_ASSERT_TYPE (scm_is_string, str, 1);
83 str = scm_simple_format (SCM_BOOL_F, str, rest);
84 message (ly_scm2string (str));
85 return SCM_UNSPECIFIED;
88 LY_DEFINE (ly_progress, "ly:progress",
89 1, 0, 1, (SCM str, SCM rest),
90 "Scheme callable function to print progress @code{str}. "
91 "The message is formatted with @code{format} and @code{rest}.")
93 LY_ASSERT_TYPE (scm_is_string, str, 1);
94 str = scm_simple_format (SCM_BOOL_F, str, rest);
95 progress_indication (ly_scm2string (str));
96 return SCM_UNSPECIFIED;
99 LY_DEFINE (ly_programming_error, "ly:programming-error",
100 1, 0, 1, (SCM str, SCM rest),
101 "Scheme callable function to issue the warning @code{msg}. "
102 "The message is formatted with @code{format} and @code{rest}.")
104 LY_ASSERT_TYPE (scm_is_string, str, 1);
105 str = scm_simple_format (SCM_BOOL_F, str, rest);
106 programming_error (ly_scm2string (str));
107 return SCM_UNSPECIFIED;
110 LY_DEFINE (ly_warning, "ly:warning",
111 1, 0, 1, (SCM str, SCM rest),
112 "Scheme callable function to issue the warning @code{str}. "
113 "The message is formatted with @code{format} and @code{rest}.")
115 LY_ASSERT_TYPE (scm_is_string, str, 1);
116 str = scm_simple_format (SCM_BOOL_F, str, rest);
117 warning (ly_scm2string (str));
118 return SCM_UNSPECIFIED;
121 LY_DEFINE (ly_dir_p, "ly:dir?",
123 "type predicate. A direction is @code{-1}, @code{0} or "
124 "@code{1}, where @code{-1} represents "
125 "left or down and @code{1} represents right or up.")
127 if (scm_is_number (s))
129 int i = scm_to_int (s);
130 return (i >= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
135 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
137 (SCM key, SCM alist, SCM default_value),
138 "Return value if KEY in ALIST, else DEFAULT-VALUE "
139 "(or #f if not specified).")
141 LY_ASSERT_TYPE(ly_cheap_is_list, alist, 2);
143 SCM handle = scm_assoc (key, alist);
144 if (scm_is_pair (handle))
145 return scm_cdr (handle);
147 if (default_value == SCM_UNDEFINED)
148 default_value = SCM_BOOL_F;
150 return default_value;
153 LY_DEFINE (ly_string_substitute, "ly:string-substitute",
154 3, 0, 0, (SCM a, SCM b, SCM s),
155 "Replace string @var{a} by string @var{b} in string @var{s}.")
157 LY_ASSERT_TYPE (scm_is_string, s, 1);
158 LY_ASSERT_TYPE (scm_is_string, b, 2);
159 LY_ASSERT_TYPE (scm_is_string, s, 3);
161 string ss = ly_scm2string (s);
162 replace_all (ss, string (scm_i_string_chars (a)),
163 string (scm_i_string_chars (b)));
164 return ly_string2scm (ss);
167 LY_DEFINE (ly_number_2_string, "ly:number->string",
169 "Convert @var{num} to a string without generating many decimals.")
171 LY_ASSERT_TYPE (scm_is_number, s, 1);
173 char str[400]; // ugh.
175 if (scm_exact_p (s) == SCM_BOOL_F)
177 Real r (scm_to_double (s));
178 if (isinf (r) || isnan (r))
180 programming_error (_ ("infinity or NaN encountered while converting Real number"));
181 programming_error (_ ("setting to zero"));
186 snprintf (str, sizeof (str), "%.4f", r);
189 snprintf (str, sizeof (str), "%d", int (scm_to_int (s)));
191 return scm_from_locale_string (str);
194 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
195 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
197 char const *vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")";
199 return scm_c_eval_string ((char *)vs);
202 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
203 "Return the unit used for lengths as a string.")
205 return scm_from_locale_string (INTERNAL_UNIT);
208 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
209 "Return @var{d} is a number. Used to distinguish length "
210 "variables from normal numbers.")
212 return scm_number_p (d);
218 LY_DEFINE (ly_protects, "ly:protects",
220 "Return hash of protected objects.")
225 LY_DEFINE (ly_gettext, "ly:gettext",
226 1, 0, 0, (SCM string),
229 LY_ASSERT_TYPE (scm_is_string, string, 1);
230 return ly_string2scm (_ (scm_i_string_chars (string)));
233 LY_DEFINE (ly_output_formats, "ly:output-formats",
235 "Formats passed to --format as a list of strings, "
236 "used for the output.")
238 vector<string> output_formats = string_split (output_format_global, ',');
241 int output_formats_count = output_formats.size ();
242 for (int i = 0; i < output_formats_count; i++)
243 lst = scm_cons (ly_string2scm (output_formats[i]), lst);
248 LY_DEFINE (ly_wide_char_2_utf_8, "ly:wide-char->utf-8",
250 "Encode the Unicode codepoint @var{wc}, an integer, as UTF-8")
254 LY_ASSERT_TYPE (scm_is_integer, wc, 1);
255 unsigned wide_char = (unsigned) scm_to_int (wc);
258 if (wide_char < 0x0080)
259 *p++ = (char)wide_char;
260 else if (wide_char < 0x0800)
262 *p++ = (char) (((wide_char >> 6)) | 0xC0);
263 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
265 else if (wide_char < 0x10000)
267 *p++ = (char) (((wide_char >> 12)) | 0xE0);
268 *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
269 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
273 *p++ = (char) (((wide_char >> 18)) | 0xF0);
274 *p++ = (char) (((wide_char >> 12) & 0x3F) | 0x80);
275 *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
276 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
280 return scm_from_locale_string (buf);
283 LY_DEFINE (ly_effective_prefix, "ly:effective-prefix",
285 "Return effective prefix.")
287 return ly_string2scm (lilypond_datadir);
290 LY_DEFINE (ly_chain_assoc_get, "ly:chain-assoc-get",
291 2, 1, 0, (SCM key, SCM achain, SCM dfault),
292 "Return value for @var{key} from a list of alists @var{achain}. "
293 "If no if no entry is found, return DFAULT, "
294 "or #f if no DFAULT not specified.")
296 if (scm_is_pair (achain))
298 SCM handle = scm_assoc (key, scm_car (achain));
299 if (scm_is_pair (handle))
300 return scm_cdr (handle);
302 return ly_chain_assoc_get (key, scm_cdr (achain), dfault);
304 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 LY_ASSERT_TYPE (scm_is_string, file_name, 1);
315 if (mode != SCM_UNDEFINED && scm_string_p (mode))
316 m = ly_scm2string (mode);
317 /* dup2 and (fileno (current-error-port)) do not work with mingw'c
320 freopen (ly_scm2string (file_name).c_str (), m.c_str (), stderr);
321 return SCM_UNSPECIFIED;
325 accumulate_symbol (void *closure, SCM key, SCM val, SCM result)
329 return scm_cons (key, result);
332 LY_DEFINE (ly_hash_table_keys, "ly:hash-table-keys",
334 "return a list of keys in @var{tab}")
336 return scm_internal_hash_fold ((Hash_closure_function) & accumulate_symbol,
340 LY_DEFINE (ly_camel_case_2_lisp_identifier, "ly:camel-case->lisp-identifier",
341 1, 0, 0, (SCM name_sym),
342 "Convert FooBar_Bla to foo-bar-bla style symbol.")
344 LY_ASSERT_TYPE (ly_is_symbol, name_sym, 1);
347 TODO: should use strings instead?
350 const string in = ly_symbol2string (name_sym);
351 string result = camel_case_to_lisp_identifier (in);
353 return ly_symbol2scm (result.c_str ());
356 LY_DEFINE (ly_expand_environment, "ly:expand-environment",
358 "Expand $VAR and $@{VAR@} in @var{str}.")
360 LY_ASSERT_TYPE (scm_is_string, str, 1);
362 return ly_string2scm (expand_environment_variables (ly_scm2string (str)));
366 LY_DEFINE (ly_truncate_list_x, "ly:truncate-list!",
367 2, 0, 0, (SCM lst, SCM i),
368 "Take at most the first @var{i} of list @var{lst}")
370 LY_ASSERT_TYPE (scm_is_integer, i, 1);
372 int k = scm_to_int (i);
379 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
383 scm_set_cdr_x (s, SCM_EOL);
389 format_single_argument (SCM arg, int precision)
391 if (scm_is_integer (arg) && scm_exact_p (arg) == SCM_BOOL_T)
392 return (String_convert::int_string (scm_to_int (arg)));
393 else if (scm_is_number (arg))
395 Real val = scm_to_double (arg);
397 if (isnan (val) || isinf (val))
399 warning (_ ("Found infinity or nan in output. Substituting 0.0"));
401 if (strict_infinity_checking)
405 return (String_convert::form_string ("%.*lf", precision, val));
407 else if (scm_is_string (arg))
408 return (ly_scm2string (arg));
409 else if (scm_is_symbol (arg))
410 return (ly_symbol2string (arg));
413 ly_progress (scm_from_locale_string ("Unsupported SCM value for format: ~a"),
421 LY_DEFINE (ly_format, "ly:format",
422 1, 0, 1, (SCM str, SCM rest),
423 "LilyPond specific format, supporting ~a ~[0-9]f.")
425 LY_ASSERT_TYPE (scm_is_string, str, 1);
427 string format = ly_scm2string (str);
428 vector<string> results;
431 while (i < format.size ())
433 vsize tilde = format.find ('~', i);
435 results.push_back (format.substr (i, (tilde-i)));
442 char spec = format.at (tilde ++);
444 results.push_back ("~");
447 if (!scm_is_pair (rest))
449 programming_error (string (__FUNCTION__)
450 + ": not enough arguments for format.");
451 return ly_string2scm ("");
454 SCM arg = scm_car (rest);
455 rest = scm_cdr (rest);
461 else if (isdigit (spec))
463 precision = spec - '0';
464 spec = format.at (tilde ++);
467 if (spec == 'a' || spec == 'f')
468 results.push_back (format_single_argument (arg, precision));
469 else if (spec == 'l')
472 for (; scm_is_pair (s); s = scm_cdr (s))
474 results.push_back (format_single_argument (scm_car (s), precision));
475 if (scm_cdr (s) != SCM_EOL)
476 results.push_back (" ");
480 results.push_back (format_single_argument (s, precision));
488 if (scm_is_pair (rest))
489 programming_error (string (__FUNCTION__)
490 + ": too many arguments");
493 for (vsize i = 0; i < results.size (); i++)
494 len += results[i].size ();
496 char *result = (char*) scm_malloc (len + 1);
498 for (vsize i = 0; i < results.size (); i++)
500 strncpy (ptr, results[i].c_str (), results[i].size ());
501 ptr += results[i].size ();
505 return scm_take_locale_stringn (result, len);