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>
10 #include "lily-guile.hh"
14 #include <math.h> /* isinf */
15 #include <cstring> /* strdup, strchr */
18 #include <libintl.h> // gettext on macos x
23 source-file.hh includes cmath which undefines isinf and isnan
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"
32 #include "file-path.hh"
34 #include "direction.hh"
37 #include "dimensions.hh"
38 #include "source-file.hh"
43 ly_to_symbol (SCM scm)
45 return scm_string_to_symbol (ly_to_string (scm));
49 ly_to_string (SCM scm)
51 return scm_call_3 (ly_scheme_function ("format"), SCM_BOOL_F,
52 scm_makfrom0str ("~S"), scm);
58 return scm_car (scm_last_pair (list));
64 SCM port = scm_mkstrport (SCM_INUM0,
65 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
68 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
69 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
71 // scm_apply (write, port, SCM_EOL);
72 scm_call_2 (write, s, port);
73 return scm_strport_to_string (port);
79 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
83 ly_symbol2string (SCM s)
86 Ugh. this is not very efficient.
88 SCM str = scm_symbol_to_string (s);
89 return ly_scm2string (str);
93 gulp_file_to_string (String fn, bool must_exist)
95 String s = global_path.find (fn);
100 String e = _f ("can't find file: `%s'", fn);
102 e += _f ("(load path: `%s')", global_path.to_string ());
109 if (verbose_global_b)
110 progress_indication ("[" + s);
113 char *str = gulp_file (s, &n);
117 if (verbose_global_b)
118 progress_indication ("]");
123 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
125 "Read the file @var{name}, and return its contents in a string. "
126 "The file is looked up using the search path.")
128 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
129 return scm_makfrom0str (gulp_file_to_string (ly_scm2string (name),
136 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
138 ly_display_scm (SCM s)
140 scm_display (s, scm_current_output_port ());
141 scm_newline (scm_current_output_port ());
146 ly_scm2string (SCM str)
148 assert (scm_is_string (str));
149 return String ((Byte*)scm_i_string_chars (str),
150 (int) scm_i_string_length (str));
154 ly_scm2newstr (SCM str, size_t *lenp)
156 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
158 size_t len = SCM_STRING_LENGTH (str);
159 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
161 memcpy (new_str, scm_i_string_chars (str), len);
173 index_get_cell (SCM s, Direction d)
177 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
181 index_set_cell (SCM s, Direction d, SCM v)
184 scm_set_car_x (s, v);
186 scm_set_cdr_x (s, v);
190 LY_DEFINE (ly_warn, "ly:warn",
191 1, 0, 1, (SCM str, SCM rest),
192 "Scheme callable function to issue the warning @code{msg}. "
193 "The message is formatted with @code{format} and @code{rest}.")
195 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
196 progress_indication ("\n");
198 str = scm_simple_format (SCM_BOOL_F, str, rest);
199 warning ("lily-guile: " + ly_scm2string (str));
200 return SCM_UNSPECIFIED;
203 LY_DEFINE (ly_programming_error, "ly:programming-error",
204 1, 0, 1, (SCM str, SCM rest),
205 "Scheme callable function to issue the warning @code{msg}. "
206 "The message is formatted with @code{format} and @code{rest}.")
208 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
209 progress_indication ("\n");
211 str = scm_simple_format (SCM_BOOL_F, str, rest);
212 programming_error (ly_scm2string (str));
213 return SCM_UNSPECIFIED;
216 LY_DEFINE (ly_dir_p, "ly:dir?",
218 "type predicate. A direction is @code{-1}, @code{0} or "
219 "@code{1}, where @code{-1} represents "
220 "left or down and @code{1} represents right or up.")
222 if (scm_is_number (s))
224 int i = scm_to_int (s);
225 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
231 is_number_pair (SCM p)
233 return scm_is_pair (p)
234 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
237 typedef void (*Void_fptr) ();
238 Array<Void_fptr> *scm_init_funcs_;
240 void add_scm_init_func (void (*f) ())
242 if (!scm_init_funcs_)
243 scm_init_funcs_ = new Array<Void_fptr>;
245 scm_init_funcs_->push (f);
249 ly_init_ly_module (void *)
251 for (int i = scm_init_funcs_->size () ; i--;)
252 (scm_init_funcs_->elem (i)) ();
254 if (verbose_global_b)
255 progress_indication ("\n");
257 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
260 SCM global_lily_module;
265 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
266 scm_c_use_module ("lily");
272 return scm_ihashv (s, ~1u);
278 if (scm_is_number (s))
280 int i = scm_to_int (s);
281 return i>= -1 && i <= 1;
286 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
288 (SCM key, SCM alist, SCM default_value),
289 "Return value if KEY in ALIST, else DEFAULT-VALUE "
290 "(or #f if not specified).")
292 SCM handle = scm_assoc (key, alist);
294 if (default_value == SCM_UNDEFINED)
295 default_value = SCM_BOOL_F;
297 if (scm_is_pair (handle))
298 return scm_cdr (handle);
300 return default_value;
306 if (scm_is_number (s))
308 int i = scm_to_int (s);
309 return i == 0 || i == 1;
317 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
321 ly_scm2interval (SCM p)
323 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
327 ly_scm2realdrul (SCM p)
329 return Drul_array<Real> (scm_to_double (scm_car (p)),
330 scm_to_double (scm_cdr (p)));
334 ly_interval2scm (Drul_array<Real> i)
336 return scm_cons (scm_make_real (i[LEFT]), scm_make_real (i[RIGHT]));
342 return scm_is_bool (s) && ly_scm2bool (s);
345 /* Appendable list L: the cdr contains the list, the car the last cons
350 SCM s = scm_cons (SCM_EOL, SCM_EOL);
351 scm_set_car_x (s, s);
357 appendable_list_append (SCM l, SCM elt)
359 SCM newcons = scm_cons (elt, SCM_EOL);
361 scm_set_cdr_x (scm_car (l), newcons);
362 scm_set_car_x (l, newcons);
366 ly_offset2scm (Offset o)
368 return scm_cons (scm_make_real (o[X_AXIS]), scm_make_real (o[Y_AXIS]));
372 ly_scm2offset (SCM s)
374 return Offset (scm_to_double (scm_car (s)),
375 scm_to_double (scm_cdr (s)));
378 LY_DEFINE (ly_number2string, "ly:number->string",
380 "Convert @var{num} to a string without generating many decimals.")
382 SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
384 char str[400]; // ugh.
386 if (scm_exact_p (s) == SCM_BOOL_F)
388 Real r (scm_to_double (s));
390 if (my_isinf (r) || my_isnan (r))
392 if (isinf (r) || isnan (r))
395 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
399 sprintf (str, "%08.4f", r);
402 sprintf (str, "%d", scm_to_int (s));
404 return scm_makfrom0str (str);
409 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
410 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
412 char const* vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
414 return scm_c_eval_string ((char*)vs);
417 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
418 "Return the unit used for lengths as a string.")
420 return scm_makfrom0str (INTERNAL_UNIT);
425 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
426 "Return @var{d} is a number. Used to distinguish length "
427 "variables from normal numbers.")
429 return scm_number_p (d);
433 ly_deep_copy (SCM src)
435 if (scm_is_pair (src))
436 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
437 else if (ly_c_vector_p (src))
439 int len = SCM_VECTOR_LENGTH (src);
440 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
441 for (int i = 0 ;i < len ; i++)
443 SCM si = scm_int2num (i);
444 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
454 ly_assoc_chain (SCM key, SCM achain)
456 if (scm_is_pair (achain))
458 SCM handle = scm_assoc (key, scm_car (achain));
459 if (scm_is_pair (handle))
462 return ly_assoc_chain (key, scm_cdr (achain));
468 /* looks the key up in the cdrs of the alist-keys
469 - ignoring the car and ignoring non-pair keys.
470 Returns first match found, i.e.
478 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
479 element with the cdr of the key = 1. In other words (alloc_cdr key)
482 (alloc (anything . key))
488 ly_assoc_cdr (SCM key, SCM alist)
490 if (scm_is_pair (alist))
492 SCM trykey = scm_caar (alist);
493 if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
494 return scm_car (alist);
496 return ly_assoc_cdr (key, scm_cdr (alist));
501 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
502 i.e. \n and ' ' can be used interchangeably as separators. */
504 parse_symbol_list (char const *lst)
506 char *s = strdup (lst);
508 SCM create_list = SCM_EOL;
510 char * e = s + strlen (s) - 1;
511 while (e >= s && isspace (*e))
514 for (char * p = s; *p; p++)
523 char *next = strchr (s, ' ');
527 create_list = scm_cons (ly_symbol2scm (s), create_list);
536 ly_truncate_list (int k, SCM lst)
544 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
548 scm_set_cdr_x (s, SCM_EOL);
554 print_scm_val (SCM val)
556 String realval = ly_scm2string (ly_write2scm (val));
557 if (realval.length () > 200)
558 realval = realval.left_string (100)
560 + realval.right_string (100);
565 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
573 TODO: should remove #f from allowed vals?
575 if (val == SCM_EOL || val == SCM_BOOL_F)
578 if (!scm_is_symbol (sym))
583 This is used for autoBeamSettings.
585 TODO: deprecate the use of \override and \revert for
588 or use a symbol autoBeamSettingS?
593 SCM type = scm_object_property (sym, type_symbol);
595 if (type != SCM_EOL && !ly_c_procedure_p (type))
597 warning (_f ("Can't find property type-check for `%s' (%s).",
598 ly_symbol2string (sym).to_str0 (),
599 ly_symbol2string (type_symbol).to_str0 ())
600 + " " + _ ("Perhaps you made a typing error?"));
602 /* Be strict when being anal :) */
603 if (internal_type_checking_global_b)
606 warning (_ ("Doing assignment anyway."));
611 && ly_c_procedure_p (type)
612 && scm_call_1 (type, val) == SCM_BOOL_F)
614 SCM errport = scm_current_error_port ();
616 SCM typefunc = ly_scheme_function ("type-name");
617 SCM type_name = scm_call_1 (typefunc, type);
620 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
621 ly_symbol2string (sym).to_str0 (),
623 ly_scm2string (type_name).to_str0 ()).to_str0 (),
625 scm_puts ("\n", errport);
634 zijn deze nou handig?
635 zijn ze er al in scheme, maar heten ze anders? */
638 /* Remove doubles from (sorted) list */
642 SCM unique = SCM_EOL;
643 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
645 if (!scm_is_pair (scm_cdr (i))
646 || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
647 unique = scm_cons (scm_car (i), unique);
649 return scm_reverse_x (unique, SCM_EOL);
654 scm_default_compare (void const *a, void const *b)
660 return pa < pb ? -1 : 1;
663 /* Modify LST in place: qsort it. */
665 ly_list_qsort_uniq_x (SCM lst)
667 int len = scm_ilength (lst);
668 SCM *arr = new SCM[len];
670 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
671 arr[k++] = scm_car (s);
674 qsort (arr, len, sizeof (SCM), &scm_default_compare);
677 for (int i = 0; i < len; i++)
678 if (!i || arr[i] != arr[i - 1])
680 SCM_SETCAR (*tail, arr[i]);
681 tail = SCM_CDRLOC (*tail);
693 ly_snoc (SCM s, SCM list)
695 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
698 /* Split list at member s, removing s.
699 Return (BEFORE . AFTER) */
701 ly_split_list (SCM s, SCM list)
703 SCM before = SCM_EOL;
705 for (; scm_is_pair (after);)
707 SCM i = scm_car (after);
708 after = scm_cdr (after);
709 if (ly_c_equal_p (i, s))
711 before = scm_cons (i, before);
713 return scm_cons ( scm_reverse_x (before, SCM_EOL), after);
727 display stuff without using stack
732 SCM p = scm_current_output_port ();
735 for (; scm_is_pair (s); s = scm_cdr (s))
737 scm_display (scm_car (s), p);
741 return SCM_UNSPECIFIED;
745 int_list_to_slice (SCM l)
749 for (; scm_is_pair (l); l = scm_cdr (l))
750 if (scm_is_number (scm_car (l)))
751 s.add_point (scm_to_int (scm_car (l)));
755 /* Return I-th element, or last elt L. If I < 0, then we take the first
758 PRE: length (L) > 0 */
760 robust_list_ref (int i, SCM l)
762 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
768 robust_scm2double (SCM k, double x)
770 if (scm_is_number (k))
771 x = scm_to_double (k);
776 robust_scm2interval (SCM k, Drul_array<Real> v)
781 if (is_number_pair (k))
782 i = ly_scm2interval (k);
787 robust_scm2drul (SCM k, Drul_array<Real> v)
789 if (is_number_pair (k))
790 v = ly_scm2interval (k);
795 robust_scm2offset (SCM k, Offset o)
797 if (is_number_pair (k))
798 o = ly_scm2offset (k);
803 robust_scm2int (SCM k, int o)
805 if (scm_integer_p (k) == SCM_BOOL_T)
811 alist_to_hashq (SCM alist)
813 int i = scm_ilength (alist);
815 return scm_c_make_hash_table (0);
817 SCM tab = scm_c_make_hash_table (i);
818 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
820 SCM pt = scm_cdar (s);
821 scm_hashq_set_x (tab, scm_caar (s), pt);
830 LY_DEFINE (ly_protects, "ly:protects",
832 "Return hash of protected objects.")
839 #if HAVE_PANGO_FC_FONT_MAP_ADD_DECODER_FIND_FUNC
841 #include "pangofc-afm-decoder.hh"
843 LY_DEFINE (ly_pango_add_afm_decoder, "ly:pango-add-afm-decoder",
844 1, 0, 0, (SCM font_family),
845 "Add pango afm decoder for FONT-FAMILY.")
847 SCM_ASSERT_TYPE (scm_is_string (font_family), font_family, SCM_ARG1,
848 __FUNCTION__, "font_family");
849 pango_fc_afm_add_decoder (ly_scm2newstr (font_family, 0));
850 return SCM_UNSPECIFIED;
855 LY_DEFINE (ly_gettext, "ly:gettext",
856 1, 0, 0, (SCM string),
859 SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
860 __FUNCTION__, "string");
861 return scm_makfrom0str (gettext (scm_i_string_chars (string)));