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_lily_module_constant ("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);
114 String result ((Byte*) str, 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);
130 return scm_from_locale_stringn (contents.get_str0 (), contents.length ());
135 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
137 ly_display_scm (SCM s)
139 scm_display (s, scm_current_output_port ());
140 scm_newline (scm_current_output_port ());
145 ly_scm2string (SCM str)
147 assert (scm_is_string (str));
148 return String ((Byte*)scm_i_string_chars (str),
149 (int) scm_i_string_length (str));
153 ly_scm2newstr (SCM str, size_t *lenp)
155 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
157 size_t len = SCM_STRING_LENGTH (str);
158 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
160 memcpy (new_str, scm_i_string_chars (str), len);
172 index_get_cell (SCM s, Direction d)
176 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
180 index_set_cell (SCM s, Direction d, SCM v)
183 scm_set_car_x (s, v);
185 scm_set_cdr_x (s, v);
189 LY_DEFINE (ly_warn, "ly:warn",
190 1, 0, 1, (SCM str, SCM rest),
191 "Scheme callable function to issue the warning @code{msg}. "
192 "The message is formatted with @code{format} and @code{rest}.")
194 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
195 progress_indication ("\n");
197 str = scm_simple_format (SCM_BOOL_F, str, rest);
198 warning (ly_scm2string (str));
199 return SCM_UNSPECIFIED;
202 LY_DEFINE (ly_programming_error, "ly:programming-error",
203 1, 0, 1, (SCM str, SCM rest),
204 "Scheme callable function to issue the warning @code{msg}. "
205 "The message is formatted with @code{format} and @code{rest}.")
207 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
208 progress_indication ("\n");
210 str = scm_simple_format (SCM_BOOL_F, str, rest);
211 programming_error (ly_scm2string (str));
212 return SCM_UNSPECIFIED;
215 LY_DEFINE (ly_dir_p, "ly:dir?",
217 "type predicate. A direction is @code{-1}, @code{0} or "
218 "@code{1}, where @code{-1} represents "
219 "left or down and @code{1} represents right or up.")
221 if (scm_is_number (s))
223 int i = scm_to_int (s);
224 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
230 is_number_pair (SCM p)
232 return scm_is_pair (p)
233 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
236 typedef void (*Void_fptr) ();
237 Array<Void_fptr> *scm_init_funcs_;
239 void add_scm_init_func (void (*f) ())
241 if (!scm_init_funcs_)
242 scm_init_funcs_ = new Array<Void_fptr>;
244 scm_init_funcs_->push (f);
248 ly_init_ly_module (void *)
250 for (int i = scm_init_funcs_->size () ; i--;)
251 (scm_init_funcs_->elem (i)) ();
253 if (verbose_global_b)
254 progress_indication ("\n");
256 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
259 SCM global_lily_module;
264 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
265 scm_c_use_module ("lily");
271 return scm_ihashv (s, ~1u);
277 if (scm_is_number (s))
279 int i = scm_to_int (s);
280 return i>= -1 && i <= 1;
285 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
287 (SCM key, SCM alist, SCM default_value),
288 "Return value if KEY in ALIST, else DEFAULT-VALUE "
289 "(or #f if not specified).")
291 SCM handle = scm_assoc (key, alist);
293 if (default_value == SCM_UNDEFINED)
294 default_value = SCM_BOOL_F;
296 if (scm_is_pair (handle))
297 return scm_cdr (handle);
299 return default_value;
305 if (scm_is_number (s))
307 int i = scm_to_int (s);
308 return i == 0 || i == 1;
316 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
320 ly_scm2interval (SCM p)
322 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
326 ly_scm2realdrul (SCM p)
328 return Drul_array<Real> (scm_to_double (scm_car (p)),
329 scm_to_double (scm_cdr (p)));
333 ly_interval2scm (Drul_array<Real> i)
335 return scm_cons (scm_make_real (i[LEFT]), scm_make_real (i[RIGHT]));
341 return scm_is_bool (s) && ly_scm2bool (s);
344 /* Appendable list L: the cdr contains the list, the car the last cons
349 SCM s = scm_cons (SCM_EOL, SCM_EOL);
350 scm_set_car_x (s, s);
356 appendable_list_append (SCM l, SCM elt)
358 SCM newcons = scm_cons (elt, SCM_EOL);
360 scm_set_cdr_x (scm_car (l), newcons);
361 scm_set_car_x (l, newcons);
365 ly_offset2scm (Offset o)
367 return scm_cons (scm_make_real (o[X_AXIS]), scm_make_real (o[Y_AXIS]));
371 ly_scm2offset (SCM s)
373 return Offset (scm_to_double (scm_car (s)),
374 scm_to_double (scm_cdr (s)));
377 LY_DEFINE (ly_number2string, "ly:number->string",
379 "Convert @var{num} to a string without generating many decimals.")
381 SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
383 char str[400]; // ugh.
385 if (scm_exact_p (s) == SCM_BOOL_F)
387 Real r (scm_to_double (s));
389 if (my_isinf (r) || my_isnan (r))
391 if (isinf (r) || isnan (r))
394 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
398 sprintf (str, "%08.4f", r);
401 sprintf (str, "%d", scm_to_int (s));
403 return scm_makfrom0str (str);
408 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
409 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
411 char const* vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
413 return scm_c_eval_string ((char*)vs);
416 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
417 "Return the unit used for lengths as a string.")
419 return scm_makfrom0str (INTERNAL_UNIT);
424 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
425 "Return @var{d} is a number. Used to distinguish length "
426 "variables from normal numbers.")
428 return scm_number_p (d);
432 ly_deep_copy (SCM src)
434 if (scm_is_pair (src))
435 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
436 else if (ly_c_vector_p (src))
438 int len = SCM_VECTOR_LENGTH (src);
439 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
440 for (int i = 0 ;i < len ; i++)
442 SCM si = scm_int2num (i);
443 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
450 ly_chain_assoc_get (SCM key, SCM achain, SCM dfault)
452 if (scm_is_pair (achain))
454 SCM handle = scm_assoc (key, scm_car (achain));
455 if (scm_is_pair (handle))
456 return scm_cdr (handle);
458 return ly_chain_assoc (key, scm_cdr (achain));
465 ly_chain_assoc (SCM key, SCM achain)
467 if (scm_is_pair (achain))
469 SCM handle = scm_assoc (key, scm_car (achain));
470 if (scm_is_pair (handle))
473 return ly_chain_assoc (key, scm_cdr (achain));
479 /* looks the key up in the cdrs of the alist-keys
480 - ignoring the car and ignoring non-pair keys.
481 Returns first match found, i.e.
489 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
490 element with the cdr of the key = 1. In other words (alloc_cdr key)
493 (alloc (anything . key))
499 ly_assoc_cdr (SCM key, SCM alist)
501 if (scm_is_pair (alist))
503 SCM trykey = scm_caar (alist);
504 if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
505 return scm_car (alist);
507 return ly_assoc_cdr (key, scm_cdr (alist));
512 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
513 i.e. \n and ' ' can be used interchangeably as separators. */
515 parse_symbol_list (char const *lst)
517 char *s = strdup (lst);
519 SCM create_list = SCM_EOL;
521 char * e = s + strlen (s) - 1;
522 while (e >= s && isspace (*e))
525 for (char * p = s; *p; p++)
534 char *next = strchr (s, ' ');
538 create_list = scm_cons (ly_symbol2scm (s), create_list);
547 ly_truncate_list (int k, SCM lst)
555 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
559 scm_set_cdr_x (s, SCM_EOL);
565 print_scm_val (SCM val)
567 String realval = ly_scm2string (ly_write2scm (val));
568 if (realval.length () > 200)
569 realval = realval.left_string (100)
571 + realval.right_string (100);
576 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
584 TODO: should remove #f from allowed vals?
586 if (val == SCM_EOL || val == SCM_BOOL_F)
589 if (!scm_is_symbol (sym))
594 This is used for autoBeamSettings.
596 TODO: deprecate the use of \override and \revert for
599 or use a symbol autoBeamSettingS?
604 SCM type = scm_object_property (sym, type_symbol);
606 if (type != SCM_EOL && !ly_c_procedure_p (type))
608 warning (_f ("Can't find property type-check for `%s' (%s).",
609 ly_symbol2string (sym).to_str0 (),
610 ly_symbol2string (type_symbol).to_str0 ())
611 + " " + _ ("Perhaps you made a typing error?"));
613 /* Be strict when being anal :) */
614 if (internal_type_checking_global_b)
617 warning (_ ("Doing assignment anyway."));
622 && ly_c_procedure_p (type)
623 && scm_call_1 (type, val) == SCM_BOOL_F)
625 SCM errport = scm_current_error_port ();
627 SCM typefunc = ly_lily_module_constant ("type-name");
628 SCM type_name = scm_call_1 (typefunc, type);
631 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
632 ly_symbol2string (sym).to_str0 (),
634 ly_scm2string (type_name).to_str0 ()).to_str0 (),
636 scm_puts ("\n", errport);
645 zijn deze nou handig?
646 zijn ze er al in scheme, maar heten ze anders? */
649 /* Remove doubles from (sorted) list */
653 SCM unique = SCM_EOL;
654 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
656 if (!scm_is_pair (scm_cdr (i))
657 || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
658 unique = scm_cons (scm_car (i), unique);
660 return scm_reverse_x (unique, SCM_EOL);
665 scm_default_compare (void const *a, void const *b)
671 return pa < pb ? -1 : 1;
674 /* Modify LST in place: qsort it. */
676 ly_list_qsort_uniq_x (SCM lst)
678 int len = scm_ilength (lst);
679 SCM *arr = new SCM[len];
681 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
682 arr[k++] = scm_car (s);
685 qsort (arr, len, sizeof (SCM), &scm_default_compare);
688 for (int i = 0; i < len; i++)
689 if (!i || arr[i] != arr[i - 1])
691 SCM_SETCAR (*tail, arr[i]);
692 tail = SCM_CDRLOC (*tail);
704 ly_snoc (SCM s, SCM list)
706 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
709 /* Split list at member s, removing s.
710 Return (BEFORE . AFTER) */
712 ly_split_list (SCM s, SCM list)
714 SCM before = SCM_EOL;
716 for (; scm_is_pair (after);)
718 SCM i = scm_car (after);
719 after = scm_cdr (after);
720 if (ly_c_equal_p (i, s))
722 before = scm_cons (i, before);
724 return scm_cons ( scm_reverse_x (before, SCM_EOL), after);
738 display stuff without using stack
743 SCM p = scm_current_output_port ();
746 for (; scm_is_pair (s); s = scm_cdr (s))
748 scm_display (scm_car (s), p);
752 return SCM_UNSPECIFIED;
756 int_list_to_slice (SCM l)
760 for (; scm_is_pair (l); l = scm_cdr (l))
761 if (scm_is_number (scm_car (l)))
762 s.add_point (scm_to_int (scm_car (l)));
766 /* Return I-th element, or last elt L. If I < 0, then we take the first
769 PRE: length (L) > 0 */
771 robust_list_ref (int i, SCM l)
773 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
779 robust_scm2double (SCM k, double x)
781 if (scm_is_number (k))
782 x = scm_to_double (k);
787 robust_scm2interval (SCM k, Drul_array<Real> v)
792 if (is_number_pair (k))
793 i = ly_scm2interval (k);
798 robust_scm2drul (SCM k, Drul_array<Real> v)
800 if (is_number_pair (k))
801 v = ly_scm2interval (k);
806 robust_scm2offset (SCM k, Offset o)
808 if (is_number_pair (k))
809 o = ly_scm2offset (k);
814 robust_scm2int (SCM k, int o)
816 if (scm_integer_p (k) == SCM_BOOL_T)
822 alist_to_hashq (SCM alist)
824 int i = scm_ilength (alist);
826 return scm_c_make_hash_table (0);
828 SCM tab = scm_c_make_hash_table (i);
829 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
831 SCM pt = scm_cdar (s);
832 scm_hashq_set_x (tab, scm_caar (s), pt);
840 LY_DEFINE (ly_protects, "ly:protects",
842 "Return hash of protected objects.")
847 LY_DEFINE (ly_gettext, "ly:gettext",
848 1, 0, 0, (SCM string),
851 SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
852 __FUNCTION__, "string");
853 return scm_makfrom0str (gettext (scm_i_string_chars (string)));