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)));
329 ly_chain_assoc_get (SCM key, SCM achain, SCM dfault)
331 if (scm_is_pair (achain))
333 SCM handle = scm_assoc (key, scm_car (achain));
334 if (scm_is_pair (handle))
335 return scm_cdr (handle);
337 return ly_chain_assoc (key, scm_cdr (achain));
344 ly_chain_assoc (SCM key, SCM achain)
346 if (scm_is_pair (achain))
348 SCM handle = scm_assoc (key, scm_car (achain));
349 if (scm_is_pair (handle))
352 return ly_chain_assoc (key, scm_cdr (achain));
358 /* looks the key up in the cdrs of the alist-keys
359 - ignoring the car and ignoring non-pair keys.
360 Returns first match found, i.e.
368 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
369 element with the cdr of the key = 1. In other words (alloc_cdr key)
372 (alloc (anything . key))
375 ly_assoc_cdr (SCM key, SCM alist)
377 if (scm_is_pair (alist))
379 SCM trykey = scm_caar (alist);
380 if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
381 return scm_car (alist);
383 return ly_assoc_cdr (key, scm_cdr (alist));
388 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
389 i.e. \n and ' ' can be used interchangeably as separators. */
391 parse_symbol_list (char const *lst)
393 char *s = strdup (lst);
395 SCM create_list = SCM_EOL;
397 char *e = s + strlen (s) - 1;
398 while (e >= s && isspace (*e))
401 for (char *p = s; *p; p++)
410 char *next = strchr (s, ' ');
414 create_list = scm_cons (ly_symbol2scm (s), create_list);
423 ly_truncate_list (int k, SCM lst)
431 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
435 scm_set_cdr_x (s, SCM_EOL);
441 print_scm_val (SCM val)
443 String realval = ly_scm2string (ly_write2scm (val));
444 if (realval.length () > 200)
445 realval = realval.left_string (100)
447 + realval.right_string (100);
452 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
460 TODO: should remove #f from allowed vals?
462 if (val == SCM_EOL || val == SCM_BOOL_F)
465 if (!scm_is_symbol (sym))
470 This is used for autoBeamSettings.
472 TODO: deprecate the use of \override and \revert for
475 or use a symbol autoBeamSettingS?
480 SCM type = scm_object_property (sym, type_symbol);
482 if (type != SCM_EOL && !ly_c_procedure_p (type))
484 warning (_f ("can't find property type-check for `%s' (%s).",
485 ly_symbol2string (sym).to_str0 (),
486 ly_symbol2string (type_symbol).to_str0 ())
487 + " " + _ ("perhaps a typing error?"));
489 /* Be strict when being anal :) */
490 if (do_internal_type_checking_global)
493 warning (_ ("doing assignment anyway"));
498 && ly_c_procedure_p (type)
499 && scm_call_1 (type, val) == SCM_BOOL_F)
502 SCM typefunc = ly_lily_module_constant ("type-name");
503 SCM type_name = scm_call_1 (typefunc, type);
505 message (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
506 ly_symbol2string (sym).to_str0 (),
508 ly_scm2string (type_name).to_str0 ()));
509 progress_indication ("\n");
517 zijn deze nou handig?
518 zijn ze er al in scheme, maar heten ze anders? */
520 /* Remove doubles from (sorted) list */
524 SCM unique = SCM_EOL;
525 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
527 if (!scm_is_pair (scm_cdr (i))
528 || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
529 unique = scm_cons (scm_car (i), unique);
531 return scm_reverse_x (unique, SCM_EOL);
535 scm_default_compare (void const *a, void const *b)
541 return pa < pb ? -1 : 1;
544 /* Modify LST in place: qsort it. */
546 ly_list_qsort_uniq_x (SCM lst)
548 int len = scm_ilength (lst);
549 SCM *arr = new SCM[len];
551 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
552 arr[k++] = scm_car (s);
555 qsort (arr, len, sizeof (SCM), &scm_default_compare);
558 for (int i = 0; i < len; i++)
559 if (!i || arr[i] != arr[i - 1])
561 SCM_SETCAR (*tail, arr[i]);
562 tail = SCM_CDRLOC (*tail);
573 ly_snoc (SCM s, SCM list)
575 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
578 /* Split list at member s, removing s.
579 Return (BEFORE . AFTER) */
581 ly_split_list (SCM s, SCM list)
583 SCM before = SCM_EOL;
585 for (; scm_is_pair (after);)
587 SCM i = scm_car (after);
588 after = scm_cdr (after);
589 if (ly_c_equal_p (i, s))
591 before = scm_cons (i, before);
593 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
605 display stuff without using stack
610 SCM p = scm_current_output_port ();
613 for (; scm_is_pair (s); s = scm_cdr (s))
615 scm_display (scm_car (s), p);
619 return SCM_UNSPECIFIED;
623 int_list_to_slice (SCM l)
627 for (; scm_is_pair (l); l = scm_cdr (l))
628 if (scm_is_number (scm_car (l)))
629 s.add_point (scm_to_int (scm_car (l)));
633 /* Return I-th element, or last elt L. If I < 0, then we take the first
636 PRE: length (L) > 0 */
638 robust_list_ref (int i, SCM l)
640 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
646 robust_scm2double (SCM k, double x)
648 if (scm_is_number (k))
649 x = scm_to_double (k);
654 robust_scm2interval (SCM k, Drul_array<Real> v)
659 if (is_number_pair (k))
660 i = ly_scm2interval (k);
665 robust_scm2drul (SCM k, Drul_array<Real> v)
667 if (is_number_pair (k))
668 v = ly_scm2interval (k);
673 robust_scm2offset (SCM k, Offset o)
675 if (is_number_pair (k))
676 o = ly_scm2offset (k);
681 robust_scm2int (SCM k, int o)
683 if (scm_integer_p (k) == SCM_BOOL_T)
689 alist_to_hashq (SCM alist)
691 int i = scm_ilength (alist);
693 return scm_c_make_hash_table (0);
695 SCM tab = scm_c_make_hash_table (i);
696 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
698 SCM pt = scm_cdar (s);
699 scm_hashq_set_x (tab, scm_caar (s), pt);
705 alist_equal_p (SCM a, SCM b)
708 scm_is_pair (s); s = scm_cdr (s))
710 SCM key = scm_caar (s);
711 SCM val = scm_cdar (s);
712 SCM l = scm_assoc (key, b);
715 || !ly_c_equal_p (scm_cdr (l), val))
723 ly_alist_vals (SCM alist)
726 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
728 x = scm_cons (scm_cdar (p), x);
734 ly_hash2alist (SCM tab)
736 SCM func = ly_lily_module_constant ("hash-table->alist");
737 return scm_call_1 (func, tab);