2 lily-guile.cc -- implement assorted guile functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2001 Jan Nieuwenhuizen <janneke@gnu.org>
8 Han-Wen Nienhuys <hanwen@cs.uu.nl>
14 #include <math.h> /* isinf */
15 #include <string.h> /* strdup, strchr */
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 ly_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);
43 SCM port = scm_mkstrport (SCM_INUM0,
44 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
47 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
48 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
50 // scm_apply (write, port, SCM_EOL);
51 gh_call2 (write, s, port);
52 return scm_strport_to_string (port);
57 Pass string to scm parser, evaluate one expression.
58 Return result value and #chars read.
60 Thanks to Gary Houston <ghouston@freewire.co.uk>
62 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
65 ly_parse_scm (char const* s, int* n)
67 SCM str = ly_str02scm (s);
68 SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
70 SCM from = scm_ftell (port);
73 SCM answer = SCM_UNSPECIFIED;
75 /* Read expression from port */
76 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
77 answer = scm_primitive_eval (form);
84 all seems fine, but after parsing
88 read_buf has been advanced to read_pos - 1,
89 so that scm_ftell returns 1, instead of #parsed chars
93 urg: reset read_buf for scm_ftell
94 shouldn't scm_read () do this for us?
96 scm_fill_input (port);
97 SCM to = scm_ftell (port);
98 *n = gh_scm2int (to) - gh_scm2int (from);
100 /* Don't close the port here; if we re-enter this function via a
101 continuation, then the next time we enter it, we'll get an error.
102 It's a string port anyway, so there's no advantage to closing it
105 scm_close_port (port);
114 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
120 ly_symbol2string (SCM s)
122 assert (gh_symbol_p (s));
123 return String ((Byte*)SCM_STRING_CHARS (s), (int) SCM_STRING_LENGTH (s));
128 gulp_file_to_string (String fn)
130 String s = global_path.find (fn);
133 String e = _f ("can't find file: `%s'", fn);
135 e += _f ("(load path: `%s')", global_path.str ());
138 else if (verbose_global_b)
139 progress_indication ("[" + s);
142 Simple_file_storage f (s);
143 String result (f.ch_C ());
144 if (verbose_global_b)
145 progress_indication ("]");
150 ly_gulp_file (SCM fn)
152 return ly_str02scm (gulp_file_to_string (ly_scm2string (fn)).ch_C ());
157 Read a file, and shove it down GUILE. GUILE also has file read
158 functions, but you can't fiddle with the path of those.
161 read_lily_scm_file (String fn)
163 gh_eval_str ((char *) gulp_file_to_string (fn).ch_C ());
167 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
169 ly_display_scm (SCM s)
177 ly_scm2string (SCM s)
179 assert (gh_string_p (s));
182 char *p = gh_scm2newstr (s , &len);
191 index_cell (SCM s, Direction d)
194 return (d == LEFT) ? ly_car (s) : ly_cdr (s);
198 index_set_cell (SCM s, Direction d, SCM v)
210 assert (gh_string_p (str));
211 warning ("lily-guile: " + ly_scm2string (str));
220 int i = gh_scm2int (s);
221 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
227 ly_number_pair_p (SCM p)
229 return gh_pair_p (p) && gh_number_p (ly_car (p)) && gh_number_p (ly_cdr (p));
235 return gh_number_p (a) && (gh_scm2int (a) == 0 || gh_scm2int (a) == 1);
238 typedef void (*Void_fptr) ();
239 Array<Void_fptr> *scm_init_funcs_;
241 void add_scm_init_func (void (*f) ())
243 if (!scm_init_funcs_)
244 scm_init_funcs_ = new Array<Void_fptr>;
246 scm_init_funcs_->push (f);
249 extern void init_cxx_function_smobs ();
254 SCM last_mod = scm_current_module ();
255 scm_set_current_module (scm_c_resolve_module ("guile"));
257 init_cxx_function_smobs ();
258 for (int i=scm_init_funcs_->size () ; i--;)
259 (scm_init_funcs_->elem (i)) ();
261 if (verbose_global_b)
262 progress_indication ("\n");
263 read_lily_scm_file ("lily.scm");
264 scm_set_current_module (last_mod);
267 unsigned int ly_scm_hash (SCM s)
269 return scm_ihashv (s, ~1u);
279 int i = gh_scm2int (s);
280 return i>= -1 && i <= 1;
291 int i = gh_scm2int (s);
292 return i== 0 || i == 1;
301 return (Direction) gh_scm2int (s);
305 ly_scm2interval (SCM p)
307 return Interval (gh_scm2double (ly_car (p)),
308 gh_scm2double (ly_cdr (p)));
312 ly_interval2scm (Interval i)
314 return gh_cons (gh_double2scm (i[LEFT]),
315 gh_double2scm (i[RIGHT]));
324 return gh_boolean_p (s) && gh_scm2bool (s);
328 Appendable list L: the cdr contains the list, the car the last cons
334 SCM s = gh_cons (SCM_EOL, SCM_EOL);
341 appendable_list_append (SCM l, SCM elt)
343 SCM newcons = gh_cons (elt, SCM_EOL);
345 gh_set_cdr_x (ly_car (l), newcons);
346 gh_set_car_x (l, newcons);
351 ly_offset2scm (Offset o)
353 return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm (o[Y_AXIS]));
357 ly_scm2offset (SCM s)
359 return Offset (gh_scm2double (ly_car (s)),
360 gh_scm2double (ly_cdr (s)));
366 char const * cp = "unknown";
367 if (gh_number_p (exp))
371 else if (gh_string_p (exp))
375 else if (gh_procedure_p (exp))
379 else if (gh_boolean_p (exp))
383 else if (gh_pair_p (exp))
388 return ly_str02scm (cp);
392 convert without too many decimals, and leave a space at the end.
397 ly_number2string (SCM s)
399 assert (gh_number_p (s));
401 char str[400]; // ugh.
403 if (scm_exact_p (s) == SCM_BOOL_F)
405 Real r (gh_scm2double (s));
407 if (isinf (r) || isnan (r))
409 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
413 sprintf (str, "%8.4f ", r);
417 sprintf (str, "%d ", gh_scm2int (s));
420 return ly_str02scm (str);
424 Undef this to see if GUILE GC is causing too many swaps.
430 #include <libguile/gc.h>
433 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
435 fprintf (stderr, "entering sweep\n");
439 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
441 fprintf (stderr, "leaving sweep\n");
446 #include "version.hh"
450 char const* vs = "\' (" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
452 return gh_eval_str ((char*)vs);
458 scm_c_define_gsubr ("ly-warn", 1, 0, 0,
459 (Scheme_function_unknown)ly_warning);
460 scm_c_define_gsubr ("ly-version", 0, 0, 0,
461 (Scheme_function_unknown)ly_version);
462 scm_c_define_gsubr ("ly-gulp-file", 1,0, 0,
463 (Scheme_function_unknown)ly_gulp_file);
464 scm_c_define_gsubr ("dir?", 1,0, 0, (Scheme_function_unknown)ly_isdir_p);
465 scm_c_define_gsubr ("ly-number->string", 1, 0,0,
466 (Scheme_function_unknown) ly_number2string);
470 scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
471 scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
475 ADD_SCM_INIT_FUNC (funcs, init_functions);
482 return gh_cons (ly_deep_copy (ly_car (l)), ly_deep_copy (ly_cdr (l)));
492 ly_assoc_chain (SCM key, SCM achain)
494 if (gh_pair_p (achain))
496 SCM handle = scm_assoc (key, ly_car (achain));
497 if (gh_pair_p (handle))
500 return ly_assoc_chain (key, ly_cdr (achain));
507 LIST has the form "sym1 sym2 sym3"
510 parse_symbol_list (const char * list)
512 char * s = strdup (list);
514 SCM create_list = SCM_EOL;
520 char *next = strchr (s, ' ');
524 create_list = gh_cons (ly_symbol2scm (s), create_list);
534 ly_truncate_list (int k, SCM l )
544 for (; gh_pair_p (s) && k--; s = ly_cdr (s))
549 gh_set_cdr_x (s, SCM_EOL);
555 SCM my_gh_symbol2scm (const char* x)
557 return gh_symbol2scm ((char*)x);