2 lily-guile.cc -- implement assorted SCM interface functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2005 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 ((Byte *) 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 ((Byte *)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 Array<Void_fptr> *scm_init_funcs_;
184 void add_scm_init_func (void (*f) ())
186 if (!scm_init_funcs_)
187 scm_init_funcs_ = new Array<Void_fptr>;
189 scm_init_funcs_->push (f);
193 ly_init_ly_module (void *)
195 for (int i = scm_init_funcs_->size (); i--;)
196 (scm_init_funcs_->elem (i)) ();
198 if (be_verbose_global)
199 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 (Array<String> a)
357 for (int i = a.size () - 1; i >= 0; i--)
358 s = scm_cons (ly_symbol2scm (a[i].to_str0 ()), s);
362 /* SYMBOLS is a whitespace separated list. */
364 parse_symbol_list (char const *symbols)
366 while (isspace (*symbols))
369 s.substitute ('\n', ' ');
370 s.substitute ('\t', ' ');
371 return ly_string_array_to_scm (String_convert::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.left_string (100)
399 + realval.right_string (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).to_str0 (),
438 ly_symbol2string (type_symbol).to_str0 ())
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).to_str0 (),
460 ly_scm2string (type_name).to_str0 ()));
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 ly_list_qsort_uniq_x (SCM lst)
500 int len = scm_ilength (lst);
501 SCM *arr = new SCM[len];
503 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
504 arr[k++] = scm_car (s);
507 qsort (arr, len, sizeof (SCM), &scm_default_compare);
510 for (int i = 0; i < len; i++)
511 if (!i || arr[i] != arr[i - 1])
513 SCM_SETCAR (*tail, arr[i]);
514 tail = SCM_CDRLOC (*tail);
525 ly_snoc (SCM s, SCM list)
527 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
530 /* Split list at member s, removing s.
531 Return (BEFORE . AFTER) */
533 ly_split_list (SCM s, SCM list)
535 SCM before = SCM_EOL;
537 for (; scm_is_pair (after);)
539 SCM i = scm_car (after);
540 after = scm_cdr (after);
541 if (ly_is_equal (i, s))
543 before = scm_cons (i, before);
545 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
557 display stuff without using stack
562 SCM p = scm_current_output_port ();
565 for (; scm_is_pair (s); s = scm_cdr (s))
567 scm_display (scm_car (s), p);
571 return SCM_UNSPECIFIED;
575 int_list_to_slice (SCM l)
579 for (; scm_is_pair (l); l = scm_cdr (l))
580 if (scm_is_number (scm_car (l)))
581 s.add_point (scm_to_int (scm_car (l)));
585 /* Return I-th element, or last elt L. If I < 0, then we take the first
588 PRE: length (L) > 0 */
590 robust_list_ref (int i, SCM l)
592 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
598 robust_scm2double (SCM k, double x)
600 if (scm_is_number (k))
601 x = scm_to_double (k);
606 robust_scm2dir (SCM d, Direction def)
608 if (is_direction (d))
614 robust_scm2interval (SCM k, Drul_array<Real> v)
619 if (is_number_pair (k))
620 i = ly_scm2interval (k);
625 robust_scm2drul (SCM k, Drul_array<Real> v)
627 if (is_number_pair (k))
628 v = ly_scm2interval (k);
633 robust_scm2offset (SCM k, Offset o)
635 if (is_number_pair (k))
636 o = ly_scm2offset (k);
641 robust_scm2int (SCM k, int o)
643 if (scm_integer_p (k) == SCM_BOOL_T)
649 alist_to_hashq (SCM alist)
651 int i = scm_ilength (alist);
653 return scm_c_make_hash_table (0);
655 SCM tab = scm_c_make_hash_table (i);
656 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
658 SCM pt = scm_cdar (s);
659 scm_hashq_set_x (tab, scm_caar (s), pt);
665 alist_equal_p (SCM a, SCM b)
668 scm_is_pair (s); s = scm_cdr (s))
670 SCM key = scm_caar (s);
671 SCM val = scm_cdar (s);
672 SCM l = scm_assoc (key, b);
675 || !ly_is_equal (scm_cdr (l), val))
683 ly_alist_vals (SCM alist)
686 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
687 x = scm_cons (scm_cdar (p), x);
692 ly_hash2alist (SCM tab)
694 SCM func = ly_lily_module_constant ("hash-table->alist");
695 return scm_call_1 (func, tab);
699 procedure_arity (SCM proc)
701 assert (ly_is_procedure (proc));
702 SCM arity = scm_procedure_property (proc,
703 ly_symbol2scm ("arity"));
705 SCM fixed = scm_car (arity);
706 return scm_to_int (fixed);