2 lily-guile.cc -- implement assorted guile functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2000 Jan Nieuwenhuizen <janneke@gnu.org>
8 Han-Wen Nienhuys <hanwen@cs.uu.nl>
14 #include <math.h> // isinf
16 #include "libc-extension.hh"
17 #include "lily-guile.hh"
19 #include "simple-file-storage.hh"
20 #include "file-path.hh"
22 #include "direction.hh"
24 #include "interval.hh"
29 return gh_car (scm_last_pair (list));
33 ly_str02scm (char const*c)
35 // this all really sucks, guile should take char const* arguments!
36 return gh_str02scm ((char*)c);
40 Pass string to scm parser, evaluate one expression.
41 Return result value and #chars read.
43 Thanks to Gary Houston <ghouston@freewire.co.uk>
45 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
48 ly_parse_scm (char const* s, int* n)
50 SCM str = ly_str02scm (s);
51 SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
53 SCM from = scm_ftell (port);
56 SCM answer = SCM_UNSPECIFIED;
58 /* Read expression from port */
59 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
60 answer = scm_eval_3 (form, 1, SCM_EOL); // guh?
67 all seems fine, but after parsing
71 read_buf has been advanced to read_pos - 1,
72 so that scm_ftell returns 1, instead of #parsed chars
76 urg: reset read_buf for scm_ftell
77 shouldn't scm_read () do this for us?
79 scm_fill_input (port);
80 SCM to = scm_ftell (port);
81 *n = gh_scm2int (to) - gh_scm2int (from);
83 /* Don't close the port here; if we re-enter this function via a
84 continuation, then the next time we enter it, we'll get an error.
85 It's a string port anyway, so there's no advantage to closing it
88 scm_close_port (port);
97 return gh_list (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
102 ly_symbol2scm(const char *s)
104 return gh_symbol2scm ((char *)s);
109 ly_symbol2string (SCM s)
111 assert (gh_symbol_p (s));
112 return String((Byte*)SCM_CHARS (s), (int) SCM_LENGTH(s));
117 gulp_file_to_string (String fn)
119 String s = global_path.find (fn);
122 String e = _f ("can't find file: `%s'", fn);
124 e += _f ("(load path: `%s')", global_path.str ());
127 else if (verbose_global_b)
128 progress_indication ("[" + s );
131 Simple_file_storage f(s);
132 String result (f.ch_C());
133 if (verbose_global_b)
134 progress_indication ("]");
139 ly_gulp_file (SCM fn)
141 return ly_str02scm (gulp_file_to_string (ly_scm2string (fn)).ch_C());
146 Read a file, and shove it down GUILE. GUILE also has file read
147 functions, but you can't fiddle with the path of those.
150 read_lily_scm_file (String fn)
152 gh_eval_str ((char *) gulp_file_to_string (fn).ch_C());
156 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
158 ly_display_scm (SCM s)
166 ly_scm2string (SCM s)
168 assert (gh_string_p (s));
170 char * p = gh_scm2newstr (s , &len);
179 index_cell (SCM s, Direction d)
182 return (d == LEFT) ? gh_car (s) : gh_cdr (s);
186 index_set_cell (SCM s, Direction d, SCM v)
198 assert (gh_string_p (str));
199 warning ("lily-guile: " + ly_scm2string (str));
208 int i = gh_scm2int (s);
209 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
216 typedef void (*Void_fptr)();
217 Array<Void_fptr> *scm_init_funcs_;
219 void add_scm_init_func (void (*f)())
221 if (!scm_init_funcs_)
222 scm_init_funcs_ = new Array<Void_fptr>;
224 scm_init_funcs_->push (f);
226 extern void init_cxx_function_smobs();
231 init_cxx_function_smobs();
232 for (int i=scm_init_funcs_->size() ; i--;)
233 (scm_init_funcs_->elem (i)) ();
236 unsigned int ly_scm_hash (SCM s)
238 return scm_ihashv (s, ~1u);
248 int i = gh_scm2int (s);
249 return i>= -1 && i <= 1;
257 return (Direction) gh_scm2int (s);
261 ly_scm2interval (SCM p)
263 return Interval (gh_scm2double (gh_car (p)),
264 gh_scm2double (gh_cdr (p)));
268 ly_interval2scm (Interval i)
270 return gh_cons (gh_double2scm (i[LEFT]),
271 gh_double2scm (i[RIGHT]));
278 return gh_boolean_p (s) && gh_scm2bool (s);
282 Appendable list L: the cdr contains the list, the car the last cons
288 SCM s = gh_cons (SCM_EOL, SCM_EOL);
295 appendable_list_append (SCM l, SCM elt)
297 SCM newcons = gh_cons (elt, SCM_EOL);
299 gh_set_cdr_x (gh_car (l), newcons);
300 gh_set_car_x (l, newcons);
305 ly_offset2scm (Offset o)
307 return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]));
311 ly_scm2offset (SCM s)
313 return Offset (gh_scm2double (gh_car (s)),
314 gh_scm2double (gh_cdr (s)));
320 char const * cp = "unknown";
321 if (gh_number_p (exp))
325 else if (gh_string_p (exp))
329 else if (gh_procedure_p (exp))
333 else if (gh_boolean_p (exp))
337 else if (gh_pair_p (exp))
342 return ly_str02scm (cp);
346 convert without too many decimals, and leave a space at the end.
351 ly_number2string (SCM s)
353 assert (gh_number_p (s));
355 char str[400]; // ugh.
357 if (scm_integer_p (s) == SCM_BOOL_F)
359 Real r (gh_scm2double (s));
361 if (isinf (r) || isnan (r))
363 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
367 sprintf (str, "%8.4f ", r);
371 sprintf (str, "%d ", gh_scm2int (s));
374 return ly_str02scm (str);
378 Undef this to see if GUILE GC is causing too many swaps.
384 #include <libguile/gc.h>
387 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
389 fprintf(stderr, "entering sweep\n");
393 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
395 fprintf(stderr, "leaving sweep\n");
400 #include "version.hh"
404 char const* vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
407 return gh_eval_str ((char*)vs);
413 scm_make_gsubr ("ly-warn", 1, 0, 0, (Scheme_function_unknown)ly_warning);
414 scm_make_gsubr ("ly-version", 0, 0, 0, (Scheme_function_unknown)ly_warning);
415 scm_make_gsubr ("ly-gulp-file", 1,0, 0, (Scheme_function_unknown)ly_gulp_file);
416 scm_make_gsubr ("dir?", 1,0, 0, (Scheme_function_unknown)ly_isdir_p);
418 scm_make_gsubr ("ly-number->string", 1, 0,0, (Scheme_function_unknown) ly_number2string);
422 scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
423 scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
427 ADD_SCM_INIT_FUNC(funcs, init_functions);
434 return gh_cons (ly_deep_copy (gh_car (l)), ly_deep_copy (gh_cdr (l)));
444 ly_assoc_chain (SCM key, SCM achain)
446 if (gh_pair_p (achain))
448 SCM handle = scm_assoc (key, gh_car (achain));
449 if (gh_pair_p (handle))
452 return ly_assoc_chain (key, gh_cdr (achain));