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 */
16 #include <wchar.h> /* wcrtomb */
18 #include "libc-extension.hh"
19 #include "lily-guile.hh"
24 #include "dimensions.hh"
26 #include "file-path.hh"
29 source-file.hh includes cmath which undefines isinf and isnan
32 inline int my_isinf (Real r) { return isinf (r); }
33 inline int my_isnan (Real r) { return isnan (r); }
36 LY_DEFINE (ly_find_file, "ly:find-file",
38 "Return the absolute file name of @var{name}, "
39 "or @code{#f} if not found.")
41 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
43 String nm = ly_scm2string (name);
44 String file_name = global_path.find (nm);
45 if (file_name.is_empty ())
48 return scm_makfrom0str (file_name.to_str0 ());
52 Ugh. Gulped file is copied twice. (maybe thrice if you count stdio
55 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
57 "Read the file @var{name}, and return its contents in a string. "
58 "The file is looked up using the search path.")
60 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
61 String contents = gulp_file_to_string (ly_scm2string (name), true);
62 return scm_from_locale_stringn (contents.get_str0 (), contents.length ());
65 LY_DEFINE (ly_warn, "ly:warn",
66 1, 0, 1, (SCM str, SCM rest),
67 "Scheme callable function to issue the warning @code{msg}. "
68 "The message is formatted with @code{format} and @code{rest}.")
70 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
71 progress_indication ("\n");
73 str = scm_simple_format (SCM_BOOL_F, str, rest);
74 warning (ly_scm2string (str));
75 return SCM_UNSPECIFIED;
78 LY_DEFINE (ly_programming_error, "ly:programming-error",
79 1, 0, 1, (SCM str, SCM rest),
80 "Scheme callable function to issue the warning @code{msg}. "
81 "The message is formatted with @code{format} and @code{rest}.")
83 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
84 progress_indication ("\n");
86 str = scm_simple_format (SCM_BOOL_F, str, rest);
87 programming_error (ly_scm2string (str));
88 return SCM_UNSPECIFIED;
91 LY_DEFINE (ly_dir_p, "ly:dir?",
93 "type predicate. A direction is @code{-1}, @code{0} or "
94 "@code{1}, where @code{-1} represents "
95 "left or down and @code{1} represents right or up.")
97 if (scm_is_number (s))
99 int i = scm_to_int (s);
100 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
105 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
107 (SCM key, SCM alist, SCM default_value),
108 "Return value if KEY in ALIST, else DEFAULT-VALUE "
109 "(or #f if not specified).")
111 SCM handle = scm_assoc (key, alist);
113 if (default_value == SCM_UNDEFINED)
114 default_value = SCM_BOOL_F;
116 if (scm_is_pair (handle))
117 return scm_cdr (handle);
119 return default_value;
122 LY_DEFINE (ly_number2string, "ly:number->string",
124 "Convert @var{num} to a string without generating many decimals.")
126 SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
128 char str[400]; // ugh.
130 if (scm_exact_p (s) == SCM_BOOL_F)
132 Real r (scm_to_double (s));
134 if (my_isinf (r) || my_isnan (r))
136 if (isinf (r) || isnan (r))
139 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
143 snprintf (str, sizeof (str), "%08.4f", r);
146 snprintf (str, sizeof (str), "%d", scm_to_int (s));
148 return scm_makfrom0str (str);
151 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
152 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
154 char const *vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")";
156 return scm_c_eval_string ((char *)vs);
159 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
160 "Return the unit used for lengths as a string.")
162 return scm_makfrom0str (INTERNAL_UNIT);
165 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
166 "Return @var{d} is a number. Used to distinguish length "
167 "variables from normal numbers.")
169 return scm_number_p (d);
175 LY_DEFINE (ly_protects, "ly:protects",
177 "Return hash of protected objects.")
182 LY_DEFINE (ly_gettext, "ly:gettext",
183 1, 0, 0, (SCM string),
186 SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
187 __FUNCTION__, "string");
188 return scm_makfrom0str (_ (scm_i_string_chars (string)).to_str0 ());
191 LY_DEFINE (ly_output_backend, "ly:output-backend",
193 "Return name of output backend.")
195 return scm_makfrom0str (output_backend_global.to_str0 ());
198 LY_DEFINE (ly_output_formats, "ly:output-formats",
200 "Formats passed to --format as a list of strings, "
201 "used for the output.")
203 Array<String> output_formats = split_string (output_format_global, ',');
206 int output_formats_count = output_formats.size ();
207 for (int i = 0; i < output_formats_count; i ++)
208 lst = scm_cons (scm_makfrom0str (output_formats[i].to_str0 ()), lst);
213 LY_DEFINE (ly_wchar_to_utf_8, "ly:wide-char->utf-8",
215 "Encode the Unicode codepoint @var{wc} as UTF-8")
219 SCM_ASSERT_TYPE (scm_is_integer (wc), wc, SCM_ARG1, __FUNCTION__, "integer");
220 wchar_t wide_char = (wchar_t) scm_to_int (wc);
223 memset (&state, '\0', sizeof (state));
224 memset (buf, '\0', sizeof (buf));
226 wcrtomb (buf, wide_char, &state);
228 return scm_makfrom0str (buf);