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