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 <libintl.h> /* gettext on MacOS X */
13 #include <math.h> /* isinf */
15 #include <string.h> /* memset */
17 #include <utf8/wchar.h> /* wcrtomb */
19 #include <wchar.h> /* wcrtomb */
22 #include "libc-extension.hh"
23 #include "lily-guile.hh"
28 #include "dimensions.hh"
30 #include "file-path.hh"
33 source-file.hh includes cmath which undefines isinf and isnan
36 inline int my_isinf (Real r) { return isinf (r); }
37 inline int my_isnan (Real r) { return isnan (r); }
40 LY_DEFINE (ly_find_file, "ly:find-file",
42 "Return the absolute file name of @var{name}, "
43 "or @code{#f} if not found.")
45 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
47 String nm = ly_scm2string (name);
48 String file_name = global_path.find (nm);
49 if (file_name.is_empty ())
52 return scm_makfrom0str (file_name.to_str0 ());
56 Ugh. Gulped file is copied twice. (maybe thrice if you count stdio
59 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
61 "Read the file @var{name}, and return its contents in a string. "
62 "The file is looked up using the search path.")
64 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
65 String contents = gulp_file_to_string (ly_scm2string (name), true);
66 return scm_from_locale_stringn (contents.get_str0 (), contents.length ());
69 LY_DEFINE (ly_warn, "ly:warn",
70 1, 0, 1, (SCM str, SCM rest),
71 "Scheme callable function to issue the warning @code{msg}. "
72 "The message is formatted with @code{format} and @code{rest}.")
74 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
75 progress_indication ("\n");
77 str = scm_simple_format (SCM_BOOL_F, str, rest);
78 warning (ly_scm2string (str));
79 return SCM_UNSPECIFIED;
82 LY_DEFINE (ly_programming_error, "ly:programming-error",
83 1, 0, 1, (SCM str, SCM rest),
84 "Scheme callable function to issue the warning @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 progress_indication ("\n");
90 str = scm_simple_format (SCM_BOOL_F, str, rest);
91 programming_error (ly_scm2string (str));
92 return SCM_UNSPECIFIED;
95 LY_DEFINE (ly_dir_p, "ly:dir?",
97 "type predicate. A direction is @code{-1}, @code{0} or "
98 "@code{1}, where @code{-1} represents "
99 "left or down and @code{1} represents right or up.")
101 if (scm_is_number (s))
103 int i = scm_to_int (s);
104 return (i >= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
109 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
111 (SCM key, SCM alist, SCM default_value),
112 "Return value if KEY in ALIST, else DEFAULT-VALUE "
113 "(or #f if not specified).")
115 SCM handle = scm_assoc (key, alist);
117 if (default_value == SCM_UNDEFINED)
118 default_value = SCM_BOOL_F;
120 if (scm_is_pair (handle))
121 return scm_cdr (handle);
123 return default_value;
126 LY_DEFINE (ly_number2string, "ly:number->string",
128 "Convert @var{num} to a string without generating many decimals.")
130 SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
132 char str[400]; // ugh.
134 if (scm_exact_p (s) == SCM_BOOL_F)
136 Real r (scm_to_double (s));
138 if (my_isinf (r) || my_isnan (r))
140 if (isinf (r) || isnan (r))
143 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
147 snprintf (str, sizeof (str), "%08.4f", r);
150 snprintf (str, sizeof (str), "%d", scm_to_int (s));
152 return scm_makfrom0str (str);
155 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
156 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
158 char const *vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")";
160 return scm_c_eval_string ((char *)vs);
163 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
164 "Return the unit used for lengths as a string.")
166 return scm_makfrom0str (INTERNAL_UNIT);
169 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
170 "Return @var{d} is a number. Used to distinguish length "
171 "variables from normal numbers.")
173 return scm_number_p (d);
179 LY_DEFINE (ly_protects, "ly:protects",
181 "Return hash of protected objects.")
186 LY_DEFINE (ly_gettext, "ly:gettext",
187 1, 0, 0, (SCM string),
190 SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
191 __FUNCTION__, "string");
192 return scm_makfrom0str (_ (scm_i_string_chars (string)).to_str0 ());
195 LY_DEFINE (ly_output_backend, "ly:output-backend",
197 "Return name of output backend.")
199 return scm_makfrom0str (output_backend_global.to_str0 ());
202 LY_DEFINE (ly_output_formats, "ly:output-formats",
204 "Formats passed to --format as a list of strings, "
205 "used for the output.")
207 Array<String> output_formats = split_string (output_format_global, ',');
210 int output_formats_count = output_formats.size ();
211 for (int i = 0; i < output_formats_count; i ++)
212 lst = scm_cons (scm_makfrom0str (output_formats[i].to_str0 ()), lst);
217 LY_DEFINE (ly_wchar_to_utf_8, "ly:wide-char->utf-8",
219 "Encode the Unicode codepoint @var{wc} as UTF-8")
223 SCM_ASSERT_TYPE (scm_is_integer (wc), wc, SCM_ARG1, __FUNCTION__, "integer");
224 wchar_t wide_char = (wchar_t) scm_to_int (wc);
227 memset (&state, '\0', sizeof (state));
228 memset (buf, '\0', sizeof (buf));
230 wcrtomb (buf, wide_char, &state);
232 return scm_makfrom0str (buf);
235 LY_DEFINE (ly_effective_prefix, "ly:effective-prefix",
237 "Return effective prefix.")
239 return scm_makfrom0str (prefix_directory.to_str0 ());