2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2012 Jan Nieuwenhuizen <janneke@gnu.org>
5 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include "lily-guile.hh"
25 #include <cstring> /* strdup, strchr */
30 #include "dimensions.hh"
31 #include "direction.hh"
32 #include "file-path.hh"
33 #include "international.hh"
34 #include "libc-extension.hh"
39 #include "string-convert.hh"
40 #include "source-file.hh"
48 ly_scm_write_string (SCM s)
50 SCM port = scm_mkstrport (SCM_INUM0,
51 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
54 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
55 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
57 // scm_apply (write, port, SCM_EOL);
58 scm_call_2 (write, s, port);
59 return ly_scm2string (scm_strport_to_string (port));
65 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
69 ly_symbol2string (SCM s)
72 Ugh. this is not very efficient.
74 return ly_scm2string (scm_symbol_to_string (s));
78 robust_symbol2string (SCM sym, string str)
80 return scm_is_symbol (sym) ? ly_symbol2string (sym) : str;
84 gulp_file_to_string (string fn, bool must_exist, int size)
86 string s = global_path.find (fn);
91 string e = _f ("cannot find file: `%s'", fn);
93 e += _f ("(load path: `%s')", global_path.to_string ());
100 debug_output ("[" + s, true);
102 vector<char> chars = gulp_file (s, size);
103 string result (&chars[0], chars.size ());
105 debug_output ("]\n", false);
111 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
113 ly_display_scm (SCM s)
115 scm_display (s, scm_current_output_port ());
116 scm_newline (scm_current_output_port ());
124 ly_scm2string (SCM str)
126 assert (scm_is_string (str));
128 size_t len = scm_c_string_length (str);
132 scm_to_locale_stringbuf (str, &result.at (0), len);
138 ly_string2scm (string const &str)
140 return scm_from_locale_stringn (str.c_str (),
145 ly_scm2str0 (SCM str)
147 return scm_to_locale_string (str);
154 index_get_cell (SCM s, Direction d)
157 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
161 index_set_cell (SCM s, Direction d, SCM v)
164 scm_set_car_x (s, v);
166 scm_set_cdr_x (s, v);
171 is_number_pair (SCM p)
173 return scm_is_pair (p)
174 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
180 return scm_ihashv (s, ~1u);
186 if (scm_is_integer (s))
188 int i = scm_to_int (s);
189 return i == 0 || i == 1;
197 return scm_is_bool (s) && ly_scm2bool (s);
206 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
210 robust_scm2dir (SCM d, Direction def)
212 if (is_direction (d))
220 if (scm_is_number (s))
222 int i = scm_to_int (s);
223 return i >= -1 && i <= 1;
232 ly_scm2interval (SCM p)
234 return Interval (scm_to_double (scm_car (p)),
235 scm_to_double (scm_cdr (p)));
239 ly_scm2realdrul (SCM p)
241 return Drul_array<Real> (scm_to_double (scm_car (p)),
242 scm_to_double (scm_cdr (p)));
246 ly_interval2scm (Drul_array<Real> i)
248 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
252 robust_scm2interval (SCM k, Drul_array<Real> v)
257 if (is_number_pair (k))
258 i = ly_scm2interval (k);
263 robust_scm2drul (SCM k, Drul_array<Real> v)
265 if (is_number_pair (k))
266 v = ly_scm2interval (k);
271 robust_scm2booldrul (SCM k, Drul_array<bool> def)
275 def[LEFT] = to_boolean (scm_car (k));
276 def[RIGHT] = to_boolean (scm_cdr (k));
285 ly_offset2scm (Offset o)
287 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
291 ly_scm2offset (SCM s)
293 return Offset (scm_to_double (scm_car (s)),
294 scm_to_double (scm_cdr (s)));
298 robust_scm2offset (SCM k, Offset o)
300 if (is_number_pair (k))
301 o = ly_scm2offset (k);
305 ly_offsets2scm (vector<Offset> os)
309 for (vsize i = 0; i < os.size (); i++)
311 *tail = scm_cons (ly_offset2scm (os[i]), SCM_EOL);
312 tail = SCM_CDRLOC (*tail);
318 ly_scm2offsets (SCM s)
321 for (; scm_is_pair (s); s = scm_cdr (s))
322 os.push_back (ly_scm2offset (scm_car (s)));
330 ly_alist_vals (SCM alist)
333 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
334 x = scm_cons (scm_cdar (p), x);
342 /* Return I-th element, or last elt L. If I < 0, then we take the first
345 PRE: length (L) > 0 */
347 robust_list_ref (int i, SCM l)
349 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
355 ly_deep_copy (SCM src)
357 if (scm_is_pair (src))
358 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
359 else if (scm_is_vector (src))
361 int len = scm_c_vector_length (src);
362 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
363 for (int i = 0; i < len; i++)
365 SCM si = scm_from_int (i);
366 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
373 print_scm_val (SCM val)
375 string realval = ly_scm_write_string (val);
376 if (realval.length () > 200)
377 realval = realval.substr (0, 100)
379 + realval.substr (realval.length () - 100);
384 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
392 TODO: should remove #f from allowed vals?
394 if (val == SCM_EOL || val == SCM_BOOL_F)
397 if (!scm_is_symbol (sym))
402 This is used for autoBeamSettings.
404 TODO: deprecate the use of \override and \revert for
407 or use a symbol autoBeamSettingS?
412 SCM type = scm_object_property (sym, type_symbol);
414 if (type != SCM_EOL && !ly_is_procedure (type))
416 warning (_f ("cannot find property type-check for `%s' (%s).",
417 ly_symbol2string (sym).c_str (),
418 ly_symbol2string (type_symbol).c_str ())
419 + " " + _ ("perhaps a typing error?"));
421 /* Be strict when being anal :) */
422 if (do_internal_type_checking_global)
423 scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
426 warning (_ ("doing assignment anyway"));
431 && ly_is_procedure (type)
432 && scm_call_1 (type, val) == SCM_BOOL_F)
435 SCM typefunc = ly_lily_module_constant ("type-name");
436 SCM type_name = scm_call_1 (typefunc, type);
438 warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
439 ly_symbol2string (sym).c_str (),
441 ly_scm2string (type_name).c_str ()));
442 progress_indication ("\n");
450 zijn deze nou handig?
451 zijn ze er al in scheme, maar heten ze anders? */
453 /* Remove doubles from (sorted) list */
457 SCM unique = SCM_EOL;
458 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
460 if (!scm_is_pair (scm_cdr (i))
461 || !ly_is_equal (scm_car (i), scm_cadr (i)))
462 unique = scm_cons (scm_car (i), unique);
464 return scm_reverse_x (unique, SCM_EOL);
467 /* Split list at member s, removing s.
468 Return (BEFORE . AFTER) */
470 ly_split_list (SCM s, SCM list)
472 SCM before = SCM_EOL;
474 for (; scm_is_pair (after);)
476 SCM i = scm_car (after);
477 after = scm_cdr (after);
478 if (ly_is_equal (i, s))
480 before = scm_cons (i, before);
482 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
494 display stuff without using stack
499 SCM p = scm_current_output_port ();
502 for (; scm_is_pair (s); s = scm_cdr (s))
504 scm_display (scm_car (s), p);
508 return SCM_UNSPECIFIED;
512 int_list_to_slice (SCM l)
516 for (; scm_is_pair (l); l = scm_cdr (l))
517 if (scm_is_number (scm_car (l)))
518 s.add_point (scm_to_int (scm_car (l)));
523 robust_scm2double (SCM k, double x)
525 if (scm_is_number (k))
526 x = scm_to_double (k);
531 ly_scm2floatvector (SCM l)
534 for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
535 floats.push_back (robust_scm2double (scm_car (s), 0.0));
540 ly_floatvector2scm (vector<Real> v)
544 for (vsize i = 0; i < v.size (); i++)
546 *tail = scm_cons (scm_from_double (v[i]), SCM_EOL);
547 tail = SCM_CDRLOC (*tail);
553 robust_scm2string (SCM k, string s)
555 if (scm_is_string (k))
556 s = ly_scm2string (k);
561 robust_scm2int (SCM k, int o)
563 if (scm_integer_p (k) == SCM_BOOL_T)
569 robust_scm2vsize (SCM k, vsize o)
571 if (scm_integer_p (k) == SCM_BOOL_T)
573 int i = scm_to_int (k);
581 ly_rational2scm (Rational r)
583 if (r.is_infinity ())
588 return scm_difference (scm_inf (), SCM_UNDEFINED);
591 return scm_divide (scm_from_int64 (r.numerator ()),
592 scm_from_int64 (r.denominator ()));
596 ly_scm2rational (SCM r)
598 if (scm_is_true (scm_inf_p (r)))
600 if (scm_is_true (scm_positive_p (r)))
614 return Rational (scm_to_int64 (scm_numerator (r)),
615 scm_to_int64 (scm_denominator (r)));
619 robust_scm2rational (SCM n, Rational rat)
622 && (scm_is_true (scm_exact_p (n))
623 || scm_is_true (scm_inf_p (n))))
624 return ly_scm2rational (n);
630 alist_to_hashq (SCM alist)
632 int i = scm_ilength (alist);
634 return scm_c_make_hash_table (0);
636 SCM tab = scm_c_make_hash_table (i);
637 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
639 SCM pt = scm_cdar (s);
640 scm_hashq_set_x (tab, scm_caar (s), pt);
646 ly_hash2alist (SCM tab)
648 SCM func = ly_lily_module_constant ("hash-table->alist");
649 return scm_call_1 (func, tab);
657 mangle_cxx_identifier (string cxx_id)
659 if (cxx_id.substr (0, 3) == "ly_")
660 cxx_id = cxx_id.replace (0, 3, "ly:");
663 cxx_id = String_convert::to_lower (cxx_id);
664 cxx_id = "ly:" + cxx_id;
666 if (cxx_id.substr (cxx_id.length () - 2) == "_p")
667 cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "?");
668 else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
669 cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "!");
671 replace_all (&cxx_id, "_less?", "<?");
672 replace_all (&cxx_id, "_2_", "->");
673 replace_all (&cxx_id, "__", "::");
674 replace_all (&cxx_id, '_', '-');
680 ly_string_array_to_scm (vector<string> a)
683 for (vsize i = a.size (); i; i--)
684 s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
688 /* SYMBOLS is a whitespace separated list. */
690 parse_symbol_list (char const *symbols)
692 while (isspace (*symbols))
695 replace_all (&s, '\n', ' ');
696 replace_all (&s, '\t', ' ');
697 replace_all (&s, " ", " ");
698 return ly_string_array_to_scm (string_split (s, ' '));
702 struct ly_t_double_cell
710 /* inserts at front, removing duplicates */
711 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
713 return scm_acons (key, val, scm_assoc_remove_x (alist, key));