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@cs.uu.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);
107 char *str = gulp_file (s, &n);
108 String result ((Byte *) str, n);
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 ((Byte *)scm_i_string_chars (str),
132 (int) scm_i_string_length (str));
136 ly_scm2newstr (SCM str, size_t *lenp)
138 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
140 size_t len = scm_i_string_length (str);
141 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
143 memcpy (new_str, scm_i_string_chars (str), len);
155 index_get_cell (SCM s, Direction d)
159 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
163 index_set_cell (SCM s, Direction d, SCM v)
166 scm_set_car_x (s, v);
168 scm_set_cdr_x (s, v);
173 is_number_pair (SCM p)
175 return scm_is_pair (p)
176 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
179 typedef void (*Void_fptr) ();
180 Array<Void_fptr> *scm_init_funcs_;
182 void add_scm_init_func (void (*f) ())
184 if (!scm_init_funcs_)
185 scm_init_funcs_ = new Array<Void_fptr>;
187 scm_init_funcs_->push (f);
192 void initialize_kpathsea ();
197 ly_init_ly_module (void *)
199 for (int i = scm_init_funcs_->size (); i--;)
200 (scm_init_funcs_->elem (i)) ();
202 if (be_verbose_global)
203 progress_indication ("\n");
206 if (is_TeX_format_global)
207 initialize_kpathsea ();
210 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
213 SCM global_lily_module;
218 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
219 scm_c_use_module ("lily");
225 return scm_ihashv (s, ~1u);
231 if (scm_is_number (s))
233 int i = scm_to_int (s);
234 return i >= -1 && i <= 1;
242 if (scm_is_number (s))
244 int i = scm_to_int (s);
245 return i == 0 || i == 1;
253 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
257 ly_scm2interval (SCM p)
259 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
263 ly_scm2realdrul (SCM p)
265 return Drul_array<Real> (scm_to_double (scm_car (p)),
266 scm_to_double (scm_cdr (p)));
270 ly_interval2scm (Drul_array<Real> i)
272 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
278 return scm_is_bool (s) && ly_scm2bool (s);
281 /* Appendable list L: the cdr contains the list, the car the last cons
286 SCM s = scm_cons (SCM_EOL, SCM_EOL);
287 scm_set_car_x (s, s);
293 appendable_list_append (SCM l, SCM elt)
295 SCM newcons = scm_cons (elt, SCM_EOL);
297 scm_set_cdr_x (scm_car (l), newcons);
298 scm_set_car_x (l, newcons);
302 ly_offset2scm (Offset o)
304 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
308 ly_scm2offset (SCM s)
310 return Offset (scm_to_double (scm_car (s)),
311 scm_to_double (scm_cdr (s)));
315 ly_deep_copy (SCM src)
317 if (scm_is_pair (src))
318 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
319 else if (scm_is_vector (src))
321 int len = scm_c_vector_length (src);
322 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
323 for (int i = 0;i < len; i++)
325 SCM si = scm_from_int (i);
326 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
332 /* looks the key up in the cdrs of the alist-keys
333 - ignoring the car and ignoring non-pair keys.
334 Returns first match found, i.e.
342 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
343 element with the cdr of the key = 1. In other words (alloc_cdr key)
346 (alloc (anything . key))
349 ly_assoc_cdr (SCM key, SCM alist)
351 if (scm_is_pair (alist))
353 SCM trykey = scm_caar (alist);
354 if (scm_is_pair (trykey)
355 && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
356 return scm_car (alist);
357 return ly_assoc_cdr (key, scm_cdr (alist));
363 ly_string_array_to_scm (Array<String> a)
366 for (int i = a.size () - 1; i >= 0; i--)
367 s = scm_cons (ly_symbol2scm (a[i].to_str0 ()), s);
371 /* SYMBOLS is a whitespace separated list. */
373 parse_symbol_list (char const *symbols)
375 while (isspace (*symbols))
378 s.substitute ('\n', ' ');
379 s.substitute ('\t', ' ');
380 return ly_string_array_to_scm (String_convert::split (s, ' '));
384 ly_truncate_list (int k, SCM lst)
392 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
396 scm_set_cdr_x (s, SCM_EOL);
402 print_scm_val (SCM val)
404 String realval = ly_scm2string (ly_write2scm (val));
405 if (realval.length () > 200)
406 realval = realval.left_string (100)
408 + realval.right_string (100);
413 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
421 TODO: should remove #f from allowed vals?
423 if (val == SCM_EOL || val == SCM_BOOL_F)
426 if (!scm_is_symbol (sym))
431 This is used for autoBeamSettings.
433 TODO: deprecate the use of \override and \revert for
436 or use a symbol autoBeamSettingS?
441 SCM type = scm_object_property (sym, type_symbol);
443 if (type != SCM_EOL && !ly_is_procedure (type))
445 warning (_f ("can't find property type-check for `%s' (%s).",
446 ly_symbol2string (sym).to_str0 (),
447 ly_symbol2string (type_symbol).to_str0 ())
448 + " " + _ ("perhaps a typing error?"));
450 /* Be strict when being anal :) */
451 if (do_internal_type_checking_global)
454 warning (_ ("doing assignment anyway"));
459 && ly_is_procedure (type)
460 && scm_call_1 (type, val) == SCM_BOOL_F)
463 SCM typefunc = ly_lily_module_constant ("type-name");
464 SCM type_name = scm_call_1 (typefunc, type);
466 message (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
467 ly_symbol2string (sym).to_str0 (),
469 ly_scm2string (type_name).to_str0 ()));
470 progress_indication ("\n");
478 zijn deze nou handig?
479 zijn ze er al in scheme, maar heten ze anders? */
481 /* Remove doubles from (sorted) list */
485 SCM unique = SCM_EOL;
486 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
488 if (!scm_is_pair (scm_cdr (i))
489 || !ly_is_equal (scm_car (i), scm_cadr (i)))
490 unique = scm_cons (scm_car (i), unique);
492 return scm_reverse_x (unique, SCM_EOL);
496 scm_default_compare (void const *a, void const *b)
502 return pa < pb ? -1 : 1;
505 /* Modify LST in place: qsort it. */
507 ly_list_qsort_uniq_x (SCM lst)
509 int len = scm_ilength (lst);
510 SCM *arr = new SCM[len];
512 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
513 arr[k++] = scm_car (s);
516 qsort (arr, len, sizeof (SCM), &scm_default_compare);
519 for (int i = 0; i < len; i++)
520 if (!i || arr[i] != arr[i - 1])
522 SCM_SETCAR (*tail, arr[i]);
523 tail = SCM_CDRLOC (*tail);
534 ly_snoc (SCM s, SCM list)
536 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
539 /* Split list at member s, removing s.
540 Return (BEFORE . AFTER) */
542 ly_split_list (SCM s, SCM list)
544 SCM before = SCM_EOL;
546 for (; scm_is_pair (after);)
548 SCM i = scm_car (after);
549 after = scm_cdr (after);
550 if (ly_is_equal (i, s))
552 before = scm_cons (i, before);
554 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
566 display stuff without using stack
571 SCM p = scm_current_output_port ();
574 for (; scm_is_pair (s); s = scm_cdr (s))
576 scm_display (scm_car (s), p);
580 return SCM_UNSPECIFIED;
584 int_list_to_slice (SCM l)
588 for (; scm_is_pair (l); l = scm_cdr (l))
589 if (scm_is_number (scm_car (l)))
590 s.add_point (scm_to_int (scm_car (l)));
594 /* Return I-th element, or last elt L. If I < 0, then we take the first
597 PRE: length (L) > 0 */
599 robust_list_ref (int i, SCM l)
601 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
607 robust_scm2double (SCM k, double x)
609 if (scm_is_number (k))
610 x = scm_to_double (k);
615 robust_scm2interval (SCM k, Drul_array<Real> v)
620 if (is_number_pair (k))
621 i = ly_scm2interval (k);
626 robust_scm2drul (SCM k, Drul_array<Real> v)
628 if (is_number_pair (k))
629 v = ly_scm2interval (k);
634 robust_scm2offset (SCM k, Offset o)
636 if (is_number_pair (k))
637 o = ly_scm2offset (k);
642 robust_scm2int (SCM k, int o)
644 if (scm_integer_p (k) == SCM_BOOL_T)
650 alist_to_hashq (SCM alist)
652 int i = scm_ilength (alist);
654 return scm_c_make_hash_table (0);
656 SCM tab = scm_c_make_hash_table (i);
657 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
659 SCM pt = scm_cdar (s);
660 scm_hashq_set_x (tab, scm_caar (s), pt);
666 alist_equal_p (SCM a, SCM b)
669 scm_is_pair (s); s = scm_cdr (s))
671 SCM key = scm_caar (s);
672 SCM val = scm_cdar (s);
673 SCM l = scm_assoc (key, b);
676 || !ly_is_equal (scm_cdr (l), val))
684 ly_alist_vals (SCM alist)
687 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
688 x = scm_cons (scm_cdar (p), x);
693 ly_hash2alist (SCM tab)
695 SCM func = ly_lily_module_constant ("hash-table->alist");
696 return scm_call_1 (func, tab);