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"
32 #include <libguile/gc.h>
38 assert (!SCM_CONSP (x) || (*(scm_t_bits*) SCM2PTR (SCM_CAR (x))) != scm_tc_free_cell);
39 assert (!SCM_CONSP (x) || (*(scm_t_bits*) SCM2PTR (SCM_CDR (x))) != scm_tc_free_cell);
40 #elif GUILE_MINOR_VERSION < 5
41 assert (!SCM_CONSP (x) || !SCM_FREEP (SCM_CAR (x)));
42 assert (!SCM_CONSP (x) || !SCM_FREEP (SCM_CDR (x)));
44 assert (!SCM_CONSP (x) || !SCM_FREE_CELL_P (SCM_CAR (x)));
45 assert (!SCM_CONSP (x) || !SCM_FREE_CELL_P (SCM_CDR (x)));
47 //return SCM_NFALSEP (scm_pair_p (x));
50 #define gh_pair_p ly_pair_p
56 return ly_car (scm_last_pair (list));
60 ly_str02scm (char const*c)
62 // this all really sucks, guile should take char const* arguments!
63 return gh_str02scm ((char*)c);
70 SCM port = scm_mkstrport (SCM_INUM0,
71 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
74 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
75 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
77 // scm_apply (write, port, SCM_EOL);
78 gh_call2 (write, s, port);
79 return scm_strport_to_string (port);
84 Pass string to scm parser, evaluate one expression.
85 Return result value and #chars read.
87 Thanks to Gary Houston <ghouston@freewire.co.uk>
89 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
92 ly_parse_scm (char const* s, int* n)
94 SCM str = ly_str02scm (s);
95 SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
97 SCM from = scm_ftell (port);
100 SCM answer = SCM_UNSPECIFIED;
102 /* Read expression from port */
103 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
104 answer = scm_primitive_eval (form);
111 all seems fine, but after parsing
115 read_buf has been advanced to read_pos - 1,
116 so that scm_ftell returns 1, instead of #parsed chars
120 urg: reset read_buf for scm_ftell
121 shouldn't scm_read () do this for us?
123 scm_fill_input (port);
124 SCM to = scm_ftell (port);
125 *n = gh_scm2int (to) - gh_scm2int (from);
127 /* Don't close the port here; if we re-enter this function via a
128 continuation, then the next time we enter it, we'll get an error.
129 It's a string port anyway, so there's no advantage to closing it
132 scm_close_port (port);
141 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
147 ly_symbol2string (SCM s)
149 assert (gh_symbol_p (s));
150 return String ((Byte*)SCM_STRING_CHARS (s), (int) SCM_STRING_LENGTH (s));
155 gulp_file_to_string (String fn)
157 String s = global_path.find (fn);
160 String e = _f ("can't find file: `%s'", fn);
162 e += _f ("(load path: `%s')", global_path.str ());
165 else if (verbose_global_b)
166 progress_indication ("[" + s);
169 Simple_file_storage f (s);
170 String result (f.ch_C ());
171 if (verbose_global_b)
172 progress_indication ("]");
176 LY_DEFINE(ly_gulp_file, "ly-gulp-file", 1,0, 0,
178 "Read the file named @var{name}, and return its contents in a string. The
179 file is looked up using the lilypond search path.
183 return ly_str02scm (gulp_file_to_string (ly_scm2string (name)).ch_C ());
188 Read a file, and shove it down GUILE. GUILE also has file read
189 functions, but you can't fiddle with the path of those.
195 read_lily_scm_file (String fn)
197 gh_eval_str ((char *) gulp_file_to_string (fn).ch_C ());
201 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
203 ly_display_scm (SCM s)
211 ly_scm2string (SCM s)
213 assert (gh_string_p (s));
215 char *p = SCM_STRING_CHARS(s);
221 index_get_cell (SCM s, Direction d)
224 return (d == LEFT) ? ly_car (s) : ly_cdr (s);
228 index_set_cell (SCM s, Direction d, SCM v)
237 LY_DEFINE(ly_warning,"ly-warn", 1, 0, 0,
238 (SCM str),"Scheme callable function to issue the warning @code{msg}.
241 assert (gh_string_p (str));
242 warning ("lily-guile: " + ly_scm2string (str));
246 LY_DEFINE(ly_isdir_p, "dir?", 1,0, 0, (SCM s),
247 "type predicate. A direction is a -1, 0 or 1, where -1 represents left or
248 down and 1 represents right or up.
253 int i = gh_scm2int (s);
254 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
260 ly_number_pair_p (SCM p)
262 return gh_pair_p (p) && gh_number_p (ly_car (p)) && gh_number_p (ly_cdr (p));
265 typedef void (*Void_fptr) ();
266 Array<Void_fptr> *scm_init_funcs_;
268 void add_scm_init_func (void (*f) ())
270 if (!scm_init_funcs_)
271 scm_init_funcs_ = new Array<Void_fptr>;
273 scm_init_funcs_->push (f);
276 extern void init_cxx_function_smobs ();
279 prepend_load_path (String p )
283 "(set! %%load-path (cons \"%s\" %%load-path))", p.ch_C());
285 scm_c_eval_string (s);
289 init_lily_guile (String p )
291 prepend_load_path (p);
293 // todo: junk this. We should make real modules iso. just loading files.
294 prepend_load_path (p + "/scm/");
296 SCM last_mod = scm_current_module ();
297 scm_set_current_module (scm_c_resolve_module ("guile"));
299 init_cxx_function_smobs ();
300 for (int i=scm_init_funcs_->size () ; i--;)
301 (scm_init_funcs_->elem (i)) ();
303 if (verbose_global_b)
304 progress_indication ("\n");
305 read_lily_scm_file ("lily.scm");
307 scm_set_current_module (last_mod);
310 unsigned int ly_scm_hash (SCM s)
312 return scm_ihashv (s, ~1u);
322 int i = gh_scm2int (s);
323 return i>= -1 && i <= 1;
334 int i = gh_scm2int (s);
335 return i== 0 || i == 1;
344 return (Direction) gh_scm2int (s);
348 ly_scm2interval (SCM p)
350 return Interval (gh_scm2double (ly_car (p)),
351 gh_scm2double (ly_cdr (p)));
355 ly_interval2scm (Drul_array<Real> i)
357 return gh_cons (gh_double2scm (i[LEFT]),
358 gh_double2scm (i[RIGHT]));
367 return gh_boolean_p (s) && gh_scm2bool (s);
371 Appendable list L: the cdr contains the list, the car the last cons
377 SCM s = gh_cons (SCM_EOL, SCM_EOL);
384 appendable_list_append (SCM l, SCM elt)
386 SCM newcons = gh_cons (elt, SCM_EOL);
388 gh_set_cdr_x (ly_car (l), newcons);
389 gh_set_car_x (l, newcons);
394 ly_offset2scm (Offset o)
396 return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm (o[Y_AXIS]));
400 ly_scm2offset (SCM s)
402 return Offset (gh_scm2double (ly_car (s)),
403 gh_scm2double (ly_cdr (s)));
409 char const * cp = "unknown";
410 if (gh_number_p (exp))
414 else if (gh_string_p (exp))
418 else if (gh_procedure_p (exp))
422 else if (gh_boolean_p (exp))
426 else if (gh_pair_p (exp))
431 return ly_str02scm (cp);
435 convert without too many decimals, and leave a space at the end.
439 LY_DEFINE(ly_number2string, "ly-number->string", 1, 0,0,
441 " converts @var{num} to a string without generating many decimals. It
442 leaves a space at the end.
445 assert (gh_number_p (s));
447 char str[400]; // ugh.
449 if (scm_exact_p (s) == SCM_BOOL_F)
451 Real r (gh_scm2double (s));
453 if (isinf (r) || isnan (r))
455 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
459 sprintf (str, "%8.4f ", r);
463 sprintf (str, "%d ", gh_scm2int (s));
466 return ly_str02scm (str);
470 Undef this to see if GUILE GC is causing too many swaps.
476 #include <libguile/gc.h>
479 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
481 fprintf (stderr, "entering sweep\n");
485 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
487 fprintf (stderr, "leaving sweep\n");
492 #include "version.hh"
493 LY_DEFINE(ly_version, "ly-version", 0, 0, 0, (),
494 "Return the current lilypond version as a list, e.g.
495 @code{(1 3 127 uu1)}.
498 char const* vs = "\' (" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
500 return gh_eval_str ((char*)vs);
503 LY_DEFINE(ly_unit, "ly-unit", 0, 0, 0, (),
504 "Return the unit used for lengths as a string.")
506 return ly_str02scm (INTERNAL_UNIT);
509 LY_DEFINE(ly_verbose, "ly-verbose", 0, 0, 0, (),
510 "Return whether lilypond is being run in verbose mode.")
512 return gh_bool2scm (verbose_global_b);
519 scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
520 scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
524 ADD_SCM_INIT_FUNC (funcs, init_functions);
527 ly_deep_copy (SCM src)
531 return gh_cons (ly_deep_copy (ly_car (src)), ly_deep_copy (ly_cdr (src)));
533 else if (gh_vector_p (src))
535 int l = SCM_VECTOR_LENGTH (src);
536 SCM nv = scm_c_make_vector (l, SCM_UNDEFINED);
537 for (int i =0 ; i< l ; i++)
539 SCM si = gh_int2scm (i);
540 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
553 ly_assoc_chain (SCM key, SCM achain)
555 if (gh_pair_p (achain))
557 SCM handle = scm_assoc (key, ly_car (achain));
558 if (gh_pair_p (handle))
561 return ly_assoc_chain (key, ly_cdr (achain));
567 /* looks the key up in the cdrs of the alist-keys
568 - ignoring the car and ignoring non-pair keys.
569 Returns first match found, i.e.
577 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
578 element with the cdr of the key = 1. In other words (alloc_cdr key)
581 (alloc (anything . key))
587 ly_assoc_cdr (SCM key, SCM alist)
589 if (gh_pair_p (alist)) {
590 SCM trykey = ly_caar(alist);
591 if(gh_pair_p(trykey) && to_boolean(scm_equal_p(key,ly_cdr(trykey))))
592 return ly_car(alist);
594 return ly_assoc_cdr (key, ly_cdr (alist));
601 LIST has the form "sym1 sym2 sym3\nsym4\nsym5"
603 i.e. \n and ' ' can be used interchangeably as separators.
606 parse_symbol_list (const char * list)
608 char * s = strdup (list);
610 SCM create_list = SCM_EOL;
612 for (char * p = s; *p; p++)
625 char *next = strchr (s, ' ');
629 create_list = gh_cons (ly_symbol2scm (s), create_list);
639 ly_truncate_list (int k, SCM l )
649 for (; gh_pair_p (s) && k--; s = ly_cdr (s))
654 gh_set_cdr_x (s, SCM_EOL);
660 SCM my_gh_symbol2scm (const char* x)
662 return gh_symbol2scm ((char*)x);
666 print_scm_val (SCM val)
668 String realval = ly_scm2string (ly_write2scm (val));
669 if (realval.length_i () > 200)
670 realval = realval.left_str (100) + "\n :\n :\n" + realval.right_str (100);
676 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
684 TODO: should remove #f from allowed vals?
686 if (val == SCM_EOL || val == SCM_BOOL_F)
690 SCM type_p = SCM_EOL;
692 if (gh_symbol_p (sym))
693 type_p = scm_object_property (sym, type_symbol);
695 if (type_p != SCM_EOL && !gh_procedure_p (type_p))
697 warning (_f ("Can't find property type-check for `%s' (%s). Perhaps you made a typing error? Doing assignment anyway.",
698 ly_symbol2string (sym).ch_C (),
699 ly_symbol2string (type_symbol).ch_C ()
706 && gh_procedure_p (type_p)
707 && gh_call1 (type_p, val) == SCM_BOOL_F)
709 SCM errport = scm_current_error_port ();
711 SCM typefunc = scm_primitive_eval (ly_symbol2scm ("type-name"));
712 SCM type_name = gh_call1 (typefunc, type_p);
715 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
716 ly_symbol2string (sym).ch_C (),
718 ly_scm2string (type_name).ch_C ()).ch_C (),
720 scm_puts ("\n", errport);
729 zijn deze nou handig?
730 zijn ze er al in scheme, maar heten ze anders? */
733 /* Remove doubles from (sorted) list */
737 SCM unique = SCM_EOL;
738 for (SCM i = list; gh_pair_p (i); i = ly_cdr (i))
740 if (!gh_pair_p (ly_cdr (i))
741 || !gh_equal_p (ly_car (i), ly_cadr (i)))
742 unique = gh_cons (ly_car (i), unique);
744 return scm_reverse_x (unique, SCM_EOL);
749 ly_snoc (SCM s, SCM list)
751 return gh_append2 (list, scm_list_n (s, SCM_UNDEFINED));
755 /* Split list at member s, removing s.
756 Return (BEFORE . AFTER) */
758 ly_split_list (SCM s, SCM list)
760 SCM before = SCM_EOL;
762 for (; gh_pair_p (after);)
764 SCM i = ly_car (after);
765 after = ly_cdr (after);
766 if (gh_equal_p (i, s))
768 before = gh_cons (i, before);
770 return gh_cons ( scm_reverse_x (before, SCM_EOL), after);
784 display stuff without using stack
789 SCM p = scm_current_output_port();
792 for (; gh_pair_p(s); s =gh_cdr(s))
794 scm_display (gh_car(s), p);
798 return SCM_UNSPECIFIED;