*** empty log message ***
[lilypond.git] / lily / lily-guile.cc
blobcab31a2124a2bbb444eb6e84e79a4db03b158ae6
1 /*
2 lily-guile.cc -- implement assorted guile functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2003 Jan Nieuwenhuizen <janneke@gnu.org>
8 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 */
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h> /* isinf */
15 #include <string.h> /* strdup, strchr */
18 #include "lily-proto.hh"
20 /* macosx fix:
23 source-file.hh includes cmath which undefines isinf and isnan
25 inline int my_isinf(Real r) { return isinf(r); }
26 inline int my_isnan(Real r) { return isnan(r); }
30 #include "libc-extension.hh"
31 #include "lily-guile.hh"
32 #include "main.hh"
33 #include "file-path.hh"
34 #include "warn.hh"
35 #include "direction.hh"
36 #include "offset.hh"
37 #include "interval.hh"
38 #include "pitch.hh"
39 #include "dimensions.hh"
40 #include "source-file.hh"
42 // #define TEST_GC
44 SCM
45 ly_last (SCM list)
47 return ly_car (scm_last_pair (list));
51 SCM
52 ly_write2scm (SCM s)
54 SCM port = scm_mkstrport (SCM_INUM0,
55 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
56 SCM_OPN | SCM_WRTNG,
57 "ly_write2string");
58 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
59 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
61 // scm_apply (write, port, SCM_EOL);
62 gh_call2 (write, s, port);
63 return scm_strport_to_string (port);
67 SCM
68 ly_quote_scm (SCM s)
70 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
73 String
74 ly_symbol2string (SCM s)
76 assert (gh_symbol_p (s));
77 return String ((Byte*)SCM_STRING_CHARS (s), (int) SCM_STRING_LENGTH (s));
80 String
81 gulp_file_to_string (String fn)
83 String s = global_path.find (fn);
84 if (s == "")
86 String e = _f ("can't find file: `%s'", fn);
87 e += " ";
88 e += _f ("(load path: `%s')", global_path.to_string ());
89 error (e);
91 else if (verbose_global_b)
92 progress_indication ("[" + s);
94 int n;
95 char * str = gulp_file (s, &n);
96 String result (str);
97 delete[] str;
99 if (verbose_global_b)
100 progress_indication ("]");
102 return result;
105 LY_DEFINE(ly_gulp_file, "ly:gulp-file", 1,0, 0,
106 (SCM name),
107 "Read the file named @var{name}, and return its contents in a string. The "
108 " file is looked up using the lilypond search path.")
110 return scm_makfrom0str (gulp_file_to_string (ly_scm2string (name)).to_str0 ());
114 extern "C" {
115 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
116 void
117 ly_display_scm (SCM s)
119 gh_display (s);
120 gh_newline ();
124 String
125 ly_scm2string (SCM s)
127 assert (gh_string_p (s));
129 char *p = SCM_STRING_CHARS(s);
130 String r (p);
131 return r;
135 index_get_cell (SCM s, Direction d)
138 assert (d);
139 return (d == LEFT) ? ly_car (s) : ly_cdr (s);
143 index_set_cell (SCM s, Direction d, SCM v)
145 if (d == LEFT)
146 gh_set_car_x (s, v);
147 else if (d == RIGHT)
148 gh_set_cdr_x (s, v);
149 return s;
152 LY_DEFINE(ly_warning,"ly:warn", 1, 0, 0,
153 (SCM str),"Scheme callable function to issue the warning @code{msg}.")
155 SCM_ASSERT_TYPE (gh_string_p (str), str, SCM_ARG1, __FUNCTION__, "string");
156 warning ("lily-guile: " + ly_scm2string (str));
157 return SCM_BOOL_T;
160 LY_DEFINE(ly_isdir, "ly:dir?", 1,0, 0, (SCM s),
161 "type predicate. A direction is a -1, 0 or 1, where -1 represents left or "
162 "down and 1 represents right or up. ")
164 if (gh_number_p (s))
166 int i = gh_scm2int (s);
167 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
169 return SCM_BOOL_F;
172 bool
173 ly_number_pair_p (SCM p)
175 return gh_pair_p (p) && gh_number_p (ly_car (p)) && gh_number_p (ly_cdr (p));
178 typedef void (*Void_fptr) ();
179 Array<Void_fptr> *scm_init_funcs_;
181 void add_scm_init_func (void (*f) ())
183 if (!scm_init_funcs_)
184 scm_init_funcs_ = new Array<Void_fptr>;
186 scm_init_funcs_->push (f);
190 void
191 ly_init_ly_module (void *data)
193 for (int i=scm_init_funcs_->size () ; i--;)
194 (scm_init_funcs_->elem (i)) ();
196 if (verbose_global_b)
197 progress_indication ("\n");
199 scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
203 SCM lily_module ;
205 void
206 ly_init_guile ()
208 lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
209 scm_c_use_module ("lily");
212 unsigned int ly_scm_hash (SCM s)
214 return scm_ihashv (s, ~1u);
219 bool
220 ly_dir_p (SCM s)
222 if (gh_number_p (s))
224 int i = gh_scm2int (s);
225 return i>= -1 && i <= 1;
227 return false;
231 bool
232 ly_axis_p (SCM s)
234 if (gh_number_p (s))
236 int i = gh_scm2int (s);
237 return i== 0 || i == 1;
239 return false;
243 Direction
244 to_dir (SCM s)
246 return SCM_INUMP (s) ? (Direction) gh_scm2int (s) : CENTER;
249 Interval
250 ly_scm2interval (SCM p)
252 return Interval (gh_scm2double (ly_car (p)),
253 gh_scm2double (ly_cdr (p)));
257 ly_interval2scm (Drul_array<Real> i)
259 return gh_cons (gh_double2scm (i[LEFT]),
260 gh_double2scm (i[RIGHT]));
266 bool
267 to_boolean (SCM s)
269 return gh_boolean_p (s) && gh_scm2bool (s);
273 Appendable list L: the cdr contains the list, the car the last cons
274 in the list.
277 appendable_list ()
279 SCM s = gh_cons (SCM_EOL, SCM_EOL);
280 gh_set_car_x (s, s);
282 return s;
285 void
286 appendable_list_append (SCM l, SCM elt)
288 SCM newcons = gh_cons (elt, SCM_EOL);
290 gh_set_cdr_x (ly_car (l), newcons);
291 gh_set_car_x (l, newcons);
296 ly_offset2scm (Offset o)
298 return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm (o[Y_AXIS]));
301 Offset
302 ly_scm2offset (SCM s)
304 return Offset (gh_scm2double (ly_car (s)),
305 gh_scm2double (ly_cdr (s)));
309 LY_DEFINE(ly_number2string, "ly:number->string", 1, 0,0,
310 (SCM s),
311 " converts @var{num} to a string without generating many decimals. It "
312 "leaves a space at the end.")
314 SCM_ASSERT_TYPE (gh_number_p (s), s, SCM_ARG1, __FUNCTION__, "number");
316 char str[400]; // ugh.
318 if (scm_exact_p (s) == SCM_BOOL_F)
320 Real r (gh_scm2double (s));
322 if (my_isinf (r) || my_isnan (r))
324 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
325 r = 0.0;
328 sprintf (str, "%8.4f ", r);
330 else
332 sprintf (str, "%d ", gh_scm2int (s));
335 return scm_makfrom0str (str);
339 Undef this to see if GUILE GC is causing too many swaps.
342 //#define TEST_GC
344 #ifdef TEST_GC
345 #include <libguile/gc.h>
347 static void *
348 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
350 fprintf (stderr, "entering sweep\n");
353 static void *
354 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
356 fprintf (stderr, "leaving sweep\n");
358 #endif
361 #include "version.hh"
362 LY_DEFINE(ly_version, "ly:version", 0, 0, 0, (),
363 "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
365 char const* vs = "\' (" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
367 return gh_eval_str ((char*)vs);
370 LY_DEFINE(ly_unit, "ly:unit", 0, 0, 0, (),
371 "Return the unit used for lengths as a string.")
373 return scm_makfrom0str (INTERNAL_UNIT);
376 LY_DEFINE(ly_verbose, "ly:verbose", 0, 0, 0, (),
377 "Return whether lilypond is being run in verbose mode.")
379 return gh_bool2scm (verbose_global_b);
382 static void
383 init_functions ()
385 #ifdef TEST_GC
386 scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
387 scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
388 #endif
391 ADD_SCM_INIT_FUNC (funcs, init_functions);
394 ly_deep_copy (SCM src)
396 if (gh_pair_p (src))
398 return gh_cons (ly_deep_copy (ly_car (src)), ly_deep_copy (ly_cdr (src)));
400 else if (gh_vector_p (src))
402 int l = SCM_VECTOR_LENGTH (src);
403 SCM nv = scm_c_make_vector (l, SCM_UNDEFINED);
404 for (int i =0 ; i< l ; i++)
406 SCM si = gh_int2scm (i);
407 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
410 else
411 return src;
413 return src;
420 ly_assoc_chain (SCM key, SCM achain)
422 if (gh_pair_p (achain))
424 SCM handle = scm_assoc (key, ly_car (achain));
425 if (gh_pair_p (handle))
426 return handle;
427 else
428 return ly_assoc_chain (key, ly_cdr (achain));
430 else
431 return SCM_BOOL_F;
434 /* looks the key up in the cdrs of the alist-keys
435 - ignoring the car and ignoring non-pair keys.
436 Returns first match found, i.e.
438 alist = ((1 . 10)
439 ((1 . 2) . 11)
440 ((2 . 1) . 12)
441 ((3 . 0) . 13)
442 ((4 . 1) . 14) )
444 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
445 element with the cdr of the key = 1. In other words (alloc_cdr key)
446 corresponds to call
448 (alloc (anything . key))
454 ly_assoc_cdr (SCM key, SCM alist)
456 if (gh_pair_p (alist)) {
457 SCM trykey = ly_caar(alist);
458 if(gh_pair_p(trykey) && to_boolean(scm_equal_p(key,ly_cdr(trykey))))
459 return ly_car(alist);
460 else
461 return ly_assoc_cdr (key, ly_cdr (alist));
463 else
464 return SCM_BOOL_F;
468 LIST has the form "sym1 sym2 sym3\nsym4\nsym5"
470 i.e. \n and ' ' can be used interchangeably as separators.
473 parse_symbol_list (const char * list)
475 char * s = strdup (list);
476 char *orig = s;
477 SCM create_list = SCM_EOL;
479 for (char * p = s; *p; p++)
481 if (*p == '\n')
482 *p = ' ' ;
485 if (!s[0] )
486 s = 0;
490 while (s)
492 char *next = strchr (s, ' ');
493 if (next)
494 *next++ = 0;
496 create_list = gh_cons (ly_symbol2scm (s), create_list);
497 s = next;
500 free (orig);
501 return create_list;
506 ly_truncate_list (int k, SCM l )
508 if (k == 0)
510 l = SCM_EOL;
512 else
514 SCM s = l;
515 k--;
516 for (; gh_pair_p (s) && k--; s = ly_cdr (s))
519 if (gh_pair_p (s))
521 gh_set_cdr_x (s, SCM_EOL);
524 return l;
528 String
529 print_scm_val (SCM val)
531 String realval = ly_scm2string (ly_write2scm (val));
532 if (realval.length () > 200)
533 realval = realval.left_string (100) + "\n :\n :\n" + realval.right_string (100);
535 return realval;
538 bool
539 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
541 bool ok = true;
544 Always succeeds.
547 TODO: should remove #f from allowed vals?
549 if (val == SCM_EOL || val == SCM_BOOL_F)
550 return ok;
553 SCM type = SCM_EOL;
555 if (gh_symbol_p (sym))
556 type = scm_object_property (sym, type_symbol);
558 if (type != SCM_EOL && !gh_procedure_p (type))
560 warning (_f ("Can't find property type-check for `%s' (%s).",
561 ly_symbol2string (sym).to_str0 (),
562 ly_symbol2string (type_symbol).to_str0 ())
563 + " " + _ ("Perhaps you made a typing error?"));
565 /* Be strict when being anal :) */
566 if (internal_type_checking_global_b)
567 abort ();
569 warning (_ ("Doing assignment anyway."));
571 else
573 if (val != SCM_EOL
574 && gh_procedure_p (type)
575 && gh_call1 (type, val) == SCM_BOOL_F)
577 SCM errport = scm_current_error_port ();
578 ok = false;
579 SCM typefunc = scm_primitive_eval (ly_symbol2scm ("type-name"));
580 SCM type_name = gh_call1 (typefunc, type);
583 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
584 ly_symbol2string (sym).to_str0 (),
585 print_scm_val (val),
586 ly_scm2string (type_name).to_str0 ()).to_str0 (),
587 errport);
588 scm_puts ("\n", errport);
591 return ok;
595 /* some SCM abbrevs
597 zijn deze nou handig?
598 zijn ze er al in scheme, maar heten ze anders? */
601 /* Remove doubles from (sorted) list */
603 ly_unique (SCM list)
605 SCM unique = SCM_EOL;
606 for (SCM i = list; gh_pair_p (i); i = ly_cdr (i))
608 if (!gh_pair_p (ly_cdr (i))
609 || !gh_equal_p (ly_car (i), ly_cadr (i)))
610 unique = gh_cons (ly_car (i), unique);
612 return scm_reverse_x (unique, SCM_EOL);
615 /* tail add */
617 ly_snoc (SCM s, SCM list)
619 return gh_append2 (list, scm_list_n (s, SCM_UNDEFINED));
623 /* Split list at member s, removing s.
624 Return (BEFORE . AFTER) */
626 ly_split_list (SCM s, SCM list)
628 SCM before = SCM_EOL;
629 SCM after = list;
630 for (; gh_pair_p (after);)
632 SCM i = ly_car (after);
633 after = ly_cdr (after);
634 if (gh_equal_p (i, s))
635 break;
636 before = gh_cons (i, before);
638 return gh_cons ( scm_reverse_x (before, SCM_EOL), after);
643 void
644 taint (SCM *)
647 nop.
652 display stuff without using stack
655 display_list (SCM s)
657 SCM p = scm_current_output_port();
659 scm_puts ("(", p);
660 for (; gh_pair_p(s); s =gh_cdr(s))
662 scm_display (gh_car(s), p);
663 scm_puts (" ", p);
665 scm_puts (")", p);
666 return SCM_UNSPECIFIED;
669 Slice
670 int_list_to_slice (SCM l)
672 Slice s;
673 s.set_empty ();
674 for (; gh_pair_p (l); l = gh_cdr (l))
676 if (gh_number_p (gh_car (l)))
677 s.add_point (gh_scm2int (gh_car (l)));
680 return s;
687 Return I-th element, or last elt L. If I < 0, then we take the first
688 element.
690 PRE: length (L) > 0
693 robust_list_ref(int i, SCM l)
695 while (i-- > 0 && gh_pair_p (gh_cdr(l)))
696 l = gh_cdr (l);
698 return gh_car(l);