]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
* lily/main.cc (setup_guile_env): new function. Set GC min_yields
[lilypond.git] / lily / lily-guile.cc
1 /*
2   lily-guile.cc -- implement assorted SCM interface functions
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7   Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9
10 #include "lily-guile.hh"
11
12 #include <cstdio>
13 #include <cstdlib>
14 #include <cstring> /* strdup, strchr */
15 #include <cctype>
16
17 #include "config.hh"
18
19 #include "dimensions.hh"
20 #include "direction.hh"
21 #include "file-path.hh"
22 #include "international.hh"
23 #include "libc-extension.hh"
24 #include "main.hh"
25 #include "misc.hh"
26 #include "offset.hh"
27 #include "pitch.hh"
28 #include "source-file.hh"
29 #include "version.hh"
30 #include "warn.hh"
31
32 // #define TEST_GC
33
34 SCM
35 ly_to_symbol (SCM scm)
36 {
37   return scm_string_to_symbol (ly_to_string (scm));
38 }
39
40 SCM
41 ly_to_string (SCM scm)
42 {
43   return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
44
45                      scm_makfrom0str ("~S"), scm);
46 }
47
48 SCM
49 ly_last (SCM list)
50 {
51   return scm_car (scm_last_pair (list));
52 }
53
54 SCM
55 ly_write2scm (SCM s)
56 {
57   SCM port = scm_mkstrport (SCM_INUM0,
58                             scm_make_string (SCM_INUM0, SCM_UNDEFINED),
59                             SCM_OPN | SCM_WRTNG,
60                             "ly_write2string");
61   //  SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
62   SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
63
64   // scm_apply (write, port, SCM_EOL);
65   scm_call_2 (write, s, port);
66   return scm_strport_to_string (port);
67 }
68
69 SCM
70 ly_quote_scm (SCM s)
71 {
72   return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
73 }
74
75 String
76 ly_symbol2string (SCM s)
77 {
78   /*
79     Ugh. this is not very efficient.
80   */
81   SCM str = scm_symbol_to_string (s);
82   return ly_scm2string (str);
83 }
84
85 String
86 gulp_file_to_string (String fn, bool must_exist)
87 {
88   String s = global_path.find (fn);
89   if (s == "")
90     {
91       if (must_exist)
92         {
93           String e = _f ("can't find file: `%s'", fn);
94           e += " ";
95           e += _f ("(load path: `%s')", global_path.to_string ());
96           error (e);
97           /* unreachable */
98         }
99       return s;
100     }
101
102   if (be_verbose_global)
103     progress_indication ("[" + s);
104
105   int n;
106   char *str = gulp_file (s, &n);
107   String result ((Byte *) str, n);
108   delete[] str;
109
110   if (be_verbose_global)
111     progress_indication ("]");
112
113   return result;
114 }
115
116 extern "C" {
117   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
118   void
119   ly_display_scm (SCM s)
120   {
121     scm_display (s, scm_current_output_port ());
122     scm_newline (scm_current_output_port ());
123   }
124 };
125
126 String
127 ly_scm2string (SCM str)
128 {
129   assert (scm_is_string (str));
130   return String ((Byte *)scm_i_string_chars (str),
131                  (int) scm_i_string_length (str));
132 }
133
134 char *
135 ly_scm2newstr (SCM str, size_t *lenp)
136 {
137   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
138
139   size_t len = scm_i_string_length (str);
140   if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
141     {
142       memcpy (new_str, scm_i_string_chars (str), len);
143       new_str[len] = '\0';
144
145       if (lenp)
146         *lenp = len;
147       
148       return new_str;
149     }
150   return 0;
151 }
152
153 SCM
154 index_get_cell (SCM s, Direction d)
155 {
156   
157   assert (d);
158   return (d == LEFT) ? scm_car (s) : scm_cdr (s);
159 }
160
161 SCM
162 index_set_cell (SCM s, Direction d, SCM v)
163 {
164   if (d == LEFT)
165     scm_set_car_x (s, v);
166   else if (d == RIGHT)
167     scm_set_cdr_x (s, v);
168   return s;
169 }
170   
171 bool
172 is_number_pair (SCM p)
173 {
174   return scm_is_pair (p)
175     && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
176 }
177
178 typedef void (*Void_fptr) ();
179 Array<Void_fptr> *scm_init_funcs_;
180
181 void add_scm_init_func (void (*f) ())
182 {
183   if (!scm_init_funcs_)
184     scm_init_funcs_ = new Array<Void_fptr>;
185
186   scm_init_funcs_->push (f);
187 }
188
189 #if KPATHSEA
190 extern "C" {
191   void initialize_kpathsea ();
192 }
193 #endif
194
195 void
196 ly_init_ly_module (void *)
197 {
198   for (int i = scm_init_funcs_->size (); i--;)
199     (scm_init_funcs_->elem (i)) ();
200
201   if (be_verbose_global)
202     progress_indication ("\n");
203
204 #if KPATHSEA
205   if (is_TeX_format_global)
206     initialize_kpathsea ();
207 #endif
208
209   scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
210 }
211
212 SCM global_lily_module;
213
214 void
215 ly_c_init_guile ()
216 {
217   global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
218   scm_c_use_module ("lily");
219 }
220
221 unsigned int
222 ly_scm_hash (SCM s)
223 {
224   return scm_ihashv (s, ~1u);
225 }
226
227 bool
228 is_direction (SCM s)
229 {
230   if (scm_is_number (s))
231     {
232       int i = scm_to_int (s);
233       return i >= -1 && i <= 1;
234     }
235   return false;
236 }
237
238 bool
239 is_axis (SCM s)
240 {
241   if (scm_is_number (s))
242     {
243       int i = scm_to_int (s);
244       return i == 0 || i == 1;
245     }
246   return false;
247 }
248
249 Direction
250 to_dir (SCM s)
251 {
252   return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
253 }
254
255 Interval
256 ly_scm2interval (SCM p)
257 {
258   return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
259 }
260
261 Drul_array<Real>
262 ly_scm2realdrul (SCM p)
263 {
264   return Drul_array<Real> (scm_to_double (scm_car (p)),
265                            scm_to_double (scm_cdr (p)));
266 }
267
268 SCM
269 ly_interval2scm (Drul_array<Real> i)
270 {
271   return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
272 }
273
274 bool
275 to_boolean (SCM s)
276 {
277   return scm_is_bool (s) && ly_scm2bool (s);
278 }
279
280 /* Appendable list L: the cdr contains the list, the car the last cons
281    in the list.  */
282 SCM
283 appendable_list ()
284 {
285   SCM s = scm_cons (SCM_EOL, SCM_EOL);
286   scm_set_car_x (s, s);
287
288   return s;
289 }
290
291 void
292 appendable_list_append (SCM l, SCM elt)
293 {
294   SCM newcons = scm_cons (elt, SCM_EOL);
295
296   scm_set_cdr_x (scm_car (l), newcons);
297   scm_set_car_x (l, newcons);
298 }
299
300 SCM
301 ly_offset2scm (Offset o)
302 {
303   return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
304 }
305
306 Offset
307 ly_scm2offset (SCM s)
308 {
309   return Offset (scm_to_double (scm_car (s)),
310                  scm_to_double (scm_cdr (s)));
311 }
312
313 SCM
314 ly_deep_copy (SCM src)
315 {
316   if (scm_is_pair (src))
317     return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
318   else if (scm_is_vector (src))
319     {
320       int len = scm_c_vector_length (src);
321       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
322       for (int i = 0;i < len; i++)
323         {
324           SCM si = scm_from_int (i);
325           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
326         }
327     }
328   return src;
329 }
330
331
332
333 /* looks the key up in the cdrs of the alist-keys
334    - ignoring the car and ignoring non-pair keys.
335    Returns first match found, i.e.
336
337    alist = ((1 . 10)
338    ((1 . 2) . 11)
339    ((2 . 1) . 12)
340    ((3 . 0) . 13)
341    ((4 . 1) . 14) )
342
343    I would like (ly_assoc_cdr 1) to return 12 - because it's the first
344    element with the cdr of the key = 1.  In other words (alloc_cdr key)
345    corresponds to call
346
347    (alloc (anything . key))
348 */
349 SCM
350 ly_assoc_cdr (SCM key, SCM alist)
351 {
352   if (scm_is_pair (alist))
353     {
354       SCM trykey = scm_caar (alist);
355       if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
356         return scm_car (alist);
357       else
358         return ly_assoc_cdr (key, scm_cdr (alist));
359     }
360   return SCM_BOOL_F;
361 }
362
363 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
364    i.e. \n and ' ' can be used interchangeably as separators.  */
365 SCM
366 parse_symbol_list (char const *lst)
367 {
368   char *s = strdup (lst);
369   char *orig = s;
370   SCM create_list = SCM_EOL;
371
372   char *e = s + strlen (s) - 1;
373   while (e >= s && isspace (*e))
374     *e-- = 0;
375
376   for (char *p = s; *p; p++)
377     if (*p == '\n')
378       *p = ' ';
379   
380   if (!s[0])
381     s = 0;
382   
383   while (s)
384     {
385       char *next = strchr (s, ' ');
386       if (next)
387         *next++ = 0;
388
389       create_list = scm_cons (ly_symbol2scm (s), create_list);
390       s = next;
391     }
392
393   free (orig);
394   return create_list;
395 }
396
397 SCM
398 ly_truncate_list (int k, SCM lst)
399 {
400   if (k == 0)
401     lst = SCM_EOL;
402   else
403     {
404       SCM s = lst;
405       k--;
406       for (; scm_is_pair (s) && k--; s = scm_cdr (s))
407         ;
408
409       if (scm_is_pair (s))
410         scm_set_cdr_x (s, SCM_EOL);
411     }
412   return lst;
413 }
414
415 String
416 print_scm_val (SCM val)
417 {
418   String realval = ly_scm2string (ly_write2scm (val));
419   if (realval.length () > 200)
420     realval = realval.left_string (100)
421       + "\n :\n :\n"
422       + realval.right_string (100);
423   return realval;
424 }
425
426 bool
427 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
428 {
429   bool ok = true;
430
431   /*
432     Always succeeds.
433
434
435     TODO: should remove #f from allowed vals?
436   */
437   if (val == SCM_EOL || val == SCM_BOOL_F)
438     return ok;
439
440   if (!scm_is_symbol (sym))
441 #if 0
442     return false;
443 #else
444   /*
445     This is used for autoBeamSettings.
446
447     TODO: deprecate the use of \override and \revert for
448     autoBeamSettings?
449
450     or use a symbol autoBeamSettingS?
451   */
452   return true;
453 #endif
454
455   SCM type = scm_object_property (sym, type_symbol);
456
457   if (type != SCM_EOL && !ly_is_procedure (type))
458     {
459       warning (_f ("can't find property type-check for `%s' (%s).",
460                    ly_symbol2string (sym).to_str0 (),
461                    ly_symbol2string (type_symbol).to_str0 ())
462                + "  " + _ ("perhaps a typing error?"));
463
464       /* Be strict when being anal :) */
465       if (do_internal_type_checking_global)
466         abort ();
467
468       warning (_ ("doing assignment anyway"));
469     }
470   else
471     {
472       if (val != SCM_EOL
473           && ly_is_procedure (type)
474           && scm_call_1 (type, val) == SCM_BOOL_F)
475         {
476           ok = false;
477           SCM typefunc = ly_lily_module_constant ("type-name");
478           SCM type_name = scm_call_1 (typefunc, type);
479
480           message (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
481                        ly_symbol2string (sym).to_str0 (),
482                        print_scm_val (val),
483                        ly_scm2string (type_name).to_str0 ()));
484           progress_indication ("\n");
485         }
486     }
487   return ok;
488 }
489
490 /* some SCM abbrevs
491
492 zijn deze nou handig?
493 zijn ze er al in scheme, maar heten ze anders? */
494
495 /* Remove doubles from (sorted) list */
496 SCM
497 ly_unique (SCM list)
498 {
499   SCM unique = SCM_EOL;
500   for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
501     {
502       if (!scm_is_pair (scm_cdr (i))
503           || !ly_is_equal (scm_car (i), scm_cadr (i)))
504         unique = scm_cons (scm_car (i), unique);
505     }
506   return scm_reverse_x (unique, SCM_EOL);
507 }
508
509 static int
510 scm_default_compare (void const *a, void const *b)
511 {
512   SCM pa = *(SCM *) a;
513   SCM pb = *(SCM *) b;
514   if (pa == pb)
515     return 0;
516   return pa < pb ? -1 : 1;
517 }
518
519 /*  Modify LST in place: qsort it.  */
520 SCM
521 ly_list_qsort_uniq_x (SCM lst)
522 {
523   int len = scm_ilength (lst);
524   SCM *arr = new SCM[len];
525   int k = 0;
526   for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
527     arr[k++] = scm_car (s);
528
529   assert (k == len);
530   qsort (arr, len, sizeof (SCM), &scm_default_compare);
531
532   SCM *tail = &lst;
533   for (int i = 0; i < len; i++)
534     if (!i || arr[i] != arr[i - 1])
535       {
536         SCM_SETCAR (*tail, arr[i]);
537         tail = SCM_CDRLOC (*tail);
538       }
539
540   *tail = SCM_EOL;
541   delete[] arr;
542
543   return lst;
544 }
545
546 /* tail add */
547 SCM
548 ly_snoc (SCM s, SCM list)
549 {
550   return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
551 }
552
553 /* Split list at member s, removing s.
554    Return (BEFORE . AFTER)  */
555 SCM
556 ly_split_list (SCM s, SCM list)
557 {
558   SCM before = SCM_EOL;
559   SCM after = list;
560   for (; scm_is_pair (after);)
561     {
562       SCM i = scm_car (after);
563       after = scm_cdr (after);
564       if (ly_is_equal (i, s))
565         break;
566       before = scm_cons (i, before);
567     }
568   return scm_cons (scm_reverse_x (before, SCM_EOL), after);
569 }
570
571 void
572 taint (SCM *)
573 {
574   /*
575     nop.
576   */
577 }
578
579 /*
580   display stuff without using stack
581 */
582 SCM
583 display_list (SCM s)
584 {
585   SCM p = scm_current_output_port ();
586
587   scm_puts ("(", p);
588   for (; scm_is_pair (s); s = scm_cdr (s))
589     {
590       scm_display (scm_car (s), p);
591       scm_puts (" ", p);
592     }
593   scm_puts (")", p);
594   return SCM_UNSPECIFIED;
595 }
596
597 Slice
598 int_list_to_slice (SCM l)
599 {
600   Slice s;
601   s.set_empty ();
602   for (; scm_is_pair (l); l = scm_cdr (l))
603     if (scm_is_number (scm_car (l)))
604       s.add_point (scm_to_int (scm_car (l)));
605   return s;
606 }
607
608 /* Return I-th element, or last elt L. If I < 0, then we take the first
609    element.
610
611    PRE: length (L) > 0  */
612 SCM
613 robust_list_ref (int i, SCM l)
614 {
615   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
616     l = scm_cdr (l);
617   return scm_car (l);
618 }
619
620 Real
621 robust_scm2double (SCM k, double x)
622 {
623   if (scm_is_number (k))
624     x = scm_to_double (k);
625   return x;
626 }
627
628 Interval
629 robust_scm2interval (SCM k, Drul_array<Real> v)
630 {
631   Interval i;
632   i[LEFT] = v[LEFT];
633   i[RIGHT] = v[RIGHT];
634   if (is_number_pair (k))
635     i = ly_scm2interval (k);
636   return i;
637 }
638
639 Drul_array<Real>
640 robust_scm2drul (SCM k, Drul_array<Real> v)
641 {
642   if (is_number_pair (k))
643     v = ly_scm2interval (k);
644   return v;
645 }
646
647 Offset
648 robust_scm2offset (SCM k, Offset o)
649 {
650   if (is_number_pair (k))
651     o = ly_scm2offset (k);
652   return o;
653 }
654
655 int
656 robust_scm2int (SCM k, int o)
657 {
658   if (scm_integer_p (k) == SCM_BOOL_T)
659     o = scm_to_int (k);
660   return o;
661 }
662
663 SCM
664 alist_to_hashq (SCM alist)
665 {
666   int i = scm_ilength (alist);
667   if (i < 0)
668     return scm_c_make_hash_table (0);
669
670   SCM tab = scm_c_make_hash_table (i);
671   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
672     {
673       SCM pt = scm_cdar (s);
674       scm_hashq_set_x (tab, scm_caar (s), pt);
675     }
676   return tab;
677 }
678
679 bool
680 alist_equal_p (SCM a, SCM b)
681 {
682   for (SCM s = a;
683        scm_is_pair (s); s = scm_cdr (s))
684     {
685       SCM key = scm_caar (s);
686       SCM val = scm_cdar (s);
687       SCM l = scm_assoc (key, b);
688
689       if (l == SCM_BOOL_F
690           || !ly_is_equal (scm_cdr (l), val))
691
692         return false;
693     }
694   return true;
695 }
696
697 SCM
698 ly_alist_vals (SCM alist)
699 {
700   SCM x = SCM_EOL;
701   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
702     {
703       x = scm_cons (scm_cdar (p), x);
704     }
705   return x;
706 }
707
708 SCM
709 ly_hash2alist (SCM tab)
710 {
711   SCM func = ly_lily_module_constant ("hash-table->alist");
712   return scm_call_1 (func, tab);
713 }
714