]> git.donarmstrong.com Git - lilypond.git/blob - lily/general-scheme.cc
* lily/general-scheme.cc: new file.
[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--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7                  Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9
10 #include <math.h>   /* isinf */
11
12 #include "lily-guile.hh"
13 #include "string.hh"
14 #include "misc.hh"
15 #include "warn.hh"
16 #include "version.hh"
17 #include "dimensions.hh"
18 #include "main.hh"
19
20 /* MacOS S fix:
21    source-file.hh includes cmath which undefines isinf and isnan
22 */
23 #ifdef __APPLE__
24 inline int my_isinf (Real r) { return isinf (r); }
25 inline int my_isnan (Real r) { return isnan (r); }
26 #endif
27
28
29 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
30            1, 0, 0, (SCM name),
31            "Read the file @var{name}, and return its contents in a string.  "
32            "The file is looked up using the search path.")
33 {
34   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
35   String contents = gulp_file_to_string (ly_scm2string (name), true);
36   return scm_from_locale_stringn (contents.get_str0 (), contents.length ());
37 }
38
39 LY_DEFINE (ly_warn, "ly:warn",
40            1, 0, 1, (SCM str, SCM rest),
41            "Scheme callable function to issue the warning @code{msg}. "
42            "The message is formatted with @code{format} and @code{rest}.")
43 {
44   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
45   progress_indication ("\n");
46
47   str = scm_simple_format (SCM_BOOL_F, str, rest);
48   warning (ly_scm2string (str));
49   return SCM_UNSPECIFIED;
50 }
51
52 LY_DEFINE (ly_programming_error, "ly:programming-error",
53            1, 0, 1, (SCM str, SCM rest),
54            "Scheme callable function to issue the warning @code{msg}. "
55            "The message is formatted with @code{format} and @code{rest}.")
56 {
57   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
58   progress_indication ("\n");
59
60   str = scm_simple_format (SCM_BOOL_F, str, rest);
61   programming_error (ly_scm2string (str));
62   return SCM_UNSPECIFIED;
63 }
64
65 LY_DEFINE (ly_dir_p, "ly:dir?",
66            1, 0, 0, (SCM s),
67           "type predicate. A direction is @code{-1}, @code{0} or "
68            "@code{1}, where @code{-1} represents "
69           "left or down and @code{1} represents right or up.")
70 {
71   if (scm_is_number (s))
72     {
73       int i = scm_to_int (s);
74       return (i>= -1 && i <= 1)  ? SCM_BOOL_T : SCM_BOOL_F; 
75     }
76   return SCM_BOOL_F;
77 }
78
79 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
80            2, 1, 0,
81            (SCM key, SCM alist, SCM default_value),
82            "Return value if KEY in ALIST, else DEFAULT-VALUE "
83            "(or #f if not specified).")
84 {
85   SCM handle = scm_assoc (key, alist);
86
87   if (default_value == SCM_UNDEFINED)
88     default_value = SCM_BOOL_F;
89   
90   if (scm_is_pair (handle))
91     return scm_cdr (handle);
92   else
93     return default_value;
94 }
95
96
97 LY_DEFINE (ly_number2string, "ly:number->string",
98            1, 0, 0, (SCM s),
99            "Convert @var{num} to a string without generating many decimals.")
100 {
101   SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
102
103   char str[400];                        // ugh.
104
105   if (scm_exact_p (s) == SCM_BOOL_F)
106     {
107       Real r (scm_to_double (s));
108 #ifdef __APPLE__
109       if (my_isinf (r) || my_isnan (r))
110 #else
111       if (isinf (r) || isnan (r))
112 #endif
113         {
114           programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
115           r = 0.0;
116         }
117
118       sprintf (str, "%08.4f", r);
119     }
120   else
121     sprintf (str, "%d", scm_to_int (s));
122
123   return scm_makfrom0str (str);
124 }
125
126
127
128 LY_DEFINE (ly_version,  "ly:version", 0, 0, 0, (),
129           "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
130 {
131   char const* vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " "  PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
132   
133   return scm_c_eval_string ((char*)vs);
134 }
135
136 LY_DEFINE (ly_unit,  "ly:unit", 0, 0, 0, (),
137           "Return the unit used for lengths as a string.")
138 {
139   return scm_makfrom0str (INTERNAL_UNIT);
140 }
141
142
143
144 LY_DEFINE (ly_dimension_p,  "ly:dimension?", 1, 0, 0, (SCM d),
145           "Return @var{d} is a number. Used to distinguish length "
146           "variables from normal numbers.")
147 {
148   return scm_number_p (d);
149 }
150
151 /*
152   Debugging mem leaks:
153  */
154 LY_DEFINE (ly_protects, "ly:protects",
155            0, 0, 0, (),
156           "Return hash of protected objects.")
157 {
158   return scm_protects;
159 }
160
161 LY_DEFINE (ly_gettext, "ly:gettext",
162            1, 0, 0, (SCM string),
163            "Gettext wrapper.")
164 {
165   SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
166                    __FUNCTION__, "string");
167   return scm_makfrom0str (gettext (scm_i_string_chars (string)));
168 }
169
170
171
172
173 LY_DEFINE (ly_output_backend, "ly:output-backend",
174            0, 0, 0, (),
175            "Return name of output backend.")
176 {
177   return scm_makfrom0str (output_backend_global.to_str0 ());
178 }
179
180
181 LY_DEFINE (ly_output_formats, "ly:output-formats",
182            0, 0, 0, (),
183            "Formats passed to --format as a list of strings, "
184            "used for the output.")
185 {
186   Array<String> output_formats = split_string (output_format_global, ',');
187
188   SCM lst = SCM_EOL;
189   int output_formats_count = output_formats.size ();
190   for (int i = 0; i < output_formats_count; i ++)
191     lst = scm_cons (scm_makfrom0str (output_formats[i].to_str0 ()), lst);
192
193   return lst;
194 }