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 */
18 #include "lily-proto.hh"
23 source-file.hh includes cmath which undefines isinf and isnan
25 inline int my_isinf(Real r) { return isinf(r); }
26 inline int my_isnan(Real r) { return isnan(r); }
30 #include "libc-extension.hh"
31 #include "lily-guile.hh"
33 #include "file-path.hh"
35 #include "direction.hh"
37 #include "interval.hh"
39 #include "dimensions.hh"
40 #include "source-file.hh"
47 return ly_car (scm_last_pair (list));
54 SCM port = scm_mkstrport (SCM_INUM0,
55 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
58 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
59 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
61 // scm_apply (write, port, SCM_EOL);
62 gh_call2 (write, s, port);
63 return scm_strport_to_string (port);
70 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
74 ly_symbol2string (SCM s)
76 assert (gh_symbol_p (s));
77 return String ((Byte*)SCM_STRING_CHARS (s), (int) SCM_STRING_LENGTH (s));
81 gulp_file_to_string (String fn)
83 String s = global_path.find (fn);
86 String e = _f ("can't find file: `%s'", fn);
88 e += _f ("(load path: `%s')", global_path.string ());
91 else if (verbose_global_b)
92 progress_indication ("[" + s);
95 char * str = gulp_file (s, &n);
100 progress_indication ("]");
105 LY_DEFINE(ly_gulp_file, "ly-gulp-file", 1,0, 0,
107 "Read the file named @var{name}, and return its contents in a string. The
108 file is looked up using the lilypond search path.
112 return scm_makfrom0str (gulp_file_to_string (ly_scm2string (name)).to_str0 ());
117 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
119 ly_display_scm (SCM s)
127 ly_scm2string (SCM s)
129 assert (gh_string_p (s));
131 char *p = SCM_STRING_CHARS(s);
137 index_get_cell (SCM s, Direction d)
141 return (d == LEFT) ? ly_car (s) : ly_cdr (s);
145 index_set_cell (SCM s, Direction d, SCM v)
154 LY_DEFINE(ly_warning,"ly-warn", 1, 0, 0,
155 (SCM str),"Scheme callable function to issue the warning @code{msg}.")
157 SCM_ASSERT_TYPE (gh_string_p (str), str, SCM_ARG1, __FUNCTION__, "string");
158 warning ("lily-guile: " + ly_scm2string (str));
162 LY_DEFINE(ly_isdir, "dir?", 1,0, 0, (SCM s),
163 "type predicate. A direction is a -1, 0 or 1, where -1 represents left or
164 down and 1 represents right or up.
169 int i = gh_scm2int (s);
170 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
176 ly_number_pair_p (SCM p)
178 return gh_pair_p (p) && gh_number_p (ly_car (p)) && gh_number_p (ly_cdr (p));
181 typedef void (*Void_fptr) ();
182 Array<Void_fptr> *scm_init_funcs_;
184 void add_scm_init_func (void (*f) ())
186 if (!scm_init_funcs_)
187 scm_init_funcs_ = new Array<Void_fptr>;
189 scm_init_funcs_->push (f);
194 ly_init_ly_module (void *data)
196 for (int i=scm_init_funcs_->size () ; i--;)
197 (scm_init_funcs_->elem (i)) ();
199 if (verbose_global_b)
200 progress_indication ("\n");
201 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
210 lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
211 scm_c_use_module ("lily");
214 unsigned int ly_scm_hash (SCM s)
216 return scm_ihashv (s, ~1u);
226 int i = gh_scm2int (s);
227 return i>= -1 && i <= 1;
238 int i = gh_scm2int (s);
239 return i== 0 || i == 1;
248 return SCM_INUMP (s) ? (Direction) gh_scm2int (s) : CENTER;
252 ly_scm2interval (SCM p)
254 return Interval (gh_scm2double (ly_car (p)),
255 gh_scm2double (ly_cdr (p)));
259 ly_interval2scm (Drul_array<Real> i)
261 return gh_cons (gh_double2scm (i[LEFT]),
262 gh_double2scm (i[RIGHT]));
271 return gh_boolean_p (s) && gh_scm2bool (s);
275 Appendable list L: the cdr contains the list, the car the last cons
281 SCM s = gh_cons (SCM_EOL, SCM_EOL);
288 appendable_list_append (SCM l, SCM elt)
290 SCM newcons = gh_cons (elt, SCM_EOL);
292 gh_set_cdr_x (ly_car (l), newcons);
293 gh_set_car_x (l, newcons);
298 ly_offset2scm (Offset o)
300 return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm (o[Y_AXIS]));
304 ly_scm2offset (SCM s)
306 return Offset (gh_scm2double (ly_car (s)),
307 gh_scm2double (ly_cdr (s)));
311 LY_DEFINE(ly_number2string, "ly-number->string", 1, 0,0,
313 " converts @var{num} to a string without generating many decimals. It
314 leaves a space at the end.
317 SCM_ASSERT_TYPE (gh_number_p (s), s, SCM_ARG1, __FUNCTION__, "number");
319 char str[400]; // ugh.
321 if (scm_exact_p (s) == SCM_BOOL_F)
323 Real r (gh_scm2double (s));
325 if (my_isinf (r) || my_isnan (r))
327 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
331 sprintf (str, "%8.4f ", r);
335 sprintf (str, "%d ", gh_scm2int (s));
338 return scm_makfrom0str (str);
342 Undef this to see if GUILE GC is causing too many swaps.
348 #include <libguile/gc.h>
351 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
353 fprintf (stderr, "entering sweep\n");
357 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
359 fprintf (stderr, "leaving sweep\n");
364 #include "version.hh"
365 LY_DEFINE(ly_version, "ly-version", 0, 0, 0, (),
366 "Return the current lilypond version as a list, e.g.
367 @code{(1 3 127 uu1)}.
370 char const* vs = "\' (" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
372 return gh_eval_str ((char*)vs);
375 LY_DEFINE(ly_unit, "ly-unit", 0, 0, 0, (),
376 "Return the unit used for lengths as a string.")
378 return scm_makfrom0str (INTERNAL_UNIT);
381 LY_DEFINE(ly_verbose, "ly-verbose", 0, 0, 0, (),
382 "Return whether lilypond is being run in verbose mode.")
384 return gh_bool2scm (verbose_global_b);
391 scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
392 scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
396 ADD_SCM_INIT_FUNC (funcs, init_functions);
399 ly_deep_copy (SCM src)
403 return gh_cons (ly_deep_copy (ly_car (src)), ly_deep_copy (ly_cdr (src)));
405 else if (gh_vector_p (src))
407 int l = SCM_VECTOR_LENGTH (src);
408 SCM nv = scm_c_make_vector (l, SCM_UNDEFINED);
409 for (int i =0 ; i< l ; i++)
411 SCM si = gh_int2scm (i);
412 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
425 ly_assoc_chain (SCM key, SCM achain)
427 if (gh_pair_p (achain))
429 SCM handle = scm_assoc (key, ly_car (achain));
430 if (gh_pair_p (handle))
433 return ly_assoc_chain (key, ly_cdr (achain));
439 /* looks the key up in the cdrs of the alist-keys
440 - ignoring the car and ignoring non-pair keys.
441 Returns first match found, i.e.
449 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
450 element with the cdr of the key = 1. In other words (alloc_cdr key)
453 (alloc (anything . key))
459 ly_assoc_cdr (SCM key, SCM alist)
461 if (gh_pair_p (alist)) {
462 SCM trykey = ly_caar(alist);
463 if(gh_pair_p(trykey) && to_boolean(scm_equal_p(key,ly_cdr(trykey))))
464 return ly_car(alist);
466 return ly_assoc_cdr (key, ly_cdr (alist));
473 LIST has the form "sym1 sym2 sym3\nsym4\nsym5"
475 i.e. \n and ' ' can be used interchangeably as separators.
478 parse_symbol_list (const char * list)
480 char * s = strdup (list);
482 SCM create_list = SCM_EOL;
484 for (char * p = s; *p; p++)
497 char *next = strchr (s, ' ');
501 create_list = gh_cons (ly_symbol2scm (s), create_list);
511 ly_truncate_list (int k, SCM l )
521 for (; gh_pair_p (s) && k--; s = ly_cdr (s))
526 gh_set_cdr_x (s, SCM_EOL);
534 print_scm_val (SCM val)
536 String realval = ly_scm2string (ly_write2scm (val));
537 if (realval.length () > 200)
538 realval = realval.left_string (100) + "\n :\n :\n" + realval.right_string (100);
544 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
552 TODO: should remove #f from allowed vals?
554 if (val == SCM_EOL || val == SCM_BOOL_F)
560 if (gh_symbol_p (sym))
561 type = scm_object_property (sym, type_symbol);
563 if (type != SCM_EOL && !gh_procedure_p (type))
565 warning (_f ("Can't find property type-check for `%s' (%s).",
566 ly_symbol2string (sym).to_str0 (),
567 ly_symbol2string (type_symbol).to_str0 ())
568 + " " + _ ("Perhaps you made a typing error?"));
570 /* Be strict when being anal :) */
571 if (internal_type_checking_global_b)
574 warning (_ ("Doing assignment anyway."));
579 && gh_procedure_p (type)
580 && gh_call1 (type, val) == SCM_BOOL_F)
582 SCM errport = scm_current_error_port ();
584 SCM typefunc = scm_primitive_eval (ly_symbol2scm ("type-name"));
585 SCM type_name = gh_call1 (typefunc, type);
588 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
589 ly_symbol2string (sym).to_str0 (),
591 ly_scm2string (type_name).to_str0 ()).to_str0 (),
593 scm_puts ("\n", errport);
602 zijn deze nou handig?
603 zijn ze er al in scheme, maar heten ze anders? */
606 /* Remove doubles from (sorted) list */
610 SCM unique = SCM_EOL;
611 for (SCM i = list; gh_pair_p (i); i = ly_cdr (i))
613 if (!gh_pair_p (ly_cdr (i))
614 || !gh_equal_p (ly_car (i), ly_cadr (i)))
615 unique = gh_cons (ly_car (i), unique);
617 return scm_reverse_x (unique, SCM_EOL);
622 ly_snoc (SCM s, SCM list)
624 return gh_append2 (list, scm_list_n (s, SCM_UNDEFINED));
628 /* Split list at member s, removing s.
629 Return (BEFORE . AFTER) */
631 ly_split_list (SCM s, SCM list)
633 SCM before = SCM_EOL;
635 for (; gh_pair_p (after);)
637 SCM i = ly_car (after);
638 after = ly_cdr (after);
639 if (gh_equal_p (i, s))
641 before = gh_cons (i, before);
643 return gh_cons ( scm_reverse_x (before, SCM_EOL), after);
657 display stuff without using stack
662 SCM p = scm_current_output_port();
665 for (; gh_pair_p(s); s =gh_cdr(s))
667 scm_display (gh_car(s), p);
671 return SCM_UNSPECIFIED;
675 int_list_to_slice (SCM l)
679 for (; gh_pair_p (l); l = gh_cdr (l))
681 if (gh_number_p (gh_car (l)))
682 s.add_point (gh_scm2int (gh_car (l)));
692 Return I-th element, or last elt L. If I < 0, then we take the first
698 robust_list_ref(int i, SCM l)
700 while (i-- > 0 && gh_pair_p (gh_cdr(l)))