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,
44 scm_makfrom0str ("~S"), scm);
50 return scm_car (scm_last_pair (list));
56 SCM port = scm_mkstrport (SCM_INUM0,
57 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
60 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
61 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
63 // scm_apply (write, port, SCM_EOL);
64 scm_call_2 (write, s, port);
65 return scm_strport_to_string (port);
71 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
75 ly_symbol2string (SCM s)
78 Ugh. this is not very efficient.
80 SCM str = scm_symbol_to_string (s);
81 return ly_scm2string (str);
85 gulp_file_to_string (String fn, bool must_exist)
87 String s = global_path.find (fn);
92 String e = _f ("can't find file: `%s'", fn);
94 e += _f ("(load path: `%s')", global_path.to_string ());
101 if (be_verbose_global)
102 progress_indication ("[" + s);
105 char *str = gulp_file (s, &n);
106 String result ((Byte *) str, n);
109 if (be_verbose_global)
110 progress_indication ("]");
116 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
118 ly_display_scm (SCM s)
120 scm_display (s, scm_current_output_port ());
121 scm_newline (scm_current_output_port ());
126 ly_scm2string (SCM str)
128 assert (scm_is_string (str));
129 return String ((Byte *)scm_i_string_chars (str),
130 (int) scm_i_string_length (str));
134 ly_scm2newstr (SCM str, size_t *lenp)
136 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
138 size_t len = SCM_STRING_LENGTH (str);
139 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
141 memcpy (new_str, scm_i_string_chars (str), len);
153 index_get_cell (SCM s, Direction d)
157 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
161 index_set_cell (SCM s, Direction d, SCM v)
164 scm_set_car_x (s, v);
166 scm_set_cdr_x (s, v);
171 is_number_pair (SCM p)
173 return scm_is_pair (p)
174 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
177 typedef void (*Void_fptr) ();
178 Array<Void_fptr> *scm_init_funcs_;
180 void add_scm_init_func (void (*f) ())
182 if (!scm_init_funcs_)
183 scm_init_funcs_ = new Array<Void_fptr>;
185 scm_init_funcs_->push (f);
190 void initialize_kpathsea ();
195 ly_init_ly_module (void *)
197 for (int i = scm_init_funcs_->size (); i--;)
198 (scm_init_funcs_->elem (i)) ();
200 if (be_verbose_global)
201 progress_indication ("\n");
204 if (is_TeX_format_global)
205 initialize_kpathsea ();
208 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
211 SCM global_lily_module;
216 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
217 scm_c_use_module ("lily");
223 return scm_ihashv (s, ~1u);
229 if (scm_is_number (s))
231 int i = scm_to_int (s);
232 return i >= -1 && i <= 1;
240 if (scm_is_number (s))
242 int i = scm_to_int (s);
243 return i == 0 || i == 1;
251 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
255 ly_scm2interval (SCM p)
257 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
261 ly_scm2realdrul (SCM p)
263 return Drul_array<Real> (scm_to_double (scm_car (p)),
264 scm_to_double (scm_cdr (p)));
268 ly_interval2scm (Drul_array<Real> i)
270 return scm_cons (scm_make_real (i[LEFT]), scm_make_real (i[RIGHT]));
276 return scm_is_bool (s) && ly_scm2bool (s);
279 /* Appendable list L: the cdr contains the list, the car the last cons
284 SCM s = scm_cons (SCM_EOL, SCM_EOL);
285 scm_set_car_x (s, s);
291 appendable_list_append (SCM l, SCM elt)
293 SCM newcons = scm_cons (elt, SCM_EOL);
295 scm_set_cdr_x (scm_car (l), newcons);
296 scm_set_car_x (l, newcons);
300 ly_offset2scm (Offset o)
302 return scm_cons (scm_make_real (o[X_AXIS]), scm_make_real (o[Y_AXIS]));
306 ly_scm2offset (SCM s)
308 return Offset (scm_to_double (scm_car (s)),
309 scm_to_double (scm_cdr (s)));
313 ly_deep_copy (SCM src)
315 if (scm_is_pair (src))
316 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
317 else if (scm_is_vector (src))
319 int len = scm_c_vector_length (src);
320 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
321 for (int i = 0;i < len; i++)
323 SCM si = scm_int2num (i);
324 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) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
355 return scm_car (alist);
357 return ly_assoc_cdr (key, scm_cdr (alist));
362 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
363 i.e. \n and ' ' can be used interchangeably as separators. */
365 parse_symbol_list (char const *lst)
367 char *s = strdup (lst);
369 SCM create_list = SCM_EOL;
371 char *e = s + strlen (s) - 1;
372 while (e >= s && isspace (*e))
375 for (char *p = s; *p; p++)
384 char *next = strchr (s, ' ');
388 create_list = scm_cons (ly_symbol2scm (s), create_list);
397 ly_truncate_list (int k, SCM lst)
405 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
409 scm_set_cdr_x (s, SCM_EOL);
415 print_scm_val (SCM val)
417 String realval = ly_scm2string (ly_write2scm (val));
418 if (realval.length () > 200)
419 realval = realval.left_string (100)
421 + realval.right_string (100);
426 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
434 TODO: should remove #f from allowed vals?
436 if (val == SCM_EOL || val == SCM_BOOL_F)
439 if (!scm_is_symbol (sym))
444 This is used for autoBeamSettings.
446 TODO: deprecate the use of \override and \revert for
449 or use a symbol autoBeamSettingS?
454 SCM type = scm_object_property (sym, type_symbol);
456 if (type != SCM_EOL && !ly_c_procedure_p (type))
458 warning (_f ("can't find property type-check for `%s' (%s).",
459 ly_symbol2string (sym).to_str0 (),
460 ly_symbol2string (type_symbol).to_str0 ())
461 + " " + _ ("perhaps a typing error?"));
463 /* Be strict when being anal :) */
464 if (do_internal_type_checking_global)
467 warning (_ ("doing assignment anyway"));
472 && ly_c_procedure_p (type)
473 && scm_call_1 (type, val) == SCM_BOOL_F)
476 SCM typefunc = ly_lily_module_constant ("type-name");
477 SCM type_name = scm_call_1 (typefunc, type);
479 message (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
480 ly_symbol2string (sym).to_str0 (),
482 ly_scm2string (type_name).to_str0 ()));
483 progress_indication ("\n");
491 zijn deze nou handig?
492 zijn ze er al in scheme, maar heten ze anders? */
494 /* Remove doubles from (sorted) list */
498 SCM unique = SCM_EOL;
499 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
501 if (!scm_is_pair (scm_cdr (i))
502 || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
503 unique = scm_cons (scm_car (i), unique);
505 return scm_reverse_x (unique, SCM_EOL);
509 scm_default_compare (void const *a, void const *b)
515 return pa < pb ? -1 : 1;
518 /* Modify LST in place: qsort it. */
520 ly_list_qsort_uniq_x (SCM lst)
522 int len = scm_ilength (lst);
523 SCM *arr = new SCM[len];
525 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
526 arr[k++] = scm_car (s);
529 qsort (arr, len, sizeof (SCM), &scm_default_compare);
532 for (int i = 0; i < len; i++)
533 if (!i || arr[i] != arr[i - 1])
535 SCM_SETCAR (*tail, arr[i]);
536 tail = SCM_CDRLOC (*tail);
547 ly_snoc (SCM s, SCM list)
549 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
552 /* Split list at member s, removing s.
553 Return (BEFORE . AFTER) */
555 ly_split_list (SCM s, SCM list)
557 SCM before = SCM_EOL;
559 for (; scm_is_pair (after);)
561 SCM i = scm_car (after);
562 after = scm_cdr (after);
563 if (ly_c_equal_p (i, s))
565 before = scm_cons (i, before);
567 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
579 display stuff without using stack
584 SCM p = scm_current_output_port ();
587 for (; scm_is_pair (s); s = scm_cdr (s))
589 scm_display (scm_car (s), p);
593 return SCM_UNSPECIFIED;
597 int_list_to_slice (SCM l)
601 for (; scm_is_pair (l); l = scm_cdr (l))
602 if (scm_is_number (scm_car (l)))
603 s.add_point (scm_to_int (scm_car (l)));
607 /* Return I-th element, or last elt L. If I < 0, then we take the first
610 PRE: length (L) > 0 */
612 robust_list_ref (int i, SCM l)
614 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
620 robust_scm2double (SCM k, double x)
622 if (scm_is_number (k))
623 x = scm_to_double (k);
628 robust_scm2interval (SCM k, Drul_array<Real> v)
633 if (is_number_pair (k))
634 i = ly_scm2interval (k);
639 robust_scm2drul (SCM k, Drul_array<Real> v)
641 if (is_number_pair (k))
642 v = ly_scm2interval (k);
647 robust_scm2offset (SCM k, Offset o)
649 if (is_number_pair (k))
650 o = ly_scm2offset (k);
655 robust_scm2int (SCM k, int o)
657 if (scm_integer_p (k) == SCM_BOOL_T)
663 alist_to_hashq (SCM alist)
665 int i = scm_ilength (alist);
667 return scm_c_make_hash_table (0);
669 SCM tab = scm_c_make_hash_table (i);
670 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
672 SCM pt = scm_cdar (s);
673 scm_hashq_set_x (tab, scm_caar (s), pt);
679 alist_equal_p (SCM a, SCM b)
682 scm_is_pair (s); s = scm_cdr (s))
684 SCM key = scm_caar (s);
685 SCM val = scm_cdar (s);
686 SCM l = scm_assoc (key, b);
689 || !ly_c_equal_p (scm_cdr (l), val))
697 ly_alist_vals (SCM alist)
700 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
702 x = scm_cons (scm_cdar (p), x);
708 ly_hash2alist (SCM tab)
710 SCM func = ly_lily_module_constant ("hash-table->alist");
711 return scm_call_1 (func, tab);