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 <math.h> /* isinf */
14 #include <string.h> /* strdup, strchr */
17 #include "lily-proto.hh"
21 source-file.hh includes cmath which undefines isinf and isnan
23 FIXME: #ifdef MACOS_X?
25 inline int my_isinf (Real r) { return isinf (r); }
26 inline int my_isnan (Real r) { return isnan (r); }
29 #include "libc-extension.hh"
30 #include "lily-guile.hh"
32 #include "file-path.hh"
34 #include "direction.hh"
36 #include "interval.hh"
38 #include "dimensions.hh"
39 #include "source-file.hh"
46 return ly_car (scm_last_pair (list));
53 SCM port = scm_mkstrport (SCM_INUM0,
54 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
57 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
58 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
60 // scm_apply (write, port, SCM_EOL);
61 scm_call_2 (write, s, port);
62 return scm_strport_to_string (port);
69 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
73 ly_symbol2string (SCM s)
75 assert (ly_c_symbol_p (s));
76 return String ((Byte*)SCM_STRING_CHARS (s), (int) SCM_STRING_LENGTH (s));
80 gulp_file_to_string (String fn)
82 String s = global_path.find (fn);
85 String e = _f ("can't find file: `%s'", fn);
87 e += _f ("(load path: `%s')", global_path.to_string ());
90 else if (verbose_global_b)
91 progress_indication ("[" + s);
94 char * str = gulp_file (s, &n);
99 progress_indication ("]");
104 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
106 "Read the file @var{name}, and return its contents in a string. "
107 "The file is looked up using the search path.")
109 SCM_ASSERT_TYPE (ly_c_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
110 return scm_makfrom0str (gulp_file_to_string (ly_scm2string (name)).to_str0 ());
115 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
117 ly_display_scm (SCM s)
119 scm_display (s, scm_current_output_port ());
120 scm_newline (scm_current_output_port ());
125 ly_scm2str0 (SCM string)
127 SCM_ASSERT_TYPE (ly_c_string_p (string), string, SCM_ARG1,
128 __FUNCTION__, "string");
129 return SCM_STRING_CHARS (string);
133 ly_scm2string (SCM string)
135 return ly_scm2str0 (string);
139 ly_scm2newstr (SCM str, size_t *lenp)
141 SCM_ASSERT_TYPE (ly_c_string_p (str), str, SCM_ARG1, __FUNCTION__, "string");
143 size_t len = SCM_STRING_LENGTH (str);
144 if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
146 memcpy (new_str, SCM_STRING_CHARS (str), len);
158 index_get_cell (SCM s, Direction d)
162 return (d == LEFT) ? ly_car (s) : ly_cdr (s);
166 index_set_cell (SCM s, Direction d, SCM v)
169 scm_set_car_x (s, v);
171 scm_set_cdr_x (s, v);
175 LY_DEFINE (ly_warn, "ly:warn",
176 1, 0, 1, (SCM str, SCM rest),
177 "Scheme callable function to issue the warning @code{msg}. "
178 "The message is formatted with @code{format} and @code{rest}.")
180 SCM_ASSERT_TYPE (ly_c_string_p (str), str, SCM_ARG1, __FUNCTION__, "string");
181 progress_indication ("\n");
183 str = scm_simple_format (SCM_BOOL_F, str, rest);
184 warning ("lily-guile: " + ly_scm2string (str));
185 return SCM_UNSPECIFIED;
188 LY_DEFINE (ly_programming_error, "ly:programming-error",
189 1, 0, 1, (SCM str, SCM rest),
190 "Scheme callable function to issue the warning @code{msg}. "
191 "The message is formatted with @code{format} and @code{rest}.")
193 SCM_ASSERT_TYPE (ly_c_string_p (str), str, SCM_ARG1, __FUNCTION__, "string");
194 progress_indication ("\n");
196 str = scm_simple_format (SCM_BOOL_F, str, rest);
197 programming_error (ly_scm2string (str));
198 return SCM_UNSPECIFIED;
201 LY_DEFINE (ly_dir_p, "ly:dir?",
203 "type predicate. A direction is @code{-1}, @code{0} or "
204 "@code{1}, where @code{-1} represents "
205 "left or down and @code{1} represents right or up.")
207 if (ly_c_number_p (s))
209 int i = ly_scm2int (s);
210 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
216 is_number_pair (SCM p)
218 return ly_c_pair_p (p)
219 && ly_c_number_p (ly_car (p)) && ly_c_number_p (ly_cdr (p));
222 typedef void (*Void_fptr) ();
223 Array<Void_fptr> *scm_init_funcs_;
225 void add_scm_init_func (void (*f) ())
227 if (!scm_init_funcs_)
228 scm_init_funcs_ = new Array<Void_fptr>;
230 scm_init_funcs_->push (f);
234 ly_init_ly_module (void *)
236 for (int i=scm_init_funcs_->size () ; i--;)
237 (scm_init_funcs_->elem (i)) ();
239 if (verbose_global_b)
240 progress_indication ("\n");
242 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
245 SCM global_lily_module;
250 global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
251 scm_c_use_module ("lily");
257 return scm_ihashv (s, ~1u);
263 if (ly_c_number_p (s))
265 int i = ly_scm2int (s);
266 return i>= -1 && i <= 1;
274 if (ly_c_number_p (s))
276 int i = ly_scm2int (s);
277 return i== 0 || i == 1;
285 return scm_is_integer (s) ? (Direction) ly_scm2int (s) : CENTER;
289 ly_scm2interval (SCM p)
291 return Interval (ly_scm2double (ly_car (p)), ly_scm2double (ly_cdr (p)));
295 ly_scm2realdrul (SCM p)
297 return Drul_array<Real> (ly_scm2double (ly_car (p)),
298 ly_scm2double (ly_cdr (p)));
302 ly_interval2scm (Drul_array<Real> i)
304 return scm_cons (scm_make_real (i[LEFT]), scm_make_real (i[RIGHT]));
310 return ly_c_boolean_p (s) && ly_scm2bool (s);
313 /* Appendable list L: the cdr contains the list, the car the last cons
318 SCM s = scm_cons (SCM_EOL, SCM_EOL);
319 scm_set_car_x (s, s);
325 appendable_list_append (SCM l, SCM elt)
327 SCM newcons = scm_cons (elt, SCM_EOL);
329 scm_set_cdr_x (ly_car (l), newcons);
330 scm_set_car_x (l, newcons);
334 ly_offset2scm (Offset o)
336 return scm_cons (scm_make_real (o[X_AXIS]), scm_make_real (o[Y_AXIS]));
340 ly_scm2offset (SCM s)
342 return Offset (ly_scm2double (ly_car (s)),
343 ly_scm2double (ly_cdr (s)));
346 LY_DEFINE (ly_number2string, "ly:number->string",
348 "Convert @var{num} to a string without generating many decimals.")
350 SCM_ASSERT_TYPE (ly_c_number_p (s), s, SCM_ARG1, __FUNCTION__, "number");
352 char str[400]; // ugh.
354 if (scm_exact_p (s) == SCM_BOOL_F)
356 Real r (ly_scm2double (s));
358 if (my_isinf (r) || my_isnan (r))
360 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
364 sprintf (str, "%08.4f", r);
367 sprintf (str, "%d", ly_scm2int (s));
369 return scm_makfrom0str (str);
374 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
375 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
377 char const* vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
379 return scm_c_eval_string ((char*)vs);
382 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
383 "Return the unit used for lengths as a string.")
385 return scm_makfrom0str (INTERNAL_UNIT);
390 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
391 "Return @var{d} is a number. Used to distinguish length "
392 "variables from normal numbers.")
394 return scm_number_p (d);
398 ly_deep_copy (SCM src)
400 if (ly_c_pair_p (src))
401 return scm_cons (ly_deep_copy (ly_car (src)), ly_deep_copy (ly_cdr (src)));
402 else if (ly_c_vector_p (src))
404 int len = SCM_VECTOR_LENGTH (src);
405 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
406 for (int i =0 ; i < len ; i++)
408 SCM si = scm_int2num (i);
409 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
419 ly_assoc_chain (SCM key, SCM achain)
421 if (ly_c_pair_p (achain))
423 SCM handle = scm_assoc (key, ly_car (achain));
424 if (ly_c_pair_p (handle))
427 return ly_assoc_chain (key, ly_cdr (achain));
433 /* looks the key up in the cdrs of the alist-keys
434 - ignoring the car and ignoring non-pair keys.
435 Returns first match found, i.e.
443 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
444 element with the cdr of the key = 1. In other words (alloc_cdr key)
447 (alloc (anything . key))
453 ly_assoc_cdr (SCM key, SCM alist)
455 if (ly_c_pair_p (alist))
457 SCM trykey = ly_caar (alist);
458 if (ly_c_pair_p (trykey) && to_boolean (scm_equal_p (key, ly_cdr (trykey))))
459 return ly_car (alist);
461 return ly_assoc_cdr (key, ly_cdr (alist));
466 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
467 i.e. \n and ' ' can be used interchangeably as separators. */
469 parse_symbol_list (char const *lst)
471 char *s = strdup (lst);
473 SCM create_list = SCM_EOL;
475 char * e = s + strlen (s) - 1;
476 while (e >= s && isspace (*e))
479 for (char * p = s; *p; p++)
488 char *next = strchr (s, ' ');
492 create_list = scm_cons (ly_symbol2scm (s), create_list);
501 ly_truncate_list (int k, SCM lst)
509 for (; ly_c_pair_p (s) && k--; s = ly_cdr (s))
513 scm_set_cdr_x (s, SCM_EOL);
519 print_scm_val (SCM val)
521 String realval = ly_scm2string (ly_write2scm (val));
522 if (realval.length () > 200)
523 realval = realval.left_string (100)
525 + realval.right_string (100);
530 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
538 TODO: should remove #f from allowed vals?
540 if (val == SCM_EOL || val == SCM_BOOL_F)
543 if (!ly_c_symbol_p (sym))
548 This is used for autoBeamSettings.
550 TODO: deprecate the use of \override and \revert for
553 or use a symbol autoBeamSettingS?
558 SCM type = scm_object_property (sym, type_symbol);
560 if (type != SCM_EOL && !ly_c_procedure_p (type))
562 warning (_f ("Can't find property type-check for `%s' (%s).",
563 ly_symbol2string (sym).to_str0 (),
564 ly_symbol2string (type_symbol).to_str0 ())
565 + " " + _ ("Perhaps you made a typing error?"));
567 /* Be strict when being anal :) */
568 if (internal_type_checking_global_b)
571 warning (_ ("Doing assignment anyway."));
576 && ly_c_procedure_p (type)
577 && scm_call_1 (type, val) == SCM_BOOL_F)
579 SCM errport = scm_current_error_port ();
581 SCM typefunc = ly_scheme_function ("type-name");
582 SCM type_name = scm_call_1 (typefunc, type);
585 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
586 ly_symbol2string (sym).to_str0 (),
588 ly_scm2string (type_name).to_str0 ()).to_str0 (),
590 scm_puts ("\n", errport);
599 zijn deze nou handig?
600 zijn ze er al in scheme, maar heten ze anders? */
603 /* Remove doubles from (sorted) list */
607 SCM unique = SCM_EOL;
608 for (SCM i = list; ly_c_pair_p (i); i = ly_cdr (i))
610 if (!ly_c_pair_p (ly_cdr (i))
611 || !ly_c_equal_p (ly_car (i), ly_cadr (i)))
612 unique = scm_cons (ly_car (i), unique);
614 return scm_reverse_x (unique, SCM_EOL);
619 scm_default_compare (void const *a, void const *b)
625 return pa < pb ? -1 : 1;
628 /* Modify LST in place: qsort it. */
630 ly_list_qsort_uniq_x (SCM lst)
632 int len = scm_ilength (lst);
633 SCM *arr = new SCM[len];
635 for (SCM s = lst; SCM_NNULLP (s); s = SCM_CDR (s))
636 arr[k++] = SCM_CAR (s);
639 qsort (arr, len, sizeof (SCM), &scm_default_compare);
642 for (int i = 0; i < len; i++)
643 if (!i || arr[i] != arr[i - 1])
645 SCM_SETCAR (*tail, arr[i]);
646 tail = SCM_CDRLOC (*tail);
658 ly_snoc (SCM s, SCM list)
660 return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
663 /* Split list at member s, removing s.
664 Return (BEFORE . AFTER) */
666 ly_split_list (SCM s, SCM list)
668 SCM before = SCM_EOL;
670 for (; ly_c_pair_p (after);)
672 SCM i = ly_car (after);
673 after = ly_cdr (after);
674 if (ly_c_equal_p (i, s))
676 before = scm_cons (i, before);
678 return scm_cons ( scm_reverse_x (before, SCM_EOL), after);
692 display stuff without using stack
697 SCM p = scm_current_output_port ();
700 for (; ly_c_pair_p (s); s =ly_cdr (s))
702 scm_display (ly_car (s), p);
706 return SCM_UNSPECIFIED;
710 int_list_to_slice (SCM l)
714 for (; ly_c_pair_p (l); l = ly_cdr (l))
715 if (ly_c_number_p (ly_car (l)))
716 s.add_point (ly_scm2int (ly_car (l)));
720 /* Return I-th element, or last elt L. If I < 0, then we take the first
723 PRE: length (L) > 0 */
725 robust_list_ref (int i, SCM l)
727 while (i-- > 0 && ly_c_pair_p (ly_cdr (l)))
733 robust_scm2double (SCM k, double x)
735 if (ly_c_number_p (k))
736 x = ly_scm2double (k);
741 robust_scm2interval (SCM k, Drul_array<Real> v)
746 if (is_number_pair (k))
747 i = ly_scm2interval (k);
752 robust_scm2drul (SCM k, Drul_array<Real> v)
754 if (is_number_pair (k))
755 v = ly_scm2interval (k);
760 robust_scm2offset (SCM k, Offset o)
762 if (is_number_pair (k))
763 o = ly_scm2offset (k);
768 robust_scm2int (SCM k, int o)
770 if (scm_integer_p (k) == SCM_BOOL_T)
776 alist_to_hashq (SCM alist)
778 int i = scm_ilength (alist);
780 return scm_c_make_hash_table (0);
782 SCM tab = scm_c_make_hash_table (i);
783 for (SCM s = alist; ly_c_pair_p (s); s = ly_cdr (s))
785 SCM pt = ly_cdar (s);
786 scm_hashq_set_x (tab, ly_caar (s), pt);
795 LY_DEFINE (ly_protects, "ly:protects",
797 "Return hash of protected objects.")
804 #if HAVE_PANGO_FC_FONT_MAP_ADD_DECODER_FIND_FUNC
806 #include "pangofc-afm-decoder.hh"
808 LY_DEFINE (ly_pango_add_afm_decoder, "ly:pango-add-afm-decoder",
809 1, 0, 0, (SCM font_family),
810 "Add pango afm decoder for FONT-FAMILY.")
812 SCM_ASSERT_TYPE (ly_c_string_p (font_family), font_family, SCM_ARG1, __FUNCTION__, "font_family");
813 pango_fc_afm_add_decoder (ly_scm2newstr (font_family, 0));
814 return SCM_UNSPECIFIED;
819 LY_DEFINE (ly_gettext, "ly:gettext",
820 1, 0, 0, (SCM string),
823 SCM_ASSERT_TYPE (ly_c_string_p (string), string, SCM_ARG1,
824 __FUNCTION__, "string");
825 return scm_makfrom0str (gettext (ly_scm2str0 (string)));