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"
26 ly_str02scm (char const*c)
28 // this all really sucks, guile should take char const* arguments!
29 return gh_str02scm ((char*)c);
35 this all really sucks, LilyPond should not communicate with GUILE using strings.
38 ly_eval_str (String s);
43 Pass string to scm parser, evaluate one expression.
44 Return result value and #chars read.
46 Thanks to Gary Houston <ghouston@freewire.co.uk>
48 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
51 ly_parse_scm (char const* s, int* n)
53 SCM str = gh_str02scm ((char*)s);
54 SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
56 SCM from = scm_ftell (port);
59 SCM answer = SCM_UNSPECIFIED;
61 /* Read expression from port */
62 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
63 answer = scm_eval_x (form);
70 all seems fine, but after parsing
74 read_buf has been advanced to read_pos - 1,
75 so that scm_ftell returns 1, instead of #parsed chars
79 urg: reset read_buf for scm_ftell
80 shouldn't scm_read () do this for us?
82 scm_fill_input (port);
83 SCM to = scm_ftell (port);
84 *n = gh_scm2int (to) - gh_scm2int (from);
86 /* Don't close the port here; if we re-enter this function via a
87 continuation, then the next time we enter it, we'll get an error.
88 It's a string port anyway, so there's no advantage to closing it
91 scm_close_port (port);
100 return gh_list (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
105 ly_symbol2scm(const char *s)
107 return gh_symbol2scm ((char *)s);
111 ly_symbol2string (SCM s)
113 assert (gh_symbol_p (s));
114 return String((Byte*)SCM_CHARS (s), (int) SCM_LENGTH(s));
119 Read a file, and shove it down GUILE. GUILE also has file read
120 functions, but you can't fiddle with the path of those.
123 read_lily_scm_file (String fn)
125 String s = global_path.find (fn);
128 String e = _f ("can't find file: `%s'", fn);
130 e += _f ("(load path: `%s')", global_path.str ());
134 progress_indication ("[" + s);
137 Simple_file_storage f(s);
139 gh_eval_str ((char *) f.ch_C());
140 progress_indication ("]");
145 ly_gulp_file (SCM name)
147 String fn (ly_scm2string (name));
148 String s = global_path.find (fn);
151 String e = _f ("can't find file: `%s'", fn);
153 e += _f ("(load path: `%s')", global_path.str ());
157 progress_indication ("[" + s );
160 Simple_file_storage f(s);
161 SCM result = ly_str02scm (f.ch_C());
162 progress_indication ("]");
167 ly_display_scm (SCM s)
174 ly_scm2string (SCM s)
176 assert (gh_string_p (s));
178 char * p = gh_scm2newstr (s , &len);
187 index_cell (SCM s, Direction d)
190 return (d == LEFT) ? gh_car (s) : gh_cdr (s);
194 index_set_cell (SCM s, Direction d, SCM v)
206 assert (gh_string_p (str));
207 warning ("lily-guile: " + ly_scm2string (str));
216 int i = gh_scm2int (s);
217 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
224 typedef void (*Void_fptr)();
225 Array<Void_fptr> *scm_init_funcs_;
227 void add_scm_init_func (void (*f)())
229 if (!scm_init_funcs_)
230 scm_init_funcs_ = new Array<Void_fptr>;
232 scm_init_funcs_->push (f);
238 for (int i=scm_init_funcs_->size() ; i--;)
239 (scm_init_funcs_->elem (i)) ();
242 unsigned int ly_scm_hash (SCM s)
244 return scm_ihashv (s, ~1u);
254 int i = gh_scm2int (s);
255 return i>= -1 && i <= 1;
263 return (Direction) gh_scm2int (s);
272 return gh_boolean_p (s) && gh_scm2bool (s);
276 Appendable list L: the cdr contains the list, the car the last cons
282 SCM s = gh_cons (SCM_EOL, SCM_EOL);
289 appendable_list_append (SCM l, SCM elt)
291 SCM newcons = gh_cons (elt, SCM_EOL);
293 gh_set_cdr_x (gh_car (l), newcons);
294 gh_set_car_x (l, newcons);
299 ly_offset2scm (Offset o)
301 return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]));
305 ly_scm2offset (SCM s)
307 return Offset (gh_scm2double (gh_car (s)),
308 gh_scm2double (gh_cdr (s)));
314 char const * cp = "unknown";
315 if (gh_number_p (exp))
319 else if (gh_string_p (exp))
323 else if (gh_procedure_p (exp))
327 else if (gh_boolean_p (exp))
331 else if (gh_pair_p (exp))
336 return ly_str02scm (cp);
340 convert without too many decimals, and leave a space at the end.
345 ly_number2string (SCM s)
347 assert (gh_number_p (s));
349 char str[100]; // ugh.
351 if (scm_integer_p (s))
353 Real r (gh_scm2double (s));
355 if (isinf (r) || isnan (r))
357 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
361 sprintf (str, "%8.4f ", r);
365 sprintf (str, "%d ", gh_scm2int (s));
368 return gh_str02scm (str);
374 #include <libguile/gc.h>
377 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
379 fprintf(stderr, "entering sweep\n");
383 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
385 fprintf(stderr, "leaving sweep\n");
392 scm_make_gsubr ("ly-warn", 1, 0, 0, (SCM(*)(...))ly_warning);
393 scm_make_gsubr ("ly-gulp-file", 1,0, 0, (SCM(*)(...))ly_gulp_file);
394 scm_make_gsubr ("dir?", 1,0, 0, (SCM(*)(...))ly_isdir_p);
395 scm_make_gsubr ("ly-number->string", 1, 0,0, (SCM(*)(...)) ly_number2string);
399 scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
400 scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
405 ADD_SCM_INIT_FUNC(funcs, init_functions);