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 ly_display_scm (void *s)
120 scm_display ((SCM)s, scm_current_output_port ());
121 scm_newline (scm_current_output_port ());
125 ly_scm2string (SCM str)
127 assert (scm_is_string (str));
128 return string (scm_i_string_chars (str),
129 (int) scm_i_string_length (str));
133 ly_scm2newstr (SCM str, size_t *lenp)
135 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
137 size_t len = scm_i_string_length (str);
138 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
140 memcpy (new_str, scm_i_string_chars (str), len);
152 index_get_cell (SCM s, Direction d)
156 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
160 index_set_cell (SCM s, Direction d, SCM v)
163 scm_set_car_x (s, v);
165 scm_set_cdr_x (s, v);
170 is_number_pair (SCM p)
172 return scm_is_pair (p)
173 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
176 typedef void (*Void_fptr) ();
177 vector<Void_fptr> *scm_init_funcs_;
179 void add_scm_init_func (void (*f) ())
181 if (!scm_init_funcs_)
182 scm_init_funcs_ = new vector<Void_fptr>;
184 scm_init_funcs_->push_back (f);
188 ly_init_ly_module (void *)
190 for (vsize i = scm_init_funcs_->size (); i--;)
191 (scm_init_funcs_->at (i)) ();
193 if (be_verbose_global)
195 progress_indication ("[");
196 scm_display (scm_c_eval_string ("(%search-load-path \"lily.scm\")"),
197 scm_current_error_port ());
198 progress_indication ("]\n");
201 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
204 SCM global_lily_module;
209 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
210 scm_c_use_module ("lily");
216 return scm_ihashv (s, ~1u);
222 if (scm_is_number (s))
224 int i = scm_to_int (s);
225 return i >= -1 && i <= 1;
233 if (scm_is_number (s))
235 int i = scm_to_int (s);
236 return i == 0 || i == 1;
244 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
248 ly_scm2interval (SCM p)
250 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
254 ly_scm2realdrul (SCM p)
256 return Drul_array<Real> (scm_to_double (scm_car (p)),
257 scm_to_double (scm_cdr (p)));
261 ly_interval2scm (Drul_array<Real> i)
263 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
269 return scm_is_bool (s) && ly_scm2bool (s);
272 /* Appendable list L: the cdr contains the list, the car the last cons
277 SCM s = scm_cons (SCM_EOL, SCM_EOL);
278 scm_set_car_x (s, s);
284 appendable_list_append (SCM l, SCM elt)
286 SCM newcons = scm_cons (elt, SCM_EOL);
288 scm_set_cdr_x (scm_car (l), newcons);
289 scm_set_car_x (l, newcons);
293 ly_offset2scm (Offset o)
295 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
299 ly_scm2offset (SCM s)
301 return Offset (scm_to_double (scm_car (s)),
302 scm_to_double (scm_cdr (s)));
306 ly_deep_copy (SCM src)
308 if (scm_is_pair (src))
309 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
310 else if (scm_is_vector (src))
312 int len = scm_c_vector_length (src);
313 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
314 for (int i = 0;i < len; i++)
316 SCM si = scm_from_int (i);
317 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
323 /* looks the key up in the cdrs of the alist-keys
324 - ignoring the car and ignoring non-pair keys.
325 Returns first match found, i.e.
333 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
334 element with the cdr of the key = 1. In other words (alloc_cdr key)
337 (alloc (anything . key))
340 ly_assoc_cdr (SCM key, SCM alist)
342 if (scm_is_pair (alist))
344 SCM trykey = scm_caar (alist);
345 if (scm_is_pair (trykey)
346 && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
347 return scm_car (alist);
348 return ly_assoc_cdr (key, scm_cdr (alist));
354 ly_string_array_to_scm (vector<string> a)
357 for (vsize i = a.size (); i ; i--)
358 s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
362 /* SYMBOLS is a whitespace separated list. */
364 parse_symbol_list (char const *symbols)
366 while (isspace (*symbols))
369 replace_all (s, '\n', ' ');
370 replace_all (s, '\t', ' ');
371 return ly_string_array_to_scm (string_split (s, ' '));
375 ly_truncate_list (int k, SCM lst)
383 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
387 scm_set_cdr_x (s, SCM_EOL);
393 print_scm_val (SCM val)
395 string realval = ly_scm2string (ly_write2scm (val));
396 if (realval.length () > 200)
397 realval = realval.substr (0, 100)
399 + realval.substr (realval.length () - 100);
404 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
412 TODO: should remove #f from allowed vals?
414 if (val == SCM_EOL || val == SCM_BOOL_F)
417 if (!scm_is_symbol (sym))
422 This is used for autoBeamSettings.
424 TODO: deprecate the use of \override and \revert for
427 or use a symbol autoBeamSettingS?
432 SCM type = scm_object_property (sym, type_symbol);
434 if (type != SCM_EOL && !ly_is_procedure (type))
436 warning (_f ("can't find property type-check for `%s' (%s).",
437 ly_symbol2string (sym).c_str (),
438 ly_symbol2string (type_symbol).c_str ())
439 + " " + _ ("perhaps a typing error?"));
441 /* Be strict when being anal :) */
442 if (do_internal_type_checking_global)
445 warning (_ ("doing assignment anyway"));
450 && ly_is_procedure (type)
451 && scm_call_1 (type, val) == SCM_BOOL_F)
454 SCM typefunc = ly_lily_module_constant ("type-name");
455 SCM type_name = scm_call_1 (typefunc, type);
457 warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
458 ly_symbol2string (sym).c_str (),
460 ly_scm2string (type_name).c_str ()));
461 progress_indication ("\n");
469 zijn deze nou handig?
470 zijn ze er al in scheme, maar heten ze anders? */
472 /* Remove doubles from (sorted) list */
476 SCM unique = SCM_EOL;
477 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
479 if (!scm_is_pair (scm_cdr (i))
480 || !ly_is_equal (scm_car (i), scm_cadr (i)))
481 unique = scm_cons (scm_car (i), unique);
483 return scm_reverse_x (unique, SCM_EOL);
487 scm_default_compare (void const *a, void const *b)
493 return pa < pb ? -1 : 1;
496 /* Modify LST in place: qsort it.
498 FIXME: unused, junk? */
500 ly_list_qsort_uniq_x (SCM lst)
502 int len = scm_ilength (lst);
503 SCM *arr = new SCM[len];
505 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
506 arr[k++] = scm_car (s);
509 qsort (arr, len, sizeof (SCM), &scm_default_compare);
512 for (int i = 0; i < len; i++)
513 if (!i || arr[i] != arr[i - 1])
515 SCM_SETCAR (*tail, arr[i]);
516 tail = SCM_CDRLOC (*tail);
527 ly_snoc (SCM s, SCM list)
529 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
532 /* Split list at member s, removing s.
533 Return (BEFORE . AFTER) */
535 ly_split_list (SCM s, SCM list)
537 SCM before = SCM_EOL;
539 for (; scm_is_pair (after);)
541 SCM i = scm_car (after);
542 after = scm_cdr (after);
543 if (ly_is_equal (i, s))
545 before = scm_cons (i, before);
547 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
559 display stuff without using stack
564 SCM p = scm_current_output_port ();
567 for (; scm_is_pair (s); s = scm_cdr (s))
569 scm_display (scm_car (s), p);
573 return SCM_UNSPECIFIED;
577 int_list_to_slice (SCM l)
581 for (; scm_is_pair (l); l = scm_cdr (l))
582 if (scm_is_number (scm_car (l)))
583 s.add_point (scm_to_int (scm_car (l)));
587 /* Return I-th element, or last elt L. If I < 0, then we take the first
590 PRE: length (L) > 0 */
592 robust_list_ref (int i, SCM l)
594 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
600 robust_scm2double (SCM k, double x)
602 if (scm_is_number (k))
603 x = scm_to_double (k);
608 robust_scm2dir (SCM d, Direction def)
610 if (is_direction (d))
616 robust_scm2interval (SCM k, Drul_array<Real> v)
621 if (is_number_pair (k))
622 i = ly_scm2interval (k);
627 robust_scm2drul (SCM k, Drul_array<Real> v)
629 if (is_number_pair (k))
630 v = ly_scm2interval (k);
635 robust_scm2booldrul (SCM k, Drul_array<bool> def)
639 def[LEFT] = to_boolean (scm_car (k));
640 def[RIGHT] = to_boolean (scm_cdr (k));
646 robust_scm2offset (SCM k, Offset o)
648 if (is_number_pair (k))
649 o = ly_scm2offset (k);
654 robust_scm2string (SCM k, string s)
656 if (scm_is_string (k))
657 s = ly_scm2string (k);
662 robust_scm2int (SCM k, int o)
664 if (scm_integer_p (k) == SCM_BOOL_T)
670 alist_to_hashq (SCM alist)
672 int i = scm_ilength (alist);
674 return scm_c_make_hash_table (0);
676 SCM tab = scm_c_make_hash_table (i);
677 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
679 SCM pt = scm_cdar (s);
680 scm_hashq_set_x (tab, scm_caar (s), pt);
686 alist_equal_p (SCM a, SCM b)
689 scm_is_pair (s); s = scm_cdr (s))
691 SCM key = scm_caar (s);
692 SCM val = scm_cdar (s);
693 SCM l = scm_assoc (key, b);
696 || !ly_is_equal (scm_cdr (l), val))
704 ly_alist_vals (SCM alist)
707 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
708 x = scm_cons (scm_cdar (p), x);
713 ly_hash2alist (SCM tab)
715 SCM func = ly_lily_module_constant ("hash-table->alist");
716 return scm_call_1 (func, tab);
720 procedure_arity (SCM proc)
722 assert (ly_is_procedure (proc));
723 SCM arity = scm_procedure_property (proc,
724 ly_symbol2scm ("arity"));
726 SCM fixed = scm_car (arity);
727 return scm_to_int (fixed);
731 mangle_cxx_identifier (string cxx_id)
733 if (cxx_id.substr (0, 3) == "ly_")
734 cxx_id = cxx_id.replace (0, 3, "ly:");
737 cxx_id = String_convert::to_lower (cxx_id);
738 cxx_id = "ly:" + cxx_id;
740 if (cxx_id.substr (cxx_id.length () - 2) == "_p")
741 cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "?");
742 else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
743 cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "!");
745 cxx_id = replace_all (cxx_id, '_', '-');