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 ly_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) ? ly_car (s) : ly_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 (ly_c_number_p (s))
206 int i = ly_scm2int (s);
207 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
213 is_number_pair (SCM p)
215 return ly_c_pair_p (p)
216 && ly_c_number_p (ly_car (p)) && ly_c_number_p (ly_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 (ly_c_number_p (s))
262 int i = ly_scm2int (s);
263 return i>= -1 && i <= 1;
271 if (ly_c_number_p (s))
273 int i = ly_scm2int (s);
274 return i== 0 || i == 1;
282 return scm_is_integer (s) ? (Direction) ly_scm2int (s) : CENTER;
286 ly_scm2interval (SCM p)
288 return Interval (ly_scm2double (ly_car (p)), ly_scm2double (ly_cdr (p)));
292 ly_scm2realdrul (SCM p)
294 return Drul_array<Real> (ly_scm2double (ly_car (p)),
295 ly_scm2double (ly_cdr (p)));
299 ly_interval2scm (Drul_array<Real> i)
301 return scm_cons (scm_make_real (i[LEFT]), scm_make_real (i[RIGHT]));
307 return ly_c_boolean_p (s) && ly_scm2bool (s);
310 /* Appendable list L: the cdr contains the list, the car the last cons
315 SCM s = scm_cons (SCM_EOL, SCM_EOL);
316 scm_set_car_x (s, s);
322 appendable_list_append (SCM l, SCM elt)
324 SCM newcons = scm_cons (elt, SCM_EOL);
326 scm_set_cdr_x (ly_car (l), newcons);
327 scm_set_car_x (l, newcons);
331 ly_offset2scm (Offset o)
333 return scm_cons (scm_make_real (o[X_AXIS]), scm_make_real (o[Y_AXIS]));
337 ly_scm2offset (SCM s)
339 return Offset (ly_scm2double (ly_car (s)),
340 ly_scm2double (ly_cdr (s)));
343 LY_DEFINE (ly_number2string, "ly:number->string",
345 "Convert @var{num} to a string without generating many decimals.")
347 SCM_ASSERT_TYPE (ly_c_number_p (s), s, SCM_ARG1, __FUNCTION__, "number");
349 char str[400]; // ugh.
351 if (scm_exact_p (s) == SCM_BOOL_F)
353 Real r (ly_scm2double (s));
355 if (my_isinf (r) || my_isnan (r))
357 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
361 sprintf (str, "%08.4f", r);
364 sprintf (str, "%d", ly_scm2int (s));
366 return scm_makfrom0str (str);
371 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
372 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
374 char const* vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
376 return scm_c_eval_string ((char*)vs);
379 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
380 "Return the unit used for lengths as a string.")
382 return scm_makfrom0str (INTERNAL_UNIT);
387 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
388 "Return @var{d} is a number. Used to distinguish length "
389 "variables from normal numbers.")
391 return scm_number_p (d);
395 ly_deep_copy (SCM src)
397 if (ly_c_pair_p (src))
398 return scm_cons (ly_deep_copy (ly_car (src)), ly_deep_copy (ly_cdr (src)));
399 else if (ly_c_vector_p (src))
401 int len = SCM_VECTOR_LENGTH (src);
402 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
403 for (int i =0 ; i < len ; i++)
405 SCM si = scm_int2num (i);
406 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
416 ly_assoc_chain (SCM key, SCM achain)
418 if (ly_c_pair_p (achain))
420 SCM handle = scm_assoc (key, ly_car (achain));
421 if (ly_c_pair_p (handle))
424 return ly_assoc_chain (key, ly_cdr (achain));
430 /* looks the key up in the cdrs of the alist-keys
431 - ignoring the car and ignoring non-pair keys.
432 Returns first match found, i.e.
440 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
441 element with the cdr of the key = 1. In other words (alloc_cdr key)
444 (alloc (anything . key))
450 ly_assoc_cdr (SCM key, SCM alist)
452 if (ly_c_pair_p (alist))
454 SCM trykey = ly_caar (alist);
455 if (ly_c_pair_p (trykey) && to_boolean (scm_equal_p (key, ly_cdr (trykey))))
456 return ly_car (alist);
458 return ly_assoc_cdr (key, ly_cdr (alist));
463 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
464 i.e. \n and ' ' can be used interchangeably as separators. */
466 parse_symbol_list (char const *lst)
468 char *s = strdup (lst);
470 SCM create_list = SCM_EOL;
472 char * e = s + strlen (s) - 1;
473 while (e >= s && isspace (*e))
476 for (char * p = s; *p; p++)
485 char *next = strchr (s, ' ');
489 create_list = scm_cons (ly_symbol2scm (s), create_list);
498 ly_truncate_list (int k, SCM lst)
506 for (; ly_c_pair_p (s) && k--; s = ly_cdr (s))
510 scm_set_cdr_x (s, SCM_EOL);
516 print_scm_val (SCM val)
518 String realval = ly_scm2string (ly_write2scm (val));
519 if (realval.length () > 200)
520 realval = realval.left_string (100)
522 + realval.right_string (100);
527 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
535 TODO: should remove #f from allowed vals?
537 if (val == SCM_EOL || val == SCM_BOOL_F)
540 if (!ly_c_symbol_p (sym))
545 This is used for autoBeamSettings.
547 TODO: deprecate the use of \override and \revert for
550 or use a symbol autoBeamSettingS?
555 SCM type = scm_object_property (sym, type_symbol);
557 if (type != SCM_EOL && !ly_c_procedure_p (type))
559 warning (_f ("Can't find property type-check for `%s' (%s).",
560 ly_symbol2string (sym).to_str0 (),
561 ly_symbol2string (type_symbol).to_str0 ())
562 + " " + _ ("Perhaps you made a typing error?"));
564 /* Be strict when being anal :) */
565 if (internal_type_checking_global_b)
568 warning (_ ("Doing assignment anyway."));
573 && ly_c_procedure_p (type)
574 && scm_call_1 (type, val) == SCM_BOOL_F)
576 SCM errport = scm_current_error_port ();
578 SCM typefunc = ly_scheme_function ("type-name");
579 SCM type_name = scm_call_1 (typefunc, type);
582 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
583 ly_symbol2string (sym).to_str0 (),
585 ly_scm2string (type_name).to_str0 ()).to_str0 (),
587 scm_puts ("\n", errport);
596 zijn deze nou handig?
597 zijn ze er al in scheme, maar heten ze anders? */
600 /* Remove doubles from (sorted) list */
604 SCM unique = SCM_EOL;
605 for (SCM i = list; ly_c_pair_p (i); i = ly_cdr (i))
607 if (!ly_c_pair_p (ly_cdr (i))
608 || !ly_c_equal_p (ly_car (i), ly_cadr (i)))
609 unique = scm_cons (ly_car (i), unique);
611 return scm_reverse_x (unique, SCM_EOL);
616 scm_default_compare (void const *a, void const *b)
622 return pa < pb ? -1 : 1;
625 /* Modify LST in place: qsort it. */
627 ly_list_qsort_uniq_x (SCM lst)
629 int len = scm_ilength (lst);
630 SCM *arr = new SCM[len];
632 for (SCM s = lst; SCM_NNULLP (s); s = SCM_CDR (s))
633 arr[k++] = SCM_CAR (s);
636 qsort (arr, len, sizeof (SCM), &scm_default_compare);
639 for (int i = 0; i < len; i++)
640 if (!i || arr[i] != arr[i - 1])
642 SCM_SETCAR (*tail, arr[i]);
643 tail = SCM_CDRLOC (*tail);
655 ly_snoc (SCM s, SCM list)
657 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
660 /* Split list at member s, removing s.
661 Return (BEFORE . AFTER) */
663 ly_split_list (SCM s, SCM list)
665 SCM before = SCM_EOL;
667 for (; ly_c_pair_p (after);)
669 SCM i = ly_car (after);
670 after = ly_cdr (after);
671 if (ly_c_equal_p (i, s))
673 before = scm_cons (i, before);
675 return scm_cons ( scm_reverse_x (before, SCM_EOL), after);
689 display stuff without using stack
694 SCM p = scm_current_output_port ();
697 for (; ly_c_pair_p (s); s =ly_cdr (s))
699 scm_display (ly_car (s), p);
703 return SCM_UNSPECIFIED;
707 int_list_to_slice (SCM l)
711 for (; ly_c_pair_p (l); l = ly_cdr (l))
712 if (ly_c_number_p (ly_car (l)))
713 s.add_point (ly_scm2int (ly_car (l)));
717 /* Return I-th element, or last elt L. If I < 0, then we take the first
720 PRE: length (L) > 0 */
722 robust_list_ref (int i, SCM l)
724 while (i-- > 0 && ly_c_pair_p (ly_cdr (l)))
730 robust_scm2double (SCM k, double x)
732 if (ly_c_number_p (k))
733 x = ly_scm2double (k);
738 robust_scm2interval (SCM k, Drul_array<Real> v)
743 if (is_number_pair (k))
744 i = ly_scm2interval (k);
749 robust_scm2drul (SCM k, Drul_array<Real> v)
751 if (is_number_pair (k))
752 v = ly_scm2interval (k);
757 robust_scm2offset (SCM k, Offset o)
759 if (is_number_pair (k))
760 o = ly_scm2offset (k);
765 robust_scm2int (SCM k, int o)
767 if (scm_integer_p (k) == SCM_BOOL_T)
773 alist_to_hashq (SCM alist)
775 int i = scm_ilength (alist);
777 return scm_c_make_hash_table (0);
779 SCM tab = scm_c_make_hash_table (i);
780 for (SCM s = alist; ly_c_pair_p (s); s = ly_cdr (s))
782 SCM pt = ly_cdar (s);
783 scm_hashq_set_x (tab, ly_caar (s), pt);
792 LY_DEFINE (ly_protects, "ly:protects",
794 "Return hash of protected objects.")
801 #if HAVE_PANGO_FC_FONT_MAP_ADD_DECODER_FIND_FUNC
803 #include "pangofc-afm-decoder.hh"
805 LY_DEFINE (ly_pango_add_afm_decoder, "ly:pango-add-afm-decoder",
806 1, 0, 0, (SCM font_family),
807 "Add pango afm decoder for FONT-FAMILY.")
809 SCM_ASSERT_TYPE (scm_is_string (font_family), font_family, SCM_ARG1,
810 __FUNCTION__, "font_family");
811 pango_fc_afm_add_decoder (ly_scm2newstr (font_family, 0));
812 return SCM_UNSPECIFIED;
817 LY_DEFINE (ly_gettext, "ly:gettext",
818 1, 0, 0, (SCM string),
821 SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
822 __FUNCTION__, "string");
823 return scm_makfrom0str (gettext (scm_i_string_chars (string)));