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>
12 #include <cstring> /* strdup, strchr */
14 #include <libintl.h> // gettext on macos x
18 #include "lily-guile.hh"
19 #include "libc-extension.hh"
21 #include "file-path.hh"
23 #include "direction.hh"
26 #include "dimensions.hh"
27 #include "source-file.hh"
33 ly_to_symbol (SCM scm)
35 return scm_string_to_symbol (ly_to_string (scm));
39 ly_to_string (SCM scm)
41 return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
42 scm_makfrom0str ("~S"), scm);
48 return scm_car (scm_last_pair (list));
54 SCM port = scm_mkstrport (SCM_INUM0,
55 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
58 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
59 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
61 // scm_apply (write, port, SCM_EOL);
62 scm_call_2 (write, s, port);
63 return scm_strport_to_string (port);
69 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
73 ly_symbol2string (SCM s)
76 Ugh. this is not very efficient.
78 SCM str = scm_symbol_to_string (s);
79 return ly_scm2string (str);
83 gulp_file_to_string (String fn, bool must_exist)
85 String s = global_path.find (fn);
90 String e = _f ("can't find file: `%s'", fn);
92 e += _f ("(load path: `%s')", global_path.to_string ());
99 if (be_verbose_global)
100 progress_indication ("[" + s);
103 char *str = gulp_file (s, &n);
104 String result ((Byte *) str, n);
107 if (be_verbose_global)
108 progress_indication ("]");
114 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
116 ly_display_scm (SCM s)
118 scm_display (s, scm_current_output_port ());
119 scm_newline (scm_current_output_port ());
124 ly_scm2string (SCM str)
126 assert (scm_is_string (str));
127 return String ((Byte *)scm_i_string_chars (str),
128 (int) scm_i_string_length (str));
132 ly_scm2newstr (SCM str, size_t *lenp)
134 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
136 size_t len = SCM_STRING_LENGTH (str);
137 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
139 memcpy (new_str, scm_i_string_chars (str), len);
151 index_get_cell (SCM s, Direction d)
155 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
159 index_set_cell (SCM s, Direction d, SCM v)
162 scm_set_car_x (s, v);
164 scm_set_cdr_x (s, v);
169 is_number_pair (SCM p)
171 return scm_is_pair (p)
172 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
175 typedef void (*Void_fptr) ();
176 Array<Void_fptr> *scm_init_funcs_;
178 void add_scm_init_func (void (*f) ())
180 if (!scm_init_funcs_)
181 scm_init_funcs_ = new Array<Void_fptr>;
183 scm_init_funcs_->push (f);
188 void initialize_kpathsea ();
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");
202 if (is_TeX_format_global)
203 initialize_kpathsea ();
206 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
209 SCM global_lily_module;
214 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
215 scm_c_use_module ("lily");
221 return scm_ihashv (s, ~1u);
227 if (scm_is_number (s))
229 int i = scm_to_int (s);
230 return i >= -1 && i <= 1;
238 if (scm_is_number (s))
240 int i = scm_to_int (s);
241 return i == 0 || i == 1;
249 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
253 ly_scm2interval (SCM p)
255 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
259 ly_scm2realdrul (SCM p)
261 return Drul_array<Real> (scm_to_double (scm_car (p)),
262 scm_to_double (scm_cdr (p)));
266 ly_interval2scm (Drul_array<Real> i)
268 return scm_cons (scm_make_real (i[LEFT]), scm_make_real (i[RIGHT]));
274 return scm_is_bool (s) && ly_scm2bool (s);
277 /* Appendable list L: the cdr contains the list, the car the last cons
282 SCM s = scm_cons (SCM_EOL, SCM_EOL);
283 scm_set_car_x (s, s);
289 appendable_list_append (SCM l, SCM elt)
291 SCM newcons = scm_cons (elt, SCM_EOL);
293 scm_set_cdr_x (scm_car (l), newcons);
294 scm_set_car_x (l, newcons);
298 ly_offset2scm (Offset o)
300 return scm_cons (scm_make_real (o[X_AXIS]), scm_make_real (o[Y_AXIS]));
304 ly_scm2offset (SCM s)
306 return Offset (scm_to_double (scm_car (s)),
307 scm_to_double (scm_cdr (s)));
311 ly_deep_copy (SCM src)
313 if (scm_is_pair (src))
314 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
315 else if (scm_is_vector (src))
317 int len = scm_c_vector_length (src);
318 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
319 for (int i = 0;i < len; i++)
321 SCM si = scm_int2num (i);
322 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
330 /* looks the key up in the cdrs of the alist-keys
331 - ignoring the car and ignoring non-pair keys.
332 Returns first match found, i.e.
340 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
341 element with the cdr of the key = 1. In other words (alloc_cdr key)
344 (alloc (anything . key))
347 ly_assoc_cdr (SCM key, SCM alist)
349 if (scm_is_pair (alist))
351 SCM trykey = scm_caar (alist);
352 if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
353 return scm_car (alist);
355 return ly_assoc_cdr (key, scm_cdr (alist));
360 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
361 i.e. \n and ' ' can be used interchangeably as separators. */
363 parse_symbol_list (char const *lst)
365 char *s = strdup (lst);
367 SCM create_list = SCM_EOL;
369 char *e = s + strlen (s) - 1;
370 while (e >= s && isspace (*e))
373 for (char *p = s; *p; p++)
382 char *next = strchr (s, ' ');
386 create_list = scm_cons (ly_symbol2scm (s), create_list);
395 ly_truncate_list (int k, SCM lst)
403 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
407 scm_set_cdr_x (s, SCM_EOL);
413 print_scm_val (SCM val)
415 String realval = ly_scm2string (ly_write2scm (val));
416 if (realval.length () > 200)
417 realval = realval.left_string (100)
419 + realval.right_string (100);
424 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
432 TODO: should remove #f from allowed vals?
434 if (val == SCM_EOL || val == SCM_BOOL_F)
437 if (!scm_is_symbol (sym))
442 This is used for autoBeamSettings.
444 TODO: deprecate the use of \override and \revert for
447 or use a symbol autoBeamSettingS?
452 SCM type = scm_object_property (sym, type_symbol);
454 if (type != SCM_EOL && !ly_c_procedure_p (type))
456 warning (_f ("can't find property type-check for `%s' (%s).",
457 ly_symbol2string (sym).to_str0 (),
458 ly_symbol2string (type_symbol).to_str0 ())
459 + " " + _ ("perhaps a typing error?"));
461 /* Be strict when being anal :) */
462 if (do_internal_type_checking_global)
465 warning (_ ("doing assignment anyway"));
470 && ly_c_procedure_p (type)
471 && scm_call_1 (type, val) == SCM_BOOL_F)
474 SCM typefunc = ly_lily_module_constant ("type-name");
475 SCM type_name = scm_call_1 (typefunc, type);
477 message (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
478 ly_symbol2string (sym).to_str0 (),
480 ly_scm2string (type_name).to_str0 ()));
481 progress_indication ("\n");
489 zijn deze nou handig?
490 zijn ze er al in scheme, maar heten ze anders? */
492 /* Remove doubles from (sorted) list */
496 SCM unique = SCM_EOL;
497 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
499 if (!scm_is_pair (scm_cdr (i))
500 || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
501 unique = scm_cons (scm_car (i), unique);
503 return scm_reverse_x (unique, SCM_EOL);
507 scm_default_compare (void const *a, void const *b)
513 return pa < pb ? -1 : 1;
516 /* Modify LST in place: qsort it. */
518 ly_list_qsort_uniq_x (SCM lst)
520 int len = scm_ilength (lst);
521 SCM *arr = new SCM[len];
523 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
524 arr[k++] = scm_car (s);
527 qsort (arr, len, sizeof (SCM), &scm_default_compare);
530 for (int i = 0; i < len; i++)
531 if (!i || arr[i] != arr[i - 1])
533 SCM_SETCAR (*tail, arr[i]);
534 tail = SCM_CDRLOC (*tail);
545 ly_snoc (SCM s, SCM list)
547 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
550 /* Split list at member s, removing s.
551 Return (BEFORE . AFTER) */
553 ly_split_list (SCM s, SCM list)
555 SCM before = SCM_EOL;
557 for (; scm_is_pair (after);)
559 SCM i = scm_car (after);
560 after = scm_cdr (after);
561 if (ly_c_equal_p (i, s))
563 before = scm_cons (i, before);
565 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
577 display stuff without using stack
582 SCM p = scm_current_output_port ();
585 for (; scm_is_pair (s); s = scm_cdr (s))
587 scm_display (scm_car (s), p);
591 return SCM_UNSPECIFIED;
595 int_list_to_slice (SCM l)
599 for (; scm_is_pair (l); l = scm_cdr (l))
600 if (scm_is_number (scm_car (l)))
601 s.add_point (scm_to_int (scm_car (l)));
605 /* Return I-th element, or last elt L. If I < 0, then we take the first
608 PRE: length (L) > 0 */
610 robust_list_ref (int i, SCM l)
612 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
618 robust_scm2double (SCM k, double x)
620 if (scm_is_number (k))
621 x = scm_to_double (k);
626 robust_scm2interval (SCM k, Drul_array<Real> v)
631 if (is_number_pair (k))
632 i = ly_scm2interval (k);
637 robust_scm2drul (SCM k, Drul_array<Real> v)
639 if (is_number_pair (k))
640 v = ly_scm2interval (k);
645 robust_scm2offset (SCM k, Offset o)
647 if (is_number_pair (k))
648 o = ly_scm2offset (k);
653 robust_scm2int (SCM k, int o)
655 if (scm_integer_p (k) == SCM_BOOL_T)
661 alist_to_hashq (SCM alist)
663 int i = scm_ilength (alist);
665 return scm_c_make_hash_table (0);
667 SCM tab = scm_c_make_hash_table (i);
668 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
670 SCM pt = scm_cdar (s);
671 scm_hashq_set_x (tab, scm_caar (s), pt);
677 alist_equal_p (SCM a, SCM b)
680 scm_is_pair (s); s = scm_cdr (s))
682 SCM key = scm_caar (s);
683 SCM val = scm_cdar (s);
684 SCM l = scm_assoc (key, b);
687 || !ly_c_equal_p (scm_cdr (l), val))
695 ly_alist_vals (SCM alist)
698 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
700 x = scm_cons (scm_cdar (p), x);
706 ly_hash2alist (SCM tab)
708 SCM func = ly_lily_module_constant ("hash-table->alist");
709 return scm_call_1 (func, tab);