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_string2scm (string const &str)
138 return scm_from_locale_stringn (str.c_str(),
143 ly_scm2newstr (SCM str, size_t *lenp)
145 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
147 size_t len = scm_i_string_length (str);
148 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
150 memcpy (new_str, scm_i_string_chars (str), len);
162 index_get_cell (SCM s, Direction d)
166 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
170 index_set_cell (SCM s, Direction d, SCM v)
173 scm_set_car_x (s, v);
175 scm_set_cdr_x (s, v);
180 is_number_pair (SCM p)
182 return scm_is_pair (p)
183 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
186 typedef void (*Void_fptr) ();
187 vector<Void_fptr> *scm_init_funcs_;
189 void add_scm_init_func (void (*f) ())
191 if (!scm_init_funcs_)
192 scm_init_funcs_ = new vector<Void_fptr>;
194 scm_init_funcs_->push_back (f);
198 ly_init_ly_module (void *)
200 for (vsize i = scm_init_funcs_->size (); i--;)
201 (scm_init_funcs_->at (i)) ();
203 if (be_verbose_global)
205 progress_indication ("[");
206 scm_display (scm_c_eval_string ("(%search-load-path \"lily.scm\")"),
207 scm_current_error_port ());
208 progress_indication ("]\n");
211 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
214 SCM global_lily_module;
219 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
220 scm_c_use_module ("lily");
226 return scm_ihashv (s, ~1u);
232 if (scm_is_number (s))
234 int i = scm_to_int (s);
235 return i >= -1 && i <= 1;
243 if (scm_is_number (s))
245 int i = scm_to_int (s);
246 return i == 0 || i == 1;
254 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
258 ly_scm2interval (SCM p)
260 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
264 ly_scm2realdrul (SCM p)
266 return Drul_array<Real> (scm_to_double (scm_car (p)),
267 scm_to_double (scm_cdr (p)));
271 ly_interval2scm (Drul_array<Real> i)
273 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
279 return scm_is_bool (s) && ly_scm2bool (s);
282 /* Appendable list L: the cdr contains the list, the car the last cons
287 SCM s = scm_cons (SCM_EOL, SCM_EOL);
288 scm_set_car_x (s, s);
294 appendable_list_append (SCM l, SCM elt)
296 SCM newcons = scm_cons (elt, SCM_EOL);
298 scm_set_cdr_x (scm_car (l), newcons);
299 scm_set_car_x (l, newcons);
303 ly_offset2scm (Offset o)
305 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
309 ly_scm2offset (SCM s)
311 return Offset (scm_to_double (scm_car (s)),
312 scm_to_double (scm_cdr (s)));
316 ly_deep_copy (SCM src)
318 if (scm_is_pair (src))
319 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
320 else if (scm_is_vector (src))
322 int len = scm_c_vector_length (src);
323 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
324 for (int i = 0;i < len; i++)
326 SCM si = scm_from_int (i);
327 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
333 /* looks the key up in the cdrs of the alist-keys
334 - ignoring the car and ignoring non-pair keys.
335 Returns first match found, i.e.
343 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
344 element with the cdr of the key = 1. In other words (alloc_cdr key)
347 (alloc (anything . key))
350 ly_assoc_cdr (SCM key, SCM alist)
352 if (scm_is_pair (alist))
354 SCM trykey = scm_caar (alist);
355 if (scm_is_pair (trykey)
356 && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
357 return scm_car (alist);
358 return ly_assoc_cdr (key, scm_cdr (alist));
364 ly_string_array_to_scm (vector<string> a)
367 for (vsize i = a.size (); i ; i--)
368 s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
372 /* SYMBOLS is a whitespace separated list. */
374 parse_symbol_list (char const *symbols)
376 while (isspace (*symbols))
379 replace_all (s, '\n', ' ');
380 replace_all (s, '\t', ' ');
381 return ly_string_array_to_scm (string_split (s, ' '));
385 ly_truncate_list (int k, SCM lst)
393 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
397 scm_set_cdr_x (s, SCM_EOL);
403 print_scm_val (SCM val)
405 string realval = ly_scm2string (ly_write2scm (val));
406 if (realval.length () > 200)
407 realval = realval.substr (0, 100)
409 + realval.substr (realval.length () - 100);
414 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
422 TODO: should remove #f from allowed vals?
424 if (val == SCM_EOL || val == SCM_BOOL_F)
427 if (!scm_is_symbol (sym))
432 This is used for autoBeamSettings.
434 TODO: deprecate the use of \override and \revert for
437 or use a symbol autoBeamSettingS?
442 SCM type = scm_object_property (sym, type_symbol);
444 if (type != SCM_EOL && !ly_is_procedure (type))
446 warning (_f ("can't find property type-check for `%s' (%s).",
447 ly_symbol2string (sym).c_str (),
448 ly_symbol2string (type_symbol).c_str ())
449 + " " + _ ("perhaps a typing error?"));
451 /* Be strict when being anal :) */
452 if (do_internal_type_checking_global)
455 warning (_ ("doing assignment anyway"));
460 && ly_is_procedure (type)
461 && scm_call_1 (type, val) == SCM_BOOL_F)
464 SCM typefunc = ly_lily_module_constant ("type-name");
465 SCM type_name = scm_call_1 (typefunc, type);
467 warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
468 ly_symbol2string (sym).c_str (),
470 ly_scm2string (type_name).c_str ()));
471 progress_indication ("\n");
479 zijn deze nou handig?
480 zijn ze er al in scheme, maar heten ze anders? */
482 /* Remove doubles from (sorted) list */
486 SCM unique = SCM_EOL;
487 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
489 if (!scm_is_pair (scm_cdr (i))
490 || !ly_is_equal (scm_car (i), scm_cadr (i)))
491 unique = scm_cons (scm_car (i), unique);
493 return scm_reverse_x (unique, SCM_EOL);
497 scm_default_compare (void const *a, void const *b)
503 return pa < pb ? -1 : 1;
506 /* Modify LST in place: qsort it.
508 FIXME: unused, junk? */
510 ly_list_qsort_uniq_x (SCM lst)
512 int len = scm_ilength (lst);
513 SCM *arr = new SCM[len];
515 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
516 arr[k++] = scm_car (s);
519 qsort (arr, len, sizeof (SCM), &scm_default_compare);
522 for (int i = 0; i < len; i++)
523 if (!i || arr[i] != arr[i - 1])
525 SCM_SETCAR (*tail, arr[i]);
526 tail = SCM_CDRLOC (*tail);
537 ly_snoc (SCM s, SCM list)
539 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
542 /* Split list at member s, removing s.
543 Return (BEFORE . AFTER) */
545 ly_split_list (SCM s, SCM list)
547 SCM before = SCM_EOL;
549 for (; scm_is_pair (after);)
551 SCM i = scm_car (after);
552 after = scm_cdr (after);
553 if (ly_is_equal (i, s))
555 before = scm_cons (i, before);
557 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
569 display stuff without using stack
574 SCM p = scm_current_output_port ();
577 for (; scm_is_pair (s); s = scm_cdr (s))
579 scm_display (scm_car (s), p);
583 return SCM_UNSPECIFIED;
587 int_list_to_slice (SCM l)
591 for (; scm_is_pair (l); l = scm_cdr (l))
592 if (scm_is_number (scm_car (l)))
593 s.add_point (scm_to_int (scm_car (l)));
597 /* Return I-th element, or last elt L. If I < 0, then we take the first
600 PRE: length (L) > 0 */
602 robust_list_ref (int i, SCM l)
604 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
610 robust_scm2double (SCM k, double x)
612 if (scm_is_number (k))
613 x = scm_to_double (k);
618 robust_scm2dir (SCM d, Direction def)
620 if (is_direction (d))
626 robust_scm2interval (SCM k, Drul_array<Real> v)
631 if (is_number_pair (k))
632 i = ly_scm2interval (k);
637 robust_scm2drul (SCM k, Drul_array<Real> v)
639 if (is_number_pair (k))
640 v = ly_scm2interval (k);
645 robust_scm2booldrul (SCM k, Drul_array<bool> def)
649 def[LEFT] = to_boolean (scm_car (k));
650 def[RIGHT] = to_boolean (scm_cdr (k));
656 robust_scm2offset (SCM k, Offset o)
658 if (is_number_pair (k))
659 o = ly_scm2offset (k);
664 robust_scm2string (SCM k, string s)
666 if (scm_is_string (k))
667 s = ly_scm2string (k);
672 robust_scm2int (SCM k, int o)
674 if (scm_integer_p (k) == SCM_BOOL_T)
680 alist_to_hashq (SCM alist)
682 int i = scm_ilength (alist);
684 return scm_c_make_hash_table (0);
686 SCM tab = scm_c_make_hash_table (i);
687 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
689 SCM pt = scm_cdar (s);
690 scm_hashq_set_x (tab, scm_caar (s), pt);
696 alist_equal_p (SCM a, SCM b)
699 scm_is_pair (s); s = scm_cdr (s))
701 SCM key = scm_caar (s);
702 SCM val = scm_cdar (s);
703 SCM l = scm_assoc (key, b);
706 || !ly_is_equal (scm_cdr (l), val))
714 ly_alist_vals (SCM alist)
717 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
718 x = scm_cons (scm_cdar (p), x);
723 ly_hash2alist (SCM tab)
725 SCM func = ly_lily_module_constant ("hash-table->alist");
726 return scm_call_1 (func, tab);
730 procedure_arity (SCM proc)
732 assert (ly_is_procedure (proc));
733 SCM arity = scm_procedure_property (proc,
734 ly_symbol2scm ("arity"));
736 SCM fixed = scm_car (arity);
737 return scm_to_int (fixed);
741 mangle_cxx_identifier (string cxx_id)
743 if (cxx_id.substr (0, 3) == "ly_")
744 cxx_id = cxx_id.replace (0, 3, "ly:");
747 cxx_id = String_convert::to_lower (cxx_id);
748 cxx_id = "ly:" + cxx_id;
750 if (cxx_id.substr (cxx_id.length () - 2) == "_p")
751 cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "?");
752 else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
753 cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "!");
755 cxx_id = replace_all (cxx_id, '_', '-');