]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/lily-guile.hh
* configure.in: Test for and accept lmodern if EC fonts not found.
[lilypond.git] / lily / include / lily-guile.hh
1 /*
2   lily-guile.hh encapsulate guile
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #ifndef LILY_GUILE_HH
10 #define LILY_GUILE_HH
11
12 #include <libguile.h>
13
14 #include "interval.hh"
15 #include "guile-compatibility.hh"
16
17 #ifndef SMOB_FREE_RETURN_VAL
18 #define SMOB_FREE_RETURN_VAL(CL) 0
19 #endif
20
21 #ifndef SCM_PACK
22 #define SCM_PACK(x) ((SCM) x)
23 #endif
24
25 #ifndef SCM_UNPACK
26 #define SCM_UNPACK(x) (x)
27 #endif
28
29 /** Conversion functions follow the GUILE naming convention, i.e.
30     A ly_B2A (B b);  */
31
32 SCM ly_last (SCM list);
33 SCM ly_write2scm (SCM s);
34 SCM ly_deep_copy (SCM);
35 SCM ly_truncate_list (int k, SCM lst);
36
37 SCM ly_to_string (SCM scm);
38 SCM ly_to_symbol (SCM scm);
39
40 #if (__GNUC__ > 2)
41 /* Unreliable with gcc-2.x
42    FIXME: should add check for x86 as well?  */
43 #define CACHE_SYMBOLS
44 #endif
45
46 #ifdef CACHE_SYMBOLS
47
48 /* Using this trick we cache the value of scm_str2symbol ("fooo") where
49   "fooo" is a constant string. This is done at the cost of one static
50   variable per ly_symbol2scm() use, and one boolean evaluation for
51   every call.
52
53   The overall speedup of lily is about 5% on a run of wtk1-fugue2.  */
54 #define ly_symbol2scm(x) \
55 ({ \
56   static SCM cached; \
57   /* We store this one locally, since G++ -O2 fucks up else */ \
58   SCM value = cached; \
59   if ( __builtin_constant_p ((x))) \
60     { \
61       if (!cached) \
62         value = cached =  scm_gc_protect_object (scm_str2symbol ((x))); \
63     } \
64   else \
65     value = scm_str2symbol ((char*) (x)); \
66   value; \
67 })
68 #else
69 inline SCM ly_symbol2scm(char const* x) { return scm_str2symbol ((x)); }
70 #endif
71
72 extern SCM global_lily_module;
73
74 /*
75   TODO: rename me to ly_c_lily_module_eval
76  */
77 #define ly_scheme_function(x) \
78 ({ \
79   static SCM cached; \
80   /* We store this one locally, since G++ -O2 fucks up else */ \
81   SCM value = cached; \
82   if ( __builtin_constant_p ((x))) \
83     { \
84       if (!cached) \
85         value = cached = scm_gc_protect_object (scm_eval (scm_str2symbol (x), \
86                                                 global_lily_module)); \
87     } \
88   else \
89     value = scm_eval (scm_str2symbol (x), global_lily_module); \
90   value; \
91 })
92
93 String gulp_file_to_string (String fn, bool must_exist);
94
95 String ly_scm2string (SCM s);
96 String ly_symbol2string (SCM);
97 SCM ly_offset2scm (Offset);
98 Offset ly_scm2offset (SCM);
99 SCM ly_assoc_chain (SCM key, SCM achain);
100 SCM ly_assoc_cdr (SCM key, SCM alist);
101 SCM ly_assoc_get (SCM key, SCM alist, SCM def);
102 Interval ly_scm2interval (SCM);
103 Drul_array<Real> ly_scm2realdrul (SCM);
104 Slice int_list_to_slice (SCM l);
105 SCM ly_interval2scm (Drul_array<Real>);
106 char *ly_scm2newstr (SCM str, size_t *lenp);
107
108 Real robust_scm2double (SCM, double);
109 int robust_scm2int (SCM, int);
110 Drul_array<Real> robust_scm2drul (SCM, Drul_array<Real>);
111 Interval robust_scm2interval (SCM, Drul_array<Real>);
112 Offset robust_scm2offset (SCM, Offset);
113
114 SCM ly_quote_scm (SCM s);
115 bool type_check_assignment (SCM val, SCM sym,  SCM type_symbol) ;
116 String print_scm_val (SCM val);
117 SCM ly_number2string (SCM s);
118
119 SCM parse_symbol_list (char const *);
120 SCM robust_list_ref(int i, SCM l);
121 SCM alist_to_hashq (SCM);
122
123
124 /* inserts at front, removing dublicates */
125 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
126 {
127   return scm_acons(key, val, scm_assoc_remove_x (alist, key));
128 }
129 inline bool ly_c_char_p (SCM x) { return SCM_CHARP (x); }
130 inline bool ly_c_vector_p (SCM x) { return SCM_VECTORP (x); }
131 inline bool ly_c_list_p (SCM x) { return SCM_NFALSEP (scm_list_p (x)); }
132 inline bool ly_c_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p (x)); }
133 inline bool ly_c_equal_p (SCM x, SCM y) {
134   return SCM_NFALSEP (scm_equal_p (x, y));
135 }
136
137
138 inline bool ly_scm2bool (SCM x) { return SCM_NFALSEP (x); }
139 inline char ly_scm2char (SCM x) { return SCM_CHAR(x); }
140 inline unsigned long ly_length (SCM x) {
141   return scm_num2ulong (scm_length (x), 0, "ly_length");
142 }
143 inline unsigned long ly_vector_length (SCM x) { return SCM_VECTOR_LENGTH (x); }
144
145 inline SCM ly_bool2scm (bool x) { return SCM_BOOL (x); }
146
147 inline SCM ly_append2 (SCM x1, SCM x2) {
148   return scm_append (scm_listify (x1, x2, SCM_UNDEFINED));
149 }
150 inline SCM ly_append3 (SCM x1, SCM x2, SCM x3) {
151   return scm_append (scm_listify (x1, x2, x3, SCM_UNDEFINED));
152 }
153 inline SCM ly_append4 (SCM x1, SCM x2, SCM x3, SCM x4) {
154   return scm_append (scm_listify (x1, x2, x3, x4, SCM_UNDEFINED));
155 }
156
157 /*
158   display and print newline.
159  */
160 extern "C" {
161 void ly_display_scm (SCM s);
162 }
163
164 void read_lily_scm_file (String);
165 void ly_c_init_guile ();
166
167 bool is_direction (SCM s);
168 bool is_number_pair (SCM);
169 bool is_axis (SCM);
170
171 /*
172   these conversion functions also do a typecheck on the argument, and
173   return a default value if S has the wrong type.
174 */
175
176 Direction to_dir (SCM s);
177 bool to_boolean (SCM s);
178
179 void init_ly_protection ();
180 unsigned int ly_scm_hash (SCM s);
181
182 SCM index_get_cell (SCM cell, Direction d);
183 SCM index_set_cell (SCM cell, Direction d, SCM val);
184
185 SCM ly_snoc (SCM s, SCM list);
186 SCM ly_split_list (SCM s, SCM lst);
187 SCM ly_unique (SCM lst);
188 SCM ly_list_qsort_uniq_x (SCM lst);
189
190 SCM ly_output_formats();
191 SCM ly_kpathsea_find_file (SCM);
192
193 /*
194   snarfing.
195  */
196 void add_scm_init_func (void (*) ());
197
198 extern "C" {
199 typedef SCM (*Scheme_function_unknown) ();
200 }
201
202 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
203 typedef SCM (*Scheme_function_0) ();
204 typedef SCM (*Scheme_function_1) (SCM);
205 typedef SCM (*Scheme_function_2) (SCM,SCM);     
206 typedef SCM (*Scheme_function_3) (SCM,SCM, SCM);        
207 #else
208 typedef SCM (*Scheme_function_0) (...);
209 typedef SCM (*Scheme_function_1) (...);
210 typedef SCM (*Scheme_function_2) (...);
211 typedef SCM (*Scheme_function_3) (...);
212 #endif
213
214
215 /*
216   Adds the NAME as a Scheme function, and a variable to store the SCM
217   version of the function in the static variable NAME_proc
218  */
219 #define DECLARE_SCHEME_CALLBACK(NAME, ARGS) \
220         static SCM NAME ARGS; \
221         static SCM NAME ## _proc
222
223 /*
224   Make TYPE::FUNC available as a Scheme function.
225  */
226 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
227 SCM TYPE :: FUNC ## _proc; \
228 void \
229 TYPE ## _ ## FUNC ## _init_functions () \
230 { \
231   TYPE :: FUNC ## _proc = scm_c_define_gsubr (#TYPE "::" #FUNC, \
232                                               (ARGCOUNT), 0, 0, \
233                           (Scheme_function_unknown)TYPE :: FUNC); \
234   scm_c_export (#TYPE "::" #FUNC, NULL); \
235 } \
236  \
237 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, \
238                    TYPE ## _ ## FUNC ## _init_functions);
239
240
241 void
242 ly_add_function_documentation (SCM proc, char const *fname,
243                                char const *varlist,
244                                char const *doc);
245
246 #define ADD_SCM_INIT_FUNC(name, func) \
247 class name ## _scm_initter \
248 { \
249 public: \
250   name ## _scm_initter () \
251   { \
252     add_scm_init_func (func); \
253   } \
254 } _ ## name ## _scm_initter; \
255 /* end define */
256
257 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, \
258                                ARGLIST, DOCSTRING) \
259 SCM FNAME ## _proc; \
260 void \
261 INITPREFIX ## init () \
262 { \
263   FNAME ## _proc = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, \
264                                        (Scheme_function_unknown) FNAME); \
265   ly_add_function_documentation (FNAME ## _proc, PRIMNAME, #ARGLIST, \
266                                  DOCSTRING); \
267   scm_c_export (PRIMNAME, NULL); \
268 } \
269 ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init); \
270 SCM \
271 FNAME ARGLIST
272
273
274 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
275 SCM FNAME ARGLIST; \
276 LY_DEFINE_WITHOUT_DECL (FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, \
277                         DOCSTRING)
278
279 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, \
280                                   ARGLIST, DOCSTRING) \
281 SCM FNAME ARGLIST; \
282 LY_DEFINE_WITHOUT_DECL (CLASS ## FNAME, CLASS::FNAME, PRIMNAME, REQ, OPT, \
283                         VAR, ARGLIST, DOCSTRING)
284
285 #define get_property(x) internal_get_property (ly_symbol2scm (x))
286 #define set_property(x, y) internal_set_property (ly_symbol2scm (x), y)
287
288 #endif /* LILY_GUILE_HH */