Update from Andrew: tweaks for cross-staff chords snippet.
[lilypond.git] / lily / lily-guile.cc
blob9f62fbb670096940e457dda8c8c79bfda6544e61
1 /*
2 lily-guile.cc -- implement assorted SCM interface functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2007 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
10 #include "lily-guile.hh"
12 #include <cstdio>
13 #include <cstdlib>
14 #include <cstring> /* strdup, strchr */
15 #include <cctype>
17 using namespace std;
19 #include "dimensions.hh"
20 #include "direction.hh"
21 #include "file-path.hh"
22 #include "international.hh"
23 #include "libc-extension.hh"
24 #include "main.hh"
25 #include "misc.hh"
26 #include "offset.hh"
27 #include "pitch.hh"
28 #include "string-convert.hh"
29 #include "source-file.hh"
30 #include "version.hh"
31 #include "warn.hh"
35 symbols/strings.
37 string
38 ly_scm_write_string (SCM s)
40 SCM port = scm_mkstrport (SCM_INUM0,
41 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
42 SCM_OPN | SCM_WRTNG,
43 "ly_write2string");
44 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
45 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
47 // scm_apply (write, port, SCM_EOL);
48 scm_call_2 (write, s, port);
49 return ly_scm2string (scm_strport_to_string (port));
52 SCM
53 ly_quote_scm (SCM s)
55 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
58 string
59 ly_symbol2string (SCM s)
62 Ugh. this is not very efficient.
64 SCM str = scm_symbol_to_string (s);
65 return ly_scm2string (str);
68 string
69 gulp_file_to_string (string fn, bool must_exist, int size)
71 string s = global_path.find (fn);
72 if (s == "")
74 if (must_exist)
76 string e = _f ("cannot find file: `%s'", fn);
77 e += " ";
78 e += _f ("(load path: `%s')", global_path.to_string ());
79 error (e);
80 /* unreachable */
82 return s;
85 if (be_verbose_global)
86 progress_indication ("[" + s);
88 vector<char> chars = gulp_file (s, size);
89 string result (&chars[0], chars.size ());
91 if (be_verbose_global)
92 progress_indication ("]");
94 return result;
97 extern "C" {
98 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
99 void
100 ly_display_scm (SCM s)
102 scm_display (s, scm_current_output_port ());
103 scm_newline (scm_current_output_port ());
108 STRINGS
110 string
111 ly_scm2string (SCM str)
113 assert (scm_is_string (str));
114 string result;
115 size_t len = scm_c_string_length (str);
116 if (len) {
117 result.resize(len);
118 scm_to_locale_stringbuf(str, &result.at(0), len);
120 return result;
124 ly_string2scm (string const &str)
126 return scm_from_locale_stringn (str.c_str (),
127 str.length ());
131 char *
132 ly_scm2newstr (SCM str, size_t *lenp)
134 char* p = scm_to_locale_stringn(str, lenp);
135 return p;
139 PAIRS
141 SCM
142 index_get_cell (SCM s, Direction d)
144 assert (d);
145 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
149 index_set_cell (SCM s, Direction d, SCM v)
151 if (d == LEFT)
152 scm_set_car_x (s, v);
153 else if (d == RIGHT)
154 scm_set_cdr_x (s, v);
155 return s;
158 bool
159 is_number_pair (SCM p)
161 return scm_is_pair (p)
162 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
166 unsigned int
167 ly_scm_hash (SCM s)
169 return scm_ihashv (s, ~1u);
172 bool
173 is_axis (SCM s)
175 if (scm_is_number (s))
177 int i = scm_to_int (s);
178 return i == 0 || i == 1;
180 return false;
183 bool
184 to_boolean (SCM s)
186 return scm_is_bool (s) && ly_scm2bool (s);
190 DIRECTIONS
192 Direction
193 to_dir (SCM s)
195 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
198 Direction
199 robust_scm2dir (SCM d, Direction def)
201 if (is_direction (d))
202 def = to_dir (d);
203 return def;
206 bool
207 is_direction (SCM s)
209 if (scm_is_number (s))
211 int i = scm_to_int (s);
212 return i >= -1 && i <= 1;
214 return false;
218 INTERVALS
220 Interval
221 ly_scm2interval (SCM p)
223 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
226 Drul_array<Real>
227 ly_scm2realdrul (SCM p)
229 return Drul_array<Real> (scm_to_double (scm_car (p)),
230 scm_to_double (scm_cdr (p)));
234 ly_interval2scm (Drul_array<Real> i)
236 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
240 Interval
241 robust_scm2interval (SCM k, Drul_array<Real> v)
243 Interval i;
244 i[LEFT] = v[LEFT];
245 i[RIGHT] = v[RIGHT];
246 if (is_number_pair (k))
247 i = ly_scm2interval (k);
248 return i;
251 Drul_array<Real>
252 robust_scm2drul (SCM k, Drul_array<Real> v)
254 if (is_number_pair (k))
255 v = ly_scm2interval (k);
256 return v;
259 Drul_array<bool>
260 robust_scm2booldrul (SCM k, Drul_array<bool> def)
262 if (scm_is_pair (k))
264 def[LEFT] = to_boolean (scm_car (k));
265 def[RIGHT] = to_boolean (scm_cdr (k));
267 return def;
271 OFFSET
274 ly_offset2scm (Offset o)
276 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
279 Offset
280 ly_scm2offset (SCM s)
282 return Offset (scm_to_double (scm_car (s)),
283 scm_to_double (scm_cdr (s)));
286 Offset
287 robust_scm2offset (SCM k, Offset o)
289 if (is_number_pair (k))
290 o = ly_scm2offset (k);
291 return o;
294 ly_offsets2scm (vector<Offset> os)
296 SCM l = SCM_EOL;
297 SCM *tail = &l;
298 for (vsize i = 0; i < os.size (); i++)
300 *tail = scm_cons (ly_offset2scm (os[i]), SCM_EOL);
301 tail = SCM_CDRLOC (*tail);
303 return l;
306 vector<Offset>
307 ly_scm2offsets (SCM s)
309 vector<Offset> os;
310 for (; scm_is_pair (s); s = scm_cdr (s))
311 os.push_back (ly_scm2offset (scm_car (s)));
312 return os;
319 ALIST
322 bool
323 alist_equal_p (SCM a, SCM b)
325 for (SCM s = a;
326 scm_is_pair (s); s = scm_cdr (s))
328 SCM key = scm_caar (s);
329 SCM val = scm_cdar (s);
330 SCM l = scm_assoc (key, b);
332 if (l == SCM_BOOL_F
333 || !ly_is_equal (scm_cdr (l), val))
335 return false;
337 return true;
341 ly_alist_vals (SCM alist)
343 SCM x = SCM_EOL;
344 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
345 x = scm_cons (scm_cdar (p), x);
346 return x;
350 LISTS
353 /* Return I-th element, or last elt L. If I < 0, then we take the first
354 element.
356 PRE: length (L) > 0 */
358 robust_list_ref (int i, SCM l)
360 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
361 l = scm_cdr (l);
362 return scm_car (l);
367 ly_deep_copy (SCM src)
369 if (scm_is_pair (src))
370 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
371 else if (scm_is_vector (src))
373 int len = scm_c_vector_length (src);
374 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
375 for (int i = 0;i < len; i++)
377 SCM si = scm_from_int (i);
378 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
381 return src;
384 string
385 print_scm_val (SCM val)
387 string realval = ly_scm_write_string (val);
388 if (realval.length () > 200)
389 realval = realval.substr (0, 100)
390 + "\n :\n :\n"
391 + realval.substr (realval.length () - 100);
392 return realval;
395 bool
396 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
398 bool ok = true;
401 Always succeeds.
404 TODO: should remove #f from allowed vals?
406 if (val == SCM_EOL || val == SCM_BOOL_F)
407 return ok;
409 if (!scm_is_symbol (sym))
410 #if 0
411 return false;
412 #else
414 This is used for autoBeamSettings.
416 TODO: deprecate the use of \override and \revert for
417 autoBeamSettings?
419 or use a symbol autoBeamSettingS?
421 return true;
422 #endif
424 SCM type = scm_object_property (sym, type_symbol);
426 if (type != SCM_EOL && !ly_is_procedure (type))
428 warning (_f ("cannot find property type-check for `%s' (%s).",
429 ly_symbol2string (sym).c_str (),
430 ly_symbol2string (type_symbol).c_str ())
431 + " " + _ ("perhaps a typing error?"));
433 /* Be strict when being anal :) */
434 if (do_internal_type_checking_global)
435 scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
436 sym, val));
438 warning (_ ("doing assignment anyway"));
440 else
442 if (val != SCM_EOL
443 && ly_is_procedure (type)
444 && scm_call_1 (type, val) == SCM_BOOL_F)
446 ok = false;
447 SCM typefunc = ly_lily_module_constant ("type-name");
448 SCM type_name = scm_call_1 (typefunc, type);
450 warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
451 ly_symbol2string (sym).c_str (),
452 print_scm_val (val),
453 ly_scm2string (type_name).c_str ()));
454 progress_indication ("\n");
457 return ok;
460 /* some SCM abbrevs
462 zijn deze nou handig?
463 zijn ze er al in scheme, maar heten ze anders? */
465 /* Remove doubles from (sorted) list */
467 ly_unique (SCM list)
469 SCM unique = SCM_EOL;
470 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
472 if (!scm_is_pair (scm_cdr (i))
473 || !ly_is_equal (scm_car (i), scm_cadr (i)))
474 unique = scm_cons (scm_car (i), unique);
476 return scm_reverse_x (unique, SCM_EOL);
480 /* Split list at member s, removing s.
481 Return (BEFORE . AFTER) */
483 ly_split_list (SCM s, SCM list)
485 SCM before = SCM_EOL;
486 SCM after = list;
487 for (; scm_is_pair (after);)
489 SCM i = scm_car (after);
490 after = scm_cdr (after);
491 if (ly_is_equal (i, s))
492 break;
493 before = scm_cons (i, before);
495 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
498 void
499 taint (SCM *)
502 nop.
507 display stuff without using stack
510 display_list (SCM s)
512 SCM p = scm_current_output_port ();
514 scm_puts ("(", p);
515 for (; scm_is_pair (s); s = scm_cdr (s))
517 scm_display (scm_car (s), p);
518 scm_puts (" ", p);
520 scm_puts (")", p);
521 return SCM_UNSPECIFIED;
524 Slice
525 int_list_to_slice (SCM l)
527 Slice s;
528 s.set_empty ();
529 for (; scm_is_pair (l); l = scm_cdr (l))
530 if (scm_is_number (scm_car (l)))
531 s.add_point (scm_to_int (scm_car (l)));
532 return s;
535 Real
536 robust_scm2double (SCM k, double x)
538 if (scm_is_number (k))
539 x = scm_to_double (k);
540 return x;
544 string
545 robust_scm2string (SCM k, string s)
547 if (scm_is_string (k))
548 s = ly_scm2string (k);
549 return s;
553 robust_scm2int (SCM k, int o)
555 if (scm_integer_p (k) == SCM_BOOL_T)
556 o = scm_to_int (k);
557 return o;
562 ly_rational2scm (Rational r)
564 return scm_divide (scm_from_int (r.numerator ()), scm_from_int (r.denominator ()));
568 Rational
569 ly_scm2rational (SCM r)
571 return Rational (scm_to_int (scm_numerator (r)),
572 scm_to_int (scm_denominator (r)));
575 Rational
576 robust_scm2rational (SCM n, Rational rat)
578 if (ly_is_fraction (n))
579 return ly_scm2rational (n);
580 else
581 return rat;
585 alist_to_hashq (SCM alist)
587 int i = scm_ilength (alist);
588 if (i < 0)
589 return scm_c_make_hash_table (0);
591 SCM tab = scm_c_make_hash_table (i);
592 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
594 SCM pt = scm_cdar (s);
595 scm_hashq_set_x (tab, scm_caar (s), pt);
597 return tab;
601 ly_hash2alist (SCM tab)
603 SCM func = ly_lily_module_constant ("hash-table->alist");
604 return scm_call_1 (func, tab);
609 C++ interfacing.
612 string
613 mangle_cxx_identifier (string cxx_id)
615 if (cxx_id.substr (0, 3) == "ly_")
616 cxx_id = cxx_id.replace (0, 3, "ly:");
617 else
619 cxx_id = String_convert::to_lower (cxx_id);
620 cxx_id = "ly:" + cxx_id;
622 if (cxx_id.substr (cxx_id.length () - 2) == "_p")
623 cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "?");
624 else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
625 cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "!");
627 replace_all (&cxx_id, "_less?", "<?");
628 replace_all (&cxx_id, "_2_", "->");
629 replace_all (&cxx_id, "__", "::");
630 replace_all (&cxx_id, '_', '-');
633 return cxx_id;
639 ly_string_array_to_scm (vector<string> a)
641 SCM s = SCM_EOL;
642 for (vsize i = a.size (); i ; i--)
643 s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
644 return s;
647 /* SYMBOLS is a whitespace separated list. */
649 parse_symbol_list (char const *symbols)
651 while (isspace (*symbols))
652 *symbols++;
653 string s = symbols;
654 replace_all (&s, '\n', ' ');
655 replace_all (&s, '\t', ' ');
656 replace_all (&s, " ", " ");
657 return ly_string_array_to_scm (string_split (s, ' '));
660 /* GDB debugging. */
661 struct ly_t_double_cell
663 SCM a;
664 SCM b;
665 SCM c;
666 SCM d;
669 /* inserts at front, removing duplicates */
670 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
672 return scm_acons (key, val, scm_assoc_remove_x (alist, key));