]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/lily-guile.hh
* lily/text-item.cc (interpret_string): new file, select font with
[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
13 /* GUILE only includes version in headers (libguile/version.h) as of
14    1.5.x.  For some strange reason, they call it SCM.*VERSION.
15
16    Not including config.h here, saves a lot of unnecessary
17    recompiles. */
18 #include <libguile.h>
19
20 #ifndef GUILE_MAJOR_VERSION
21 #ifdef SCM_MAJOR_VERSION
22 #define GUILE_MAJOR_VERSION SCM_MAJOR_VERSION
23 #define GUILE_MINOR_VERSION SCM_MINOR_VERSION
24 #define GUILE_PATCH_LEVEL SCM_MICRO_VERSION
25 #else
26 #include "config.h"
27 #endif
28 #endif
29
30 #include "drul-array.hh"
31
32
33 /* Guile 1.4.x compatibility */
34 #if GUILE_MINOR_VERSION < 5
35
36 #define scm_t_bits scm_bits_t
37
38 #define fix_guile_1_4_scm_primitive_eval(form) scm_eval_3 (form, 1, SCM_EOL)
39 #define scm_primitive_eval(form) fix_guile_1_4_scm_primitive_eval (form)
40
41 #define scm_int2num(x) scm_long2num (x)  
42 #define scm_current_module() (SCM)0
43 #define scm_set_current_module(x) (void)x
44 #define scm_c_resolve_module(x) (SCM)0
45
46 inline SCM scm_c_make_vector  (int k, SCM val) {
47   return scm_make_vector (SCM_MAKINUM (k), val);
48 }
49 #define scm_c_define_gsubr scm_make_gsubr
50 #define scm_remember_upto_here_1(s) scm_remember (&s)
51 #define scm_gc_protect_object scm_protect_object
52 #define scm_gc_unprotect_object scm_unprotect_object
53 #define scm_list_n scm_listify
54 #define SCM_STRING_CHARS SCM_CHARS
55 #define SCM_STRING_LENGTH SCM_LENGTH
56 #define SCM_SYMBOL_CHARS SCM_CHARS
57 #define SCM_SYMBOL_LENGTH SCM_LENGTH
58 #define SCM_VECTOR_LENGTH SCM_LENGTH
59
60 #define SMOB_FREE_RETURN_VAL(CL) sizeof(CL) 
61 #define scm_done_free(x) 
62 #endif
63
64 #ifndef SMOB_FREE_RETURN_VAL
65 #define SMOB_FREE_RETURN_VAL(CL) 0
66 #endif
67
68
69 #if GUILE_MINOR_VERSION < 7
70 #define scm_gc_unregister_collectable_memory(a,b,c) scm_done_free(b)
71 #define scm_gc_register_collectable_memory(a,b,c) scm_done_malloc(b)
72 #define SCM_VECTOR_REF(v,i) (SCM_VELTS((v))[(i)])
73 #define scm_hash_table_p scm_vector_p 
74
75 #endif
76
77 #include "direction.hh"
78 #include "flower-proto.hh"
79
80 #ifndef SCM_PACK
81 #define SCM_PACK(x) ((SCM) x)
82
83 #endif
84 #ifndef SCM_UNPACK
85 #define SCM_UNPACK(x) ( x)
86 #endif
87
88 /*
89   conversion functions follow the GUILE naming convention, i.e.
90
91     A ly_B2A (B b);
92  */
93
94 SCM ly_last (SCM list);
95 SCM ly_write2scm (SCM s);
96 SCM ly_deep_copy (SCM);
97 SCM ly_truncate_list (int k, SCM l );
98
99
100 /*
101   Unreliable on gcc2
102  */
103 // #define CACHE_SYMBOLS
104
105
106 #if (__GNUC__ > 2)
107 /*
108   todo: should add check for x86 as well
109  */
110 #define CACHE_SYMBOLS
111 #endif
112
113
114 #ifdef CACHE_SYMBOLS
115
116
117 /*
118   Using this trick we cache the value of scm_str2symbol ("fooo") where
119   "fooo" is a constant string. This is done at the cost of one static
120   variable per ly_symbol2scm() use, and one boolean evaluation for
121   every call.
122
123   The overall speedup of lily is about 5% on a run of wtk1-fugue2
124
125 */
126 #define ly_symbol2scm(x) ({ static SCM cached;  \
127  SCM value = cached;  /* We store this one locally, since G++ -O2 fucks up else */   \
128  if ( __builtin_constant_p ((x)))\
129  {  if (!cached)\
130      value = cached =  scm_gc_protect_object (scm_str2symbol((x)));\
131  } else\
132   value = scm_str2symbol ((char*) (x)); \
133   value; })
134 #else
135 inline SCM ly_symbol2scm(char const* x) { return scm_str2symbol((x)); }
136 #endif 
137
138 extern SCM global_lily_module;
139
140 #define ly_scheme_function(x) ({static SCM cached; \
141  SCM value = cached;  /* We store this one locally, since G++ -O2 fucks up else */   \
142  if ( __builtin_constant_p ((x)))\
143  {  if (!cached)\
144      value = cached =  scm_gc_protect_object (scm_eval(scm_str2symbol (x), global_lily_module));\
145  } else\
146   value = scm_eval(scm_str2symbol (x), global_lily_module);\
147   value; })
148
149
150
151 String ly_scm2string (SCM s);
152 String ly_symbol2string (SCM);
153 SCM ly_offset2scm (Offset);
154 Offset ly_scm2offset (SCM);
155 SCM ly_assoc_chain (SCM key, SCM achain);
156 SCM ly_assoc_cdr (SCM key, SCM alist);
157 Interval ly_scm2interval (SCM);
158 Drul_array<Real> ly_scm2realdrul (SCM);
159 Slice int_list_to_slice (SCM l);
160 SCM ly_interval2scm (Drul_array<Real>);
161 char *ly_scm2newstr (SCM str, size_t *lenp);
162
163 Real robust_scm2double (SCM, double);
164 int robust_scm2int (SCM, int);
165 Drul_array<Real> robust_scm2drul (SCM, Drul_array<Real>);
166 Interval robust_scm2interval (SCM, Drul_array<Real>);
167 Offset robust_scm2offset (SCM, Offset);
168
169 SCM ly_quote_scm (SCM s);
170 bool type_check_assignment (SCM val, SCM sym,  SCM type_symbol) ;
171 String print_scm_val (SCM val);
172 SCM ly_number2string (SCM s);
173
174 SCM parse_symbol_list (char const *);
175 SCM robust_list_ref(int i, SCM l);
176 SCM alist_to_hashq (SCM);
177
178 inline SCM ly_cdr (SCM x) { return SCM_CDR (x); }
179 inline SCM ly_car (SCM x) { return SCM_CAR (x); } 
180 inline SCM ly_caar (SCM x) { return SCM_CAAR (x); }
181 inline SCM ly_cdar (SCM x) { return SCM_CDAR (x); }
182 inline SCM ly_cadr (SCM x) { return SCM_CADR (x); }
183 inline SCM ly_cddr (SCM x) { return SCM_CDDR (x); }
184 inline SCM ly_caddr (SCM x) { return SCM_CADDR (x); }
185 inline SCM ly_cdadr (SCM x) { return SCM_CDADR (x); }
186 inline SCM ly_caadr (SCM x) { return SCM_CAADR (x); }
187 inline SCM ly_cadar (SCM x) { return SCM_CADAR (x); }
188 /* inserts at front, removing dublicates */
189 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
190 {
191   return scm_acons(key, val, scm_assoc_remove_x (alist, key));
192 }
193 inline bool is_pair (SCM x) { return SCM_NFALSEP (scm_pair_p (x)); }
194 inline bool is_symbol (SCM x) { return SCM_SYMBOLP (x); }
195 inline bool is_boolean (SCM x) { return SCM_BOOLP (x); }
196 inline bool is_char (SCM x) { return SCM_CHARP (x); }
197 inline bool is_number (SCM x) { return SCM_NUMBERP (x); }
198 inline bool is_string (SCM x) { return SCM_STRINGP (x); }
199 inline bool is_vector (SCM x) { return SCM_VECTORP (x); }
200 inline bool is_list (SCM x) { return SCM_NFALSEP (scm_list_p (x)); }
201 inline bool is_procedure (SCM x) { return SCM_NFALSEP (scm_procedure_p (x)); }
202 inline bool is_eq (SCM x, SCM y) { return SCM_EQ_P (x, y); }
203 inline bool is_equal (SCM x, SCM y) { 
204   return SCM_NFALSEP (scm_equal_p (x, y)); 
205 }
206
207 inline bool ly_scm2bool (SCM x) { return SCM_NFALSEP (x); }
208 inline char ly_scm2char (SCM x) { return SCM_CHAR(x); }
209 inline int ly_scm2int (SCM x) { return scm_num2int (x, 0, "ly_scm2int"); }
210 inline double ly_scm2double (SCM x) { return scm_num2dbl (x, "ly_scm2double"); }
211 inline unsigned long ly_length (SCM x) { 
212   return scm_num2ulong (scm_length (x), 0, "ly_length");
213 }
214 inline unsigned long ly_vector_length (SCM x) { return SCM_VECTOR_LENGTH (x); }
215
216 inline SCM ly_bool2scm (bool x) { return SCM_BOOL (x); }
217
218 inline SCM ly_append2 (SCM x1, SCM x2) { 
219   return scm_append (scm_listify (x1, x2, SCM_UNDEFINED)); 
220 }
221 inline SCM ly_append3 (SCM x1, SCM x2, SCM x3) { 
222   return scm_append (scm_listify (x1, x2, x3, SCM_UNDEFINED)); 
223 }
224 inline SCM ly_append4 (SCM x1, SCM x2, SCM x3, SCM x4) { 
225   return scm_append (scm_listify (x1, x2, x3, x4, SCM_UNDEFINED)); 
226 }
227
228 /*
229   display and print newline.
230  */
231 extern "C" { 
232 void ly_display_scm (SCM s);
233 }
234
235 #include "array.hh"
236
237 void read_lily_scm_file (String);
238 void ly_init_guile ();
239
240 bool is_direction (SCM s);
241 bool is_number_pair (SCM);
242 bool is_axis (SCM);
243
244 /*
245   these conversion functions also do a typecheck on the argument, and
246   return a default value if S has the wrong type.
247 */
248
249 Direction to_dir (SCM s);
250 bool to_boolean (SCM s);
251
252 void init_ly_protection ();
253 unsigned int ly_scm_hash (SCM s);
254
255 SCM index_get_cell (SCM cell, Direction d);
256 SCM index_set_cell (SCM cell, Direction d, SCM val);
257
258 SCM ly_snoc (SCM s, SCM list);
259 SCM ly_split_list (SCM s, SCM list);
260 SCM ly_unique (SCM list);
261
262
263
264 /*
265   snarfing.
266  */
267 void add_scm_init_func (void (*) ());
268
269 extern "C" {
270 typedef SCM (*Scheme_function_unknown) ();
271 }
272
273 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
274 typedef SCM (*Scheme_function_0) ();
275 typedef SCM (*Scheme_function_1) (SCM);
276 typedef SCM (*Scheme_function_2) (SCM,SCM);      
277 typedef SCM (*Scheme_function_3) (SCM,SCM, SCM);         
278 #else
279 typedef SCM (*Scheme_function_0) (...);
280 typedef SCM (*Scheme_function_1) (...);
281 typedef SCM (*Scheme_function_2) (...);
282 typedef SCM (*Scheme_function_3) (...);
283 #endif
284
285
286 /*
287   Adds the NAME as a Scheme function, and a variable to store the SCM
288   version of the function in the static variable NAME_proc
289  */
290 #define DECLARE_SCHEME_CALLBACK(NAME,ARGS) \
291         static SCM NAME ARGS; \
292         static SCM NAME ## _proc
293
294 /*
295   Make TYPE::FUNC available as a Scheme function. 
296  */
297 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
298 SCM TYPE :: FUNC ## _proc;\
299 void                                                            \
300 TYPE ## _ ## FUNC ## _init_functions ()                                 \
301 {                                                               \
302   TYPE :: FUNC ## _proc = scm_c_define_gsubr (#TYPE "::" #FUNC, \
303                                               (ARGCOUNT), 0, 0, \
304                           (Scheme_function_unknown)TYPE :: FUNC); \
305   scm_c_export (#TYPE "::" #FUNC, NULL);\
306 }                                                               \
307                                                                 \
308 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, TYPE ## _ ## FUNC ## _init_functions);       \
309
310
311 void ly_add_function_documentation (SCM proc, char const * fname,
312                                     char const * varlist,
313                                     char const * doc);
314
315 #define ADD_SCM_INIT_FUNC(name, func)\
316 class name ## _scm_initter {                    \
317 public:\
318   name ## _scm_initter ()                       \
319   {                                             \
320     add_scm_init_func (func);           \
321   }                                             \
322 } _ ## name ## _scm_initter;                    \
323 /* end define */
324
325 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
326 SCM FNAME ## _proc;\
327 void \
328 INITPREFIX ## init ()\
329 {\
330  FNAME ## _proc \
331     = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, (Scheme_function_unknown) FNAME);\
332   ly_add_function_documentation ( FNAME ## _proc ,PRIMNAME, #ARGLIST,  DOCSTRING);\
333   scm_c_export (PRIMNAME,NULL);\
334 }\
335 ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init);\
336 SCM \
337 FNAME ARGLIST\
338
339
340 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
341 SCM FNAME ARGLIST ; \
342 LY_DEFINE_WITHOUT_DECL(FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) 
343
344
345 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
346 SCM FNAME ARGLIST ; \
347 LY_DEFINE_WITHOUT_DECL(CLASS ## FNAME,  CLASS::FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) 
348
349
350 #define get_property(x) internal_get_property(ly_symbol2scm(x))
351 #define set_property(x,y) internal_set_property(ly_symbol2scm (x), y)
352
353
354 #endif /* LILY_GUILE_HH */