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"
27 ly_str02scm (char const*c)
29 // this all really sucks, guile should take char const* arguments!
30 return gh_str02scm ((char*)c);
36 this all really sucks, LilyPond should not communicate with GUILE using strings.
39 ly_eval_str (String s);
44 Pass string to scm parser, evaluate one expression.
45 Return result value and #chars read.
47 Thanks to Gary Houston <ghouston@freewire.co.uk>
49 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
52 ly_parse_scm (char const* s, int* n)
54 SCM str = gh_str02scm ((char*)s);
55 SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
57 SCM from = scm_ftell (port);
60 SCM answer = SCM_UNSPECIFIED;
62 /* Read expression from port */
63 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
64 answer = scm_eval_x (form);
71 all seems fine, but after parsing
75 read_buf has been advanced to read_pos - 1,
76 so that scm_ftell returns 1, instead of #parsed chars
80 urg: reset read_buf for scm_ftell
81 shouldn't scm_read () do this for us?
83 scm_fill_input (port);
84 SCM to = scm_ftell (port);
85 *n = gh_scm2int (to) - gh_scm2int (from);
87 /* Don't close the port here; if we re-enter this function via a
88 continuation, then the next time we enter it, we'll get an error.
89 It's a string port anyway, so there's no advantage to closing it
92 scm_close_port (port);
101 return gh_list (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
106 ly_symbol2scm(const char *s)
108 return gh_symbol2scm ((char *)s);
112 ly_symbol2string (SCM s)
114 assert (gh_symbol_p (s));
115 return String((Byte*)SCM_CHARS (s), (int) SCM_LENGTH(s));
120 Read a file, and shove it down GUILE. GUILE also has file read
121 functions, but you can't fiddle with the path of those.
124 read_lily_scm_file (String fn)
126 String s = global_path.find (fn);
129 String e = _f ("can't find file: `%s'", fn);
131 e += _f ("(load path: `%s')", global_path.str ());
135 progress_indication ("[" + s);
138 Simple_file_storage f(s);
140 gh_eval_str ((char *) f.ch_C());
141 progress_indication ("]");
146 ly_gulp_file (SCM name)
148 String fn (ly_scm2string (name));
149 String s = global_path.find (fn);
152 String e = _f ("can't find file: `%s'", fn);
154 e += _f ("(load path: `%s')", global_path.str ());
158 progress_indication ("[" + s );
161 Simple_file_storage f(s);
162 SCM result = ly_str02scm (f.ch_C());
163 progress_indication ("]");
168 ly_display_scm (SCM s)
175 ly_scm2string (SCM s)
177 assert (gh_string_p (s));
179 char * p = gh_scm2newstr (s , &len);
188 index_cell (SCM s, Direction d)
191 return (d == LEFT) ? gh_car (s) : gh_cdr (s);
195 index_set_cell (SCM s, Direction d, SCM v)
207 assert (gh_string_p (str));
208 warning ("lily-guile: " + ly_scm2string (str));
217 int i = gh_scm2int (s);
218 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
225 typedef void (*Void_fptr)();
226 Array<Void_fptr> *scm_init_funcs_;
228 void add_scm_init_func (void (*f)())
230 if (!scm_init_funcs_)
231 scm_init_funcs_ = new Array<Void_fptr>;
233 scm_init_funcs_->push (f);
239 for (int i=scm_init_funcs_->size() ; i--;)
240 (scm_init_funcs_->elem (i)) ();
243 unsigned int ly_scm_hash (SCM s)
245 return scm_ihashv (s, ~1u);
255 int i = gh_scm2int (s);
256 return i>= -1 && i <= 1;
264 return (Direction) gh_scm2int (s);
268 ly_scm2interval (SCM p)
270 return Interval (gh_scm2double (gh_car (p)),
271 gh_scm2double (gh_cdr (p)));
275 ly_interval2scm (Interval i)
277 return gh_cons (gh_double2scm (i[LEFT]),
278 gh_double2scm (i[RIGHT]));
285 return gh_boolean_p (s) && gh_scm2bool (s);
289 Appendable list L: the cdr contains the list, the car the last cons
295 SCM s = gh_cons (SCM_EOL, SCM_EOL);
302 appendable_list_append (SCM l, SCM elt)
304 SCM newcons = gh_cons (elt, SCM_EOL);
306 gh_set_cdr_x (gh_car (l), newcons);
307 gh_set_car_x (l, newcons);
312 ly_offset2scm (Offset o)
314 return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]));
318 ly_scm2offset (SCM s)
320 return Offset (gh_scm2double (gh_car (s)),
321 gh_scm2double (gh_cdr (s)));
327 char const * cp = "unknown";
328 if (gh_number_p (exp))
332 else if (gh_string_p (exp))
336 else if (gh_procedure_p (exp))
340 else if (gh_boolean_p (exp))
344 else if (gh_pair_p (exp))
349 return ly_str02scm (cp);
353 convert without too many decimals, and leave a space at the end.
358 ly_number2string (SCM s)
360 assert (gh_number_p (s));
362 char str[100]; // ugh.
364 if (scm_integer_p (s))
366 Real r (gh_scm2double (s));
368 if (isinf (r) || isnan (r))
370 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
374 sprintf (str, "%8.4f ", r);
378 sprintf (str, "%d ", gh_scm2int (s));
381 return gh_str02scm (str);
387 #include <libguile/gc.h>
390 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
392 fprintf(stderr, "entering sweep\n");
396 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
398 fprintf(stderr, "leaving sweep\n");
405 scm_make_gsubr ("ly-warn", 1, 0, 0, (SCM(*)(...))ly_warning);
406 scm_make_gsubr ("ly-gulp-file", 1,0, 0, (SCM(*)(...))ly_gulp_file);
407 scm_make_gsubr ("dir?", 1,0, 0, (SCM(*)(...))ly_isdir_p);
408 scm_make_gsubr ("ly-number->string", 1, 0,0, (SCM(*)(...)) ly_number2string);
412 scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
413 scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
418 ADD_SCM_INIT_FUNC(funcs, init_functions);