2 lily-guile.cc -- implement assorted guile functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@cs.uu.nl>
13 #include <libintl.h> // gettext on macos x
14 #include <math.h> /* isinf */
15 #include <string.h> /* strdup, strchr */
18 #include "lily-proto.hh"
22 source-file.hh includes cmath which undefines isinf and isnan
24 FIXME: #ifdef MACOS_X?
26 inline int my_isinf (Real r) { return isinf (r); }
27 inline int my_isnan (Real r) { return isnan (r); }
30 #include "libc-extension.hh"
31 #include "lily-guile.hh"
33 #include "file-path.hh"
35 #include "direction.hh"
37 #include "interval.hh"
39 #include "dimensions.hh"
40 #include "source-file.hh"
47 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);
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)
86 String s = global_path.find (fn);
89 String e = _f ("can't find file: `%s'", fn);
91 e += _f ("(load path: `%s')", global_path.to_string ());
94 else if (verbose_global_b)
95 progress_indication ("[" + s);
98 char * str = gulp_file (s, &n);
102 if (verbose_global_b)
103 progress_indication ("]");
108 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
110 "Read the file @var{name}, and return its contents in a string. "
111 "The file is looked up using the search path.")
113 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
114 return scm_makfrom0str (gulp_file_to_string (ly_scm2string (name)).to_str0 ());
119 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
121 ly_display_scm (SCM s)
123 scm_display (s, scm_current_output_port ());
124 scm_newline (scm_current_output_port ());
129 ly_scm2string (SCM str)
131 return String ((Byte*)scm_i_string_chars (str),
132 (int) scm_i_string_length (str));
136 ly_scm2newstr (SCM str, size_t *lenp)
138 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
140 size_t len = SCM_STRING_LENGTH (str);
141 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
143 memcpy (new_str, scm_i_string_chars (str), len);
155 index_get_cell (SCM s, Direction d)
159 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
163 index_set_cell (SCM s, Direction d, SCM v)
166 scm_set_car_x (s, v);
168 scm_set_cdr_x (s, v);
172 LY_DEFINE (ly_warn, "ly:warn",
173 1, 0, 1, (SCM str, SCM rest),
174 "Scheme callable function to issue the warning @code{msg}. "
175 "The message is formatted with @code{format} and @code{rest}.")
177 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
178 progress_indication ("\n");
180 str = scm_simple_format (SCM_BOOL_F, str, rest);
181 warning ("lily-guile: " + ly_scm2string (str));
182 return SCM_UNSPECIFIED;
185 LY_DEFINE (ly_programming_error, "ly:programming-error",
186 1, 0, 1, (SCM str, SCM rest),
187 "Scheme callable function to issue the warning @code{msg}. "
188 "The message is formatted with @code{format} and @code{rest}.")
190 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
191 progress_indication ("\n");
193 str = scm_simple_format (SCM_BOOL_F, str, rest);
194 programming_error (ly_scm2string (str));
195 return SCM_UNSPECIFIED;
198 LY_DEFINE (ly_dir_p, "ly:dir?",
200 "type predicate. A direction is @code{-1}, @code{0} or "
201 "@code{1}, where @code{-1} represents "
202 "left or down and @code{1} represents right or up.")
204 if (scm_is_number (s))
206 int i = scm_to_int (s);
207 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
213 is_number_pair (SCM p)
215 return scm_is_pair (p)
216 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
219 typedef void (*Void_fptr) ();
220 Array<Void_fptr> *scm_init_funcs_;
222 void add_scm_init_func (void (*f) ())
224 if (!scm_init_funcs_)
225 scm_init_funcs_ = new Array<Void_fptr>;
227 scm_init_funcs_->push (f);
231 ly_init_ly_module (void *)
233 for (int i=scm_init_funcs_->size () ; i--;)
234 (scm_init_funcs_->elem (i)) ();
236 if (verbose_global_b)
237 progress_indication ("\n");
239 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
242 SCM global_lily_module;
247 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
248 scm_c_use_module ("lily");
254 return scm_ihashv (s, ~1u);
260 if (scm_is_number (s))
262 int i = scm_to_int (s);
263 return i>= -1 && i <= 1;
268 LY_DEFINE(ly_assoc_get, "ly:assoc-get",
270 (SCM key, SCM alist, SCM default_value),
271 "Return value if KEY in ALIST, else DEFAULT-VALUE (or #f if not specified).")
273 SCM handle = scm_assoc (key, alist);
275 if (default_value == SCM_UNDEFINED)
276 default_value = SCM_BOOL_F;
278 if (scm_is_pair (handle))
279 return scm_cdr (handle);
281 return default_value;
287 if (scm_is_number (s))
289 int i = scm_to_int (s);
290 return i== 0 || i == 1;
298 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
302 ly_scm2interval (SCM p)
304 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
308 ly_scm2realdrul (SCM p)
310 return Drul_array<Real> (scm_to_double (scm_car (p)),
311 scm_to_double (scm_cdr (p)));
315 ly_interval2scm (Drul_array<Real> i)
317 return scm_cons (scm_make_real (i[LEFT]), scm_make_real (i[RIGHT]));
323 return scm_is_bool (s) && ly_scm2bool (s);
326 /* Appendable list L: the cdr contains the list, the car the last cons
331 SCM s = scm_cons (SCM_EOL, SCM_EOL);
332 scm_set_car_x (s, s);
338 appendable_list_append (SCM l, SCM elt)
340 SCM newcons = scm_cons (elt, SCM_EOL);
342 scm_set_cdr_x (scm_car (l), newcons);
343 scm_set_car_x (l, newcons);
347 ly_offset2scm (Offset o)
349 return scm_cons (scm_make_real (o[X_AXIS]), scm_make_real (o[Y_AXIS]));
353 ly_scm2offset (SCM s)
355 return Offset (scm_to_double (scm_car (s)),
356 scm_to_double (scm_cdr (s)));
359 LY_DEFINE (ly_number2string, "ly:number->string",
361 "Convert @var{num} to a string without generating many decimals.")
363 SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
365 char str[400]; // ugh.
367 if (scm_exact_p (s) == SCM_BOOL_F)
369 Real r (scm_to_double (s));
371 if (my_isinf (r) || my_isnan (r))
373 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
377 sprintf (str, "%08.4f", r);
380 sprintf (str, "%d", scm_to_int (s));
382 return scm_makfrom0str (str);
387 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
388 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
390 char const* vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
392 return scm_c_eval_string ((char*)vs);
395 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
396 "Return the unit used for lengths as a string.")
398 return scm_makfrom0str (INTERNAL_UNIT);
403 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
404 "Return @var{d} is a number. Used to distinguish length "
405 "variables from normal numbers.")
407 return scm_number_p (d);
411 ly_deep_copy (SCM src)
413 if (scm_is_pair (src))
414 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
415 else if (ly_c_vector_p (src))
417 int len = SCM_VECTOR_LENGTH (src);
418 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
419 for (int i =0 ; i < len ; i++)
421 SCM si = scm_int2num (i);
422 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
432 ly_assoc_chain (SCM key, SCM achain)
434 if (scm_is_pair (achain))
436 SCM handle = scm_assoc (key, scm_car (achain));
437 if (scm_is_pair (handle))
440 return ly_assoc_chain (key, scm_cdr (achain));
446 /* looks the key up in the cdrs of the alist-keys
447 - ignoring the car and ignoring non-pair keys.
448 Returns first match found, i.e.
456 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
457 element with the cdr of the key = 1. In other words (alloc_cdr key)
460 (alloc (anything . key))
466 ly_assoc_cdr (SCM key, SCM alist)
468 if (scm_is_pair (alist))
470 SCM trykey = scm_caar (alist);
471 if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
472 return scm_car (alist);
474 return ly_assoc_cdr (key, scm_cdr (alist));
479 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
480 i.e. \n and ' ' can be used interchangeably as separators. */
482 parse_symbol_list (char const *lst)
484 char *s = strdup (lst);
486 SCM create_list = SCM_EOL;
488 char * e = s + strlen (s) - 1;
489 while (e >= s && isspace (*e))
492 for (char * p = s; *p; p++)
501 char *next = strchr (s, ' ');
505 create_list = scm_cons (ly_symbol2scm (s), create_list);
514 ly_truncate_list (int k, SCM lst)
522 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
526 scm_set_cdr_x (s, SCM_EOL);
532 print_scm_val (SCM val)
534 String realval = ly_scm2string (ly_write2scm (val));
535 if (realval.length () > 200)
536 realval = realval.left_string (100)
538 + realval.right_string (100);
543 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
551 TODO: should remove #f from allowed vals?
553 if (val == SCM_EOL || val == SCM_BOOL_F)
556 if (!scm_is_symbol (sym))
561 This is used for autoBeamSettings.
563 TODO: deprecate the use of \override and \revert for
566 or use a symbol autoBeamSettingS?
571 SCM type = scm_object_property (sym, type_symbol);
573 if (type != SCM_EOL && !ly_c_procedure_p (type))
575 warning (_f ("Can't find property type-check for `%s' (%s).",
576 ly_symbol2string (sym).to_str0 (),
577 ly_symbol2string (type_symbol).to_str0 ())
578 + " " + _ ("Perhaps you made a typing error?"));
580 /* Be strict when being anal :) */
581 if (internal_type_checking_global_b)
584 warning (_ ("Doing assignment anyway."));
589 && ly_c_procedure_p (type)
590 && scm_call_1 (type, val) == SCM_BOOL_F)
592 SCM errport = scm_current_error_port ();
594 SCM typefunc = ly_scheme_function ("type-name");
595 SCM type_name = scm_call_1 (typefunc, type);
598 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
599 ly_symbol2string (sym).to_str0 (),
601 ly_scm2string (type_name).to_str0 ()).to_str0 (),
603 scm_puts ("\n", errport);
612 zijn deze nou handig?
613 zijn ze er al in scheme, maar heten ze anders? */
616 /* Remove doubles from (sorted) list */
620 SCM unique = SCM_EOL;
621 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
623 if (!scm_is_pair (scm_cdr (i))
624 || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
625 unique = scm_cons (scm_car (i), unique);
627 return scm_reverse_x (unique, SCM_EOL);
632 scm_default_compare (void const *a, void const *b)
638 return pa < pb ? -1 : 1;
641 /* Modify LST in place: qsort it. */
643 ly_list_qsort_uniq_x (SCM lst)
645 int len = scm_ilength (lst);
646 SCM *arr = new SCM[len];
648 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
649 arr[k++] = scm_car (s);
652 qsort (arr, len, sizeof (SCM), &scm_default_compare);
655 for (int i = 0; i < len; i++)
656 if (!i || arr[i] != arr[i - 1])
658 SCM_SETCAR (*tail, arr[i]);
659 tail = SCM_CDRLOC (*tail);
671 ly_snoc (SCM s, SCM list)
673 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
676 /* Split list at member s, removing s.
677 Return (BEFORE . AFTER) */
679 ly_split_list (SCM s, SCM list)
681 SCM before = SCM_EOL;
683 for (; scm_is_pair (after);)
685 SCM i = scm_car (after);
686 after = scm_cdr (after);
687 if (ly_c_equal_p (i, s))
689 before = scm_cons (i, before);
691 return scm_cons ( scm_reverse_x (before, SCM_EOL), after);
705 display stuff without using stack
710 SCM p = scm_current_output_port ();
713 for (; scm_is_pair (s); s =scm_cdr (s))
715 scm_display (scm_car (s), p);
719 return SCM_UNSPECIFIED;
723 int_list_to_slice (SCM l)
727 for (; scm_is_pair (l); l = scm_cdr (l))
728 if (scm_is_number (scm_car (l)))
729 s.add_point (scm_to_int (scm_car (l)));
733 /* Return I-th element, or last elt L. If I < 0, then we take the first
736 PRE: length (L) > 0 */
738 robust_list_ref (int i, SCM l)
740 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
746 robust_scm2double (SCM k, double x)
748 if (scm_is_number (k))
749 x = scm_to_double (k);
754 robust_scm2interval (SCM k, Drul_array<Real> v)
759 if (is_number_pair (k))
760 i = ly_scm2interval (k);
765 robust_scm2drul (SCM k, Drul_array<Real> v)
767 if (is_number_pair (k))
768 v = ly_scm2interval (k);
773 robust_scm2offset (SCM k, Offset o)
775 if (is_number_pair (k))
776 o = ly_scm2offset (k);
781 robust_scm2int (SCM k, int o)
783 if (scm_integer_p (k) == SCM_BOOL_T)
789 alist_to_hashq (SCM alist)
791 int i = scm_ilength (alist);
793 return scm_c_make_hash_table (0);
795 SCM tab = scm_c_make_hash_table (i);
796 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
798 SCM pt = scm_cdar (s);
799 scm_hashq_set_x (tab, scm_caar (s), pt);
808 LY_DEFINE (ly_protects, "ly:protects",
810 "Return hash of protected objects.")
817 #if HAVE_PANGO_FC_FONT_MAP_ADD_DECODER_FIND_FUNC
819 #include "pangofc-afm-decoder.hh"
821 LY_DEFINE (ly_pango_add_afm_decoder, "ly:pango-add-afm-decoder",
822 1, 0, 0, (SCM font_family),
823 "Add pango afm decoder for FONT-FAMILY.")
825 SCM_ASSERT_TYPE (scm_is_string (font_family), font_family, SCM_ARG1,
826 __FUNCTION__, "font_family");
827 pango_fc_afm_add_decoder (ly_scm2newstr (font_family, 0));
828 return SCM_UNSPECIFIED;
833 LY_DEFINE (ly_gettext, "ly:gettext",
834 1, 0, 0, (SCM string),
837 SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
838 __FUNCTION__, "string");
839 return scm_makfrom0str (gettext (scm_i_string_chars (string)));