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);
109 char *str = gulp_file (s, &n);
110 string result (str, n);
113 if (be_verbose_global)
114 progress_indication ("]");
120 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
122 ly_display_scm (SCM s)
124 scm_display (s, scm_current_output_port ());
125 scm_newline (scm_current_output_port ());
130 ly_scm2string (SCM str)
132 assert (scm_is_string (str));
133 return string (scm_i_string_chars (str),
134 (int) scm_i_string_length (str));
138 ly_scm2newstr (SCM str, size_t *lenp)
140 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
142 size_t len = scm_i_string_length (str);
143 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
145 memcpy (new_str, scm_i_string_chars (str), len);
157 index_get_cell (SCM s, Direction d)
161 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
165 index_set_cell (SCM s, Direction d, SCM v)
168 scm_set_car_x (s, v);
170 scm_set_cdr_x (s, v);
175 is_number_pair (SCM p)
177 return scm_is_pair (p)
178 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
181 typedef void (*Void_fptr) ();
182 vector<Void_fptr> *scm_init_funcs_;
184 void add_scm_init_func (void (*f) ())
186 if (!scm_init_funcs_)
187 scm_init_funcs_ = new vector<Void_fptr>;
189 scm_init_funcs_->push_back (f);
193 ly_init_ly_module (void *)
195 for (vsize i = scm_init_funcs_->size (); i--;)
196 (scm_init_funcs_->at (i)) ();
198 if (be_verbose_global)
200 progress_indication ("[");
201 scm_display (scm_c_eval_string ("(%search-load-path \"lily.scm\")"),
202 scm_current_error_port ());
203 progress_indication ("]\n");
206 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
209 SCM global_lily_module;
214 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
215 scm_c_use_module ("lily");
221 return scm_ihashv (s, ~1u);
227 if (scm_is_number (s))
229 int i = scm_to_int (s);
230 return i >= -1 && i <= 1;
238 if (scm_is_number (s))
240 int i = scm_to_int (s);
241 return i == 0 || i == 1;
249 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
253 ly_scm2interval (SCM p)
255 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
259 ly_scm2realdrul (SCM p)
261 return Drul_array<Real> (scm_to_double (scm_car (p)),
262 scm_to_double (scm_cdr (p)));
266 ly_interval2scm (Drul_array<Real> i)
268 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
274 return scm_is_bool (s) && ly_scm2bool (s);
277 /* Appendable list L: the cdr contains the list, the car the last cons
282 SCM s = scm_cons (SCM_EOL, SCM_EOL);
283 scm_set_car_x (s, s);
289 appendable_list_append (SCM l, SCM elt)
291 SCM newcons = scm_cons (elt, SCM_EOL);
293 scm_set_cdr_x (scm_car (l), newcons);
294 scm_set_car_x (l, newcons);
298 ly_offset2scm (Offset o)
300 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
304 ly_scm2offset (SCM s)
306 return Offset (scm_to_double (scm_car (s)),
307 scm_to_double (scm_cdr (s)));
311 ly_deep_copy (SCM src)
313 if (scm_is_pair (src))
314 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
315 else if (scm_is_vector (src))
317 int len = scm_c_vector_length (src);
318 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
319 for (int i = 0;i < len; i++)
321 SCM si = scm_from_int (i);
322 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
328 /* looks the key up in the cdrs of the alist-keys
329 - ignoring the car and ignoring non-pair keys.
330 Returns first match found, i.e.
338 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
339 element with the cdr of the key = 1. In other words (alloc_cdr key)
342 (alloc (anything . key))
345 ly_assoc_cdr (SCM key, SCM alist)
347 if (scm_is_pair (alist))
349 SCM trykey = scm_caar (alist);
350 if (scm_is_pair (trykey)
351 && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
352 return scm_car (alist);
353 return ly_assoc_cdr (key, scm_cdr (alist));
359 ly_string_array_to_scm (vector<string> a)
362 for (vsize i = a.size (); i ; i--)
363 s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
367 /* SYMBOLS is a whitespace separated list. */
369 parse_symbol_list (char const *symbols)
371 while (isspace (*symbols))
374 replace_all (s, '\n', ' ');
375 replace_all (s, '\t', ' ');
376 return ly_string_array_to_scm (string_split (s, ' '));
380 ly_truncate_list (int k, SCM lst)
388 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
392 scm_set_cdr_x (s, SCM_EOL);
398 print_scm_val (SCM val)
400 string realval = ly_scm2string (ly_write2scm (val));
401 if (realval.length () > 200)
402 realval = realval.substr (0, 100)
404 + realval.substr (realval.length () - 100);
409 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
417 TODO: should remove #f from allowed vals?
419 if (val == SCM_EOL || val == SCM_BOOL_F)
422 if (!scm_is_symbol (sym))
427 This is used for autoBeamSettings.
429 TODO: deprecate the use of \override and \revert for
432 or use a symbol autoBeamSettingS?
437 SCM type = scm_object_property (sym, type_symbol);
439 if (type != SCM_EOL && !ly_is_procedure (type))
441 warning (_f ("can't find property type-check for `%s' (%s).",
442 ly_symbol2string (sym).c_str (),
443 ly_symbol2string (type_symbol).c_str ())
444 + " " + _ ("perhaps a typing error?"));
446 /* Be strict when being anal :) */
447 if (do_internal_type_checking_global)
450 warning (_ ("doing assignment anyway"));
455 && ly_is_procedure (type)
456 && scm_call_1 (type, val) == SCM_BOOL_F)
459 SCM typefunc = ly_lily_module_constant ("type-name");
460 SCM type_name = scm_call_1 (typefunc, type);
462 warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
463 ly_symbol2string (sym).c_str (),
465 ly_scm2string (type_name).c_str ()));
466 progress_indication ("\n");
474 zijn deze nou handig?
475 zijn ze er al in scheme, maar heten ze anders? */
477 /* Remove doubles from (sorted) list */
481 SCM unique = SCM_EOL;
482 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
484 if (!scm_is_pair (scm_cdr (i))
485 || !ly_is_equal (scm_car (i), scm_cadr (i)))
486 unique = scm_cons (scm_car (i), unique);
488 return scm_reverse_x (unique, SCM_EOL);
492 scm_default_compare (void const *a, void const *b)
498 return pa < pb ? -1 : 1;
501 /* Modify LST in place: qsort it.
503 FIXME: unused, junk? */
505 ly_list_qsort_uniq_x (SCM lst)
507 int len = scm_ilength (lst);
508 SCM *arr = new SCM[len];
510 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
511 arr[k++] = scm_car (s);
514 qsort (arr, len, sizeof (SCM), &scm_default_compare);
517 for (int i = 0; i < len; i++)
518 if (!i || arr[i] != arr[i - 1])
520 SCM_SETCAR (*tail, arr[i]);
521 tail = SCM_CDRLOC (*tail);
532 ly_snoc (SCM s, SCM list)
534 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
537 /* Split list at member s, removing s.
538 Return (BEFORE . AFTER) */
540 ly_split_list (SCM s, SCM list)
542 SCM before = SCM_EOL;
544 for (; scm_is_pair (after);)
546 SCM i = scm_car (after);
547 after = scm_cdr (after);
548 if (ly_is_equal (i, s))
550 before = scm_cons (i, before);
552 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
564 display stuff without using stack
569 SCM p = scm_current_output_port ();
572 for (; scm_is_pair (s); s = scm_cdr (s))
574 scm_display (scm_car (s), p);
578 return SCM_UNSPECIFIED;
582 int_list_to_slice (SCM l)
586 for (; scm_is_pair (l); l = scm_cdr (l))
587 if (scm_is_number (scm_car (l)))
588 s.add_point (scm_to_int (scm_car (l)));
592 /* Return I-th element, or last elt L. If I < 0, then we take the first
595 PRE: length (L) > 0 */
597 robust_list_ref (int i, SCM l)
599 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
605 robust_scm2double (SCM k, double x)
607 if (scm_is_number (k))
608 x = scm_to_double (k);
613 robust_scm2dir (SCM d, Direction def)
615 if (is_direction (d))
621 robust_scm2interval (SCM k, Drul_array<Real> v)
626 if (is_number_pair (k))
627 i = ly_scm2interval (k);
632 robust_scm2drul (SCM k, Drul_array<Real> v)
634 if (is_number_pair (k))
635 v = ly_scm2interval (k);
640 robust_scm2booldrul (SCM k, Drul_array<bool> def)
644 def[LEFT] = to_boolean (scm_car (k));
645 def[RIGHT] = to_boolean (scm_cdr (k));
651 robust_scm2offset (SCM k, Offset o)
653 if (is_number_pair (k))
654 o = ly_scm2offset (k);
659 robust_scm2int (SCM k, int o)
661 if (scm_integer_p (k) == SCM_BOOL_T)
667 alist_to_hashq (SCM alist)
669 int i = scm_ilength (alist);
671 return scm_c_make_hash_table (0);
673 SCM tab = scm_c_make_hash_table (i);
674 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
676 SCM pt = scm_cdar (s);
677 scm_hashq_set_x (tab, scm_caar (s), pt);
683 alist_equal_p (SCM a, SCM b)
686 scm_is_pair (s); s = scm_cdr (s))
688 SCM key = scm_caar (s);
689 SCM val = scm_cdar (s);
690 SCM l = scm_assoc (key, b);
693 || !ly_is_equal (scm_cdr (l), val))
701 ly_alist_vals (SCM alist)
704 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
705 x = scm_cons (scm_cdar (p), x);
710 ly_hash2alist (SCM tab)
712 SCM func = ly_lily_module_constant ("hash-table->alist");
713 return scm_call_1 (func, tab);
717 procedure_arity (SCM proc)
719 assert (ly_is_procedure (proc));
720 SCM arity = scm_procedure_property (proc,
721 ly_symbol2scm ("arity"));
723 SCM fixed = scm_car (arity);
724 return scm_to_int (fixed);
728 mangle_cxx_identifier (string cxx_id)
730 if (cxx_id.substr (0, 3) == "ly_")
731 cxx_id = cxx_id.replace (0, 3, "ly:");
734 cxx_id = String_convert::to_lower (cxx_id);
735 cxx_id = "ly:" + cxx_id;
737 if (cxx_id.substr (cxx_id.length () - 2) == "_p")
738 cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "?");
739 else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
740 cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "!");
742 cxx_id = replace_all (cxx_id, '_', '-');