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)
204 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)));
331 ly_chain_assoc_get (SCM key, SCM achain, SCM dfault)
333 if (scm_is_pair (achain))
335 SCM handle = scm_assoc (key, scm_car (achain));
336 if (scm_is_pair (handle))
337 return scm_cdr (handle);
339 return ly_chain_assoc (key, scm_cdr (achain));
346 ly_chain_assoc (SCM key, SCM achain)
348 if (scm_is_pair (achain))
350 SCM handle = scm_assoc (key, scm_car (achain));
351 if (scm_is_pair (handle))
354 return ly_chain_assoc (key, scm_cdr (achain));
360 /* looks the key up in the cdrs of the alist-keys
361 - ignoring the car and ignoring non-pair keys.
362 Returns first match found, i.e.
370 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
371 element with the cdr of the key = 1. In other words (alloc_cdr key)
374 (alloc (anything . key))
377 ly_assoc_cdr (SCM key, SCM alist)
379 if (scm_is_pair (alist))
381 SCM trykey = scm_caar (alist);
382 if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
383 return scm_car (alist);
385 return ly_assoc_cdr (key, scm_cdr (alist));
390 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
391 i.e. \n and ' ' can be used interchangeably as separators. */
393 parse_symbol_list (char const *lst)
395 char *s = strdup (lst);
397 SCM create_list = SCM_EOL;
399 char *e = s + strlen (s) - 1;
400 while (e >= s && isspace (*e))
403 for (char *p = s; *p; p++)
412 char *next = strchr (s, ' ');
416 create_list = scm_cons (ly_symbol2scm (s), create_list);
425 ly_truncate_list (int k, SCM lst)
433 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
437 scm_set_cdr_x (s, SCM_EOL);
443 print_scm_val (SCM val)
445 String realval = ly_scm2string (ly_write2scm (val));
446 if (realval.length () > 200)
447 realval = realval.left_string (100)
449 + realval.right_string (100);
454 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
462 TODO: should remove #f from allowed vals?
464 if (val == SCM_EOL || val == SCM_BOOL_F)
467 if (!scm_is_symbol (sym))
472 This is used for autoBeamSettings.
474 TODO: deprecate the use of \override and \revert for
477 or use a symbol autoBeamSettingS?
482 SCM type = scm_object_property (sym, type_symbol);
484 if (type != SCM_EOL && !ly_c_procedure_p (type))
486 warning (_f ("Can't find property type-check for `%s' (%s).",
487 ly_symbol2string (sym).to_str0 (),
488 ly_symbol2string (type_symbol).to_str0 ())
489 + " " + _ ("Perhaps you made a typing error?"));
491 /* Be strict when being anal :) */
492 if (do_internal_type_checking_global)
495 warning (_ ("Doing assignment anyway."));
500 && ly_c_procedure_p (type)
501 && scm_call_1 (type, val) == SCM_BOOL_F)
503 SCM errport = scm_current_error_port ();
505 SCM typefunc = ly_lily_module_constant ("type-name");
506 SCM type_name = scm_call_1 (typefunc, type);
508 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
509 ly_symbol2string (sym).to_str0 (),
511 ly_scm2string (type_name).to_str0 ()).to_str0 (),
513 scm_puts ("\n", errport);
521 zijn deze nou handig?
522 zijn ze er al in scheme, maar heten ze anders? */
524 /* Remove doubles from (sorted) list */
528 SCM unique = SCM_EOL;
529 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
531 if (!scm_is_pair (scm_cdr (i))
532 || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
533 unique = scm_cons (scm_car (i), unique);
535 return scm_reverse_x (unique, SCM_EOL);
539 scm_default_compare (void const *a, void const *b)
545 return pa < pb ? -1 : 1;
548 /* Modify LST in place: qsort it. */
550 ly_list_qsort_uniq_x (SCM lst)
552 int len = scm_ilength (lst);
553 SCM *arr = new SCM[len];
555 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
556 arr[k++] = scm_car (s);
559 qsort (arr, len, sizeof (SCM), &scm_default_compare);
562 for (int i = 0; i < len; i++)
563 if (!i || arr[i] != arr[i - 1])
565 SCM_SETCAR (*tail, arr[i]);
566 tail = SCM_CDRLOC (*tail);
577 ly_snoc (SCM s, SCM list)
579 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
582 /* Split list at member s, removing s.
583 Return (BEFORE . AFTER) */
585 ly_split_list (SCM s, SCM list)
587 SCM before = SCM_EOL;
589 for (; scm_is_pair (after);)
591 SCM i = scm_car (after);
592 after = scm_cdr (after);
593 if (ly_c_equal_p (i, s))
595 before = scm_cons (i, before);
597 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
610 display stuff without using stack
615 SCM p = scm_current_output_port ();
618 for (; scm_is_pair (s); s = scm_cdr (s))
620 scm_display (scm_car (s), p);
624 return SCM_UNSPECIFIED;
628 int_list_to_slice (SCM l)
632 for (; scm_is_pair (l); l = scm_cdr (l))
633 if (scm_is_number (scm_car (l)))
634 s.add_point (scm_to_int (scm_car (l)));
638 /* Return I-th element, or last elt L. If I < 0, then we take the first
641 PRE: length (L) > 0 */
643 robust_list_ref (int i, SCM l)
645 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
651 robust_scm2double (SCM k, double x)
653 if (scm_is_number (k))
654 x = scm_to_double (k);
659 robust_scm2interval (SCM k, Drul_array<Real> v)
664 if (is_number_pair (k))
665 i = ly_scm2interval (k);
670 robust_scm2drul (SCM k, Drul_array<Real> v)
672 if (is_number_pair (k))
673 v = ly_scm2interval (k);
678 robust_scm2offset (SCM k, Offset o)
680 if (is_number_pair (k))
681 o = ly_scm2offset (k);
686 robust_scm2int (SCM k, int o)
688 if (scm_integer_p (k) == SCM_BOOL_T)
694 alist_to_hashq (SCM alist)
696 int i = scm_ilength (alist);
698 return scm_c_make_hash_table (0);
700 SCM tab = scm_c_make_hash_table (i);
701 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
703 SCM pt = scm_cdar (s);
704 scm_hashq_set_x (tab, scm_caar (s), pt);
710 alist_equal_p (SCM a, SCM b)
713 scm_is_pair (s); s = scm_cdr (s))
715 SCM key = scm_caar (s);
716 SCM val = scm_cdar (s);
717 SCM l = scm_assoc (key, b);
720 || !ly_c_equal_p (scm_cdr (l), val))
728 ly_alist_vals (SCM alist)
731 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
733 x = scm_cons (scm_cdar (p), x);
739 ly_hash2alist (SCM tab)
741 SCM func = ly_lily_module_constant ("hash-table->alist");
742 return scm_call_1 (func, tab);