]> git.donarmstrong.com Git - lilypond.git/blob - lily/general-scheme.cc
* flower
[lilypond.git] / lily / general-scheme.cc
1 /*
2   lily-guile.cc -- implement assorted Guile bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7   Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9
10 #include "config.hh"
11
12 #include <libintl.h>  /* gettext on MacOS X */
13 #include <math.h>  /* isinf */
14 #include <stdio.h>
15 #include <string.h>  /* memset */
16 #include <wchar.h>  /* wcrtomb */
17
18 #include "libc-extension.hh"
19 #include "lily-guile.hh"
20 #include "string.hh"
21 #include "misc.hh"
22 #include "warn.hh"
23 #include "version.hh"
24 #include "dimensions.hh"
25 #include "main.hh"
26 #include "file-path.hh"
27
28 /* MacOS S fix:
29    source-file.hh includes cmath which undefines isinf and isnan
30 */
31 #ifdef __APPLE__
32 inline int my_isinf (Real r) { return isinf (r); }
33 inline int my_isnan (Real r) { return isnan (r); }
34 #endif
35
36 LY_DEFINE (ly_find_file, "ly:find-file",
37            1, 0, 0, (SCM name),
38            "Return the absolute file name of @var{name}, "
39            "or @code{#f} if not found.")
40 {
41   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
42
43   String nm = ly_scm2string (name);
44   String file_name = global_path.find (nm);
45   if (file_name.is_empty ())
46     return SCM_BOOL_F;
47
48   return scm_makfrom0str (file_name.to_str0 ());
49 }
50
51 /*
52   Ugh. Gulped file is copied twice. (maybe thrice if you count stdio
53   buffering.)
54 */
55 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
56            1, 0, 0, (SCM name),
57            "Read the file @var{name}, and return its contents in a string.  "
58            "The file is looked up using the search path.")
59 {
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 ());
63 }
64
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}.")
69 {
70   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
71   progress_indication ("\n");
72
73   str = scm_simple_format (SCM_BOOL_F, str, rest);
74   warning (ly_scm2string (str));
75   return SCM_UNSPECIFIED;
76 }
77
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}.")
82 {
83   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
84   progress_indication ("\n");
85
86   str = scm_simple_format (SCM_BOOL_F, str, rest);
87   programming_error (ly_scm2string (str));
88   return SCM_UNSPECIFIED;
89 }
90
91 LY_DEFINE (ly_dir_p, "ly:dir?",
92            1, 0, 0, (SCM s),
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.")
96 {
97   if (scm_is_number (s))
98     {
99       int i = scm_to_int (s);
100       return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
101     }
102   return SCM_BOOL_F;
103 }
104
105 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
106            2, 1, 0,
107            (SCM key, SCM alist, SCM default_value),
108            "Return value if KEY in ALIST, else DEFAULT-VALUE "
109            "(or #f if not specified).")
110 {
111   SCM handle = scm_assoc (key, alist);
112
113   if (default_value == SCM_UNDEFINED)
114     default_value = SCM_BOOL_F;
115
116   if (scm_is_pair (handle))
117     return scm_cdr (handle);
118   else
119     return default_value;
120 }
121
122 LY_DEFINE (ly_number2string, "ly:number->string",
123            1, 0, 0, (SCM s),
124            "Convert @var{num} to a string without generating many decimals.")
125 {
126   SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
127
128   char str[400];                        // ugh.
129
130   if (scm_exact_p (s) == SCM_BOOL_F)
131     {
132       Real r (scm_to_double (s));
133 #ifdef __APPLE__
134       if (my_isinf (r) || my_isnan (r))
135 #else
136         if (isinf (r) || isnan (r))
137 #endif
138           {
139             programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
140             r = 0.0;
141           }
142
143       snprintf (str, sizeof (str), "%08.4f", r);
144     }
145   else
146     snprintf (str, sizeof (str), "%d", scm_to_int (s));
147
148   return scm_makfrom0str (str);
149 }
150
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)}. ")
153 {
154   char const *vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")";
155
156   return scm_c_eval_string ((char *)vs);
157 }
158
159 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
160            "Return the unit used for lengths as a string.")
161 {
162   return scm_makfrom0str (INTERNAL_UNIT);
163 }
164
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.")
168 {
169   return scm_number_p (d);
170 }
171
172 /*
173   Debugging mem leaks:
174 */
175 LY_DEFINE (ly_protects, "ly:protects",
176            0, 0, 0, (),
177            "Return hash of protected objects.")
178 {
179   return scm_protects;
180 }
181
182 LY_DEFINE (ly_gettext, "ly:gettext",
183            1, 0, 0, (SCM string),
184            "Gettext wrapper.")
185 {
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 ());
189 }
190
191 LY_DEFINE (ly_output_backend, "ly:output-backend",
192            0, 0, 0, (),
193            "Return name of output backend.")
194 {
195   return scm_makfrom0str (output_backend_global.to_str0 ());
196 }
197
198 LY_DEFINE (ly_output_formats, "ly:output-formats",
199            0, 0, 0, (),
200            "Formats passed to --format as a list of strings, "
201            "used for the output.")
202 {
203   Array<String> output_formats = split_string (output_format_global, ',');
204
205   SCM lst = SCM_EOL;
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);
209
210   return lst;
211 }
212
213 LY_DEFINE (ly_wchar_to_utf_8, "ly:wide-char->utf-8",
214            1, 0, 0, (SCM wc),
215            "Encode the Unicode codepoint @var{wc} as UTF-8")
216 {
217   char buf[100];
218
219   SCM_ASSERT_TYPE (scm_is_integer (wc), wc, SCM_ARG1, __FUNCTION__, "integer");
220   wchar_t wide_char = (wchar_t) scm_to_int (wc);
221
222   mbstate_t state;
223   memset (&state, '\0', sizeof (state));
224   memset (buf, '\0', sizeof (buf));
225
226   wcrtomb (buf, wide_char, &state);
227   
228   return scm_makfrom0str (buf);
229 }
230