]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
small backport fixes.
[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 "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   vector<char> chars = gulp_file (s, size);
109   string result (&chars[0], chars.size ());
110
111   if (be_verbose_global)
112     progress_indication ("]");
113
114   return result;
115 }
116
117 extern "C" {
118   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
119   void
120   ly_display_scm (SCM s)
121   {
122     scm_display (s, scm_current_output_port ());
123     scm_newline (scm_current_output_port ());
124   }
125 };
126
127 string
128 ly_scm2string (SCM str)
129 {
130   assert (scm_is_string (str));
131   return string (scm_i_string_chars (str),
132                  (int) scm_i_string_length (str));
133 }
134
135 SCM
136 ly_string2scm (string const &str)
137 {
138   return scm_from_locale_stringn (str.c_str(),
139                                   str.length ());
140 }
141
142 char *
143 ly_scm2newstr (SCM str, size_t *lenp)
144 {
145   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
146
147   size_t len = scm_i_string_length (str);
148   if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
149     {
150       memcpy (new_str, scm_i_string_chars (str), len);
151       new_str[len] = '\0';
152
153       if (lenp)
154         *lenp = len;
155
156       return new_str;
157     }
158   return 0;
159 }
160
161 SCM
162 index_get_cell (SCM s, Direction d)
163 {
164
165   assert (d);
166   return (d == LEFT) ? scm_car (s) : scm_cdr (s);
167 }
168
169 SCM
170 index_set_cell (SCM s, Direction d, SCM v)
171 {
172   if (d == LEFT)
173     scm_set_car_x (s, v);
174   else if (d == RIGHT)
175     scm_set_cdr_x (s, v);
176   return s;
177 }
178
179 bool
180 is_number_pair (SCM p)
181 {
182   return scm_is_pair (p)
183     && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
184 }
185
186 typedef void (*Void_fptr) ();
187 vector<Void_fptr> *scm_init_funcs_;
188
189 void add_scm_init_func (void (*f) ())
190 {
191   if (!scm_init_funcs_)
192     scm_init_funcs_ = new vector<Void_fptr>;
193
194   scm_init_funcs_->push_back (f);
195 }
196
197 void
198 ly_init_ly_module (void *)
199 {
200   for (vsize i = scm_init_funcs_->size (); i--;)
201     (scm_init_funcs_->at (i)) ();
202
203   if (be_verbose_global)
204     {
205       progress_indication ("[");
206       scm_display (scm_c_eval_string ("(%search-load-path \"lily.scm\")"),
207                    scm_current_error_port ());
208       progress_indication ("]\n");
209     }
210
211   scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
212 }
213
214 SCM global_lily_module;
215
216 void
217 ly_c_init_guile ()
218 {
219   global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
220   scm_c_use_module ("lily");
221 }
222
223 unsigned int
224 ly_scm_hash (SCM s)
225 {
226   return scm_ihashv (s, ~1u);
227 }
228
229 bool
230 is_direction (SCM s)
231 {
232   if (scm_is_number (s))
233     {
234       int i = scm_to_int (s);
235       return i >= -1 && i <= 1;
236     }
237   return false;
238 }
239
240 bool
241 is_axis (SCM s)
242 {
243   if (scm_is_number (s))
244     {
245       int i = scm_to_int (s);
246       return i == 0 || i == 1;
247     }
248   return false;
249 }
250
251 Direction
252 to_dir (SCM s)
253 {
254   return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
255 }
256
257 Interval
258 ly_scm2interval (SCM p)
259 {
260   return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
261 }
262
263 Drul_array<Real>
264 ly_scm2realdrul (SCM p)
265 {
266   return Drul_array<Real> (scm_to_double (scm_car (p)),
267                            scm_to_double (scm_cdr (p)));
268 }
269
270 SCM
271 ly_interval2scm (Drul_array<Real> i)
272 {
273   return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
274 }
275
276 bool
277 to_boolean (SCM s)
278 {
279   return scm_is_bool (s) && ly_scm2bool (s);
280 }
281
282 /* Appendable list L: the cdr contains the list, the car the last cons
283    in the list.  */
284 SCM
285 appendable_list ()
286 {
287   SCM s = scm_cons (SCM_EOL, SCM_EOL);
288   scm_set_car_x (s, s);
289
290   return s;
291 }
292
293 void
294 appendable_list_append (SCM l, SCM elt)
295 {
296   SCM newcons = scm_cons (elt, SCM_EOL);
297
298   scm_set_cdr_x (scm_car (l), newcons);
299   scm_set_car_x (l, newcons);
300 }
301
302 SCM
303 ly_offset2scm (Offset o)
304 {
305   return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
306 }
307
308 Offset
309 ly_scm2offset (SCM s)
310 {
311   return Offset (scm_to_double (scm_car (s)),
312                  scm_to_double (scm_cdr (s)));
313 }
314
315 SCM
316 ly_deep_copy (SCM src)
317 {
318   if (scm_is_pair (src))
319     return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
320   else if (scm_is_vector (src))
321     {
322       int len = scm_c_vector_length (src);
323       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
324       for (int i = 0;i < len; i++)
325         {
326           SCM si = scm_from_int (i);
327           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
328         }
329     }
330   return src;
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)
356           && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
357         return scm_car (alist);
358       return ly_assoc_cdr (key, scm_cdr (alist));
359     }
360   return SCM_BOOL_F;
361 }
362
363 SCM
364 ly_string_array_to_scm (vector<string> a)
365 {
366   SCM s = SCM_EOL;
367   for (vsize i = a.size (); i ; i--)
368     s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
369   return s;
370 }
371
372 /* SYMBOLS is a whitespace separated list.  */
373 SCM
374 parse_symbol_list (char const *symbols)
375 {
376   while (isspace (*symbols))
377     *symbols++;
378   string s = symbols;
379   replace_all (s, '\n', ' ');
380   replace_all (s, '\t', ' ');
381   return ly_string_array_to_scm (string_split (s, ' '));
382 }
383
384 SCM
385 ly_truncate_list (int k, SCM lst)
386 {
387   if (k == 0)
388     lst = SCM_EOL;
389   else
390     {
391       SCM s = lst;
392       k--;
393       for (; scm_is_pair (s) && k--; s = scm_cdr (s))
394         ;
395
396       if (scm_is_pair (s))
397         scm_set_cdr_x (s, SCM_EOL);
398     }
399   return lst;
400 }
401
402 string
403 print_scm_val (SCM val)
404 {
405   string realval = ly_scm2string (ly_write2scm (val));
406   if (realval.length () > 200)
407     realval = realval.substr (0, 100)
408       + "\n :\n :\n"
409       + realval.substr (realval.length () - 100);
410   return realval;
411 }
412
413 bool
414 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
415 {
416   bool ok = true;
417
418   /*
419     Always succeeds.
420
421
422     TODO: should remove #f from allowed vals?
423   */
424   if (val == SCM_EOL || val == SCM_BOOL_F)
425     return ok;
426
427   if (!scm_is_symbol (sym))
428 #if 0
429     return false;
430 #else
431   /*
432     This is used for autoBeamSettings.
433
434     TODO: deprecate the use of \override and \revert for
435     autoBeamSettings?
436
437     or use a symbol autoBeamSettingS?
438   */
439   return true;
440 #endif
441
442   SCM type = scm_object_property (sym, type_symbol);
443
444   if (type != SCM_EOL && !ly_is_procedure (type))
445     {
446       warning (_f ("can't find property type-check for `%s' (%s).",
447                    ly_symbol2string (sym).c_str (),
448                    ly_symbol2string (type_symbol).c_str ())
449                + "  " + _ ("perhaps a typing error?"));
450
451       /* Be strict when being anal :) */
452       if (do_internal_type_checking_global)
453         abort ();
454
455       warning (_ ("doing assignment anyway"));
456     }
457   else
458     {
459       if (val != SCM_EOL
460           && ly_is_procedure (type)
461           && scm_call_1 (type, val) == SCM_BOOL_F)
462         {
463           ok = false;
464           SCM typefunc = ly_lily_module_constant ("type-name");
465           SCM type_name = scm_call_1 (typefunc, type);
466
467           warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
468                        ly_symbol2string (sym).c_str (),
469                        print_scm_val (val),
470                        ly_scm2string (type_name).c_str ()));
471           progress_indication ("\n");
472         }
473     }
474   return ok;
475 }
476
477 /* some SCM abbrevs
478
479 zijn deze nou handig?
480 zijn ze er al in scheme, maar heten ze anders? */
481
482 /* Remove doubles from (sorted) list */
483 SCM
484 ly_unique (SCM list)
485 {
486   SCM unique = SCM_EOL;
487   for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
488     {
489       if (!scm_is_pair (scm_cdr (i))
490           || !ly_is_equal (scm_car (i), scm_cadr (i)))
491         unique = scm_cons (scm_car (i), unique);
492     }
493   return scm_reverse_x (unique, SCM_EOL);
494 }
495
496 static int
497 scm_default_compare (void const *a, void const *b)
498 {
499   SCM pa = *(SCM *) a;
500   SCM pb = *(SCM *) b;
501   if (pa == pb)
502     return 0;
503   return pa < pb ? -1 : 1;
504 }
505
506 /*  Modify LST in place: qsort it.
507
508 FIXME: unused, junk? */
509 SCM
510 ly_list_qsort_uniq_x (SCM lst)
511 {
512   int len = scm_ilength (lst);
513   SCM *arr = new SCM[len];
514   int k = 0;
515   for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
516     arr[k++] = scm_car (s);
517
518   assert (k == len);
519   qsort (arr, len, sizeof (SCM), &scm_default_compare);
520
521   SCM *tail = &lst;
522   for (int i = 0; i < len; i++)
523     if (!i || arr[i] != arr[i - 1])
524       {
525         SCM_SETCAR (*tail, arr[i]);
526         tail = SCM_CDRLOC (*tail);
527       }
528
529   *tail = SCM_EOL;
530   delete[] arr;
531
532   return lst;
533 }
534
535 /* tail add */
536 SCM
537 ly_snoc (SCM s, SCM list)
538 {
539   return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
540 }
541
542 /* Split list at member s, removing s.
543    Return (BEFORE . AFTER)  */
544 SCM
545 ly_split_list (SCM s, SCM list)
546 {
547   SCM before = SCM_EOL;
548   SCM after = list;
549   for (; scm_is_pair (after);)
550     {
551       SCM i = scm_car (after);
552       after = scm_cdr (after);
553       if (ly_is_equal (i, s))
554         break;
555       before = scm_cons (i, before);
556     }
557   return scm_cons (scm_reverse_x (before, SCM_EOL), after);
558 }
559
560 void
561 taint (SCM *)
562 {
563   /*
564     nop.
565   */
566 }
567
568 /*
569   display stuff without using stack
570 */
571 SCM
572 display_list (SCM s)
573 {
574   SCM p = scm_current_output_port ();
575
576   scm_puts ("(", p);
577   for (; scm_is_pair (s); s = scm_cdr (s))
578     {
579       scm_display (scm_car (s), p);
580       scm_puts (" ", p);
581     }
582   scm_puts (")", p);
583   return SCM_UNSPECIFIED;
584 }
585
586 Slice
587 int_list_to_slice (SCM l)
588 {
589   Slice s;
590   s.set_empty ();
591   for (; scm_is_pair (l); l = scm_cdr (l))
592     if (scm_is_number (scm_car (l)))
593       s.add_point (scm_to_int (scm_car (l)));
594   return s;
595 }
596
597 /* Return I-th element, or last elt L. If I < 0, then we take the first
598    element.
599
600    PRE: length (L) > 0  */
601 SCM
602 robust_list_ref (int i, SCM l)
603 {
604   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
605     l = scm_cdr (l);
606   return scm_car (l);
607 }
608
609 Real
610 robust_scm2double (SCM k, double x)
611 {
612   if (scm_is_number (k))
613     x = scm_to_double (k);
614   return x;
615 }
616
617 Direction
618 robust_scm2dir (SCM d, Direction def)
619 {
620   if (is_direction (d))
621     def = to_dir (d);
622   return def;
623 }
624
625 Interval
626 robust_scm2interval (SCM k, Drul_array<Real> v)
627 {
628   Interval i;
629   i[LEFT] = v[LEFT];
630   i[RIGHT] = v[RIGHT];
631   if (is_number_pair (k))
632     i = ly_scm2interval (k);
633   return i;
634 }
635
636 Drul_array<Real>
637 robust_scm2drul (SCM k, Drul_array<Real> v)
638 {
639   if (is_number_pair (k))
640     v = ly_scm2interval (k);
641   return v;
642 }
643
644 Drul_array<bool>
645 robust_scm2booldrul (SCM k, Drul_array<bool> def)
646 {
647   if (scm_is_pair (k))
648     {
649       def[LEFT] = to_boolean (scm_car (k));
650       def[RIGHT] = to_boolean (scm_cdr (k));
651     }
652   return def;
653 }
654
655 Offset
656 robust_scm2offset (SCM k, Offset o)
657 {
658   if (is_number_pair (k))
659     o = ly_scm2offset (k);
660   return o;
661 }
662
663 string
664 robust_scm2string (SCM k, string s)
665 {
666   if (scm_is_string (k))
667     s = ly_scm2string (k);
668   return s;
669 }
670
671 int
672 robust_scm2int (SCM k, int o)
673 {
674   if (scm_integer_p (k) == SCM_BOOL_T)
675     o = scm_to_int (k);
676   return o;
677 }
678
679 SCM
680 alist_to_hashq (SCM alist)
681 {
682   int i = scm_ilength (alist);
683   if (i < 0)
684     return scm_c_make_hash_table (0);
685
686   SCM tab = scm_c_make_hash_table (i);
687   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
688     {
689       SCM pt = scm_cdar (s);
690       scm_hashq_set_x (tab, scm_caar (s), pt);
691     }
692   return tab;
693 }
694
695 bool
696 alist_equal_p (SCM a, SCM b)
697 {
698   for (SCM s = a;
699        scm_is_pair (s); s = scm_cdr (s))
700     {
701       SCM key = scm_caar (s);
702       SCM val = scm_cdar (s);
703       SCM l = scm_assoc (key, b);
704
705       if (l == SCM_BOOL_F
706           || !ly_is_equal (scm_cdr (l), val))
707
708         return false;
709     }
710   return true;
711 }
712
713 SCM
714 ly_alist_vals (SCM alist)
715 {
716   SCM x = SCM_EOL;
717   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
718     x = scm_cons (scm_cdar (p), x);
719   return x;
720 }
721
722 SCM
723 ly_hash2alist (SCM tab)
724 {
725   SCM func = ly_lily_module_constant ("hash-table->alist");
726   return scm_call_1 (func, tab);
727 }
728
729 int
730 procedure_arity (SCM proc)
731 {
732   assert (ly_is_procedure (proc));
733   SCM arity = scm_procedure_property (proc,
734                                       ly_symbol2scm ("arity"));
735
736   SCM fixed = scm_car (arity);
737   return scm_to_int (fixed);
738 }
739
740 string
741 mangle_cxx_identifier (string cxx_id)
742 {
743   if (cxx_id.substr (0, 3) == "ly_")
744     cxx_id = cxx_id.replace (0, 3, "ly:");
745   else
746     {
747       cxx_id = String_convert::to_lower (cxx_id);
748       cxx_id = "ly:" + cxx_id;
749     }
750   if (cxx_id.substr (cxx_id.length () - 2) == "_p")
751     cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "?");
752   else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
753     cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "!");
754
755   cxx_id = replace_all (cxx_id, '_', '-');
756   return cxx_id;
757 }
758