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 "source-file.hh"
35 ly_to_symbol (SCM scm)
37 return scm_string_to_symbol (ly_to_string (scm));
41 ly_to_string (SCM scm)
43 return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
45 scm_makfrom0str ("~S"), scm);
51 return scm_car (scm_last_pair (list));
57 SCM port = scm_mkstrport (SCM_INUM0,
58 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
61 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
62 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
64 // scm_apply (write, port, SCM_EOL);
65 scm_call_2 (write, s, port);
66 return scm_strport_to_string (port);
72 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
76 ly_symbol2string (SCM s)
79 Ugh. this is not very efficient.
81 SCM str = scm_symbol_to_string (s);
82 return ly_scm2string (str);
86 gulp_file_to_string (String fn, bool must_exist)
88 String s = global_path.find (fn);
93 String e = _f ("can't find file: `%s'", fn);
95 e += _f ("(load path: `%s')", global_path.to_string ());
102 if (be_verbose_global)
103 progress_indication ("[" + s);
106 char *str = gulp_file (s, &n);
107 String result ((Byte *) str, n);
110 if (be_verbose_global)
111 progress_indication ("]");
117 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
119 ly_display_scm (SCM s)
121 scm_display (s, scm_current_output_port ());
122 scm_newline (scm_current_output_port ());
127 ly_scm2string (SCM str)
129 assert (scm_is_string (str));
130 return String ((Byte *)scm_i_string_chars (str),
131 (int) scm_i_string_length (str));
135 ly_scm2newstr (SCM str, size_t *lenp)
137 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
139 size_t len = scm_i_string_length (str);
140 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
142 memcpy (new_str, scm_i_string_chars (str), len);
154 index_get_cell (SCM s, Direction d)
158 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
162 index_set_cell (SCM s, Direction d, SCM v)
165 scm_set_car_x (s, v);
167 scm_set_cdr_x (s, v);
172 is_number_pair (SCM p)
174 return scm_is_pair (p)
175 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
178 typedef void (*Void_fptr) ();
179 Array<Void_fptr> *scm_init_funcs_;
181 void add_scm_init_func (void (*f) ())
183 if (!scm_init_funcs_)
184 scm_init_funcs_ = new Array<Void_fptr>;
186 scm_init_funcs_->push (f);
191 void initialize_kpathsea ();
196 ly_init_ly_module (void *)
198 for (int i = scm_init_funcs_->size (); i--;)
199 (scm_init_funcs_->elem (i)) ();
201 if (be_verbose_global)
202 progress_indication ("\n");
205 if (is_TeX_format_global)
206 initialize_kpathsea ();
209 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
212 SCM global_lily_module;
217 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
218 scm_c_use_module ("lily");
224 return scm_ihashv (s, ~1u);
230 if (scm_is_number (s))
232 int i = scm_to_int (s);
233 return i >= -1 && i <= 1;
241 if (scm_is_number (s))
243 int i = scm_to_int (s);
244 return i == 0 || i == 1;
252 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
256 ly_scm2interval (SCM p)
258 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
262 ly_scm2realdrul (SCM p)
264 return Drul_array<Real> (scm_to_double (scm_car (p)),
265 scm_to_double (scm_cdr (p)));
269 ly_interval2scm (Drul_array<Real> i)
271 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
277 return scm_is_bool (s) && ly_scm2bool (s);
280 /* Appendable list L: the cdr contains the list, the car the last cons
285 SCM s = scm_cons (SCM_EOL, SCM_EOL);
286 scm_set_car_x (s, s);
292 appendable_list_append (SCM l, SCM elt)
294 SCM newcons = scm_cons (elt, SCM_EOL);
296 scm_set_cdr_x (scm_car (l), newcons);
297 scm_set_car_x (l, newcons);
301 ly_offset2scm (Offset o)
303 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
307 ly_scm2offset (SCM s)
309 return Offset (scm_to_double (scm_car (s)),
310 scm_to_double (scm_cdr (s)));
314 ly_deep_copy (SCM src)
316 if (scm_is_pair (src))
317 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
318 else if (scm_is_vector (src))
320 int len = scm_c_vector_length (src);
321 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
322 for (int i = 0;i < len; i++)
324 SCM si = scm_from_int (i);
325 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
333 /* looks the key up in the cdrs of the alist-keys
334 - ignoring the car and ignoring non-pair keys.
335 Returns first match found, i.e.
343 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
344 element with the cdr of the key = 1. In other words (alloc_cdr key)
347 (alloc (anything . key))
350 ly_assoc_cdr (SCM key, SCM alist)
352 if (scm_is_pair (alist))
354 SCM trykey = scm_caar (alist);
355 if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
356 return scm_car (alist);
358 return ly_assoc_cdr (key, scm_cdr (alist));
363 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
364 i.e. \n and ' ' can be used interchangeably as separators. */
366 parse_symbol_list (char const *lst)
368 char *s = strdup (lst);
370 SCM create_list = SCM_EOL;
372 char *e = s + strlen (s) - 1;
373 while (e >= s && isspace (*e))
376 for (char *p = s; *p; p++)
385 char *next = strchr (s, ' ');
389 create_list = scm_cons (ly_symbol2scm (s), create_list);
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.left_string (100)
422 + realval.right_string (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).to_str0 (),
461 ly_symbol2string (type_symbol).to_str0 ())
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 message (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
481 ly_symbol2string (sym).to_str0 (),
483 ly_scm2string (type_name).to_str0 ()));
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 ly_list_qsort_uniq_x (SCM lst)
523 int len = scm_ilength (lst);
524 SCM *arr = new SCM[len];
526 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
527 arr[k++] = scm_car (s);
530 qsort (arr, len, sizeof (SCM), &scm_default_compare);
533 for (int i = 0; i < len; i++)
534 if (!i || arr[i] != arr[i - 1])
536 SCM_SETCAR (*tail, arr[i]);
537 tail = SCM_CDRLOC (*tail);
548 ly_snoc (SCM s, SCM list)
550 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
553 /* Split list at member s, removing s.
554 Return (BEFORE . AFTER) */
556 ly_split_list (SCM s, SCM list)
558 SCM before = SCM_EOL;
560 for (; scm_is_pair (after);)
562 SCM i = scm_car (after);
563 after = scm_cdr (after);
564 if (ly_is_equal (i, s))
566 before = scm_cons (i, before);
568 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
580 display stuff without using stack
585 SCM p = scm_current_output_port ();
588 for (; scm_is_pair (s); s = scm_cdr (s))
590 scm_display (scm_car (s), p);
594 return SCM_UNSPECIFIED;
598 int_list_to_slice (SCM l)
602 for (; scm_is_pair (l); l = scm_cdr (l))
603 if (scm_is_number (scm_car (l)))
604 s.add_point (scm_to_int (scm_car (l)));
608 /* Return I-th element, or last elt L. If I < 0, then we take the first
611 PRE: length (L) > 0 */
613 robust_list_ref (int i, SCM l)
615 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
621 robust_scm2double (SCM k, double x)
623 if (scm_is_number (k))
624 x = scm_to_double (k);
629 robust_scm2interval (SCM k, Drul_array<Real> v)
634 if (is_number_pair (k))
635 i = ly_scm2interval (k);
640 robust_scm2drul (SCM k, Drul_array<Real> v)
642 if (is_number_pair (k))
643 v = ly_scm2interval (k);
648 robust_scm2offset (SCM k, Offset o)
650 if (is_number_pair (k))
651 o = ly_scm2offset (k);
656 robust_scm2int (SCM k, int o)
658 if (scm_integer_p (k) == SCM_BOOL_T)
664 alist_to_hashq (SCM alist)
666 int i = scm_ilength (alist);
668 return scm_c_make_hash_table (0);
670 SCM tab = scm_c_make_hash_table (i);
671 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
673 SCM pt = scm_cdar (s);
674 scm_hashq_set_x (tab, scm_caar (s), pt);
680 alist_equal_p (SCM a, SCM b)
683 scm_is_pair (s); s = scm_cdr (s))
685 SCM key = scm_caar (s);
686 SCM val = scm_cdar (s);
687 SCM l = scm_assoc (key, b);
690 || !ly_is_equal (scm_cdr (l), val))
698 ly_alist_vals (SCM alist)
701 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
703 x = scm_cons (scm_cdar (p), x);
709 ly_hash2alist (SCM tab)
711 SCM func = ly_lily_module_constant ("hash-table->alist");
712 return scm_call_1 (func, tab);