2 lily-guile.cc -- implement assorted guile functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2004 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.to_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",
107 "Read the file @var{name}, and return its contents in a string. "
108 "The file is looked up using the search path.")
110 return scm_makfrom0str (gulp_file_to_string (ly_scm2string (name)).to_str0 ());
115 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
117 ly_display_scm (SCM s)
125 ly_scm2string (SCM s)
127 assert (gh_string_p (s));
129 char *p = SCM_STRING_CHARS (s);
135 index_get_cell (SCM s, Direction d)
139 return (d == LEFT) ? ly_car (s) : ly_cdr (s);
143 index_set_cell (SCM s, Direction d, SCM v)
152 LY_DEFINE (ly_warning,"ly:warn", 1, 0, 0,
153 (SCM str),"Scheme callable function to issue the warning @code{msg}.")
155 SCM_ASSERT_TYPE (gh_string_p (str), str, SCM_ARG1, __FUNCTION__, "string");
156 warning ("lily-guile: " + ly_scm2string (str));
160 LY_DEFINE (ly_dir_p, "ly:dir?", 1,0, 0, (SCM s),
161 "type predicate. A direction is @code{-1}, @code{0} or "
162 "@code{1}, where @code{-1} represents "
163 "left or down and @code{1} represents right or up.")
167 int i = gh_scm2int (s);
168 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
174 is_number_pair (SCM p)
176 return gh_pair_p (p) && gh_number_p (ly_car (p)) && gh_number_p (ly_cdr (p));
179 typedef void (*Void_fptr) ();
180 Array<Void_fptr> *scm_init_funcs_;
182 void add_scm_init_func (void (*f) ())
184 if (!scm_init_funcs_)
185 scm_init_funcs_ = new Array<Void_fptr>;
187 scm_init_funcs_->push (f);
192 ly_init_ly_module (void *)
194 for (int i=scm_init_funcs_->size () ; i--;)
195 (scm_init_funcs_->elem (i)) ();
197 if (verbose_global_b)
198 progress_indication ("\n");
200 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
209 lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
210 scm_c_use_module ("lily");
213 unsigned int ly_scm_hash (SCM s)
215 return scm_ihashv (s, ~1u);
225 int i = gh_scm2int (s);
226 return i>= -1 && i <= 1;
237 int i = gh_scm2int (s);
238 return i== 0 || i == 1;
247 return SCM_INUMP (s) ? (Direction) gh_scm2int (s) : CENTER;
251 ly_scm2interval (SCM p)
253 return Interval (gh_scm2double (ly_car (p)),
254 gh_scm2double (ly_cdr (p)));
258 ly_scm2realdrul (SCM p)
260 return Drul_array<Real> (gh_scm2double (ly_car (p)),
261 gh_scm2double (ly_cdr (p)));
265 ly_interval2scm (Drul_array<Real> i)
267 return gh_cons (gh_double2scm (i[LEFT]),
268 gh_double2scm (i[RIGHT]));
277 return gh_boolean_p (s) && gh_scm2bool (s);
281 Appendable list L: the cdr contains the list, the car the last cons
287 SCM s = gh_cons (SCM_EOL, SCM_EOL);
294 appendable_list_append (SCM l, SCM elt)
296 SCM newcons = gh_cons (elt, SCM_EOL);
298 gh_set_cdr_x (ly_car (l), newcons);
299 gh_set_car_x (l, newcons);
304 ly_offset2scm (Offset o)
306 return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm (o[Y_AXIS]));
310 ly_scm2offset (SCM s)
312 return Offset (gh_scm2double (ly_car (s)),
313 gh_scm2double (ly_cdr (s)));
317 LY_DEFINE (ly_number2string, "ly:number->string", 1, 0,0,
319 " converts @var{num} to a string without generating many decimals. It "
320 "leaves a space at the end.")
322 SCM_ASSERT_TYPE (gh_number_p (s), s, SCM_ARG1, __FUNCTION__, "number");
324 char str[400]; // ugh.
326 if (scm_exact_p (s) == SCM_BOOL_F)
328 Real r (gh_scm2double (s));
330 if (my_isinf (r) || my_isnan (r))
332 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
336 sprintf (str, "%8.4f ", r);
340 sprintf (str, "%d ", gh_scm2int (s));
343 return scm_makfrom0str (str);
347 Undef this to see if GUILE GC is causing too many swaps.
353 #include <libguile/gc.h>
356 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
358 fprintf (stderr, "entering sweep\n");
362 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
364 fprintf (stderr, "leaving sweep\n");
369 #include "version.hh"
370 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
371 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
373 char const* vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
375 return gh_eval_str ((char*)vs);
378 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
379 "Return the unit used for lengths as a string.")
381 return scm_makfrom0str (INTERNAL_UNIT);
386 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
387 "Return @var{d} is a number. Used to distinguish length "
388 "variables from normal numbers.")
390 return scm_number_p (d);
397 scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
398 scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
402 ADD_SCM_INIT_FUNC (funcs, init_functions);
405 ly_deep_copy (SCM src)
409 return gh_cons (ly_deep_copy (ly_car (src)), ly_deep_copy (ly_cdr (src)));
411 else if (gh_vector_p (src))
413 int l = SCM_VECTOR_LENGTH (src);
414 SCM nv = scm_c_make_vector (l, SCM_UNDEFINED);
415 for (int i =0 ; i< l ; i++)
417 SCM si = gh_int2scm (i);
418 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
431 ly_assoc_chain (SCM key, SCM achain)
433 if (gh_pair_p (achain))
435 SCM handle = scm_assoc (key, ly_car (achain));
436 if (gh_pair_p (handle))
439 return ly_assoc_chain (key, ly_cdr (achain));
445 /* looks the key up in the cdrs of the alist-keys
446 - ignoring the car and ignoring non-pair keys.
447 Returns first match found, i.e.
455 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
456 element with the cdr of the key = 1. In other words (alloc_cdr key)
459 (alloc (anything . key))
465 ly_assoc_cdr (SCM key, SCM alist)
467 if (gh_pair_p (alist)) {
468 SCM trykey = ly_caar (alist);
469 if (gh_pair_p (trykey) && to_boolean (scm_equal_p (key,ly_cdr (trykey))))
470 return ly_car (alist);
472 return ly_assoc_cdr (key, ly_cdr (alist));
479 LIST has the form "sym1 sym2 sym3\nsym4\nsym5"
481 i.e. \n and ' ' can be used interchangeably as separators.
484 parse_symbol_list (const char * list)
486 char * s = strdup (list);
488 SCM create_list = SCM_EOL;
490 char * e = s + strlen (s) - 1;
491 while (e >= s && isspace (*e))
494 for (char * p = s; *p; p++)
507 char *next = strchr (s, ' ');
511 create_list = gh_cons (ly_symbol2scm (s), create_list);
521 ly_truncate_list (int k, SCM l )
531 for (; gh_pair_p (s) && k--; s = ly_cdr (s))
536 gh_set_cdr_x (s, SCM_EOL);
544 print_scm_val (SCM val)
546 String realval = ly_scm2string (ly_write2scm (val));
547 if (realval.length () > 200)
548 realval = realval.left_string (100) + "\n :\n :\n" + realval.right_string (100);
554 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
562 TODO: should remove #f from allowed vals?
564 if (val == SCM_EOL || val == SCM_BOOL_F)
567 if (!gh_symbol_p (sym))
572 This is used for autoBeamSettings.
574 TODO: deprecate the use of \override and \revert for
577 or use a symbol autoBeamSettingS?
582 SCM type = scm_object_property (sym, type_symbol);
584 if (type != SCM_EOL && !gh_procedure_p (type))
586 warning (_f ("Can't find property type-check for `%s' (%s).",
587 ly_symbol2string (sym).to_str0 (),
588 ly_symbol2string (type_symbol).to_str0 ())
589 + " " + _ ("Perhaps you made a typing error?"));
591 /* Be strict when being anal :) */
592 if (internal_type_checking_global_b)
595 warning (_ ("Doing assignment anyway."));
600 && gh_procedure_p (type)
601 && gh_call1 (type, val) == SCM_BOOL_F)
603 SCM errport = scm_current_error_port ();
605 SCM typefunc = scm_primitive_eval (ly_symbol2scm ("type-name"));
606 SCM type_name = gh_call1 (typefunc, type);
609 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
610 ly_symbol2string (sym).to_str0 (),
612 ly_scm2string (type_name).to_str0 ()).to_str0 (),
614 scm_puts ("\n", errport);
623 zijn deze nou handig?
624 zijn ze er al in scheme, maar heten ze anders? */
627 /* Remove doubles from (sorted) list */
631 SCM unique = SCM_EOL;
632 for (SCM i = list; gh_pair_p (i); i = ly_cdr (i))
634 if (!gh_pair_p (ly_cdr (i))
635 || !gh_equal_p (ly_car (i), ly_cadr (i)))
636 unique = gh_cons (ly_car (i), unique);
638 return scm_reverse_x (unique, SCM_EOL);
643 ly_snoc (SCM s, SCM list)
645 return gh_append2 (list, scm_list_n (s, SCM_UNDEFINED));
649 /* Split list at member s, removing s.
650 Return (BEFORE . AFTER) */
652 ly_split_list (SCM s, SCM list)
654 SCM before = SCM_EOL;
656 for (; gh_pair_p (after);)
658 SCM i = ly_car (after);
659 after = ly_cdr (after);
660 if (gh_equal_p (i, s))
662 before = gh_cons (i, before);
664 return gh_cons ( scm_reverse_x (before, SCM_EOL), after);
678 display stuff without using stack
683 SCM p = scm_current_output_port ();
686 for (; gh_pair_p (s); s =gh_cdr (s))
688 scm_display (gh_car (s), p);
692 return SCM_UNSPECIFIED;
696 int_list_to_slice (SCM l)
700 for (; gh_pair_p (l); l = gh_cdr (l))
702 if (gh_number_p (gh_car (l)))
703 s.add_point (gh_scm2int (gh_car (l)));
711 Return I-th element, or last elt L. If I < 0, then we take the first
717 robust_list_ref (int i, SCM l)
719 while (i-- > 0 && gh_pair_p (gh_cdr (l)))
728 robust_scm2double (SCM k, double x)
731 x = gh_scm2double (k);
736 robust_scm2interval (SCM k, Drul_array<Real> v)
741 if (is_number_pair (k))
742 i = ly_scm2interval (k);
747 robust_scm2drul (SCM k, Drul_array<Real> v)
749 if (is_number_pair (k))
750 v = ly_scm2interval (k);
755 robust_scm2offset (SCM k, Offset o)
757 if (is_number_pair (k))
758 o = ly_scm2offset (k);
765 robust_scm2int (SCM k, int o)
767 if (scm_integer_p (k) == SCM_BOOL_T)
775 alist_to_hashq (SCM alist)
777 int i = scm_ilength (alist);
779 return scm_make_vector (gh_int2scm (0), SCM_EOL);
781 SCM tab = scm_make_vector (gh_int2scm (i), SCM_EOL);
782 for (SCM s = alist; gh_pair_p (s); s = ly_cdr (s))
784 SCM pt = ly_cdar (s);
785 scm_hashq_set_x (tab, ly_caar (s), pt);
794 LY_DEFINE (ly_protects, "ly:protects", 0, 0, 0, (),
795 "Return hash of protected objects.")