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>
13 #include <cstring> /* strdup, strchr */
15 #include <libintl.h> // gettext on macos x
19 #include "lily-guile.hh"
20 #include "libc-extension.hh"
22 #include "file-path.hh"
24 #include "direction.hh"
27 #include "dimensions.hh"
28 #include "source-file.hh"
34 ly_to_symbol (SCM scm)
36 return scm_string_to_symbol (ly_to_string (scm));
40 ly_to_string (SCM scm)
42 return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
43 scm_makfrom0str ("~S"), scm);
49 return scm_car (scm_last_pair (list));
55 SCM port = scm_mkstrport (SCM_INUM0,
56 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
59 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
60 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
62 // scm_apply (write, port, SCM_EOL);
63 scm_call_2 (write, s, port);
64 return scm_strport_to_string (port);
70 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
74 ly_symbol2string (SCM s)
77 Ugh. this is not very efficient.
79 SCM str = scm_symbol_to_string (s);
80 return ly_scm2string (str);
84 gulp_file_to_string (String fn, bool must_exist)
86 String s = global_path.find (fn);
91 String e = _f ("can't find file: `%s'", fn);
93 e += _f ("(load path: `%s')", global_path.to_string ());
100 if (be_verbose_global)
101 progress_indication ("[" + s);
104 char *str = gulp_file (s, &n);
105 String result ((Byte*) str, n);
108 if (be_verbose_global)
109 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_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)
207 initialize_kpathsea ();
211 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
214 SCM global_lily_module;
219 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
220 scm_c_use_module ("lily");
226 return scm_ihashv (s, ~1u);
232 if (scm_is_number (s))
234 int i = scm_to_int (s);
235 return i>= -1 && i <= 1;
243 if (scm_is_number (s))
245 int i = scm_to_int (s);
246 return i == 0 || i == 1;
254 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
258 ly_scm2interval (SCM p)
260 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
264 ly_scm2realdrul (SCM p)
266 return Drul_array<Real> (scm_to_double (scm_car (p)),
267 scm_to_double (scm_cdr (p)));
271 ly_interval2scm (Drul_array<Real> i)
273 return scm_cons (scm_make_real (i[LEFT]), scm_make_real (i[RIGHT]));
279 return scm_is_bool (s) && ly_scm2bool (s);
282 /* Appendable list L: the cdr contains the list, the car the last cons
287 SCM s = scm_cons (SCM_EOL, SCM_EOL);
288 scm_set_car_x (s, s);
294 appendable_list_append (SCM l, SCM elt)
296 SCM newcons = scm_cons (elt, SCM_EOL);
298 scm_set_cdr_x (scm_car (l), newcons);
299 scm_set_car_x (l, newcons);
303 ly_offset2scm (Offset o)
305 return scm_cons (scm_make_real (o[X_AXIS]), scm_make_real (o[Y_AXIS]));
309 ly_scm2offset (SCM s)
311 return Offset (scm_to_double (scm_car (s)),
312 scm_to_double (scm_cdr (s)));
316 ly_deep_copy (SCM src)
318 if (scm_is_pair (src))
319 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
320 else if (scm_is_vector (src))
322 int len = scm_c_vector_length (src);
323 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
324 for (int i = 0 ;i < len ; i++)
326 SCM si = scm_int2num (i);
327 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
334 ly_chain_assoc_get (SCM key, SCM achain, SCM dfault)
336 if (scm_is_pair (achain))
338 SCM handle = scm_assoc (key, scm_car (achain));
339 if (scm_is_pair (handle))
340 return scm_cdr (handle);
342 return ly_chain_assoc (key, scm_cdr (achain));
349 ly_chain_assoc (SCM key, SCM achain)
351 if (scm_is_pair (achain))
353 SCM handle = scm_assoc (key, scm_car (achain));
354 if (scm_is_pair (handle))
357 return ly_chain_assoc (key, scm_cdr (achain));
363 /* looks the key up in the cdrs of the alist-keys
364 - ignoring the car and ignoring non-pair keys.
365 Returns first match found, i.e.
373 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
374 element with the cdr of the key = 1. In other words (alloc_cdr key)
377 (alloc (anything . key))
383 ly_assoc_cdr (SCM key, SCM alist)
385 if (scm_is_pair (alist))
387 SCM trykey = scm_caar (alist);
388 if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
389 return scm_car (alist);
391 return ly_assoc_cdr (key, scm_cdr (alist));
396 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
397 i.e. \n and ' ' can be used interchangeably as separators. */
399 parse_symbol_list (char const *lst)
401 char *s = strdup (lst);
403 SCM create_list = SCM_EOL;
405 char * e = s + strlen (s) - 1;
406 while (e >= s && isspace (*e))
409 for (char * p = s; *p; p++)
418 char *next = strchr (s, ' ');
422 create_list = scm_cons (ly_symbol2scm (s), create_list);
431 ly_truncate_list (int k, SCM lst)
439 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
443 scm_set_cdr_x (s, SCM_EOL);
449 print_scm_val (SCM val)
451 String realval = ly_scm2string (ly_write2scm (val));
452 if (realval.length () > 200)
453 realval = realval.left_string (100)
455 + realval.right_string (100);
460 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
468 TODO: should remove #f from allowed vals?
470 if (val == SCM_EOL || val == SCM_BOOL_F)
473 if (!scm_is_symbol (sym))
478 This is used for autoBeamSettings.
480 TODO: deprecate the use of \override and \revert for
483 or use a symbol autoBeamSettingS?
488 SCM type = scm_object_property (sym, type_symbol);
490 if (type != SCM_EOL && !ly_c_procedure_p (type))
492 warning (_f ("Can't find property type-check for `%s' (%s).",
493 ly_symbol2string (sym).to_str0 (),
494 ly_symbol2string (type_symbol).to_str0 ())
495 + " " + _ ("Perhaps you made a typing error?"));
497 /* Be strict when being anal :) */
498 if (do_internal_type_checking_global)
501 warning (_ ("Doing assignment anyway."));
506 && ly_c_procedure_p (type)
507 && scm_call_1 (type, val) == SCM_BOOL_F)
509 SCM errport = scm_current_error_port ();
511 SCM typefunc = ly_lily_module_constant ("type-name");
512 SCM type_name = scm_call_1 (typefunc, type);
515 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
516 ly_symbol2string (sym).to_str0 (),
518 ly_scm2string (type_name).to_str0 ()).to_str0 (),
520 scm_puts ("\n", errport);
529 zijn deze nou handig?
530 zijn ze er al in scheme, maar heten ze anders? */
533 /* Remove doubles from (sorted) list */
537 SCM unique = SCM_EOL;
538 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
540 if (!scm_is_pair (scm_cdr (i))
541 || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
542 unique = scm_cons (scm_car (i), unique);
544 return scm_reverse_x (unique, SCM_EOL);
549 scm_default_compare (void const *a, void const *b)
555 return pa < pb ? -1 : 1;
558 /* Modify LST in place: qsort it. */
560 ly_list_qsort_uniq_x (SCM lst)
562 int len = scm_ilength (lst);
563 SCM *arr = new SCM[len];
565 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
566 arr[k++] = scm_car (s);
569 qsort (arr, len, sizeof (SCM), &scm_default_compare);
572 for (int i = 0; i < len; i++)
573 if (!i || arr[i] != arr[i - 1])
575 SCM_SETCAR (*tail, arr[i]);
576 tail = SCM_CDRLOC (*tail);
588 ly_snoc (SCM s, SCM list)
590 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
593 /* Split list at member s, removing s.
594 Return (BEFORE . AFTER) */
596 ly_split_list (SCM s, SCM list)
598 SCM before = SCM_EOL;
600 for (; scm_is_pair (after);)
602 SCM i = scm_car (after);
603 after = scm_cdr (after);
604 if (ly_c_equal_p (i, s))
606 before = scm_cons (i, before);
608 return scm_cons ( scm_reverse_x (before, SCM_EOL), after);
622 display stuff without using stack
627 SCM p = scm_current_output_port ();
630 for (; scm_is_pair (s); s = scm_cdr (s))
632 scm_display (scm_car (s), p);
636 return SCM_UNSPECIFIED;
640 int_list_to_slice (SCM l)
644 for (; scm_is_pair (l); l = scm_cdr (l))
645 if (scm_is_number (scm_car (l)))
646 s.add_point (scm_to_int (scm_car (l)));
650 /* Return I-th element, or last elt L. If I < 0, then we take the first
653 PRE: length (L) > 0 */
655 robust_list_ref (int i, SCM l)
657 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
663 robust_scm2double (SCM k, double x)
665 if (scm_is_number (k))
666 x = scm_to_double (k);
671 robust_scm2interval (SCM k, Drul_array<Real> v)
676 if (is_number_pair (k))
677 i = ly_scm2interval (k);
682 robust_scm2drul (SCM k, Drul_array<Real> v)
684 if (is_number_pair (k))
685 v = ly_scm2interval (k);
690 robust_scm2offset (SCM k, Offset o)
692 if (is_number_pair (k))
693 o = ly_scm2offset (k);
698 robust_scm2int (SCM k, int o)
700 if (scm_integer_p (k) == SCM_BOOL_T)
706 alist_to_hashq (SCM alist)
708 int i = scm_ilength (alist);
710 return scm_c_make_hash_table (0);
712 SCM tab = scm_c_make_hash_table (i);
713 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
715 SCM pt = scm_cdar (s);
716 scm_hashq_set_x (tab, scm_caar (s), pt);
723 alist_equal_p (SCM a, SCM b)
726 scm_is_pair (s); s = scm_cdr (s))
728 SCM key = scm_caar (s);
729 SCM val = scm_cdar (s);
730 SCM l = scm_assoc (key, b);
733 || !ly_c_equal_p ( scm_cdr (l), val))
743 ly_alist_vals (SCM alist)
746 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
748 x = scm_cons (scm_cdar (p), x);
754 ly_hash2alist (SCM tab)
756 SCM func = ly_lily_module_constant ("hash-table->alist");
757 return scm_call_1 (func, tab);