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 SCM str = scm_symbol_to_string (s);
75 return ly_scm2string (str);
79 gulp_file_to_string (string fn, bool must_exist, int size)
81 string s = global_path.find (fn);
86 string e = _f ("cannot find file: `%s'", fn);
88 e += _f ("(load path: `%s')", global_path.to_string ());
95 debug_output ("[" + s, true);
97 vector<char> chars = gulp_file (s, size);
98 string result (&chars[0], chars.size ());
100 debug_output ("]\n", false);
106 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
108 ly_display_scm (SCM s)
110 scm_display (s, scm_current_output_port ());
111 scm_newline (scm_current_output_port ());
119 ly_scm2string (SCM str)
121 assert (scm_is_string (str));
123 size_t len = scm_c_string_length (str);
127 scm_to_locale_stringbuf (str, &result.at (0), len);
133 ly_string2scm (string const &str)
135 return scm_from_locale_stringn (str.c_str (),
140 ly_scm2str0 (SCM str)
142 return scm_to_locale_string (str);
149 index_get_cell (SCM s, Direction d)
152 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
156 index_set_cell (SCM s, Direction d, SCM v)
159 scm_set_car_x (s, v);
161 scm_set_cdr_x (s, v);
166 is_number_pair (SCM p)
168 return scm_is_pair (p)
169 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
175 return scm_ihashv (s, ~1u);
181 if (scm_is_integer (s))
183 int i = scm_to_int (s);
184 return i == 0 || i == 1;
192 return scm_is_bool (s) && ly_scm2bool (s);
201 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
205 robust_scm2dir (SCM d, Direction def)
207 if (is_direction (d))
215 if (scm_is_integer (s))
217 int i = scm_to_int (s);
218 return i >= -1 && i <= 1;
227 ly_scm2interval (SCM p)
229 return is_number_pair (p) ?
230 Interval (scm_to_double (scm_car (p)),
231 scm_to_double (scm_cdr (p))) :
236 ly_scm2realdrul (SCM p)
238 return Drul_array<Real> (scm_to_double (scm_car (p)),
239 scm_to_double (scm_cdr (p)));
243 ly_interval2scm (Drul_array<Real> i)
245 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
249 robust_scm2interval (SCM k, Drul_array<Real> v)
254 if (is_number_pair (k))
255 i = ly_scm2interval (k);
260 robust_scm2drul (SCM k, Drul_array<Real> v)
262 if (is_number_pair (k))
263 v = ly_scm2interval (k);
268 robust_scm2booldrul (SCM k, Drul_array<bool> def)
272 def[LEFT] = to_boolean (scm_car (k));
273 def[RIGHT] = to_boolean (scm_cdr (k));
282 ly_offset2scm (Offset o)
284 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
288 ly_scm2offset (SCM s)
290 return is_number_pair (s) ?
291 Offset (scm_to_double (scm_car (s)),
292 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)));
328 // This one is used nowhere.
330 ly_is_alist_equal (SCM a, SCM b)
332 if (!scm_is_pair (a) || !scm_is_pair (b))
334 for (SCM s = a; scm_is_pair (s); s = scm_cdr (s))
336 SCM key = scm_caar (s);
337 SCM val = scm_cdar (s);
338 SCM l = scm_assoc (key, b);
340 if (scm_is_false (l) || !ly_is_equal (scm_cdr (l), val))
347 ly_alist_vals (SCM alist)
350 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
351 x = scm_cons (scm_cdar (p), x);
359 /* Return I-th element, or last elt L. If I < 0, then we take the first
362 PRE: length (L) > 0 */
364 robust_list_ref (int i, SCM l)
366 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
372 ly_deep_copy (SCM src)
374 if (scm_is_pair (src))
375 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
376 else if (scm_is_vector (src))
378 int len = scm_c_vector_length (src);
379 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
380 for (int i = 0; i < len; i++)
382 SCM si = scm_from_int (i);
383 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
390 print_scm_val (SCM val)
392 string realval = ly_scm_write_string (val);
393 if (realval.length () > 200)
394 realval = realval.substr (0, 100)
396 + realval.substr (realval.length () - 100);
401 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
409 TODO: should remove #f from allowed vals?
411 if (val == SCM_EOL || val == SCM_BOOL_F)
414 if (!scm_is_symbol (sym))
419 This is used for autoBeamSettings.
421 TODO: deprecate the use of \override and \revert for
424 or use a symbol autoBeamSettingS?
429 SCM type = scm_object_property (sym, type_symbol);
431 if (type != SCM_EOL && !ly_is_procedure (type))
433 warning (_f ("cannot find property type-check for `%s' (%s).",
434 ly_symbol2string (sym).c_str (),
435 ly_symbol2string (type_symbol).c_str ())
436 + " " + _ ("perhaps a typing error?"));
438 /* Be strict when being anal :) */
439 if (do_internal_type_checking_global)
440 scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
443 warning (_ ("doing assignment anyway"));
448 && ly_is_procedure (type)
449 && scm_call_1 (type, val) == SCM_BOOL_F)
452 SCM typefunc = ly_lily_module_constant ("type-name");
453 SCM type_name = scm_call_1 (typefunc, type);
455 warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
456 ly_symbol2string (sym).c_str (),
458 ly_scm2string (type_name).c_str ()));
459 progress_indication ("\n");
467 zijn deze nou handig?
468 zijn ze er al in scheme, maar heten ze anders? */
470 /* Remove doubles from (sorted) list */
474 SCM unique = SCM_EOL;
475 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
477 if (!scm_is_pair (scm_cdr (i))
478 || !ly_is_equal (scm_car (i), scm_cadr (i)))
479 unique = scm_cons (scm_car (i), unique);
481 return scm_reverse_x (unique, SCM_EOL);
484 /* Split list at member s, removing s.
485 Return (BEFORE . AFTER) */
487 ly_split_list (SCM s, SCM list)
489 SCM before = SCM_EOL;
491 for (; scm_is_pair (after);)
493 SCM i = scm_car (after);
494 after = scm_cdr (after);
495 if (ly_is_equal (i, s))
497 before = scm_cons (i, before);
499 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
511 display stuff without using stack
516 SCM p = scm_current_output_port ();
519 for (; scm_is_pair (s); s = scm_cdr (s))
521 scm_display (scm_car (s), p);
525 return SCM_UNSPECIFIED;
529 int_list_to_slice (SCM l)
533 for (; scm_is_pair (l); l = scm_cdr (l))
534 if (scm_is_number (scm_car (l)))
535 s.add_point (scm_to_int (scm_car (l)));
540 robust_scm2double (SCM k, double x)
542 if (scm_is_number (k))
543 x = scm_to_double (k);
548 robust_scm2string (SCM k, string s)
550 if (scm_is_string (k))
551 s = ly_scm2string (k);
556 robust_scm2int (SCM k, int o)
558 if (scm_integer_p (k) == SCM_BOOL_T)
564 ly_rational2scm (Rational r)
566 return scm_divide (scm_from_int64 (r.numerator ()),
567 scm_from_int64 (r.denominator ()));
571 ly_scm2rational (SCM r)
573 return Rational (scm_to_int64 (scm_numerator (r)),
574 scm_to_int64 (scm_denominator (r)));
578 robust_scm2rational (SCM n, Rational rat)
580 if (ly_is_fraction (n))
581 return ly_scm2rational (n);
587 alist_to_hashq (SCM alist)
589 int i = scm_ilength (alist);
591 return scm_c_make_hash_table (0);
593 SCM tab = scm_c_make_hash_table (i);
594 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
596 SCM pt = scm_cdar (s);
597 scm_hashq_set_x (tab, scm_caar (s), pt);
603 ly_hash2alist (SCM tab)
605 SCM func = ly_lily_module_constant ("hash-table->alist");
606 return scm_call_1 (func, tab);
614 mangle_cxx_identifier (string cxx_id)
616 if (cxx_id.substr (0, 3) == "ly_")
617 cxx_id = cxx_id.replace (0, 3, "ly:");
620 cxx_id = String_convert::to_lower (cxx_id);
621 cxx_id = "ly:" + cxx_id;
623 if (cxx_id.substr (cxx_id.length () - 2) == "_p")
624 cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "?");
625 else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
626 cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "!");
628 replace_all (&cxx_id, "_less?", "<?");
629 replace_all (&cxx_id, "_2_", "->");
630 replace_all (&cxx_id, "__", "::");
631 replace_all (&cxx_id, '_', '-');
637 ly_string_array_to_scm (vector<string> a)
640 for (vsize i = a.size (); i; i--)
641 s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
645 /* SYMBOLS is a whitespace separated list. */
647 parse_symbol_list (char const *symbols)
649 while (isspace (*symbols))
652 replace_all (&s, '\n', ' ');
653 replace_all (&s, '\t', ' ');
654 replace_all (&s, " ", " ");
655 return ly_string_array_to_scm (string_split (s, ' '));
659 struct ly_t_double_cell
667 /* inserts at front, removing duplicates */
668 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
670 return scm_acons (key, val, scm_assoc_remove_x (alist, key));