2 lily-guile.cc -- implement assorted Guile bindings
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@cs.uu.nl>
12 #include <math.h> /* isinf */
14 #include <libintl.h> // gettext on macos x
16 #include "libc-extension.hh"
17 #include "lily-guile.hh"
22 #include "dimensions.hh"
26 source-file.hh includes cmath which undefines isinf and isnan
29 inline int my_isinf (Real r) { return isinf (r); }
30 inline int my_isnan (Real r) { return isnan (r); }
34 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
36 "Read the file @var{name}, and return its contents in a string. "
37 "The file is looked up using the search path.")
39 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
40 String contents = gulp_file_to_string (ly_scm2string (name), true);
41 return scm_from_locale_stringn (contents.get_str0 (), contents.length ());
44 LY_DEFINE (ly_warn, "ly:warn",
45 1, 0, 1, (SCM str, SCM rest),
46 "Scheme callable function to issue the warning @code{msg}. "
47 "The message is formatted with @code{format} and @code{rest}.")
49 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
50 progress_indication ("\n");
52 str = scm_simple_format (SCM_BOOL_F, str, rest);
53 warning (ly_scm2string (str));
54 return SCM_UNSPECIFIED;
57 LY_DEFINE (ly_programming_error, "ly:programming-error",
58 1, 0, 1, (SCM str, SCM rest),
59 "Scheme callable function to issue the warning @code{msg}. "
60 "The message is formatted with @code{format} and @code{rest}.")
62 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
63 progress_indication ("\n");
65 str = scm_simple_format (SCM_BOOL_F, str, rest);
66 programming_error (ly_scm2string (str));
67 return SCM_UNSPECIFIED;
70 LY_DEFINE (ly_dir_p, "ly:dir?",
72 "type predicate. A direction is @code{-1}, @code{0} or "
73 "@code{1}, where @code{-1} represents "
74 "left or down and @code{1} represents right or up.")
76 if (scm_is_number (s))
78 int i = scm_to_int (s);
79 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
84 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
86 (SCM key, SCM alist, SCM default_value),
87 "Return value if KEY in ALIST, else DEFAULT-VALUE "
88 "(or #f if not specified).")
90 SCM handle = scm_assoc (key, alist);
92 if (default_value == SCM_UNDEFINED)
93 default_value = SCM_BOOL_F;
95 if (scm_is_pair (handle))
96 return scm_cdr (handle);
102 LY_DEFINE (ly_number2string, "ly:number->string",
104 "Convert @var{num} to a string without generating many decimals.")
106 SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
108 char str[400]; // ugh.
110 if (scm_exact_p (s) == SCM_BOOL_F)
112 Real r (scm_to_double (s));
114 if (my_isinf (r) || my_isnan (r))
116 if (isinf (r) || isnan (r))
119 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
123 snprintf (str, sizeof (str), "%08.4f", r);
126 snprintf (str, sizeof (str), "%d", scm_to_int (s));
128 return scm_makfrom0str (str);
133 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
134 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
136 char const* vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
138 return scm_c_eval_string ((char*)vs);
141 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
142 "Return the unit used for lengths as a string.")
144 return scm_makfrom0str (INTERNAL_UNIT);
149 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
150 "Return @var{d} is a number. Used to distinguish length "
151 "variables from normal numbers.")
153 return scm_number_p (d);
159 LY_DEFINE (ly_protects, "ly:protects",
161 "Return hash of protected objects.")
166 LY_DEFINE (ly_gettext, "ly:gettext",
167 1, 0, 0, (SCM string),
170 SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
171 __FUNCTION__, "string");
172 return scm_makfrom0str (gettext (scm_i_string_chars (string)));
178 LY_DEFINE (ly_output_backend, "ly:output-backend",
180 "Return name of output backend.")
182 return scm_makfrom0str (output_backend_global.to_str0 ());
186 LY_DEFINE (ly_output_formats, "ly:output-formats",
188 "Formats passed to --format as a list of strings, "
189 "used for the output.")
191 Array<String> output_formats = split_string (output_format_global, ',');
194 int output_formats_count = output_formats.size ();
195 for (int i = 0; i < output_formats_count; i ++)
196 lst = scm_cons (scm_makfrom0str (output_formats[i].to_str0 ()), lst);