2 lily-guile.cc -- implement assorted SCM interface functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "lily-guile.hh"
14 #include <cstring> /* strdup, strchr */
21 #include "dimensions.hh"
22 #include "direction.hh"
23 #include "file-path.hh"
24 #include "international.hh"
25 #include "libc-extension.hh"
30 #include "string-convert.hh"
31 #include "source-file.hh"
38 ly_to_symbol (SCM scm)
40 return scm_string_to_symbol (ly_to_string (scm));
44 ly_to_string (SCM scm)
46 return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
48 scm_makfrom0str ("~S"), scm);
54 return scm_car (scm_last_pair (list));
60 SCM port = scm_mkstrport (SCM_INUM0,
61 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
64 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
65 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
67 // scm_apply (write, port, SCM_EOL);
68 scm_call_2 (write, s, port);
69 return scm_strport_to_string (port);
75 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
79 ly_symbol2string (SCM s)
82 Ugh. this is not very efficient.
84 SCM str = scm_symbol_to_string (s);
85 return ly_scm2string (str);
89 gulp_file_to_string (string fn, bool must_exist, int size)
91 string s = global_path.find (fn);
96 string e = _f ("can't find file: `%s'", fn);
98 e += _f ("(load path: `%s')", global_path.to_string ());
105 if (be_verbose_global)
106 progress_indication ("[" + s);
108 vector<char> chars = gulp_file (s, size);
109 string result (&chars[0], chars.size ());
111 if (be_verbose_global)
112 progress_indication ("]");
118 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
120 ly_display_scm (SCM s)
122 scm_display (s, scm_current_output_port ());
123 scm_newline (scm_current_output_port ());
128 ly_scm2string (SCM str)
130 assert (scm_is_string (str));
131 return string (scm_i_string_chars (str),
132 (int) scm_i_string_length (str));
136 ly_scm2newstr (SCM str, size_t *lenp)
138 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
140 size_t len = scm_i_string_length (str);
141 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
143 memcpy (new_str, scm_i_string_chars (str), len);
155 index_get_cell (SCM s, Direction d)
159 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
163 index_set_cell (SCM s, Direction d, SCM v)
166 scm_set_car_x (s, v);
168 scm_set_cdr_x (s, v);
173 is_number_pair (SCM p)
175 return scm_is_pair (p)
176 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
179 typedef void (*Void_fptr) ();
180 vector<Void_fptr> *scm_init_funcs_;
182 void add_scm_init_func (void (*f) ())
184 if (!scm_init_funcs_)
185 scm_init_funcs_ = new vector<Void_fptr>;
187 scm_init_funcs_->push_back (f);
191 ly_init_ly_module (void *)
193 for (vsize i = scm_init_funcs_->size (); i--;)
194 (scm_init_funcs_->at (i)) ();
196 if (be_verbose_global)
198 progress_indication ("[");
199 scm_display (scm_c_eval_string ("(%search-load-path \"lily.scm\")"),
200 scm_current_error_port ());
201 progress_indication ("]\n");
204 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
207 SCM global_lily_module;
212 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
213 scm_c_use_module ("lily");
219 return scm_ihashv (s, ~1u);
225 if (scm_is_number (s))
227 int i = scm_to_int (s);
228 return i >= -1 && i <= 1;
236 if (scm_is_number (s))
238 int i = scm_to_int (s);
239 return i == 0 || i == 1;
247 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
251 ly_scm2interval (SCM p)
253 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
257 ly_scm2realdrul (SCM p)
259 return Drul_array<Real> (scm_to_double (scm_car (p)),
260 scm_to_double (scm_cdr (p)));
264 ly_interval2scm (Drul_array<Real> i)
266 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
272 return scm_is_bool (s) && ly_scm2bool (s);
275 /* Appendable list L: the cdr contains the list, the car the last cons
280 SCM s = scm_cons (SCM_EOL, SCM_EOL);
281 scm_set_car_x (s, s);
287 appendable_list_append (SCM l, SCM elt)
289 SCM newcons = scm_cons (elt, SCM_EOL);
291 scm_set_cdr_x (scm_car (l), newcons);
292 scm_set_car_x (l, newcons);
296 ly_offset2scm (Offset o)
298 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
302 ly_scm2offset (SCM s)
304 return Offset (scm_to_double (scm_car (s)),
305 scm_to_double (scm_cdr (s)));
309 ly_deep_copy (SCM src)
311 if (scm_is_pair (src))
312 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
313 else if (scm_is_vector (src))
315 int len = scm_c_vector_length (src);
316 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
317 for (int i = 0;i < len; i++)
319 SCM si = scm_from_int (i);
320 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
326 /* looks the key up in the cdrs of the alist-keys
327 - ignoring the car and ignoring non-pair keys.
328 Returns first match found, i.e.
336 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
337 element with the cdr of the key = 1. In other words (alloc_cdr key)
340 (alloc (anything . key))
343 ly_assoc_cdr (SCM key, SCM alist)
345 if (scm_is_pair (alist))
347 SCM trykey = scm_caar (alist);
348 if (scm_is_pair (trykey)
349 && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
350 return scm_car (alist);
351 return ly_assoc_cdr (key, scm_cdr (alist));
357 ly_string_array_to_scm (vector<string> a)
360 for (vsize i = a.size (); i ; i--)
361 s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
365 /* SYMBOLS is a whitespace separated list. */
367 parse_symbol_list (char const *symbols)
369 while (isspace (*symbols))
372 replace_all (s, '\n', ' ');
373 replace_all (s, '\t', ' ');
374 return ly_string_array_to_scm (string_split (s, ' '));
378 ly_truncate_list (int k, SCM lst)
386 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
390 scm_set_cdr_x (s, SCM_EOL);
396 print_scm_val (SCM val)
398 string realval = ly_scm2string (ly_write2scm (val));
399 if (realval.length () > 200)
400 realval = realval.substr (0, 100)
402 + realval.substr (realval.length () - 100);
407 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
415 TODO: should remove #f from allowed vals?
417 if (val == SCM_EOL || val == SCM_BOOL_F)
420 if (!scm_is_symbol (sym))
425 This is used for autoBeamSettings.
427 TODO: deprecate the use of \override and \revert for
430 or use a symbol autoBeamSettingS?
435 SCM type = scm_object_property (sym, type_symbol);
437 if (type != SCM_EOL && !ly_is_procedure (type))
439 warning (_f ("can't find property type-check for `%s' (%s).",
440 ly_symbol2string (sym).c_str (),
441 ly_symbol2string (type_symbol).c_str ())
442 + " " + _ ("perhaps a typing error?"));
444 /* Be strict when being anal :) */
445 if (do_internal_type_checking_global)
448 warning (_ ("doing assignment anyway"));
453 && ly_is_procedure (type)
454 && scm_call_1 (type, val) == SCM_BOOL_F)
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 (),
463 ly_scm2string (type_name).c_str ()));
464 progress_indication ("\n");
472 zijn deze nou handig?
473 zijn ze er al in scheme, maar heten ze anders? */
475 /* Remove doubles from (sorted) 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 scm_default_compare (void const *a, void const *b)
496 return pa < pb ? -1 : 1;
499 /* Modify LST in place: qsort it.
501 FIXME: unused, junk? */
503 ly_list_qsort_uniq_x (SCM lst)
505 int len = scm_ilength (lst);
506 SCM *arr = new SCM[len];
508 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
509 arr[k++] = scm_car (s);
512 qsort (arr, len, sizeof (SCM), &scm_default_compare);
515 for (int i = 0; i < len; i++)
516 if (!i || arr[i] != arr[i - 1])
518 SCM_SETCAR (*tail, arr[i]);
519 tail = SCM_CDRLOC (*tail);
530 ly_snoc (SCM s, SCM list)
532 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
535 /* Split list at member s, removing s.
536 Return (BEFORE . AFTER) */
538 ly_split_list (SCM s, SCM list)
540 SCM before = SCM_EOL;
542 for (; scm_is_pair (after);)
544 SCM i = scm_car (after);
545 after = scm_cdr (after);
546 if (ly_is_equal (i, s))
548 before = scm_cons (i, before);
550 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
562 display stuff without using stack
567 SCM p = scm_current_output_port ();
570 for (; scm_is_pair (s); s = scm_cdr (s))
572 scm_display (scm_car (s), p);
576 return SCM_UNSPECIFIED;
580 int_list_to_slice (SCM l)
584 for (; scm_is_pair (l); l = scm_cdr (l))
585 if (scm_is_number (scm_car (l)))
586 s.add_point (scm_to_int (scm_car (l)));
590 /* Return I-th element, or last elt L. If I < 0, then we take the first
593 PRE: length (L) > 0 */
595 robust_list_ref (int i, SCM l)
597 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
603 robust_scm2double (SCM k, double x)
605 if (scm_is_number (k))
606 x = scm_to_double (k);
611 robust_scm2dir (SCM d, Direction def)
613 if (is_direction (d))
619 robust_scm2interval (SCM k, Drul_array<Real> v)
624 if (is_number_pair (k))
625 i = ly_scm2interval (k);
630 robust_scm2drul (SCM k, Drul_array<Real> v)
632 if (is_number_pair (k))
633 v = ly_scm2interval (k);
638 robust_scm2booldrul (SCM k, Drul_array<bool> def)
642 def[LEFT] = to_boolean (scm_car (k));
643 def[RIGHT] = to_boolean (scm_cdr (k));
649 robust_scm2offset (SCM k, Offset o)
651 if (is_number_pair (k))
652 o = ly_scm2offset (k);
657 robust_scm2string (SCM k, string s)
659 if (scm_is_string (k))
660 s = ly_scm2string (k);
665 robust_scm2int (SCM k, int o)
667 if (scm_integer_p (k) == SCM_BOOL_T)
673 alist_to_hashq (SCM alist)
675 int i = scm_ilength (alist);
677 return scm_c_make_hash_table (0);
679 SCM tab = scm_c_make_hash_table (i);
680 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
682 SCM pt = scm_cdar (s);
683 scm_hashq_set_x (tab, scm_caar (s), pt);
689 alist_equal_p (SCM a, SCM b)
692 scm_is_pair (s); s = scm_cdr (s))
694 SCM key = scm_caar (s);
695 SCM val = scm_cdar (s);
696 SCM l = scm_assoc (key, b);
699 || !ly_is_equal (scm_cdr (l), val))
707 ly_alist_vals (SCM alist)
710 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
711 x = scm_cons (scm_cdar (p), x);
716 ly_hash2alist (SCM tab)
718 SCM func = ly_lily_module_constant ("hash-table->alist");
719 return scm_call_1 (func, tab);
723 procedure_arity (SCM proc)
725 assert (ly_is_procedure (proc));
726 SCM arity = scm_procedure_property (proc,
727 ly_symbol2scm ("arity"));
729 SCM fixed = scm_car (arity);
730 return scm_to_int (fixed);
734 mangle_cxx_identifier (string cxx_id)
736 if (cxx_id.substr (0, 3) == "ly_")
737 cxx_id = cxx_id.replace (0, 3, "ly:");
740 cxx_id = String_convert::to_lower (cxx_id);
741 cxx_id = "ly:" + cxx_id;
743 if (cxx_id.substr (cxx_id.length () - 2) == "_p")
744 cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "?");
745 else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
746 cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "!");
748 cxx_id = replace_all (cxx_id, '_', '-');