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