2 lily-guile.cc -- implement assorted SCM interface functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "lily-guile.hh"
14 #include <cstring> /* strdup, strchr */
19 #include "dimensions.hh"
20 #include "direction.hh"
21 #include "file-path.hh"
22 #include "international.hh"
23 #include "libc-extension.hh"
28 #include "string-convert.hh"
29 #include "source-file.hh"
38 ly_to_symbol (SCM scm)
40 return scm_string_to_symbol (ly_to_string (scm));
44 ly_to_string (SCM scm)
46 return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
48 scm_from_locale_string ("~S"), scm);
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 scm_call_2 (write, s, port);
63 return scm_strport_to_string (port);
69 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
73 ly_symbol2string (SCM s)
76 Ugh. this is not very efficient.
78 SCM str = scm_symbol_to_string (s);
79 return ly_scm2string (str);
83 gulp_file_to_string (string fn, bool must_exist, int size)
85 string s = global_path.find (fn);
90 string e = _f ("cannot find file: `%s'", fn);
92 e += _f ("(load path: `%s')", global_path.to_string ());
99 if (be_verbose_global)
100 progress_indication ("[" + s);
102 vector<char> chars = gulp_file (s, size);
103 string result (&chars[0], chars.size ());
105 if (be_verbose_global)
106 progress_indication ("]");
112 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
114 ly_display_scm (SCM s)
116 scm_display (s, scm_current_output_port ());
117 scm_newline (scm_current_output_port ());
125 ly_scm2string (SCM str)
127 assert (scm_is_string (str));
128 return string (scm_i_string_chars (str),
129 (int) scm_i_string_length (str));
133 ly_string2scm (string const &str)
135 return scm_from_locale_stringn (str.c_str(),
141 ly_scm2newstr (SCM str, size_t *lenp)
143 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
145 size_t len = scm_i_string_length (str);
146 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
148 memcpy (new_str, scm_i_string_chars (str), len);
164 index_get_cell (SCM s, Direction d)
168 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
172 index_set_cell (SCM s, Direction d, SCM v)
175 scm_set_car_x (s, v);
177 scm_set_cdr_x (s, v);
182 is_number_pair (SCM p)
184 return scm_is_pair (p)
185 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
192 return scm_ihashv (s, ~1u);
199 if (scm_is_number (s))
201 int i = scm_to_int (s);
202 return i == 0 || i == 1;
210 return scm_is_bool (s) && ly_scm2bool (s);
219 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
223 robust_scm2dir (SCM d, Direction def)
225 if (is_direction (d))
233 if (scm_is_number (s))
235 int i = scm_to_int (s);
236 return i >= -1 && i <= 1;
245 ly_scm2interval (SCM p)
247 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
251 ly_scm2realdrul (SCM p)
253 return Drul_array<Real> (scm_to_double (scm_car (p)),
254 scm_to_double (scm_cdr (p)));
258 ly_interval2scm (Drul_array<Real> i)
260 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
265 robust_scm2interval (SCM k, Drul_array<Real> v)
270 if (is_number_pair (k))
271 i = ly_scm2interval (k);
276 robust_scm2drul (SCM k, Drul_array<Real> v)
278 if (is_number_pair (k))
279 v = ly_scm2interval (k);
284 robust_scm2booldrul (SCM k, Drul_array<bool> def)
288 def[LEFT] = to_boolean (scm_car (k));
289 def[RIGHT] = to_boolean (scm_cdr (k));
298 ly_offset2scm (Offset o)
300 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
304 ly_scm2offset (SCM s)
306 return Offset (scm_to_double (scm_car (s)),
307 scm_to_double (scm_cdr (s)));
311 robust_scm2offset (SCM k, Offset o)
313 if (is_number_pair (k))
314 o = ly_scm2offset (k);
318 ly_offsets2scm (vector<Offset> os)
322 for (vsize i = 0; i < os.size (); i++)
324 *tail = scm_cons (ly_offset2scm (os[i]), SCM_EOL);
325 tail = SCM_CDRLOC(*tail);
331 ly_scm2offsets (SCM s)
334 for (; scm_is_pair (s); s = scm_cdr (s))
335 os.push_back (ly_scm2offset (scm_car (s)));
347 alist_equal_p (SCM a, SCM b)
350 scm_is_pair (s); s = scm_cdr (s))
352 SCM key = scm_caar (s);
353 SCM val = scm_cdar (s);
354 SCM l = scm_assoc (key, b);
357 || !ly_is_equal (scm_cdr (l), val))
365 ly_alist_vals (SCM alist)
368 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
369 x = scm_cons (scm_cdar (p), x);
377 /* Return I-th element, or last elt L. If I < 0, then we take the first
380 PRE: length (L) > 0 */
382 robust_list_ref (int i, SCM l)
384 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
391 ly_deep_copy (SCM src)
393 if (scm_is_pair (src))
394 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
395 else if (scm_is_vector (src))
397 int len = scm_c_vector_length (src);
398 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
399 for (int i = 0;i < len; i++)
401 SCM si = scm_from_int (i);
402 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
410 ly_truncate_list (int k, SCM lst)
418 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
422 scm_set_cdr_x (s, SCM_EOL);
432 print_scm_val (SCM val)
434 string realval = ly_scm2string (ly_write2scm (val));
435 if (realval.length () > 200)
436 realval = realval.substr (0, 100)
438 + realval.substr (realval.length () - 100);
443 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
451 TODO: should remove #f from allowed vals?
453 if (val == SCM_EOL || val == SCM_BOOL_F)
456 if (!scm_is_symbol (sym))
461 This is used for autoBeamSettings.
463 TODO: deprecate the use of \override and \revert for
466 or use a symbol autoBeamSettingS?
471 SCM type = scm_object_property (sym, type_symbol);
473 if (type != SCM_EOL && !ly_is_procedure (type))
475 warning (_f ("cannot find property type-check for `%s' (%s).",
476 ly_symbol2string (sym).c_str (),
477 ly_symbol2string (type_symbol).c_str ())
478 + " " + _ ("perhaps a typing error?"));
480 /* Be strict when being anal :) */
481 if (do_internal_type_checking_global)
484 warning (_ ("doing assignment anyway"));
489 && ly_is_procedure (type)
490 && scm_call_1 (type, val) == SCM_BOOL_F)
493 SCM typefunc = ly_lily_module_constant ("type-name");
494 SCM type_name = scm_call_1 (typefunc, type);
496 warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
497 ly_symbol2string (sym).c_str (),
499 ly_scm2string (type_name).c_str ()));
500 progress_indication ("\n");
508 zijn deze nou handig?
509 zijn ze er al in scheme, maar heten ze anders? */
511 /* Remove doubles from (sorted) list */
515 SCM unique = SCM_EOL;
516 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
518 if (!scm_is_pair (scm_cdr (i))
519 || !ly_is_equal (scm_car (i), scm_cadr (i)))
520 unique = scm_cons (scm_car (i), unique);
522 return scm_reverse_x (unique, SCM_EOL);
526 /* Split list at member s, removing s.
527 Return (BEFORE . AFTER) */
529 ly_split_list (SCM s, SCM list)
531 SCM before = SCM_EOL;
533 for (; scm_is_pair (after);)
535 SCM i = scm_car (after);
536 after = scm_cdr (after);
537 if (ly_is_equal (i, s))
539 before = scm_cons (i, before);
541 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
553 display stuff without using stack
558 SCM p = scm_current_output_port ();
561 for (; scm_is_pair (s); s = scm_cdr (s))
563 scm_display (scm_car (s), p);
567 return SCM_UNSPECIFIED;
571 int_list_to_slice (SCM l)
575 for (; scm_is_pair (l); l = scm_cdr (l))
576 if (scm_is_number (scm_car (l)))
577 s.add_point (scm_to_int (scm_car (l)));
582 robust_scm2double (SCM k, double x)
584 if (scm_is_number (k))
585 x = scm_to_double (k);
591 robust_scm2string (SCM k, string s)
593 if (scm_is_string (k))
594 s = ly_scm2string (k);
599 robust_scm2int (SCM k, int o)
601 if (scm_integer_p (k) == SCM_BOOL_T)
608 ly_rational2scm (Rational r)
610 return scm_divide (scm_from_int (r.numerator ()), scm_from_int (r.denominator ()));
615 ly_scm2rational (SCM r)
617 return Rational (scm_to_int (scm_numerator (r)),
618 scm_to_int (scm_denominator (r)));
623 alist_to_hashq (SCM alist)
625 int i = scm_ilength (alist);
627 return scm_c_make_hash_table (0);
629 SCM tab = scm_c_make_hash_table (i);
630 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
632 SCM pt = scm_cdar (s);
633 scm_hashq_set_x (tab, scm_caar (s), pt);
639 ly_hash2alist (SCM tab)
641 SCM func = ly_lily_module_constant ("hash-table->alist");
642 return scm_call_1 (func, tab);
646 procedure_arity (SCM proc)
648 assert (ly_is_procedure (proc));
649 SCM arity = scm_procedure_property (proc,
650 ly_symbol2scm ("arity"));
652 SCM fixed = scm_car (arity);
653 return scm_to_int (fixed);
661 mangle_cxx_identifier (string cxx_id)
663 if (cxx_id.substr (0, 3) == "ly_")
664 cxx_id = cxx_id.replace (0, 3, "ly:");
667 cxx_id = String_convert::to_lower (cxx_id);
668 cxx_id = "ly:" + cxx_id;
670 if (cxx_id.substr (cxx_id.length () - 2) == "_p")
671 cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "?");
672 else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
673 cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "!");
675 cxx_id = replace_all (cxx_id, '_', '-');
682 ly_string_array_to_scm (vector<string> a)
685 for (vsize i = a.size (); i ; i--)
686 s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
690 /* SYMBOLS is a whitespace separated list. */
692 parse_symbol_list (char const *symbols)
694 while (isspace (*symbols))
697 replace_all (s, '\n', ' ');
698 replace_all (s, '\t', ' ');
699 return ly_string_array_to_scm (string_split (s, ' '));
704 ly_is_fraction (SCM x)
706 return SCM_FRACTIONP(x);
709 struct ly_t_double_cell
717 /* inserts at front, removing duplicates */
718 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
720 return scm_acons (key, val, scm_assoc_remove_x (alist, key));