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_start_environment, "ly:start-environment",
32 "Return the environment, a list of strings, that was in effect at program start")
37 for (vsize i = 0; i < start_environment_global.size (); i++)
39 *tail = scm_cons (ly_string2scm (start_environment_global[i]),
41 tail = SCM_CDRLOC(*tail);
48 LY_DEFINE (ly_find_file, "ly:find-file",
50 "Return the absolute file name of @var{name}, "
51 "or @code{#f} if not found.")
53 LY_ASSERT_TYPE (scm_is_string, name, 1);
55 string nm = ly_scm2string (name);
56 string file_name = global_path.find (nm);
57 if (file_name.empty ())
60 return ly_string2scm (file_name);
64 Ugh. Gulped file is copied twice. (maybe thrice if you count stdio
67 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
68 1, 1, 0, (SCM name, SCM size),
69 "Read the file @var{name}, and return its contents in a string. "
70 "The file is looked up using the search path. ")
72 LY_ASSERT_TYPE (scm_is_string, name, 1);
74 if (size != SCM_UNDEFINED)
76 LY_ASSERT_TYPE (scm_is_number, size, 2);
77 sz = scm_to_int (size);
80 string contents = gulp_file_to_string (ly_scm2string (name), true, sz);
81 return scm_from_locale_stringn (contents.c_str (), contents.length ());
84 LY_DEFINE (ly_error, "ly:error",
85 1, 0, 1, (SCM str, SCM rest),
86 "Scheme callable function to issue the error @code{msg}. "
87 "The error is formatted with @code{format} and @code{rest}.")
89 LY_ASSERT_TYPE (scm_is_string, str, 1);
90 str = scm_simple_format (SCM_BOOL_F, str, rest);
91 error (ly_scm2string (str));
92 return SCM_UNSPECIFIED;
95 LY_DEFINE (ly_message, "ly:message",
96 1, 0, 1, (SCM str, SCM rest),
97 "Scheme callable function to issue the message @code{msg}. "
98 "The message is formatted with @code{format} and @code{rest}.")
100 LY_ASSERT_TYPE (scm_is_string, str, 1);
101 str = scm_simple_format (SCM_BOOL_F, str, rest);
102 message (ly_scm2string (str));
103 return SCM_UNSPECIFIED;
106 LY_DEFINE (ly_progress, "ly:progress",
107 1, 0, 1, (SCM str, SCM rest),
108 "Scheme callable function to print progress @code{str}. "
109 "The message is formatted with @code{format} and @code{rest}.")
111 LY_ASSERT_TYPE (scm_is_string, str, 1);
112 str = scm_simple_format (SCM_BOOL_F, str, rest);
113 progress_indication (ly_scm2string (str));
114 return SCM_UNSPECIFIED;
117 LY_DEFINE (ly_programming_error, "ly:programming-error",
118 1, 0, 1, (SCM str, SCM rest),
119 "Scheme callable function to issue the warning @code{msg}. "
120 "The message is formatted with @code{format} and @code{rest}.")
122 LY_ASSERT_TYPE (scm_is_string, str, 1);
123 str = scm_simple_format (SCM_BOOL_F, str, rest);
124 programming_error (ly_scm2string (str));
125 return SCM_UNSPECIFIED;
128 LY_DEFINE (ly_warning, "ly:warning",
129 1, 0, 1, (SCM str, SCM rest),
130 "Scheme callable function to issue the warning @code{str}. "
131 "The message is formatted with @code{format} and @code{rest}.")
133 LY_ASSERT_TYPE (scm_is_string, str, 1);
134 str = scm_simple_format (SCM_BOOL_F, str, rest);
135 warning (ly_scm2string (str));
136 return SCM_UNSPECIFIED;
139 LY_DEFINE (ly_dir_p, "ly:dir?",
141 "type predicate. A direction is @code{-1}, @code{0} or "
142 "@code{1}, where @code{-1} represents "
143 "left or down and @code{1} represents right or up.")
145 if (scm_is_number (s))
147 int i = scm_to_int (s);
148 return (i >= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
153 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
155 (SCM key, SCM alist, SCM default_value),
156 "Return value if KEY in ALIST, else DEFAULT-VALUE "
157 "(or #f if not specified).")
159 LY_ASSERT_TYPE(ly_cheap_is_list, alist, 2);
161 SCM handle = scm_assoc (key, alist);
162 if (scm_is_pair (handle))
163 return scm_cdr (handle);
165 if (default_value == SCM_UNDEFINED)
166 default_value = SCM_BOOL_F;
168 return default_value;
171 LY_DEFINE (ly_string_substitute, "ly:string-substitute",
172 3, 0, 0, (SCM a, SCM b, SCM s),
173 "Replace string @var{a} by string @var{b} in string @var{s}.")
175 LY_ASSERT_TYPE (scm_is_string, s, 1);
176 LY_ASSERT_TYPE (scm_is_string, b, 2);
177 LY_ASSERT_TYPE (scm_is_string, s, 3);
179 string ss = ly_scm2string (s);
180 replace_all (ss, string (scm_i_string_chars (a)),
181 string (scm_i_string_chars (b)));
182 return ly_string2scm (ss);
185 LY_DEFINE (ly_number_2_string, "ly:number->string",
187 "Convert @var{num} to a string without generating many decimals.")
189 LY_ASSERT_TYPE (scm_is_number, s, 1);
191 char str[400]; // ugh.
193 if (scm_exact_p (s) == SCM_BOOL_F)
195 Real r (scm_to_double (s));
196 if (isinf (r) || isnan (r))
198 programming_error (_ ("infinity or NaN encountered while converting Real number"));
199 programming_error (_ ("setting to zero"));
204 snprintf (str, sizeof (str), "%.4f", r);
207 snprintf (str, sizeof (str), "%d", int (scm_to_int (s)));
209 return scm_from_locale_string (str);
212 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
213 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
215 char const *vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")";
217 return scm_c_eval_string ((char *)vs);
220 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
221 "Return the unit used for lengths as a string.")
223 return scm_from_locale_string (INTERNAL_UNIT);
226 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
227 "Return @var{d} is a number. Used to distinguish length "
228 "variables from normal numbers.")
230 return scm_number_p (d);
236 LY_DEFINE (ly_protects, "ly:protects",
238 "Return hash of protected objects.")
243 LY_DEFINE (ly_gettext, "ly:gettext",
244 1, 0, 0, (SCM string),
247 LY_ASSERT_TYPE (scm_is_string, string, 1);
248 return ly_string2scm (_ (scm_i_string_chars (string)));
251 LY_DEFINE (ly_output_formats, "ly:output-formats",
253 "Formats passed to --format as a list of strings, "
254 "used for the output.")
256 vector<string> output_formats = string_split (output_format_global, ',');
259 int output_formats_count = output_formats.size ();
260 for (int i = 0; i < output_formats_count; i++)
261 lst = scm_cons (ly_string2scm (output_formats[i]), lst);
266 LY_DEFINE (ly_wide_char_2_utf_8, "ly:wide-char->utf-8",
268 "Encode the Unicode codepoint @var{wc}, an integer, as UTF-8")
272 LY_ASSERT_TYPE (scm_is_integer, wc, 1);
273 unsigned wide_char = (unsigned) scm_to_int (wc);
276 if (wide_char < 0x0080)
277 *p++ = (char)wide_char;
278 else if (wide_char < 0x0800)
280 *p++ = (char) (((wide_char >> 6)) | 0xC0);
281 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
283 else if (wide_char < 0x10000)
285 *p++ = (char) (((wide_char >> 12)) | 0xE0);
286 *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
287 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
291 *p++ = (char) (((wide_char >> 18)) | 0xF0);
292 *p++ = (char) (((wide_char >> 12) & 0x3F) | 0x80);
293 *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
294 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
298 return scm_from_locale_string (buf);
301 LY_DEFINE (ly_effective_prefix, "ly:effective-prefix",
303 "Return effective prefix.")
305 return ly_string2scm (lilypond_datadir);
308 LY_DEFINE (ly_chain_assoc_get, "ly:chain-assoc-get",
309 2, 1, 0, (SCM key, SCM achain, SCM dfault),
310 "Return value for @var{key} from a list of alists @var{achain}. "
311 "If no if no entry is found, return DFAULT, "
312 "or #f if no DFAULT not specified.")
314 if (scm_is_pair (achain))
316 SCM handle = scm_assoc (key, scm_car (achain));
317 if (scm_is_pair (handle))
318 return scm_cdr (handle);
320 return ly_chain_assoc_get (key, scm_cdr (achain), dfault);
322 return dfault == SCM_UNDEFINED ? SCM_BOOL_F : dfault;
326 LY_DEFINE (ly_stderr_redirect, "ly:stderr-redirect",
327 1, 1, 0, (SCM file_name, SCM mode),
328 "Redirect stderr to FILE-NAME, opened with MODE.")
330 LY_ASSERT_TYPE (scm_is_string, file_name, 1);
333 if (mode != SCM_UNDEFINED && scm_string_p (mode))
334 m = ly_scm2string (mode);
335 /* dup2 and (fileno (current-error-port)) do not work with mingw'c
338 freopen (ly_scm2string (file_name).c_str (), m.c_str (), stderr);
339 return SCM_UNSPECIFIED;
343 accumulate_symbol (void *closure, SCM key, SCM val, SCM result)
347 return scm_cons (key, result);
350 LY_DEFINE (ly_hash_table_keys, "ly:hash-table-keys",
352 "return a list of keys in @var{tab}")
354 return scm_internal_hash_fold ((Hash_closure_function) & accumulate_symbol,
358 LY_DEFINE (ly_camel_case_2_lisp_identifier, "ly:camel-case->lisp-identifier",
359 1, 0, 0, (SCM name_sym),
360 "Convert FooBar_Bla to foo-bar-bla style symbol.")
362 LY_ASSERT_TYPE (ly_is_symbol, name_sym, 1);
365 TODO: should use strings instead?
368 const string in = ly_symbol2string (name_sym);
369 string result = camel_case_to_lisp_identifier (in);
371 return ly_symbol2scm (result.c_str ());
374 LY_DEFINE (ly_expand_environment, "ly:expand-environment",
376 "Expand $VAR and $@{VAR@} in @var{str}.")
378 LY_ASSERT_TYPE (scm_is_string, str, 1);
380 return ly_string2scm (expand_environment_variables (ly_scm2string (str)));
384 LY_DEFINE (ly_truncate_list_x, "ly:truncate-list!",
385 2, 0, 0, (SCM lst, SCM i),
386 "Take at most the first @var{i} of list @var{lst}")
388 LY_ASSERT_TYPE (scm_is_integer, i, 1);
390 int k = scm_to_int (i);
397 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
401 scm_set_cdr_x (s, SCM_EOL);
407 format_single_argument (SCM arg, int precision)
409 if (scm_is_integer (arg) && scm_exact_p (arg) == SCM_BOOL_T)
410 return (String_convert::int_string (scm_to_int (arg)));
411 else if (scm_is_number (arg))
413 Real val = scm_to_double (arg);
415 if (isnan (val) || isinf (val))
417 warning (_ ("Found infinity or nan in output. Substituting 0.0"));
419 if (strict_infinity_checking)
423 return (String_convert::form_string ("%.*lf", precision, val));
425 else if (scm_is_string (arg))
426 return (ly_scm2string (arg));
427 else if (scm_is_symbol (arg))
428 return (ly_symbol2string (arg));
431 ly_progress (scm_from_locale_string ("Unsupported SCM value for format: ~a"),
439 LY_DEFINE (ly_format, "ly:format",
440 1, 0, 1, (SCM str, SCM rest),
441 "LilyPond specific format, supporting ~a ~[0-9]f.")
443 LY_ASSERT_TYPE (scm_is_string, str, 1);
445 string format = ly_scm2string (str);
446 vector<string> results;
449 while (i < format.size ())
451 vsize tilde = format.find ('~', i);
453 results.push_back (format.substr (i, (tilde-i)));
460 char spec = format.at (tilde ++);
462 results.push_back ("~");
465 if (!scm_is_pair (rest))
467 programming_error (string (__FUNCTION__)
468 + ": not enough arguments for format.");
469 return ly_string2scm ("");
472 SCM arg = scm_car (rest);
473 rest = scm_cdr (rest);
479 else if (isdigit (spec))
481 precision = spec - '0';
482 spec = format.at (tilde ++);
485 if (spec == 'a' || spec == 'f')
486 results.push_back (format_single_argument (arg, precision));
487 else if (spec == 'l')
490 for (; scm_is_pair (s); s = scm_cdr (s))
492 results.push_back (format_single_argument (scm_car (s), precision));
493 if (scm_cdr (s) != SCM_EOL)
494 results.push_back (" ");
498 results.push_back (format_single_argument (s, precision));
506 if (scm_is_pair (rest))
507 programming_error (string (__FUNCTION__)
508 + ": too many arguments");
511 for (vsize i = 0; i < results.size (); i++)
512 len += results[i].size ();
514 char *result = (char*) scm_malloc (len + 1);
516 for (vsize i = 0; i < results.size (); i++)
518 strncpy (ptr, results[i].c_str (), results[i].size ());
519 ptr += results[i].size ();
523 return scm_take_locale_stringn (result, len);