2 lily-guile.cc -- implement assorted guile functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2002 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"
26 #include "dimensions.hh"
31 return ly_car (scm_last_pair (list));
35 ly_str02scm (char const*c)
37 // this all really sucks, guile should take char const* arguments!
38 return gh_str02scm ((char*)c);
45 SCM port = scm_mkstrport (SCM_INUM0,
46 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
49 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
50 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
52 // scm_apply (write, port, SCM_EOL);
53 gh_call2 (write, s, port);
54 return scm_strport_to_string (port);
59 Pass string to scm parser, evaluate one expression.
60 Return result value and #chars read.
62 Thanks to Gary Houston <ghouston@freewire.co.uk>
64 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
67 ly_parse_scm (char const* s, int* n)
69 SCM str = ly_str02scm (s);
70 SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
72 SCM from = scm_ftell (port);
75 SCM answer = SCM_UNSPECIFIED;
77 /* Read expression from port */
78 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
79 answer = scm_primitive_eval (form);
86 all seems fine, but after parsing
90 read_buf has been advanced to read_pos - 1,
91 so that scm_ftell returns 1, instead of #parsed chars
95 urg: reset read_buf for scm_ftell
96 shouldn't scm_read () do this for us?
98 scm_fill_input (port);
99 SCM to = scm_ftell (port);
100 *n = gh_scm2int (to) - gh_scm2int (from);
102 /* Don't close the port here; if we re-enter this function via a
103 continuation, then the next time we enter it, we'll get an error.
104 It's a string port anyway, so there's no advantage to closing it
107 scm_close_port (port);
116 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
122 ly_symbol2string (SCM s)
124 assert (gh_symbol_p (s));
125 return String ((Byte*)SCM_STRING_CHARS (s), (int) SCM_STRING_LENGTH (s));
130 gulp_file_to_string (String fn)
132 String s = global_path.find (fn);
135 String e = _f ("can't find file: `%s'", fn);
137 e += _f ("(load path: `%s')", global_path.str ());
140 else if (verbose_global_b)
141 progress_indication ("[" + s);
144 Simple_file_storage f (s);
145 String result (f.ch_C ());
146 if (verbose_global_b)
147 progress_indication ("]");
152 ly_gulp_file (SCM fn)
154 return ly_str02scm (gulp_file_to_string (ly_scm2string (fn)).ch_C ());
159 Read a file, and shove it down GUILE. GUILE also has file read
160 functions, but you can't fiddle with the path of those.
163 read_lily_scm_file (String fn)
165 gh_eval_str ((char *) gulp_file_to_string (fn).ch_C ());
169 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
171 ly_display_scm (SCM s)
179 ly_scm2string (SCM s)
181 assert (gh_string_p (s));
184 char *p = gh_scm2newstr (s , &len);
193 index_cell (SCM s, Direction d)
196 return (d == LEFT) ? ly_car (s) : ly_cdr (s);
200 index_set_cell (SCM s, Direction d, SCM v)
212 assert (gh_string_p (str));
213 warning ("lily-guile: " + ly_scm2string (str));
222 int i = gh_scm2int (s);
223 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
229 ly_number_pair_p (SCM p)
231 return gh_pair_p (p) && gh_number_p (ly_car (p)) && gh_number_p (ly_cdr (p));
234 typedef void (*Void_fptr) ();
235 Array<Void_fptr> *scm_init_funcs_;
237 void add_scm_init_func (void (*f) ())
239 if (!scm_init_funcs_)
240 scm_init_funcs_ = new Array<Void_fptr>;
242 scm_init_funcs_->push (f);
245 extern void init_cxx_function_smobs ();
248 prepend_load_path (String p )
252 "(set! %%load-path (cons \"%s\" %%load-path))", p.ch_C());
254 scm_c_eval_string (s);
258 init_lily_guile (String p )
260 prepend_load_path (p);
262 // todo: junk this. We should make real modules iso. just loading files.
263 prepend_load_path (p + "/scm/");
265 SCM last_mod = scm_current_module ();
266 scm_set_current_module (scm_c_resolve_module ("guile"));
268 init_cxx_function_smobs ();
269 for (int i=scm_init_funcs_->size () ; i--;)
270 (scm_init_funcs_->elem (i)) ();
272 if (verbose_global_b)
273 progress_indication ("\n");
274 read_lily_scm_file ("lily.scm");
276 scm_set_current_module (last_mod);
279 unsigned int ly_scm_hash (SCM s)
281 return scm_ihashv (s, ~1u);
291 int i = gh_scm2int (s);
292 return i>= -1 && i <= 1;
303 int i = gh_scm2int (s);
304 return i== 0 || i == 1;
313 return (Direction) gh_scm2int (s);
317 ly_scm2interval (SCM p)
319 return Interval (gh_scm2double (ly_car (p)),
320 gh_scm2double (ly_cdr (p)));
324 ly_interval2scm (Interval i)
326 return gh_cons (gh_double2scm (i[LEFT]),
327 gh_double2scm (i[RIGHT]));
336 return gh_boolean_p (s) && gh_scm2bool (s);
340 Appendable list L: the cdr contains the list, the car the last cons
346 SCM s = gh_cons (SCM_EOL, SCM_EOL);
353 appendable_list_append (SCM l, SCM elt)
355 SCM newcons = gh_cons (elt, SCM_EOL);
357 gh_set_cdr_x (ly_car (l), newcons);
358 gh_set_car_x (l, newcons);
363 ly_offset2scm (Offset o)
365 return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm (o[Y_AXIS]));
369 ly_scm2offset (SCM s)
371 return Offset (gh_scm2double (ly_car (s)),
372 gh_scm2double (ly_cdr (s)));
378 char const * cp = "unknown";
379 if (gh_number_p (exp))
383 else if (gh_string_p (exp))
387 else if (gh_procedure_p (exp))
391 else if (gh_boolean_p (exp))
395 else if (gh_pair_p (exp))
400 return ly_str02scm (cp);
404 convert without too many decimals, and leave a space at the end.
409 ly_number2string (SCM s)
411 assert (gh_number_p (s));
413 char str[400]; // ugh.
415 if (scm_exact_p (s) == SCM_BOOL_F)
417 Real r (gh_scm2double (s));
419 if (isinf (r) || isnan (r))
421 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
425 sprintf (str, "%8.4f ", r);
429 sprintf (str, "%d ", gh_scm2int (s));
432 return ly_str02scm (str);
436 Undef this to see if GUILE GC is causing too many swaps.
442 #include <libguile/gc.h>
445 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
447 fprintf (stderr, "entering sweep\n");
451 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
453 fprintf (stderr, "leaving sweep\n");
458 #include "version.hh"
462 char const* vs = "\' (" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
464 return gh_eval_str ((char*)vs);
470 return ly_str02scm (INTERNAL_UNIT);
476 scm_c_define_gsubr ("ly-warn", 1, 0, 0,
477 (Scheme_function_unknown)ly_warning);
478 scm_c_define_gsubr ("ly-version", 0, 0, 0,
479 (Scheme_function_unknown)ly_version);
480 scm_c_define_gsubr ("ly-unit", 0, 0, 0,
481 (Scheme_function_unknown)ly_unit);
482 scm_c_define_gsubr ("ly-gulp-file", 1,0, 0,
483 (Scheme_function_unknown)ly_gulp_file);
484 scm_c_define_gsubr ("dir?", 1,0, 0, (Scheme_function_unknown)ly_isdir_p);
485 scm_c_define_gsubr ("ly-number->string", 1, 0,0,
486 (Scheme_function_unknown) ly_number2string);
490 scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
491 scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
495 ADD_SCM_INIT_FUNC (funcs, init_functions);
502 return gh_cons (ly_deep_copy (ly_car (l)), ly_deep_copy (ly_cdr (l)));
512 ly_assoc_chain (SCM key, SCM achain)
514 if (gh_pair_p (achain))
516 SCM handle = scm_assoc (key, ly_car (achain));
517 if (gh_pair_p (handle))
520 return ly_assoc_chain (key, ly_cdr (achain));
526 /* looks the key up in the cdrs of the alist-keys
527 - ignoring the car and ignoring non-pair keys.
528 Returns first match found, i.e.
536 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
537 element with the cdr of the key = 1. In other words (alloc_cdr key)
540 (alloc (anything . key))
546 ly_assoc_cdr (SCM key, SCM alist)
548 if (gh_pair_p (alist)) {
549 SCM trykey = ly_caar(alist);
550 if(gh_pair_p(trykey) && to_boolean(scm_equal_p(key,ly_cdr(trykey))))
551 return ly_car(alist);
553 return ly_assoc_cdr (key, ly_cdr (alist));
560 LIST has the form "sym1 sym2 sym3\nsym4\nsym5"
562 i.e. \n and ' ' can be used interchangeably as separators.
565 parse_symbol_list (const char * list)
567 char * s = strdup (list);
569 SCM create_list = SCM_EOL;
571 for (char * p = s; *p; p++)
584 char *next = strchr (s, ' ');
588 create_list = gh_cons (ly_symbol2scm (s), create_list);
598 ly_truncate_list (int k, SCM l )
608 for (; gh_pair_p (s) && k--; s = ly_cdr (s))
613 gh_set_cdr_x (s, SCM_EOL);
619 SCM my_gh_symbol2scm (const char* x)
621 return gh_symbol2scm ((char*)x);
625 print_scm_val (SCM val)
627 String realval = ly_scm2string (ly_write2scm (val));
628 if (realval.length_i () > 200)
629 realval = realval.left_str (100) + "\n :\n :\n" + realval.right_str (100);
635 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
643 TODO: should remove #f from allowed vals?
645 if (val == SCM_EOL || val == SCM_BOOL_F)
649 SCM type_p = SCM_EOL;
651 if (gh_symbol_p (sym))
652 type_p = scm_object_property (sym, type_symbol);
654 if (type_p != SCM_EOL && !gh_procedure_p (type_p))
656 warning (_f ("Can't find property type-check for `%s' (%s). Perhaps you made a typing error? Doing assignment anyway.",
657 ly_symbol2string (sym).ch_C (),
658 ly_symbol2string (type_symbol).ch_C ()
665 && gh_procedure_p (type_p)
666 && gh_call1 (type_p, val) == SCM_BOOL_F)
668 SCM errport = scm_current_error_port ();
670 SCM typefunc = scm_primitive_eval (ly_symbol2scm ("type-name"));
671 SCM type_name = gh_call1 (typefunc, type_p);
674 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
675 ly_symbol2string (sym).ch_C (),
677 ly_scm2string (type_name).ch_C ()).ch_C (),
679 scm_puts ("\n", errport);
688 zijn deze nou handig?
689 zijn ze er al in scheme, maar heten ze anders? */
692 /* Remove doubles from (sorted) list */
696 SCM unique = SCM_EOL;
697 for (SCM i = list; gh_pair_p (i); i = ly_cdr (i))
699 if (!gh_pair_p (ly_cdr (i))
700 || !gh_equal_p (ly_car (i), ly_cadr (i)))
701 unique = gh_cons (ly_car (i), unique);
703 return scm_reverse_x (unique, SCM_EOL);
708 ly_snoc (SCM s, SCM list)
710 return gh_append2 (list, scm_list_n (s, SCM_UNDEFINED));
714 /* Split list at member s, removing s.
715 Return (BEFORE . AFTER) */
717 ly_split_list (SCM s, SCM list)
719 SCM before = SCM_EOL;
721 for (; gh_pair_p (after);)
723 SCM i = ly_car (after);
724 after = ly_cdr (after);
725 if (gh_equal_p (i, s))
727 before = gh_cons (i, before);
729 return gh_cons ( scm_reverse_x (before, SCM_EOL), after);