-b eps -> -dbackend=eps
[lilypond.git] / lily / lily-guile.cc
blob9da6823f194e89e91ab15a13c4ab751ed3b4d03e
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 return string (scm_i_string_chars (str),
115 (int) scm_i_string_length (str));
119 ly_string2scm (string const &str)
121 return scm_from_locale_stringn (str.c_str (),
122 str.length ());
126 char *
127 ly_scm2newstr (SCM str, size_t *lenp)
129 LY_ASSERT_TYPE (scm_is_string, str, 1);
131 size_t len = scm_i_string_length (str);
132 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
134 memcpy (new_str, scm_i_string_chars (str), len);
135 new_str[len] = '\0';
137 if (lenp)
138 *lenp = len;
140 return new_str;
142 return 0;
147 PAIRS
150 index_get_cell (SCM s, Direction d)
153 assert (d);
154 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
158 index_set_cell (SCM s, Direction d, SCM v)
160 if (d == LEFT)
161 scm_set_car_x (s, v);
162 else if (d == RIGHT)
163 scm_set_cdr_x (s, v);
164 return s;
167 bool
168 is_number_pair (SCM p)
170 return scm_is_pair (p)
171 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
175 unsigned int
176 ly_scm_hash (SCM s)
178 return scm_ihashv (s, ~1u);
182 bool
183 is_axis (SCM s)
185 if (scm_is_number (s))
187 int i = scm_to_int (s);
188 return i == 0 || i == 1;
190 return false;
193 bool
194 to_boolean (SCM s)
196 return scm_is_bool (s) && ly_scm2bool (s);
200 DIRECTIONS
202 Direction
203 to_dir (SCM s)
205 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
208 Direction
209 robust_scm2dir (SCM d, Direction def)
211 if (is_direction (d))
212 def = to_dir (d);
213 return def;
216 bool
217 is_direction (SCM s)
219 if (scm_is_number (s))
221 int i = scm_to_int (s);
222 return i >= -1 && i <= 1;
224 return false;
228 INTERVALS
230 Interval
231 ly_scm2interval (SCM p)
233 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
236 Drul_array<Real>
237 ly_scm2realdrul (SCM p)
239 return Drul_array<Real> (scm_to_double (scm_car (p)),
240 scm_to_double (scm_cdr (p)));
244 ly_interval2scm (Drul_array<Real> i)
246 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
250 Interval
251 robust_scm2interval (SCM k, Drul_array<Real> v)
253 Interval i;
254 i[LEFT] = v[LEFT];
255 i[RIGHT] = v[RIGHT];
256 if (is_number_pair (k))
257 i = ly_scm2interval (k);
258 return i;
261 Drul_array<Real>
262 robust_scm2drul (SCM k, Drul_array<Real> v)
264 if (is_number_pair (k))
265 v = ly_scm2interval (k);
266 return v;
269 Drul_array<bool>
270 robust_scm2booldrul (SCM k, Drul_array<bool> def)
272 if (scm_is_pair (k))
274 def[LEFT] = to_boolean (scm_car (k));
275 def[RIGHT] = to_boolean (scm_cdr (k));
277 return def;
281 OFFSET
284 ly_offset2scm (Offset o)
286 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
289 Offset
290 ly_scm2offset (SCM s)
292 return Offset (scm_to_double (scm_car (s)),
293 scm_to_double (scm_cdr (s)));
296 Offset
297 robust_scm2offset (SCM k, Offset o)
299 if (is_number_pair (k))
300 o = ly_scm2offset (k);
301 return o;
304 ly_offsets2scm (vector<Offset> os)
306 SCM l = SCM_EOL;
307 SCM *tail = &l;
308 for (vsize i = 0; i < os.size (); i++)
310 *tail = scm_cons (ly_offset2scm (os[i]), SCM_EOL);
311 tail = SCM_CDRLOC (*tail);
313 return l;
316 vector<Offset>
317 ly_scm2offsets (SCM s)
319 vector<Offset> os;
320 for (; scm_is_pair (s); s = scm_cdr (s))
321 os.push_back (ly_scm2offset (scm_car (s)));
322 return os;
329 ALIST
332 bool
333 alist_equal_p (SCM a, SCM b)
335 for (SCM s = a;
336 scm_is_pair (s); s = scm_cdr (s))
338 SCM key = scm_caar (s);
339 SCM val = scm_cdar (s);
340 SCM l = scm_assoc (key, b);
342 if (l == SCM_BOOL_F
343 || !ly_is_equal (scm_cdr (l), val))
345 return false;
347 return true;
351 ly_alist_vals (SCM alist)
353 SCM x = SCM_EOL;
354 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
355 x = scm_cons (scm_cdar (p), x);
356 return x;
360 LISTS
363 /* Return I-th element, or last elt L. If I < 0, then we take the first
364 element.
366 PRE: length (L) > 0 */
368 robust_list_ref (int i, SCM l)
370 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
371 l = scm_cdr (l);
372 return scm_car (l);
377 ly_deep_copy (SCM src)
379 if (scm_is_pair (src))
380 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
381 else if (scm_is_vector (src))
383 int len = scm_c_vector_length (src);
384 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
385 for (int i = 0;i < len; i++)
387 SCM si = scm_from_int (i);
388 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
391 return src;
394 string
395 print_scm_val (SCM val)
397 string realval = ly_scm_write_string (val);
398 if (realval.length () > 200)
399 realval = realval.substr (0, 100)
400 + "\n :\n :\n"
401 + realval.substr (realval.length () - 100);
402 return realval;
405 bool
406 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
408 bool ok = true;
411 Always succeeds.
414 TODO: should remove #f from allowed vals?
416 if (val == SCM_EOL || val == SCM_BOOL_F)
417 return ok;
419 if (!scm_is_symbol (sym))
420 #if 0
421 return false;
422 #else
424 This is used for autoBeamSettings.
426 TODO: deprecate the use of \override and \revert for
427 autoBeamSettings?
429 or use a symbol autoBeamSettingS?
431 return true;
432 #endif
434 SCM type = scm_object_property (sym, type_symbol);
436 if (type != SCM_EOL && !ly_is_procedure (type))
438 warning (_f ("cannot find property type-check for `%s' (%s).",
439 ly_symbol2string (sym).c_str (),
440 ly_symbol2string (type_symbol).c_str ())
441 + " " + _ ("perhaps a typing error?"));
443 /* Be strict when being anal :) */
444 if (do_internal_type_checking_global)
445 scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
446 sym, val));
448 warning (_ ("doing assignment anyway"));
450 else
452 if (val != SCM_EOL
453 && ly_is_procedure (type)
454 && scm_call_1 (type, val) == SCM_BOOL_F)
456 ok = false;
457 SCM typefunc = ly_lily_module_constant ("type-name");
458 SCM type_name = scm_call_1 (typefunc, type);
460 warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
461 ly_symbol2string (sym).c_str (),
462 print_scm_val (val),
463 ly_scm2string (type_name).c_str ()));
464 progress_indication ("\n");
467 return ok;
470 /* some SCM abbrevs
472 zijn deze nou handig?
473 zijn ze er al in scheme, maar heten ze anders? */
475 /* Remove doubles from (sorted) list */
477 ly_unique (SCM list)
479 SCM unique = SCM_EOL;
480 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
482 if (!scm_is_pair (scm_cdr (i))
483 || !ly_is_equal (scm_car (i), scm_cadr (i)))
484 unique = scm_cons (scm_car (i), unique);
486 return scm_reverse_x (unique, SCM_EOL);
490 /* Split list at member s, removing s.
491 Return (BEFORE . AFTER) */
493 ly_split_list (SCM s, SCM list)
495 SCM before = SCM_EOL;
496 SCM after = list;
497 for (; scm_is_pair (after);)
499 SCM i = scm_car (after);
500 after = scm_cdr (after);
501 if (ly_is_equal (i, s))
502 break;
503 before = scm_cons (i, before);
505 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
508 void
509 taint (SCM *)
512 nop.
517 display stuff without using stack
520 display_list (SCM s)
522 SCM p = scm_current_output_port ();
524 scm_puts ("(", p);
525 for (; scm_is_pair (s); s = scm_cdr (s))
527 scm_display (scm_car (s), p);
528 scm_puts (" ", p);
530 scm_puts (")", p);
531 return SCM_UNSPECIFIED;
534 Slice
535 int_list_to_slice (SCM l)
537 Slice s;
538 s.set_empty ();
539 for (; scm_is_pair (l); l = scm_cdr (l))
540 if (scm_is_number (scm_car (l)))
541 s.add_point (scm_to_int (scm_car (l)));
542 return s;
545 Real
546 robust_scm2double (SCM k, double x)
548 if (scm_is_number (k))
549 x = scm_to_double (k);
550 return x;
554 string
555 robust_scm2string (SCM k, string s)
557 if (scm_is_string (k))
558 s = ly_scm2string (k);
559 return s;
563 robust_scm2int (SCM k, int o)
565 if (scm_integer_p (k) == SCM_BOOL_T)
566 o = scm_to_int (k);
567 return o;
572 ly_rational2scm (Rational r)
574 return scm_divide (scm_from_int (r.numerator ()), scm_from_int (r.denominator ()));
578 Rational
579 ly_scm2rational (SCM r)
581 return Rational (scm_to_int (scm_numerator (r)),
582 scm_to_int (scm_denominator (r)));
585 Rational
586 robust_scm2rational (SCM n, Rational rat)
588 if (ly_is_fraction (n))
589 return ly_scm2rational (n);
590 else
591 return rat;
595 alist_to_hashq (SCM alist)
597 int i = scm_ilength (alist);
598 if (i < 0)
599 return scm_c_make_hash_table (0);
601 SCM tab = scm_c_make_hash_table (i);
602 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
604 SCM pt = scm_cdar (s);
605 scm_hashq_set_x (tab, scm_caar (s), pt);
607 return tab;
611 ly_hash2alist (SCM tab)
613 SCM func = ly_lily_module_constant ("hash-table->alist");
614 return scm_call_1 (func, tab);
619 C++ interfacing.
622 string
623 mangle_cxx_identifier (string cxx_id)
625 if (cxx_id.substr (0, 3) == "ly_")
626 cxx_id = cxx_id.replace (0, 3, "ly:");
627 else
629 cxx_id = String_convert::to_lower (cxx_id);
630 cxx_id = "ly:" + cxx_id;
632 if (cxx_id.substr (cxx_id.length () - 2) == "_p")
633 cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "?");
634 else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
635 cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "!");
637 cxx_id = replace_all (cxx_id, "_less?", "<?");
638 cxx_id = replace_all (cxx_id, "_2_", "->");
639 cxx_id = replace_all (cxx_id, "__", "::");
640 cxx_id = replace_all (cxx_id, '_', '-');
643 return cxx_id;
649 ly_string_array_to_scm (vector<string> a)
651 SCM s = SCM_EOL;
652 for (vsize i = a.size (); i ; i--)
653 s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
654 return s;
657 /* SYMBOLS is a whitespace separated list. */
659 parse_symbol_list (char const *symbols)
661 while (isspace (*symbols))
662 *symbols++;
663 string s = symbols;
664 replace_all (s, '\n', ' ');
665 replace_all (s, '\t', ' ');
666 replace_all (s, " ", " ");
667 return ly_string_array_to_scm (string_split (s, ' '));
670 /* GDB debugging. */
671 struct ly_t_double_cell
673 SCM a;
674 SCM b;
675 SCM c;
676 SCM d;
679 /* inserts at front, removing duplicates */
680 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
682 return scm_acons (key, val, scm_assoc_remove_x (alist, key));