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)
208 TODO: load this dynamically
210 initialize_kpathsea ();
214 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
217 SCM global_lily_module;
222 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
223 scm_c_use_module ("lily");
229 return scm_ihashv (s, ~1u);
235 if (scm_is_number (s))
237 int i = scm_to_int (s);
238 return i>= -1 && i <= 1;
246 if (scm_is_number (s))
248 int i = scm_to_int (s);
249 return i == 0 || i == 1;
257 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
261 ly_scm2interval (SCM p)
263 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
267 ly_scm2realdrul (SCM p)
269 return Drul_array<Real> (scm_to_double (scm_car (p)),
270 scm_to_double (scm_cdr (p)));
274 ly_interval2scm (Drul_array<Real> i)
276 return scm_cons (scm_make_real (i[LEFT]), scm_make_real (i[RIGHT]));
282 return scm_is_bool (s) && ly_scm2bool (s);
285 /* Appendable list L: the cdr contains the list, the car the last cons
290 SCM s = scm_cons (SCM_EOL, SCM_EOL);
291 scm_set_car_x (s, s);
297 appendable_list_append (SCM l, SCM elt)
299 SCM newcons = scm_cons (elt, SCM_EOL);
301 scm_set_cdr_x (scm_car (l), newcons);
302 scm_set_car_x (l, newcons);
306 ly_offset2scm (Offset o)
308 return scm_cons (scm_make_real (o[X_AXIS]), scm_make_real (o[Y_AXIS]));
312 ly_scm2offset (SCM s)
314 return Offset (scm_to_double (scm_car (s)),
315 scm_to_double (scm_cdr (s)));
319 ly_deep_copy (SCM src)
321 if (scm_is_pair (src))
322 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
323 else if (scm_is_vector (src))
325 int len = scm_c_vector_length (src);
326 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
327 for (int i = 0 ;i < len ; i++)
329 SCM si = scm_int2num (i);
330 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
337 ly_chain_assoc_get (SCM key, SCM achain, SCM dfault)
339 if (scm_is_pair (achain))
341 SCM handle = scm_assoc (key, scm_car (achain));
342 if (scm_is_pair (handle))
343 return scm_cdr (handle);
345 return ly_chain_assoc (key, scm_cdr (achain));
352 ly_chain_assoc (SCM key, SCM achain)
354 if (scm_is_pair (achain))
356 SCM handle = scm_assoc (key, scm_car (achain));
357 if (scm_is_pair (handle))
360 return ly_chain_assoc (key, scm_cdr (achain));
366 /* looks the key up in the cdrs of the alist-keys
367 - ignoring the car and ignoring non-pair keys.
368 Returns first match found, i.e.
376 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
377 element with the cdr of the key = 1. In other words (alloc_cdr key)
380 (alloc (anything . key))
386 ly_assoc_cdr (SCM key, SCM alist)
388 if (scm_is_pair (alist))
390 SCM trykey = scm_caar (alist);
391 if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
392 return scm_car (alist);
394 return ly_assoc_cdr (key, scm_cdr (alist));
399 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
400 i.e. \n and ' ' can be used interchangeably as separators. */
402 parse_symbol_list (char const *lst)
404 char *s = strdup (lst);
406 SCM create_list = SCM_EOL;
408 char * e = s + strlen (s) - 1;
409 while (e >= s && isspace (*e))
412 for (char * p = s; *p; p++)
421 char *next = strchr (s, ' ');
425 create_list = scm_cons (ly_symbol2scm (s), create_list);
434 ly_truncate_list (int k, SCM lst)
442 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
446 scm_set_cdr_x (s, SCM_EOL);
452 print_scm_val (SCM val)
454 String realval = ly_scm2string (ly_write2scm (val));
455 if (realval.length () > 200)
456 realval = realval.left_string (100)
458 + realval.right_string (100);
463 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
471 TODO: should remove #f from allowed vals?
473 if (val == SCM_EOL || val == SCM_BOOL_F)
476 if (!scm_is_symbol (sym))
481 This is used for autoBeamSettings.
483 TODO: deprecate the use of \override and \revert for
486 or use a symbol autoBeamSettingS?
491 SCM type = scm_object_property (sym, type_symbol);
493 if (type != SCM_EOL && !ly_c_procedure_p (type))
495 warning (_f ("Can't find property type-check for `%s' (%s).",
496 ly_symbol2string (sym).to_str0 (),
497 ly_symbol2string (type_symbol).to_str0 ())
498 + " " + _ ("Perhaps you made a typing error?"));
500 /* Be strict when being anal :) */
501 if (do_internal_type_checking_global)
504 warning (_ ("Doing assignment anyway."));
509 && ly_c_procedure_p (type)
510 && scm_call_1 (type, val) == SCM_BOOL_F)
512 SCM errport = scm_current_error_port ();
514 SCM typefunc = ly_lily_module_constant ("type-name");
515 SCM type_name = scm_call_1 (typefunc, type);
518 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
519 ly_symbol2string (sym).to_str0 (),
521 ly_scm2string (type_name).to_str0 ()).to_str0 (),
523 scm_puts ("\n", errport);
532 zijn deze nou handig?
533 zijn ze er al in scheme, maar heten ze anders? */
536 /* Remove doubles from (sorted) list */
540 SCM unique = SCM_EOL;
541 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
543 if (!scm_is_pair (scm_cdr (i))
544 || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
545 unique = scm_cons (scm_car (i), unique);
547 return scm_reverse_x (unique, SCM_EOL);
552 scm_default_compare (void const *a, void const *b)
558 return pa < pb ? -1 : 1;
561 /* Modify LST in place: qsort it. */
563 ly_list_qsort_uniq_x (SCM lst)
565 int len = scm_ilength (lst);
566 SCM *arr = new SCM[len];
568 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
569 arr[k++] = scm_car (s);
572 qsort (arr, len, sizeof (SCM), &scm_default_compare);
575 for (int i = 0; i < len; i++)
576 if (!i || arr[i] != arr[i - 1])
578 SCM_SETCAR (*tail, arr[i]);
579 tail = SCM_CDRLOC (*tail);
591 ly_snoc (SCM s, SCM list)
593 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
596 /* Split list at member s, removing s.
597 Return (BEFORE . AFTER) */
599 ly_split_list (SCM s, SCM list)
601 SCM before = SCM_EOL;
603 for (; scm_is_pair (after);)
605 SCM i = scm_car (after);
606 after = scm_cdr (after);
607 if (ly_c_equal_p (i, s))
609 before = scm_cons (i, before);
611 return scm_cons ( scm_reverse_x (before, SCM_EOL), after);
625 display stuff without using stack
630 SCM p = scm_current_output_port ();
633 for (; scm_is_pair (s); s = scm_cdr (s))
635 scm_display (scm_car (s), p);
639 return SCM_UNSPECIFIED;
643 int_list_to_slice (SCM l)
647 for (; scm_is_pair (l); l = scm_cdr (l))
648 if (scm_is_number (scm_car (l)))
649 s.add_point (scm_to_int (scm_car (l)));
653 /* Return I-th element, or last elt L. If I < 0, then we take the first
656 PRE: length (L) > 0 */
658 robust_list_ref (int i, SCM l)
660 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
666 robust_scm2double (SCM k, double x)
668 if (scm_is_number (k))
669 x = scm_to_double (k);
674 robust_scm2interval (SCM k, Drul_array<Real> v)
679 if (is_number_pair (k))
680 i = ly_scm2interval (k);
685 robust_scm2drul (SCM k, Drul_array<Real> v)
687 if (is_number_pair (k))
688 v = ly_scm2interval (k);
693 robust_scm2offset (SCM k, Offset o)
695 if (is_number_pair (k))
696 o = ly_scm2offset (k);
701 robust_scm2int (SCM k, int o)
703 if (scm_integer_p (k) == SCM_BOOL_T)
709 alist_to_hashq (SCM alist)
711 int i = scm_ilength (alist);
713 return scm_c_make_hash_table (0);
715 SCM tab = scm_c_make_hash_table (i);
716 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
718 SCM pt = scm_cdar (s);
719 scm_hashq_set_x (tab, scm_caar (s), pt);
726 alist_equal_p (SCM a, SCM b)
729 scm_is_pair (s); s = scm_cdr (s))
731 SCM key = scm_caar (s);
732 SCM val = scm_cdar (s);
733 SCM l = scm_assoc (key, b);
736 || !ly_c_equal_p ( scm_cdr (l), val))
746 ly_alist_vals (SCM alist)
749 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
751 x = scm_cons (scm_cdar (p), x);
757 ly_hash2alist (SCM tab)
759 SCM func = ly_lily_module_constant ("hash-table->alist");
760 return scm_call_1 (func, tab);