2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2011 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_number (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)), scm_to_double (scm_cdr (p)));
238 ly_scm2realdrul (SCM p)
240 return Drul_array<Real> (scm_to_double (scm_car (p)),
241 scm_to_double (scm_cdr (p)));
245 ly_interval2scm (Drul_array<Real> i)
247 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
251 robust_scm2interval (SCM k, Drul_array<Real> v)
256 if (is_number_pair (k))
257 i = ly_scm2interval (k);
262 robust_scm2drul (SCM k, Drul_array<Real> v)
264 if (is_number_pair (k))
265 v = ly_scm2interval (k);
270 robust_scm2booldrul (SCM k, Drul_array<bool> def)
274 def[LEFT] = to_boolean (scm_car (k));
275 def[RIGHT] = to_boolean (scm_cdr (k));
284 ly_offset2scm (Offset o)
286 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
290 ly_scm2offset (SCM s)
292 return Offset (scm_to_double (scm_car (s)),
293 scm_to_double (scm_cdr (s)));
297 robust_scm2offset (SCM k, Offset o)
299 if (is_number_pair (k))
300 o = ly_scm2offset (k);
304 ly_offsets2scm (vector<Offset> os)
308 for (vsize i = 0; i < os.size (); i++)
310 *tail = scm_cons (ly_offset2scm (os[i]), SCM_EOL);
311 tail = SCM_CDRLOC (*tail);
317 ly_scm2offsets (SCM s)
320 for (; scm_is_pair (s); s = scm_cdr (s))
321 os.push_back (ly_scm2offset (scm_car (s)));
330 alist_equal_p (SCM a, SCM b)
333 scm_is_pair (s); s = scm_cdr (s))
335 SCM key = scm_caar (s);
336 SCM val = scm_cdar (s);
337 SCM l = scm_assoc (key, b);
340 || !ly_is_equal (scm_cdr (l), val))
348 ly_alist_vals (SCM alist)
351 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
352 x = scm_cons (scm_cdar (p), x);
360 /* Return I-th element, or last elt L. If I < 0, then we take the first
363 PRE: length (L) > 0 */
365 robust_list_ref (int i, SCM l)
367 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
373 ly_deep_copy (SCM src)
375 if (scm_is_pair (src))
376 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
377 else if (scm_is_vector (src))
379 int len = scm_c_vector_length (src);
380 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
381 for (int i = 0; i < len; i++)
383 SCM si = scm_from_int (i);
384 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
391 print_scm_val (SCM val)
393 string realval = ly_scm_write_string (val);
394 if (realval.length () > 200)
395 realval = realval.substr (0, 100)
397 + realval.substr (realval.length () - 100);
402 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
410 TODO: should remove #f from allowed vals?
412 if (val == SCM_EOL || val == SCM_BOOL_F)
415 if (!scm_is_symbol (sym))
420 This is used for autoBeamSettings.
422 TODO: deprecate the use of \override and \revert for
425 or use a symbol autoBeamSettingS?
430 SCM type = scm_object_property (sym, type_symbol);
432 if (type != SCM_EOL && !ly_is_procedure (type))
434 warning (_f ("cannot find property type-check for `%s' (%s).",
435 ly_symbol2string (sym).c_str (),
436 ly_symbol2string (type_symbol).c_str ())
437 + " " + _ ("perhaps a typing error?"));
439 /* Be strict when being anal :) */
440 if (do_internal_type_checking_global)
441 scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
444 warning (_ ("doing assignment anyway"));
449 && ly_is_procedure (type)
450 && scm_call_1 (type, val) == SCM_BOOL_F)
453 SCM typefunc = ly_lily_module_constant ("type-name");
454 SCM type_name = scm_call_1 (typefunc, type);
456 warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
457 ly_symbol2string (sym).c_str (),
459 ly_scm2string (type_name).c_str ()));
460 progress_indication ("\n");
468 zijn deze nou handig?
469 zijn ze er al in scheme, maar heten ze anders? */
471 /* Remove doubles from (sorted) list */
475 SCM unique = SCM_EOL;
476 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
478 if (!scm_is_pair (scm_cdr (i))
479 || !ly_is_equal (scm_car (i), scm_cadr (i)))
480 unique = scm_cons (scm_car (i), unique);
482 return scm_reverse_x (unique, SCM_EOL);
485 /* Split list at member s, removing s.
486 Return (BEFORE . AFTER) */
488 ly_split_list (SCM s, SCM list)
490 SCM before = SCM_EOL;
492 for (; scm_is_pair (after);)
494 SCM i = scm_car (after);
495 after = scm_cdr (after);
496 if (ly_is_equal (i, s))
498 before = scm_cons (i, before);
500 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
512 display stuff without using stack
517 SCM p = scm_current_output_port ();
520 for (; scm_is_pair (s); s = scm_cdr (s))
522 scm_display (scm_car (s), p);
526 return SCM_UNSPECIFIED;
530 int_list_to_slice (SCM l)
534 for (; scm_is_pair (l); l = scm_cdr (l))
535 if (scm_is_number (scm_car (l)))
536 s.add_point (scm_to_int (scm_car (l)));
541 robust_scm2double (SCM k, double x)
543 if (scm_is_number (k))
544 x = scm_to_double (k);
549 robust_scm2string (SCM k, string s)
551 if (scm_is_string (k))
552 s = ly_scm2string (k);
557 robust_scm2int (SCM k, int o)
559 if (scm_integer_p (k) == SCM_BOOL_T)
565 ly_rational2scm (Rational r)
567 return scm_divide (scm_from_int64 (r.numerator ()),
568 scm_from_int64 (r.denominator ()));
572 ly_scm2rational (SCM r)
574 return Rational (scm_to_int64 (scm_numerator (r)),
575 scm_to_int64 (scm_denominator (r)));
579 robust_scm2rational (SCM n, Rational rat)
581 if (ly_is_fraction (n))
582 return ly_scm2rational (n);
588 alist_to_hashq (SCM alist)
590 int i = scm_ilength (alist);
592 return scm_c_make_hash_table (0);
594 SCM tab = scm_c_make_hash_table (i);
595 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
597 SCM pt = scm_cdar (s);
598 scm_hashq_set_x (tab, scm_caar (s), pt);
604 ly_hash2alist (SCM tab)
606 SCM func = ly_lily_module_constant ("hash-table->alist");
607 return scm_call_1 (func, tab);
615 mangle_cxx_identifier (string cxx_id)
617 if (cxx_id.substr (0, 3) == "ly_")
618 cxx_id = cxx_id.replace (0, 3, "ly:");
621 cxx_id = String_convert::to_lower (cxx_id);
622 cxx_id = "ly:" + cxx_id;
624 if (cxx_id.substr (cxx_id.length () - 2) == "_p")
625 cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "?");
626 else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
627 cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "!");
629 replace_all (&cxx_id, "_less?", "<?");
630 replace_all (&cxx_id, "_2_", "->");
631 replace_all (&cxx_id, "__", "::");
632 replace_all (&cxx_id, '_', '-');
638 ly_string_array_to_scm (vector<string> a)
641 for (vsize i = a.size (); i; i--)
642 s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
646 /* SYMBOLS is a whitespace separated list. */
648 parse_symbol_list (char const *symbols)
650 while (isspace (*symbols))
653 replace_all (&s, '\n', ' ');
654 replace_all (&s, '\t', ' ');
655 replace_all (&s, " ", " ");
656 return ly_string_array_to_scm (string_split (s, ' '));
660 struct ly_t_double_cell
668 /* inserts at front, removing duplicates */
669 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
671 return scm_acons (key, val, scm_assoc_remove_x (alist, key));