]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
initial.
[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--2006 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 "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 "string-convert.hh"
29 #include "source-file.hh"
30 #include "version.hh"
31 #include "warn.hh"
32
33 // #define TEST_GC
34
35 SCM
36 ly_to_symbol (SCM scm)
37 {
38   return scm_string_to_symbol (ly_to_string (scm));
39 }
40
41 SCM
42 ly_to_string (SCM scm)
43 {
44   return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
45
46                      scm_makfrom0str ("~S"), scm);
47 }
48
49 SCM
50 ly_last (SCM list)
51 {
52   return scm_car (scm_last_pair (list));
53 }
54
55 SCM
56 ly_write2scm (SCM s)
57 {
58   SCM port = scm_mkstrport (SCM_INUM0,
59                             scm_make_string (SCM_INUM0, SCM_UNDEFINED),
60                             SCM_OPN | SCM_WRTNG,
61                             "ly_write2string");
62   //  SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
63   SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
64
65   // scm_apply (write, port, SCM_EOL);
66   scm_call_2 (write, s, port);
67   return scm_strport_to_string (port);
68 }
69
70 SCM
71 ly_quote_scm (SCM s)
72 {
73   return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
74 }
75
76 string
77 ly_symbol2string (SCM s)
78 {
79   /*
80     Ugh. this is not very efficient.
81   */
82   SCM str = scm_symbol_to_string (s);
83   return ly_scm2string (str);
84 }
85
86 string
87 gulp_file_to_string (string fn, bool must_exist, int size)
88 {
89   string s = global_path.find (fn);
90   if (s == "")
91     {
92       if (must_exist)
93         {
94           string e = _f (cannot find file: `%s'", fn);
95           e += " ";
96           e += _f ("(load path: `%s')", global_path.to_string ());
97           error (e);
98           /* unreachable */
99         }
100       return s;
101     }
102
103   if (be_verbose_global)
104     progress_indication ("[" + s);
105
106   vector<char> chars = gulp_file (s, size);
107   string result (&chars[0], chars.size ());
108
109   if (be_verbose_global)
110     progress_indication ("]");
111
112   return result;
113 }
114
115 extern "C" {
116   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
117   void
118   ly_display_scm (SCM s)
119   {
120     scm_display (s, scm_current_output_port ());
121     scm_newline (scm_current_output_port ());
122   }
123 };
124
125 string
126 ly_scm2string (SCM str)
127 {
128   assert (scm_is_string (str));
129   return string (scm_i_string_chars (str),
130                  (int) scm_i_string_length (str));
131 }
132
133 char *
134 ly_scm2newstr (SCM str, size_t *lenp)
135 {
136   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
137
138   size_t len = scm_i_string_length (str);
139   if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
140     {
141       memcpy (new_str, scm_i_string_chars (str), len);
142       new_str[len] = '\0';
143
144       if (lenp)
145         *lenp = len;
146
147       return new_str;
148     }
149   return 0;
150 }
151
152 SCM
153 index_get_cell (SCM s, Direction d)
154 {
155
156   assert (d);
157   return (d == LEFT) ? scm_car (s) : scm_cdr (s);
158 }
159
160 SCM
161 index_set_cell (SCM s, Direction d, SCM v)
162 {
163   if (d == LEFT)
164     scm_set_car_x (s, v);
165   else if (d == RIGHT)
166     scm_set_cdr_x (s, v);
167   return s;
168 }
169
170 bool
171 is_number_pair (SCM p)
172 {
173   return scm_is_pair (p)
174     && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
175 }
176
177 typedef void (*Void_fptr) ();
178 vector<Void_fptr> *scm_init_funcs_;
179
180 void add_scm_init_func (void (*f) ())
181 {
182   if (!scm_init_funcs_)
183     scm_init_funcs_ = new vector<Void_fptr>;
184
185   scm_init_funcs_->push_back (f);
186 }
187
188 void
189 ly_init_ly_module (void *)
190 {
191   for (vsize i = scm_init_funcs_->size (); i--;)
192     (scm_init_funcs_->at (i)) ();
193
194   if (be_verbose_global)
195     {
196       progress_indication ("[");
197       scm_display (scm_c_eval_string ("(%search-load-path \"lily.scm\")"),
198                    scm_current_error_port ());
199       progress_indication ("]\n");
200     }
201
202   scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
203 }
204
205 SCM global_lily_module;
206
207 void
208 ly_c_init_guile ()
209 {
210   global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
211   scm_c_use_module ("lily");
212 }
213
214 unsigned int
215 ly_scm_hash (SCM s)
216 {
217   return scm_ihashv (s, ~1u);
218 }
219
220 bool
221 is_direction (SCM s)
222 {
223   if (scm_is_number (s))
224     {
225       int i = scm_to_int (s);
226       return i >= -1 && i <= 1;
227     }
228   return false;
229 }
230
231 bool
232 is_axis (SCM s)
233 {
234   if (scm_is_number (s))
235     {
236       int i = scm_to_int (s);
237       return i == 0 || i == 1;
238     }
239   return false;
240 }
241
242 Direction
243 to_dir (SCM s)
244 {
245   return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
246 }
247
248 Interval
249 ly_scm2interval (SCM p)
250 {
251   return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
252 }
253
254 Drul_array<Real>
255 ly_scm2realdrul (SCM p)
256 {
257   return Drul_array<Real> (scm_to_double (scm_car (p)),
258                            scm_to_double (scm_cdr (p)));
259 }
260
261 SCM
262 ly_interval2scm (Drul_array<Real> i)
263 {
264   return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
265 }
266
267 bool
268 to_boolean (SCM s)
269 {
270   return scm_is_bool (s) && ly_scm2bool (s);
271 }
272
273 /* Appendable list L: the cdr contains the list, the car the last cons
274    in the list.  */
275 SCM
276 appendable_list ()
277 {
278   SCM s = scm_cons (SCM_EOL, SCM_EOL);
279   scm_set_car_x (s, s);
280
281   return s;
282 }
283
284 void
285 appendable_list_append (SCM l, SCM elt)
286 {
287   SCM newcons = scm_cons (elt, SCM_EOL);
288
289   scm_set_cdr_x (scm_car (l), newcons);
290   scm_set_car_x (l, newcons);
291 }
292
293 SCM
294 ly_offset2scm (Offset o)
295 {
296   return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
297 }
298
299 Offset
300 ly_scm2offset (SCM s)
301 {
302   return Offset (scm_to_double (scm_car (s)),
303                  scm_to_double (scm_cdr (s)));
304 }
305
306 SCM
307 ly_offsets2scm (vector<Offset> os)
308 {
309   SCM l = SCM_EOL;
310   SCM *tail = &l;
311   for (vsize i = 0; i < os.size (); i++)
312     {
313       *tail = scm_cons (ly_offset2scm (os[i]), SCM_EOL);
314       tail = SCM_CDRLOC(*tail);
315     }
316   return l;
317 }
318
319 vector<Offset>
320 ly_scm2offsets (SCM s)
321 {
322   vector<Offset> os;
323   for (; scm_is_pair (s); s = scm_cdr (s))
324     os.push_back (ly_scm2offset (scm_car (s)));
325   return os;
326 }
327
328 SCM
329 ly_deep_copy (SCM src)
330 {
331   if (scm_is_pair (src))
332     return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
333   else if (scm_is_vector (src))
334     {
335       int len = scm_c_vector_length (src);
336       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
337       for (int i = 0;i < len; i++)
338         {
339           SCM si = scm_from_int (i);
340           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
341         }
342     }
343   return src;
344 }
345
346 /* looks the key up in the cdrs of the alist-keys
347    - ignoring the car and ignoring non-pair keys.
348    Returns first match found, i.e.
349
350    alist = ((1 . 10)
351    ((1 . 2) . 11)
352    ((2 . 1) . 12)
353    ((3 . 0) . 13)
354    ((4 . 1) . 14) )
355
356    I would like (ly_assoc_cdr 1) to return 12 - because it's the first
357    element with the cdr of the key = 1.  In other words (alloc_cdr key)
358    corresponds to call
359
360    (alloc (anything . key))
361 */
362 SCM
363 ly_assoc_cdr (SCM key, SCM alist)
364 {
365   if (scm_is_pair (alist))
366     {
367       SCM trykey = scm_caar (alist);
368       if (scm_is_pair (trykey)
369           && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
370         return scm_car (alist);
371       return ly_assoc_cdr (key, scm_cdr (alist));
372     }
373   return SCM_BOOL_F;
374 }
375
376 SCM
377 ly_string_array_to_scm (vector<string> a)
378 {
379   SCM s = SCM_EOL;
380   for (vsize i = a.size (); i ; i--)
381     s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
382   return s;
383 }
384
385 /* SYMBOLS is a whitespace separated list.  */
386 SCM
387 parse_symbol_list (char const *symbols)
388 {
389   while (isspace (*symbols))
390     *symbols++;
391   string s = symbols;
392   replace_all (s, '\n', ' ');
393   replace_all (s, '\t', ' ');
394   return ly_string_array_to_scm (string_split (s, ' '));
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.substr (0, 100)
421       + "\n :\n :\n"
422       + realval.substr (realval.length () - 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 (cannot find property type-check for `%s' (%s).",
460                    ly_symbol2string (sym).c_str (),
461                    ly_symbol2string (type_symbol).c_str ())
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           warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
481                        ly_symbol2string (sym).c_str (),
482                        print_scm_val (val),
483                        ly_scm2string (type_name).c_str ()));
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
521 FIXME: unused, junk? */
522 SCM
523 ly_list_qsort_uniq_x (SCM lst)
524 {
525   int len = scm_ilength (lst);
526   SCM *arr = new SCM[len];
527   int k = 0;
528   for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
529     arr[k++] = scm_car (s);
530
531   assert (k == len);
532   qsort (arr, len, sizeof (SCM), &scm_default_compare);
533
534   SCM *tail = &lst;
535   for (int i = 0; i < len; i++)
536     if (!i || arr[i] != arr[i - 1])
537       {
538         SCM_SETCAR (*tail, arr[i]);
539         tail = SCM_CDRLOC (*tail);
540       }
541
542   *tail = SCM_EOL;
543   delete[] arr;
544
545   return lst;
546 }
547
548 /* tail add */
549 SCM
550 ly_snoc (SCM s, SCM list)
551 {
552   return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
553 }
554
555 /* Split list at member s, removing s.
556    Return (BEFORE . AFTER)  */
557 SCM
558 ly_split_list (SCM s, SCM list)
559 {
560   SCM before = SCM_EOL;
561   SCM after = list;
562   for (; scm_is_pair (after);)
563     {
564       SCM i = scm_car (after);
565       after = scm_cdr (after);
566       if (ly_is_equal (i, s))
567         break;
568       before = scm_cons (i, before);
569     }
570   return scm_cons (scm_reverse_x (before, SCM_EOL), after);
571 }
572
573 void
574 taint (SCM *)
575 {
576   /*
577     nop.
578   */
579 }
580
581 /*
582   display stuff without using stack
583 */
584 SCM
585 display_list (SCM s)
586 {
587   SCM p = scm_current_output_port ();
588
589   scm_puts ("(", p);
590   for (; scm_is_pair (s); s = scm_cdr (s))
591     {
592       scm_display (scm_car (s), p);
593       scm_puts (" ", p);
594     }
595   scm_puts (")", p);
596   return SCM_UNSPECIFIED;
597 }
598
599 Slice
600 int_list_to_slice (SCM l)
601 {
602   Slice s;
603   s.set_empty ();
604   for (; scm_is_pair (l); l = scm_cdr (l))
605     if (scm_is_number (scm_car (l)))
606       s.add_point (scm_to_int (scm_car (l)));
607   return s;
608 }
609
610 /* Return I-th element, or last elt L. If I < 0, then we take the first
611    element.
612
613    PRE: length (L) > 0  */
614 SCM
615 robust_list_ref (int i, SCM l)
616 {
617   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
618     l = scm_cdr (l);
619   return scm_car (l);
620 }
621
622 Real
623 robust_scm2double (SCM k, double x)
624 {
625   if (scm_is_number (k))
626     x = scm_to_double (k);
627   return x;
628 }
629
630 Direction
631 robust_scm2dir (SCM d, Direction def)
632 {
633   if (is_direction (d))
634     def = to_dir (d);
635   return def;
636 }
637
638 Interval
639 robust_scm2interval (SCM k, Drul_array<Real> v)
640 {
641   Interval i;
642   i[LEFT] = v[LEFT];
643   i[RIGHT] = v[RIGHT];
644   if (is_number_pair (k))
645     i = ly_scm2interval (k);
646   return i;
647 }
648
649 Drul_array<Real>
650 robust_scm2drul (SCM k, Drul_array<Real> v)
651 {
652   if (is_number_pair (k))
653     v = ly_scm2interval (k);
654   return v;
655 }
656
657 Drul_array<bool>
658 robust_scm2booldrul (SCM k, Drul_array<bool> def)
659 {
660   if (scm_is_pair (k))
661     {
662       def[LEFT] = to_boolean (scm_car (k));
663       def[RIGHT] = to_boolean (scm_cdr (k));
664     }
665   return def;
666 }
667
668 Offset
669 robust_scm2offset (SCM k, Offset o)
670 {
671   if (is_number_pair (k))
672     o = ly_scm2offset (k);
673   return o;
674 }
675
676 string
677 robust_scm2string (SCM k, string s)
678 {
679   if (scm_is_string (k))
680     s = ly_scm2string (k);
681   return s;
682 }
683
684 int
685 robust_scm2int (SCM k, int o)
686 {
687   if (scm_integer_p (k) == SCM_BOOL_T)
688     o = scm_to_int (k);
689   return o;
690 }
691
692 SCM
693 alist_to_hashq (SCM alist)
694 {
695   int i = scm_ilength (alist);
696   if (i < 0)
697     return scm_c_make_hash_table (0);
698
699   SCM tab = scm_c_make_hash_table (i);
700   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
701     {
702       SCM pt = scm_cdar (s);
703       scm_hashq_set_x (tab, scm_caar (s), pt);
704     }
705   return tab;
706 }
707
708 bool
709 alist_equal_p (SCM a, SCM b)
710 {
711   for (SCM s = a;
712        scm_is_pair (s); s = scm_cdr (s))
713     {
714       SCM key = scm_caar (s);
715       SCM val = scm_cdar (s);
716       SCM l = scm_assoc (key, b);
717
718       if (l == SCM_BOOL_F
719           || !ly_is_equal (scm_cdr (l), val))
720
721         return false;
722     }
723   return true;
724 }
725
726 SCM
727 ly_alist_vals (SCM alist)
728 {
729   SCM x = SCM_EOL;
730   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
731     x = scm_cons (scm_cdar (p), x);
732   return x;
733 }
734
735 SCM
736 ly_hash2alist (SCM tab)
737 {
738   SCM func = ly_lily_module_constant ("hash-table->alist");
739   return scm_call_1 (func, tab);
740 }
741
742 int
743 procedure_arity (SCM proc)
744 {
745   assert (ly_is_procedure (proc));
746   SCM arity = scm_procedure_property (proc,
747                                       ly_symbol2scm ("arity"));
748
749   SCM fixed = scm_car (arity);
750   return scm_to_int (fixed);
751 }
752
753 string
754 mangle_cxx_identifier (string cxx_id)
755 {
756   if (cxx_id.substr (0, 3) == "ly_")
757     cxx_id = cxx_id.replace (0, 3, "ly:");
758   else
759     {
760       cxx_id = String_convert::to_lower (cxx_id);
761       cxx_id = "ly:" + cxx_id;
762     }
763   if (cxx_id.substr (cxx_id.length () - 2) == "_p")
764     cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "?");
765   else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
766     cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "!");
767
768   cxx_id = replace_all (cxx_id, '_', '-');
769   return cxx_id;
770 }
771