2 lily-guile.cc -- implement assorted SCM interface functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2007 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@xs4all.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 "string-convert.hh"
29 #include "source-file.hh"
38 ly_scm_write_string (SCM s
)
40 SCM port
= scm_mkstrport (SCM_INUM0
,
41 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
44 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
45 SCM write
= scm_primitive_eval (ly_symbol2scm ("write"));
47 // scm_apply (write, port, SCM_EOL);
48 scm_call_2 (write
, s
, port
);
49 return ly_scm2string (scm_strport_to_string (port
));
55 return scm_list_n (ly_symbol2scm ("quote"), s
, SCM_UNDEFINED
);
59 ly_symbol2string (SCM s
)
62 Ugh. this is not very efficient.
64 SCM str
= scm_symbol_to_string (s
);
65 return ly_scm2string (str
);
69 gulp_file_to_string (string fn
, bool must_exist
, int size
)
71 string s
= global_path
.find (fn
);
76 string e
= _f ("cannot find file: `%s'", fn
);
78 e
+= _f ("(load path: `%s')", global_path
.to_string ());
85 if (be_verbose_global
)
86 progress_indication ("[" + s
);
88 vector
<char> chars
= gulp_file (s
, size
);
89 string
result (&chars
[0], chars
.size ());
91 if (be_verbose_global
)
92 progress_indication ("]");
98 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
100 ly_display_scm (SCM s
)
102 scm_display (s
, scm_current_output_port ());
103 scm_newline (scm_current_output_port ());
111 ly_scm2string (SCM str
)
113 assert (scm_is_string (str
));
115 size_t len
= scm_c_string_length (str
);
118 scm_to_locale_stringbuf(str
, &result
.at(0), len
);
124 ly_string2scm (string
const &str
)
126 return scm_from_locale_stringn (str
.c_str (),
132 ly_scm2newstr (SCM str
, size_t *lenp
)
134 char* p
= scm_to_locale_stringn(str
, lenp
);
142 index_get_cell (SCM s
, Direction d
)
145 return (d
== LEFT
) ? scm_car (s
) : scm_cdr (s
);
149 index_set_cell (SCM s
, Direction d
, SCM v
)
152 scm_set_car_x (s
, v
);
154 scm_set_cdr_x (s
, v
);
159 is_number_pair (SCM p
)
161 return scm_is_pair (p
)
162 && scm_is_number (scm_car (p
)) && scm_is_number (scm_cdr (p
));
169 return scm_ihashv (s
, ~1u);
175 if (scm_is_number (s
))
177 int i
= scm_to_int (s
);
178 return i
== 0 || i
== 1;
186 return scm_is_bool (s
) && ly_scm2bool (s
);
195 return scm_is_integer (s
) ? (Direction
) scm_to_int (s
) : CENTER
;
199 robust_scm2dir (SCM d
, Direction def
)
201 if (is_direction (d
))
209 if (scm_is_number (s
))
211 int i
= scm_to_int (s
);
212 return i
>= -1 && i
<= 1;
221 ly_scm2interval (SCM p
)
223 return Interval (scm_to_double (scm_car (p
)), scm_to_double (scm_cdr (p
)));
227 ly_scm2realdrul (SCM p
)
229 return Drul_array
<Real
> (scm_to_double (scm_car (p
)),
230 scm_to_double (scm_cdr (p
)));
234 ly_interval2scm (Drul_array
<Real
> i
)
236 return scm_cons (scm_from_double (i
[LEFT
]), scm_from_double (i
[RIGHT
]));
241 robust_scm2interval (SCM k
, Drul_array
<Real
> v
)
246 if (is_number_pair (k
))
247 i
= ly_scm2interval (k
);
252 robust_scm2drul (SCM k
, Drul_array
<Real
> v
)
254 if (is_number_pair (k
))
255 v
= ly_scm2interval (k
);
260 robust_scm2booldrul (SCM k
, Drul_array
<bool> def
)
264 def
[LEFT
] = to_boolean (scm_car (k
));
265 def
[RIGHT
] = to_boolean (scm_cdr (k
));
274 ly_offset2scm (Offset o
)
276 return scm_cons (scm_from_double (o
[X_AXIS
]), scm_from_double (o
[Y_AXIS
]));
280 ly_scm2offset (SCM s
)
282 return Offset (scm_to_double (scm_car (s
)),
283 scm_to_double (scm_cdr (s
)));
287 robust_scm2offset (SCM k
, Offset o
)
289 if (is_number_pair (k
))
290 o
= ly_scm2offset (k
);
294 ly_offsets2scm (vector
<Offset
> os
)
298 for (vsize i
= 0; i
< os
.size (); i
++)
300 *tail
= scm_cons (ly_offset2scm (os
[i
]), SCM_EOL
);
301 tail
= SCM_CDRLOC (*tail
);
307 ly_scm2offsets (SCM s
)
310 for (; scm_is_pair (s
); s
= scm_cdr (s
))
311 os
.push_back (ly_scm2offset (scm_car (s
)));
323 alist_equal_p (SCM a
, SCM b
)
326 scm_is_pair (s
); s
= scm_cdr (s
))
328 SCM key
= scm_caar (s
);
329 SCM val
= scm_cdar (s
);
330 SCM l
= scm_assoc (key
, b
);
333 || !ly_is_equal (scm_cdr (l
), val
))
341 ly_alist_vals (SCM alist
)
344 for (SCM p
= alist
; scm_is_pair (p
); p
= scm_cdr (p
))
345 x
= scm_cons (scm_cdar (p
), x
);
353 /* Return I-th element, or last elt L. If I < 0, then we take the first
356 PRE: length (L) > 0 */
358 robust_list_ref (int i
, SCM l
)
360 while (i
-- > 0 && scm_is_pair (scm_cdr (l
)))
367 ly_deep_copy (SCM src
)
369 if (scm_is_pair (src
))
370 return scm_cons (ly_deep_copy (scm_car (src
)), ly_deep_copy (scm_cdr (src
)));
371 else if (scm_is_vector (src
))
373 int len
= scm_c_vector_length (src
);
374 SCM nv
= scm_c_make_vector (len
, SCM_UNDEFINED
);
375 for (int i
= 0;i
< len
; i
++)
377 SCM si
= scm_from_int (i
);
378 scm_vector_set_x (nv
, si
, ly_deep_copy (scm_vector_ref (src
, si
)));
385 print_scm_val (SCM val
)
387 string realval
= ly_scm_write_string (val
);
388 if (realval
.length () > 200)
389 realval
= realval
.substr (0, 100)
391 + realval
.substr (realval
.length () - 100);
396 type_check_assignment (SCM sym
, SCM val
, SCM type_symbol
)
404 TODO: should remove #f from allowed vals?
406 if (val
== SCM_EOL
|| val
== SCM_BOOL_F
)
409 if (!scm_is_symbol (sym
))
414 This is used for autoBeamSettings.
416 TODO: deprecate the use of \override and \revert for
419 or use a symbol autoBeamSettingS?
424 SCM type
= scm_object_property (sym
, type_symbol
);
426 if (type
!= SCM_EOL
&& !ly_is_procedure (type
))
428 warning (_f ("cannot find property type-check for `%s' (%s).",
429 ly_symbol2string (sym
).c_str (),
430 ly_symbol2string (type_symbol
).c_str ())
431 + " " + _ ("perhaps a typing error?"));
433 /* Be strict when being anal :) */
434 if (do_internal_type_checking_global
)
435 scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
438 warning (_ ("doing assignment anyway"));
443 && ly_is_procedure (type
)
444 && scm_call_1 (type
, val
) == SCM_BOOL_F
)
447 SCM typefunc
= ly_lily_module_constant ("type-name");
448 SCM type_name
= scm_call_1 (typefunc
, type
);
450 warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
451 ly_symbol2string (sym
).c_str (),
453 ly_scm2string (type_name
).c_str ()));
454 progress_indication ("\n");
462 zijn deze nou handig?
463 zijn ze er al in scheme, maar heten ze anders? */
465 /* Remove doubles from (sorted) list */
469 SCM unique
= SCM_EOL
;
470 for (SCM i
= list
; scm_is_pair (i
); i
= scm_cdr (i
))
472 if (!scm_is_pair (scm_cdr (i
))
473 || !ly_is_equal (scm_car (i
), scm_cadr (i
)))
474 unique
= scm_cons (scm_car (i
), unique
);
476 return scm_reverse_x (unique
, SCM_EOL
);
480 /* Split list at member s, removing s.
481 Return (BEFORE . AFTER) */
483 ly_split_list (SCM s
, SCM list
)
485 SCM before
= SCM_EOL
;
487 for (; scm_is_pair (after
);)
489 SCM i
= scm_car (after
);
490 after
= scm_cdr (after
);
491 if (ly_is_equal (i
, s
))
493 before
= scm_cons (i
, before
);
495 return scm_cons (scm_reverse_x (before
, SCM_EOL
), after
);
507 display stuff without using stack
512 SCM p
= scm_current_output_port ();
515 for (; scm_is_pair (s
); s
= scm_cdr (s
))
517 scm_display (scm_car (s
), p
);
521 return SCM_UNSPECIFIED
;
525 int_list_to_slice (SCM l
)
529 for (; scm_is_pair (l
); l
= scm_cdr (l
))
530 if (scm_is_number (scm_car (l
)))
531 s
.add_point (scm_to_int (scm_car (l
)));
536 robust_scm2double (SCM k
, double x
)
538 if (scm_is_number (k
))
539 x
= scm_to_double (k
);
545 robust_scm2string (SCM k
, string s
)
547 if (scm_is_string (k
))
548 s
= ly_scm2string (k
);
553 robust_scm2int (SCM k
, int o
)
555 if (scm_integer_p (k
) == SCM_BOOL_T
)
562 ly_rational2scm (Rational r
)
564 return scm_divide (scm_from_int (r
.numerator ()), scm_from_int (r
.denominator ()));
569 ly_scm2rational (SCM r
)
571 return Rational (scm_to_int (scm_numerator (r
)),
572 scm_to_int (scm_denominator (r
)));
576 robust_scm2rational (SCM n
, Rational rat
)
578 if (ly_is_fraction (n
))
579 return ly_scm2rational (n
);
585 alist_to_hashq (SCM alist
)
587 int i
= scm_ilength (alist
);
589 return scm_c_make_hash_table (0);
591 SCM tab
= scm_c_make_hash_table (i
);
592 for (SCM s
= alist
; scm_is_pair (s
); s
= scm_cdr (s
))
594 SCM pt
= scm_cdar (s
);
595 scm_hashq_set_x (tab
, scm_caar (s
), pt
);
601 ly_hash2alist (SCM tab
)
603 SCM func
= ly_lily_module_constant ("hash-table->alist");
604 return scm_call_1 (func
, tab
);
613 mangle_cxx_identifier (string cxx_id
)
615 if (cxx_id
.substr (0, 3) == "ly_")
616 cxx_id
= cxx_id
.replace (0, 3, "ly:");
619 cxx_id
= String_convert::to_lower (cxx_id
);
620 cxx_id
= "ly:" + cxx_id
;
622 if (cxx_id
.substr (cxx_id
.length () - 2) == "_p")
623 cxx_id
= cxx_id
.replace (cxx_id
.length () - 2, 2, "?");
624 else if (cxx_id
.substr (cxx_id
.length () - 2) == "_x")
625 cxx_id
= cxx_id
.replace (cxx_id
.length () - 2, 2, "!");
627 replace_all (&cxx_id
, "_less?", "<?");
628 replace_all (&cxx_id
, "_2_", "->");
629 replace_all (&cxx_id
, "__", "::");
630 replace_all (&cxx_id
, '_', '-');
639 ly_string_array_to_scm (vector
<string
> a
)
642 for (vsize i
= a
.size (); i
; i
--)
643 s
= scm_cons (ly_symbol2scm (a
[i
- 1].c_str ()), s
);
647 /* SYMBOLS is a whitespace separated list. */
649 parse_symbol_list (char const *symbols
)
651 while (isspace (*symbols
))
654 replace_all (&s
, '\n', ' ');
655 replace_all (&s
, '\t', ' ');
656 replace_all (&s
, " ", " ");
657 return ly_string_array_to_scm (string_split (s
, ' '));
661 struct ly_t_double_cell
669 /* inserts at front, removing duplicates */
670 SCM
ly_assoc_prepend_x (SCM alist
, SCM key
, SCM val
)
672 return scm_acons (key
, val
, scm_assoc_remove_x (alist
, key
));