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 String contents = gulp_file_to_string (ly_scm2string (name), true);
132 SCM scm_str = scm_i_make_string (contents.length(), &data);
133 memcpy (data, contents.get_bytes(), contents.length());
140 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
142 ly_display_scm (SCM s)
144 scm_display (s, scm_current_output_port ());
145 scm_newline (scm_current_output_port ());
150 ly_scm2string (SCM str)
152 assert (scm_is_string (str));
153 return String ((Byte*)scm_i_string_chars (str),
154 (int) scm_i_string_length (str));
158 ly_scm2newstr (SCM str, size_t *lenp)
160 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
162 size_t len = SCM_STRING_LENGTH (str);
163 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
165 memcpy (new_str, scm_i_string_chars (str), len);
177 index_get_cell (SCM s, Direction d)
181 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
185 index_set_cell (SCM s, Direction d, SCM v)
188 scm_set_car_x (s, v);
190 scm_set_cdr_x (s, v);
194 LY_DEFINE (ly_warn, "ly:warn",
195 1, 0, 1, (SCM str, SCM rest),
196 "Scheme callable function to issue the warning @code{msg}. "
197 "The message is formatted with @code{format} and @code{rest}.")
199 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
200 progress_indication ("\n");
202 str = scm_simple_format (SCM_BOOL_F, str, rest);
203 warning ("lily-guile: " + ly_scm2string (str));
204 return SCM_UNSPECIFIED;
207 LY_DEFINE (ly_programming_error, "ly:programming-error",
208 1, 0, 1, (SCM str, SCM rest),
209 "Scheme callable function to issue the warning @code{msg}. "
210 "The message is formatted with @code{format} and @code{rest}.")
212 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
213 progress_indication ("\n");
215 str = scm_simple_format (SCM_BOOL_F, str, rest);
216 programming_error (ly_scm2string (str));
217 return SCM_UNSPECIFIED;
220 LY_DEFINE (ly_dir_p, "ly:dir?",
222 "type predicate. A direction is @code{-1}, @code{0} or "
223 "@code{1}, where @code{-1} represents "
224 "left or down and @code{1} represents right or up.")
226 if (scm_is_number (s))
228 int i = scm_to_int (s);
229 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
235 is_number_pair (SCM p)
237 return scm_is_pair (p)
238 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
241 typedef void (*Void_fptr) ();
242 Array<Void_fptr> *scm_init_funcs_;
244 void add_scm_init_func (void (*f) ())
246 if (!scm_init_funcs_)
247 scm_init_funcs_ = new Array<Void_fptr>;
249 scm_init_funcs_->push (f);
253 ly_init_ly_module (void *)
255 for (int i = scm_init_funcs_->size () ; i--;)
256 (scm_init_funcs_->elem (i)) ();
258 if (verbose_global_b)
259 progress_indication ("\n");
261 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
264 SCM global_lily_module;
269 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
270 scm_c_use_module ("lily");
276 return scm_ihashv (s, ~1u);
282 if (scm_is_number (s))
284 int i = scm_to_int (s);
285 return i>= -1 && i <= 1;
290 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
292 (SCM key, SCM alist, SCM default_value),
293 "Return value if KEY in ALIST, else DEFAULT-VALUE "
294 "(or #f if not specified).")
296 SCM handle = scm_assoc (key, alist);
298 if (default_value == SCM_UNDEFINED)
299 default_value = SCM_BOOL_F;
301 if (scm_is_pair (handle))
302 return scm_cdr (handle);
304 return default_value;
310 if (scm_is_number (s))
312 int i = scm_to_int (s);
313 return i == 0 || i == 1;
321 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
325 ly_scm2interval (SCM p)
327 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
331 ly_scm2realdrul (SCM p)
333 return Drul_array<Real> (scm_to_double (scm_car (p)),
334 scm_to_double (scm_cdr (p)));
338 ly_interval2scm (Drul_array<Real> i)
340 return scm_cons (scm_make_real (i[LEFT]), scm_make_real (i[RIGHT]));
346 return scm_is_bool (s) && ly_scm2bool (s);
349 /* Appendable list L: the cdr contains the list, the car the last cons
354 SCM s = scm_cons (SCM_EOL, SCM_EOL);
355 scm_set_car_x (s, s);
361 appendable_list_append (SCM l, SCM elt)
363 SCM newcons = scm_cons (elt, SCM_EOL);
365 scm_set_cdr_x (scm_car (l), newcons);
366 scm_set_car_x (l, newcons);
370 ly_offset2scm (Offset o)
372 return scm_cons (scm_make_real (o[X_AXIS]), scm_make_real (o[Y_AXIS]));
376 ly_scm2offset (SCM s)
378 return Offset (scm_to_double (scm_car (s)),
379 scm_to_double (scm_cdr (s)));
382 LY_DEFINE (ly_number2string, "ly:number->string",
384 "Convert @var{num} to a string without generating many decimals.")
386 SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
388 char str[400]; // ugh.
390 if (scm_exact_p (s) == SCM_BOOL_F)
392 Real r (scm_to_double (s));
394 if (my_isinf (r) || my_isnan (r))
396 if (isinf (r) || isnan (r))
399 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
403 sprintf (str, "%08.4f", r);
406 sprintf (str, "%d", scm_to_int (s));
408 return scm_makfrom0str (str);
413 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
414 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
416 char const* vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
418 return scm_c_eval_string ((char*)vs);
421 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
422 "Return the unit used for lengths as a string.")
424 return scm_makfrom0str (INTERNAL_UNIT);
429 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
430 "Return @var{d} is a number. Used to distinguish length "
431 "variables from normal numbers.")
433 return scm_number_p (d);
437 ly_deep_copy (SCM src)
439 if (scm_is_pair (src))
440 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
441 else if (ly_c_vector_p (src))
443 int len = SCM_VECTOR_LENGTH (src);
444 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
445 for (int i = 0 ;i < len ; i++)
447 SCM si = scm_int2num (i);
448 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
458 ly_assoc_chain (SCM key, SCM achain)
460 if (scm_is_pair (achain))
462 SCM handle = scm_assoc (key, scm_car (achain));
463 if (scm_is_pair (handle))
466 return ly_assoc_chain (key, scm_cdr (achain));
472 /* looks the key up in the cdrs of the alist-keys
473 - ignoring the car and ignoring non-pair keys.
474 Returns first match found, i.e.
482 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
483 element with the cdr of the key = 1. In other words (alloc_cdr key)
486 (alloc (anything . key))
492 ly_assoc_cdr (SCM key, SCM alist)
494 if (scm_is_pair (alist))
496 SCM trykey = scm_caar (alist);
497 if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
498 return scm_car (alist);
500 return ly_assoc_cdr (key, scm_cdr (alist));
505 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
506 i.e. \n and ' ' can be used interchangeably as separators. */
508 parse_symbol_list (char const *lst)
510 char *s = strdup (lst);
512 SCM create_list = SCM_EOL;
514 char * e = s + strlen (s) - 1;
515 while (e >= s && isspace (*e))
518 for (char * p = s; *p; p++)
527 char *next = strchr (s, ' ');
531 create_list = scm_cons (ly_symbol2scm (s), create_list);
540 ly_truncate_list (int k, SCM lst)
548 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
552 scm_set_cdr_x (s, SCM_EOL);
558 print_scm_val (SCM val)
560 String realval = ly_scm2string (ly_write2scm (val));
561 if (realval.length () > 200)
562 realval = realval.left_string (100)
564 + realval.right_string (100);
569 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
577 TODO: should remove #f from allowed vals?
579 if (val == SCM_EOL || val == SCM_BOOL_F)
582 if (!scm_is_symbol (sym))
587 This is used for autoBeamSettings.
589 TODO: deprecate the use of \override and \revert for
592 or use a symbol autoBeamSettingS?
597 SCM type = scm_object_property (sym, type_symbol);
599 if (type != SCM_EOL && !ly_c_procedure_p (type))
601 warning (_f ("Can't find property type-check for `%s' (%s).",
602 ly_symbol2string (sym).to_str0 (),
603 ly_symbol2string (type_symbol).to_str0 ())
604 + " " + _ ("Perhaps you made a typing error?"));
606 /* Be strict when being anal :) */
607 if (internal_type_checking_global_b)
610 warning (_ ("Doing assignment anyway."));
615 && ly_c_procedure_p (type)
616 && scm_call_1 (type, val) == SCM_BOOL_F)
618 SCM errport = scm_current_error_port ();
620 SCM typefunc = ly_scheme_function ("type-name");
621 SCM type_name = scm_call_1 (typefunc, type);
624 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
625 ly_symbol2string (sym).to_str0 (),
627 ly_scm2string (type_name).to_str0 ()).to_str0 (),
629 scm_puts ("\n", errport);
638 zijn deze nou handig?
639 zijn ze er al in scheme, maar heten ze anders? */
642 /* Remove doubles from (sorted) list */
646 SCM unique = SCM_EOL;
647 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
649 if (!scm_is_pair (scm_cdr (i))
650 || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
651 unique = scm_cons (scm_car (i), unique);
653 return scm_reverse_x (unique, SCM_EOL);
658 scm_default_compare (void const *a, void const *b)
664 return pa < pb ? -1 : 1;
667 /* Modify LST in place: qsort it. */
669 ly_list_qsort_uniq_x (SCM lst)
671 int len = scm_ilength (lst);
672 SCM *arr = new SCM[len];
674 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
675 arr[k++] = scm_car (s);
678 qsort (arr, len, sizeof (SCM), &scm_default_compare);
681 for (int i = 0; i < len; i++)
682 if (!i || arr[i] != arr[i - 1])
684 SCM_SETCAR (*tail, arr[i]);
685 tail = SCM_CDRLOC (*tail);
697 ly_snoc (SCM s, SCM list)
699 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
702 /* Split list at member s, removing s.
703 Return (BEFORE . AFTER) */
705 ly_split_list (SCM s, SCM list)
707 SCM before = SCM_EOL;
709 for (; scm_is_pair (after);)
711 SCM i = scm_car (after);
712 after = scm_cdr (after);
713 if (ly_c_equal_p (i, s))
715 before = scm_cons (i, before);
717 return scm_cons ( scm_reverse_x (before, SCM_EOL), after);
731 display stuff without using stack
736 SCM p = scm_current_output_port ();
739 for (; scm_is_pair (s); s = scm_cdr (s))
741 scm_display (scm_car (s), p);
745 return SCM_UNSPECIFIED;
749 int_list_to_slice (SCM l)
753 for (; scm_is_pair (l); l = scm_cdr (l))
754 if (scm_is_number (scm_car (l)))
755 s.add_point (scm_to_int (scm_car (l)));
759 /* Return I-th element, or last elt L. If I < 0, then we take the first
762 PRE: length (L) > 0 */
764 robust_list_ref (int i, SCM l)
766 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
772 robust_scm2double (SCM k, double x)
774 if (scm_is_number (k))
775 x = scm_to_double (k);
780 robust_scm2interval (SCM k, Drul_array<Real> v)
785 if (is_number_pair (k))
786 i = ly_scm2interval (k);
791 robust_scm2drul (SCM k, Drul_array<Real> v)
793 if (is_number_pair (k))
794 v = ly_scm2interval (k);
799 robust_scm2offset (SCM k, Offset o)
801 if (is_number_pair (k))
802 o = ly_scm2offset (k);
807 robust_scm2int (SCM k, int o)
809 if (scm_integer_p (k) == SCM_BOOL_T)
815 alist_to_hashq (SCM alist)
817 int i = scm_ilength (alist);
819 return scm_c_make_hash_table (0);
821 SCM tab = scm_c_make_hash_table (i);
822 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
824 SCM pt = scm_cdar (s);
825 scm_hashq_set_x (tab, scm_caar (s), pt);
834 LY_DEFINE (ly_protects, "ly:protects",
836 "Return hash of protected objects.")
843 #if HAVE_PANGO_FC_FONT_MAP_ADD_DECODER_FIND_FUNC
845 #include "pangofc-afm-decoder.hh"
847 LY_DEFINE (ly_pango_add_afm_decoder, "ly:pango-add-afm-decoder",
848 1, 0, 0, (SCM font_family),
849 "Add pango afm decoder for FONT-FAMILY.")
851 SCM_ASSERT_TYPE (scm_is_string (font_family), font_family, SCM_ARG1,
852 __FUNCTION__, "font_family");
853 pango_fc_afm_add_decoder (ly_scm2newstr (font_family, 0));
854 return SCM_UNSPECIFIED;
859 LY_DEFINE (ly_gettext, "ly:gettext",
860 1, 0, 0, (SCM string),
863 SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
864 __FUNCTION__, "string");
865 return scm_makfrom0str (gettext (scm_i_string_chars (string)));