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 */
19 #include "dimensions.hh"
20 #include "direction.hh"
21 #include "file-path.hh"
22 #include "international.hh"
23 #include "libc-extension.hh"
28 #include "string-convert.hh"
29 #include "source-file.hh"
36 ly_to_symbol (SCM scm)
38 return scm_string_to_symbol (ly_to_string (scm));
42 ly_to_string (SCM scm)
44 return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
46 scm_makfrom0str ("~S"), scm);
52 return scm_car (scm_last_pair (list));
58 SCM port = scm_mkstrport (SCM_INUM0,
59 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
62 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
63 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
65 // scm_apply (write, port, SCM_EOL);
66 scm_call_2 (write, s, port);
67 return scm_strport_to_string (port);
73 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
77 ly_symbol2string (SCM s)
80 Ugh. this is not very efficient.
82 SCM str = scm_symbol_to_string (s);
83 return ly_scm2string (str);
87 gulp_file_to_string (string fn, bool must_exist, int size)
89 string s = global_path.find (fn);
94 string e = _f ("can't find file: `%s'", fn);
96 e += _f ("(load path: `%s')", global_path.to_string ());
103 if (be_verbose_global)
104 progress_indication ("[" + s);
106 vector<char> chars = gulp_file (s, size);
107 string result (&chars[0], chars.size ());
109 if (be_verbose_global)
110 progress_indication ("]");
116 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
118 ly_display_scm (SCM s)
120 scm_display (s, scm_current_output_port ());
121 scm_newline (scm_current_output_port ());
126 ly_scm2string (SCM str)
128 assert (scm_is_string (str));
129 return string (scm_i_string_chars (str),
130 (int) scm_i_string_length (str));
134 ly_scm2newstr (SCM str, size_t *lenp)
136 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
138 size_t len = scm_i_string_length (str);
139 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
141 memcpy (new_str, scm_i_string_chars (str), len);
153 index_get_cell (SCM s, Direction d)
157 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
161 index_set_cell (SCM s, Direction d, SCM v)
164 scm_set_car_x (s, v);
166 scm_set_cdr_x (s, v);
171 is_number_pair (SCM p)
173 return scm_is_pair (p)
174 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
177 typedef void (*Void_fptr) ();
178 vector<Void_fptr> *scm_init_funcs_;
180 void add_scm_init_func (void (*f) ())
182 if (!scm_init_funcs_)
183 scm_init_funcs_ = new vector<Void_fptr>;
185 scm_init_funcs_->push_back (f);
189 ly_init_ly_module (void *)
191 for (vsize i = scm_init_funcs_->size (); i--;)
192 (scm_init_funcs_->at (i)) ();
194 if (be_verbose_global)
196 progress_indication ("[");
197 scm_display (scm_c_eval_string ("(%search-load-path \"lily.scm\")"),
198 scm_current_error_port ());
199 progress_indication ("]\n");
202 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
205 SCM global_lily_module;
210 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
211 scm_c_use_module ("lily");
217 return scm_ihashv (s, ~1u);
223 if (scm_is_number (s))
225 int i = scm_to_int (s);
226 return i >= -1 && i <= 1;
234 if (scm_is_number (s))
236 int i = scm_to_int (s);
237 return i == 0 || i == 1;
245 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
249 ly_scm2interval (SCM p)
251 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
255 ly_scm2realdrul (SCM p)
257 return Drul_array<Real> (scm_to_double (scm_car (p)),
258 scm_to_double (scm_cdr (p)));
262 ly_interval2scm (Drul_array<Real> i)
264 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
270 return scm_is_bool (s) && ly_scm2bool (s);
273 /* Appendable list L: the cdr contains the list, the car the last cons
278 SCM s = scm_cons (SCM_EOL, SCM_EOL);
279 scm_set_car_x (s, s);
285 appendable_list_append (SCM l, SCM elt)
287 SCM newcons = scm_cons (elt, SCM_EOL);
289 scm_set_cdr_x (scm_car (l), newcons);
290 scm_set_car_x (l, newcons);
294 ly_offset2scm (Offset o)
296 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
300 ly_scm2offset (SCM s)
302 return Offset (scm_to_double (scm_car (s)),
303 scm_to_double (scm_cdr (s)));
307 ly_offsets2scm (vector<Offset> os)
311 for (vsize i = 0; i < os.size (); i++)
313 *tail = scm_cons (ly_offset2scm (os[i]), SCM_EOL);
314 tail = SCM_CDRLOC(*tail);
320 ly_scm2offsets (SCM s)
323 for (; scm_is_pair (s); s = scm_cdr (s))
324 os.push_back (ly_scm2offset (scm_car (s)));
329 ly_deep_copy (SCM src)
331 if (scm_is_pair (src))
332 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
333 else if (scm_is_vector (src))
335 int len = scm_c_vector_length (src);
336 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
337 for (int i = 0;i < len; i++)
339 SCM si = scm_from_int (i);
340 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
346 /* looks the key up in the cdrs of the alist-keys
347 - ignoring the car and ignoring non-pair keys.
348 Returns first match found, i.e.
356 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
357 element with the cdr of the key = 1. In other words (alloc_cdr key)
360 (alloc (anything . key))
363 ly_assoc_cdr (SCM key, SCM alist)
365 if (scm_is_pair (alist))
367 SCM trykey = scm_caar (alist);
368 if (scm_is_pair (trykey)
369 && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
370 return scm_car (alist);
371 return ly_assoc_cdr (key, scm_cdr (alist));
377 ly_string_array_to_scm (vector<string> a)
380 for (vsize i = a.size (); i ; i--)
381 s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
385 /* SYMBOLS is a whitespace separated list. */
387 parse_symbol_list (char const *symbols)
389 while (isspace (*symbols))
392 replace_all (s, '\n', ' ');
393 replace_all (s, '\t', ' ');
394 return ly_string_array_to_scm (string_split (s, ' '));
398 ly_truncate_list (int k, SCM lst)
406 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
410 scm_set_cdr_x (s, SCM_EOL);
416 print_scm_val (SCM val)
418 string realval = ly_scm2string (ly_write2scm (val));
419 if (realval.length () > 200)
420 realval = realval.substr (0, 100)
422 + realval.substr (realval.length () - 100);
427 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
435 TODO: should remove #f from allowed vals?
437 if (val == SCM_EOL || val == SCM_BOOL_F)
440 if (!scm_is_symbol (sym))
445 This is used for autoBeamSettings.
447 TODO: deprecate the use of \override and \revert for
450 or use a symbol autoBeamSettingS?
455 SCM type = scm_object_property (sym, type_symbol);
457 if (type != SCM_EOL && !ly_is_procedure (type))
459 warning (_f ("can't find property type-check for `%s' (%s).",
460 ly_symbol2string (sym).c_str (),
461 ly_symbol2string (type_symbol).c_str ())
462 + " " + _ ("perhaps a typing error?"));
464 /* Be strict when being anal :) */
465 if (do_internal_type_checking_global)
468 warning (_ ("doing assignment anyway"));
473 && ly_is_procedure (type)
474 && scm_call_1 (type, val) == SCM_BOOL_F)
477 SCM typefunc = ly_lily_module_constant ("type-name");
478 SCM type_name = scm_call_1 (typefunc, type);
480 warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
481 ly_symbol2string (sym).c_str (),
483 ly_scm2string (type_name).c_str ()));
484 progress_indication ("\n");
492 zijn deze nou handig?
493 zijn ze er al in scheme, maar heten ze anders? */
495 /* Remove doubles from (sorted) list */
499 SCM unique = SCM_EOL;
500 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
502 if (!scm_is_pair (scm_cdr (i))
503 || !ly_is_equal (scm_car (i), scm_cadr (i)))
504 unique = scm_cons (scm_car (i), unique);
506 return scm_reverse_x (unique, SCM_EOL);
510 scm_default_compare (void const *a, void const *b)
516 return pa < pb ? -1 : 1;
519 /* Modify LST in place: qsort it.
521 FIXME: unused, junk? */
523 ly_list_qsort_uniq_x (SCM lst)
525 int len = scm_ilength (lst);
526 SCM *arr = new SCM[len];
528 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
529 arr[k++] = scm_car (s);
532 qsort (arr, len, sizeof (SCM), &scm_default_compare);
535 for (int i = 0; i < len; i++)
536 if (!i || arr[i] != arr[i - 1])
538 SCM_SETCAR (*tail, arr[i]);
539 tail = SCM_CDRLOC (*tail);
550 ly_snoc (SCM s, SCM list)
552 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
555 /* Split list at member s, removing s.
556 Return (BEFORE . AFTER) */
558 ly_split_list (SCM s, SCM list)
560 SCM before = SCM_EOL;
562 for (; scm_is_pair (after);)
564 SCM i = scm_car (after);
565 after = scm_cdr (after);
566 if (ly_is_equal (i, s))
568 before = scm_cons (i, before);
570 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
582 display stuff without using stack
587 SCM p = scm_current_output_port ();
590 for (; scm_is_pair (s); s = scm_cdr (s))
592 scm_display (scm_car (s), p);
596 return SCM_UNSPECIFIED;
600 int_list_to_slice (SCM l)
604 for (; scm_is_pair (l); l = scm_cdr (l))
605 if (scm_is_number (scm_car (l)))
606 s.add_point (scm_to_int (scm_car (l)));
610 /* Return I-th element, or last elt L. If I < 0, then we take the first
613 PRE: length (L) > 0 */
615 robust_list_ref (int i, SCM l)
617 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
623 robust_scm2double (SCM k, double x)
625 if (scm_is_number (k))
626 x = scm_to_double (k);
631 robust_scm2dir (SCM d, Direction def)
633 if (is_direction (d))
639 robust_scm2interval (SCM k, Drul_array<Real> v)
644 if (is_number_pair (k))
645 i = ly_scm2interval (k);
650 robust_scm2drul (SCM k, Drul_array<Real> v)
652 if (is_number_pair (k))
653 v = ly_scm2interval (k);
658 robust_scm2booldrul (SCM k, Drul_array<bool> def)
662 def[LEFT] = to_boolean (scm_car (k));
663 def[RIGHT] = to_boolean (scm_cdr (k));
669 robust_scm2offset (SCM k, Offset o)
671 if (is_number_pair (k))
672 o = ly_scm2offset (k);
677 robust_scm2string (SCM k, string s)
679 if (scm_is_string (k))
680 s = ly_scm2string (k);
685 robust_scm2int (SCM k, int o)
687 if (scm_integer_p (k) == SCM_BOOL_T)
693 alist_to_hashq (SCM alist)
695 int i = scm_ilength (alist);
697 return scm_c_make_hash_table (0);
699 SCM tab = scm_c_make_hash_table (i);
700 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
702 SCM pt = scm_cdar (s);
703 scm_hashq_set_x (tab, scm_caar (s), pt);
709 alist_equal_p (SCM a, SCM b)
712 scm_is_pair (s); s = scm_cdr (s))
714 SCM key = scm_caar (s);
715 SCM val = scm_cdar (s);
716 SCM l = scm_assoc (key, b);
719 || !ly_is_equal (scm_cdr (l), val))
727 ly_alist_vals (SCM alist)
730 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
731 x = scm_cons (scm_cdar (p), x);
736 ly_hash2alist (SCM tab)
738 SCM func = ly_lily_module_constant ("hash-table->alist");
739 return scm_call_1 (func, tab);
743 procedure_arity (SCM proc)
745 assert (ly_is_procedure (proc));
746 SCM arity = scm_procedure_property (proc,
747 ly_symbol2scm ("arity"));
749 SCM fixed = scm_car (arity);
750 return scm_to_int (fixed);
754 mangle_cxx_identifier (string cxx_id)
756 if (cxx_id.substr (0, 3) == "ly_")
757 cxx_id = cxx_id.replace (0, 3, "ly:");
760 cxx_id = String_convert::to_lower (cxx_id);
761 cxx_id = "ly:" + cxx_id;
763 if (cxx_id.substr (cxx_id.length () - 2) == "_p")
764 cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "?");
765 else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
766 cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "!");
768 cxx_id = replace_all (cxx_id, '_', '-');