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"
45 ly_to_symbol (SCM scm)
47 return scm_string_to_symbol (ly_to_string (scm));
51 ly_to_string (SCM scm)
53 return scm_call_3 (ly_scheme_function ("format"), SCM_BOOL_F,
54 scm_makfrom0str ("~S"), scm);
60 return scm_car (scm_last_pair (list));
66 SCM port = scm_mkstrport (SCM_INUM0,
67 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
70 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
71 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
73 // scm_apply (write, port, SCM_EOL);
74 scm_call_2 (write, s, port);
75 return scm_strport_to_string (port);
81 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
85 ly_symbol2string (SCM s)
88 Ugh. this is not very efficient.
90 SCM str = scm_symbol_to_string (s);
91 return ly_scm2string (str);
95 gulp_file_to_string (String fn, bool must_exist)
97 String s = global_path.find (fn);
102 String e = _f ("can't find file: `%s'", fn);
104 e += _f ("(load path: `%s')", global_path.to_string ());
111 if (verbose_global_b)
112 progress_indication ("[" + s);
115 char *str = gulp_file (s, &n);
119 if (verbose_global_b)
120 progress_indication ("]");
125 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
127 "Read the file @var{name}, and return its contents in a string. "
128 "The file is looked up using the search path.")
130 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
131 return scm_makfrom0str (gulp_file_to_string (ly_scm2string (name),
138 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
140 ly_display_scm (SCM s)
142 scm_display (s, scm_current_output_port ());
143 scm_newline (scm_current_output_port ());
148 ly_scm2string (SCM str)
150 return String ((Byte*)scm_i_string_chars (str),
151 (int) scm_i_string_length (str));
155 ly_scm2newstr (SCM str, size_t *lenp)
157 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
159 size_t len = SCM_STRING_LENGTH (str);
160 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
162 memcpy (new_str, scm_i_string_chars (str), len);
174 index_get_cell (SCM s, Direction d)
178 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
182 index_set_cell (SCM s, Direction d, SCM v)
185 scm_set_car_x (s, v);
187 scm_set_cdr_x (s, v);
191 LY_DEFINE (ly_warn, "ly:warn",
192 1, 0, 1, (SCM str, SCM rest),
193 "Scheme callable function to issue the warning @code{msg}. "
194 "The message is formatted with @code{format} and @code{rest}.")
196 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
197 progress_indication ("\n");
199 str = scm_simple_format (SCM_BOOL_F, str, rest);
200 warning ("lily-guile: " + ly_scm2string (str));
201 return SCM_UNSPECIFIED;
204 LY_DEFINE (ly_programming_error, "ly:programming-error",
205 1, 0, 1, (SCM str, SCM rest),
206 "Scheme callable function to issue the warning @code{msg}. "
207 "The message is formatted with @code{format} and @code{rest}.")
209 SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
210 progress_indication ("\n");
212 str = scm_simple_format (SCM_BOOL_F, str, rest);
213 programming_error (ly_scm2string (str));
214 return SCM_UNSPECIFIED;
217 LY_DEFINE (ly_dir_p, "ly:dir?",
219 "type predicate. A direction is @code{-1}, @code{0} or "
220 "@code{1}, where @code{-1} represents "
221 "left or down and @code{1} represents right or up.")
223 if (scm_is_number (s))
225 int i = scm_to_int (s);
226 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
232 is_number_pair (SCM p)
234 return scm_is_pair (p)
235 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
238 typedef void (*Void_fptr) ();
239 Array<Void_fptr> *scm_init_funcs_;
241 void add_scm_init_func (void (*f) ())
243 if (!scm_init_funcs_)
244 scm_init_funcs_ = new Array<Void_fptr>;
246 scm_init_funcs_->push (f);
250 ly_init_ly_module (void *)
252 for (int i = scm_init_funcs_->size () ; i--;)
253 (scm_init_funcs_->elem (i)) ();
255 if (verbose_global_b)
256 progress_indication ("\n");
258 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
261 SCM global_lily_module;
266 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
267 scm_c_use_module ("lily");
273 return scm_ihashv (s, ~1u);
279 if (scm_is_number (s))
281 int i = scm_to_int (s);
282 return i>= -1 && i <= 1;
287 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
289 (SCM key, SCM alist, SCM default_value),
290 "Return value if KEY in ALIST, else DEFAULT-VALUE "
291 "(or #f if not specified).")
293 SCM handle = scm_assoc (key, alist);
295 if (default_value == SCM_UNDEFINED)
296 default_value = SCM_BOOL_F;
298 if (scm_is_pair (handle))
299 return scm_cdr (handle);
301 return default_value;
307 if (scm_is_number (s))
309 int i = scm_to_int (s);
310 return i== 0 || i == 1;
318 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
322 ly_scm2interval (SCM p)
324 return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
328 ly_scm2realdrul (SCM p)
330 return Drul_array<Real> (scm_to_double (scm_car (p)),
331 scm_to_double (scm_cdr (p)));
335 ly_interval2scm (Drul_array<Real> i)
337 return scm_cons (scm_make_real (i[LEFT]), scm_make_real (i[RIGHT]));
343 return scm_is_bool (s) && ly_scm2bool (s);
346 /* Appendable list L: the cdr contains the list, the car the last cons
351 SCM s = scm_cons (SCM_EOL, SCM_EOL);
352 scm_set_car_x (s, s);
358 appendable_list_append (SCM l, SCM elt)
360 SCM newcons = scm_cons (elt, SCM_EOL);
362 scm_set_cdr_x (scm_car (l), newcons);
363 scm_set_car_x (l, newcons);
367 ly_offset2scm (Offset o)
369 return scm_cons (scm_make_real (o[X_AXIS]), scm_make_real (o[Y_AXIS]));
373 ly_scm2offset (SCM s)
375 return Offset (scm_to_double (scm_car (s)),
376 scm_to_double (scm_cdr (s)));
379 LY_DEFINE (ly_number2string, "ly:number->string",
381 "Convert @var{num} to a string without generating many decimals.")
383 SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
385 char str[400]; // ugh.
387 if (scm_exact_p (s) == SCM_BOOL_F)
389 Real r (scm_to_double (s));
391 if (my_isinf (r) || my_isnan (r))
393 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
397 sprintf (str, "%08.4f", r);
400 sprintf (str, "%d", scm_to_int (s));
402 return scm_makfrom0str (str);
407 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
408 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
410 char const* vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
412 return scm_c_eval_string ((char*)vs);
415 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
416 "Return the unit used for lengths as a string.")
418 return scm_makfrom0str (INTERNAL_UNIT);
423 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
424 "Return @var{d} is a number. Used to distinguish length "
425 "variables from normal numbers.")
427 return scm_number_p (d);
431 ly_deep_copy (SCM src)
433 if (scm_is_pair (src))
434 return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
435 else if (ly_c_vector_p (src))
437 int len = SCM_VECTOR_LENGTH (src);
438 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
439 for (int i = 0 ;i < len ; i++)
441 SCM si = scm_int2num (i);
442 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
452 ly_assoc_chain (SCM key, SCM achain)
454 if (scm_is_pair (achain))
456 SCM handle = scm_assoc (key, scm_car (achain));
457 if (scm_is_pair (handle))
460 return ly_assoc_chain (key, scm_cdr (achain));
466 /* looks the key up in the cdrs of the alist-keys
467 - ignoring the car and ignoring non-pair keys.
468 Returns first match found, i.e.
476 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
477 element with the cdr of the key = 1. In other words (alloc_cdr key)
480 (alloc (anything . key))
486 ly_assoc_cdr (SCM key, SCM alist)
488 if (scm_is_pair (alist))
490 SCM trykey = scm_caar (alist);
491 if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
492 return scm_car (alist);
494 return ly_assoc_cdr (key, scm_cdr (alist));
499 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
500 i.e. \n and ' ' can be used interchangeably as separators. */
502 parse_symbol_list (char const *lst)
504 char *s = strdup (lst);
506 SCM create_list = SCM_EOL;
508 char * e = s + strlen (s) - 1;
509 while (e >= s && isspace (*e))
512 for (char * p = s; *p; p++)
521 char *next = strchr (s, ' ');
525 create_list = scm_cons (ly_symbol2scm (s), create_list);
534 ly_truncate_list (int k, SCM lst)
542 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
546 scm_set_cdr_x (s, SCM_EOL);
552 print_scm_val (SCM val)
554 String realval = ly_scm2string (ly_write2scm (val));
555 if (realval.length () > 200)
556 realval = realval.left_string (100)
558 + realval.right_string (100);
563 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
571 TODO: should remove #f from allowed vals?
573 if (val == SCM_EOL || val == SCM_BOOL_F)
576 if (!scm_is_symbol (sym))
581 This is used for autoBeamSettings.
583 TODO: deprecate the use of \override and \revert for
586 or use a symbol autoBeamSettingS?
591 SCM type = scm_object_property (sym, type_symbol);
593 if (type != SCM_EOL && !ly_c_procedure_p (type))
595 warning (_f ("Can't find property type-check for `%s' (%s).",
596 ly_symbol2string (sym).to_str0 (),
597 ly_symbol2string (type_symbol).to_str0 ())
598 + " " + _ ("Perhaps you made a typing error?"));
600 /* Be strict when being anal :) */
601 if (internal_type_checking_global_b)
604 warning (_ ("Doing assignment anyway."));
609 && ly_c_procedure_p (type)
610 && scm_call_1 (type, val) == SCM_BOOL_F)
612 SCM errport = scm_current_error_port ();
614 SCM typefunc = ly_scheme_function ("type-name");
615 SCM type_name = scm_call_1 (typefunc, type);
618 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
619 ly_symbol2string (sym).to_str0 (),
621 ly_scm2string (type_name).to_str0 ()).to_str0 (),
623 scm_puts ("\n", errport);
632 zijn deze nou handig?
633 zijn ze er al in scheme, maar heten ze anders? */
636 /* Remove doubles from (sorted) list */
640 SCM unique = SCM_EOL;
641 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
643 if (!scm_is_pair (scm_cdr (i))
644 || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
645 unique = scm_cons (scm_car (i), unique);
647 return scm_reverse_x (unique, SCM_EOL);
652 scm_default_compare (void const *a, void const *b)
658 return pa < pb ? -1 : 1;
661 /* Modify LST in place: qsort it. */
663 ly_list_qsort_uniq_x (SCM lst)
665 int len = scm_ilength (lst);
666 SCM *arr = new SCM[len];
668 for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
669 arr[k++] = scm_car (s);
672 qsort (arr, len, sizeof (SCM), &scm_default_compare);
675 for (int i = 0; i < len; i++)
676 if (!i || arr[i] != arr[i - 1])
678 SCM_SETCAR (*tail, arr[i]);
679 tail = SCM_CDRLOC (*tail);
691 ly_snoc (SCM s, SCM list)
693 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
696 /* Split list at member s, removing s.
697 Return (BEFORE . AFTER) */
699 ly_split_list (SCM s, SCM list)
701 SCM before = SCM_EOL;
703 for (; scm_is_pair (after);)
705 SCM i = scm_car (after);
706 after = scm_cdr (after);
707 if (ly_c_equal_p (i, s))
709 before = scm_cons (i, before);
711 return scm_cons ( scm_reverse_x (before, SCM_EOL), after);
725 display stuff without using stack
730 SCM p = scm_current_output_port ();
733 for (; scm_is_pair (s); s =scm_cdr (s))
735 scm_display (scm_car (s), p);
739 return SCM_UNSPECIFIED;
743 int_list_to_slice (SCM l)
747 for (; scm_is_pair (l); l = scm_cdr (l))
748 if (scm_is_number (scm_car (l)))
749 s.add_point (scm_to_int (scm_car (l)));
753 /* Return I-th element, or last elt L. If I < 0, then we take the first
756 PRE: length (L) > 0 */
758 robust_list_ref (int i, SCM l)
760 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
766 robust_scm2double (SCM k, double x)
768 if (scm_is_number (k))
769 x = scm_to_double (k);
774 robust_scm2interval (SCM k, Drul_array<Real> v)
779 if (is_number_pair (k))
780 i = ly_scm2interval (k);
785 robust_scm2drul (SCM k, Drul_array<Real> v)
787 if (is_number_pair (k))
788 v = ly_scm2interval (k);
793 robust_scm2offset (SCM k, Offset o)
795 if (is_number_pair (k))
796 o = ly_scm2offset (k);
801 robust_scm2int (SCM k, int o)
803 if (scm_integer_p (k) == SCM_BOOL_T)
809 alist_to_hashq (SCM alist)
811 int i = scm_ilength (alist);
813 return scm_c_make_hash_table (0);
815 SCM tab = scm_c_make_hash_table (i);
816 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
818 SCM pt = scm_cdar (s);
819 scm_hashq_set_x (tab, scm_caar (s), pt);
828 LY_DEFINE (ly_protects, "ly:protects",
830 "Return hash of protected objects.")
837 #if HAVE_PANGO_FC_FONT_MAP_ADD_DECODER_FIND_FUNC
839 #include "pangofc-afm-decoder.hh"
841 LY_DEFINE (ly_pango_add_afm_decoder, "ly:pango-add-afm-decoder",
842 1, 0, 0, (SCM font_family),
843 "Add pango afm decoder for FONT-FAMILY.")
845 SCM_ASSERT_TYPE (scm_is_string (font_family), font_family, SCM_ARG1,
846 __FUNCTION__, "font_family");
847 pango_fc_afm_add_decoder (ly_scm2newstr (font_family, 0));
848 return SCM_UNSPECIFIED;
853 LY_DEFINE (ly_gettext, "ly:gettext",
854 1, 0, 0, (SCM string),
857 SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
858 __FUNCTION__, "string");
859 return scm_makfrom0str (gettext (scm_i_string_chars (string)));