]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
trim duplicate headers.
[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 ("can't 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_deep_copy (SCM src)
308 {
309   if (scm_is_pair (src))
310     return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
311   else if (scm_is_vector (src))
312     {
313       int len = scm_c_vector_length (src);
314       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
315       for (int i = 0;i < len; i++)
316         {
317           SCM si = scm_from_int (i);
318           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
319         }
320     }
321   return src;
322 }
323
324 /* looks the key up in the cdrs of the alist-keys
325    - ignoring the car and ignoring non-pair keys.
326    Returns first match found, i.e.
327
328    alist = ((1 . 10)
329    ((1 . 2) . 11)
330    ((2 . 1) . 12)
331    ((3 . 0) . 13)
332    ((4 . 1) . 14) )
333
334    I would like (ly_assoc_cdr 1) to return 12 - because it's the first
335    element with the cdr of the key = 1.  In other words (alloc_cdr key)
336    corresponds to call
337
338    (alloc (anything . key))
339 */
340 SCM
341 ly_assoc_cdr (SCM key, SCM alist)
342 {
343   if (scm_is_pair (alist))
344     {
345       SCM trykey = scm_caar (alist);
346       if (scm_is_pair (trykey)
347           && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
348         return scm_car (alist);
349       return ly_assoc_cdr (key, scm_cdr (alist));
350     }
351   return SCM_BOOL_F;
352 }
353
354 SCM
355 ly_string_array_to_scm (vector<string> a)
356 {
357   SCM s = SCM_EOL;
358   for (vsize i = a.size (); i ; i--)
359     s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
360   return s;
361 }
362
363 /* SYMBOLS is a whitespace separated list.  */
364 SCM
365 parse_symbol_list (char const *symbols)
366 {
367   while (isspace (*symbols))
368     *symbols++;
369   string s = symbols;
370   replace_all (s, '\n', ' ');
371   replace_all (s, '\t', ' ');
372   return ly_string_array_to_scm (string_split (s, ' '));
373 }
374
375 SCM
376 ly_truncate_list (int k, SCM lst)
377 {
378   if (k == 0)
379     lst = SCM_EOL;
380   else
381     {
382       SCM s = lst;
383       k--;
384       for (; scm_is_pair (s) && k--; s = scm_cdr (s))
385         ;
386
387       if (scm_is_pair (s))
388         scm_set_cdr_x (s, SCM_EOL);
389     }
390   return lst;
391 }
392
393 string
394 print_scm_val (SCM val)
395 {
396   string realval = ly_scm2string (ly_write2scm (val));
397   if (realval.length () > 200)
398     realval = realval.substr (0, 100)
399       + "\n :\n :\n"
400       + realval.substr (realval.length () - 100);
401   return realval;
402 }
403
404 bool
405 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
406 {
407   bool ok = true;
408
409   /*
410     Always succeeds.
411
412
413     TODO: should remove #f from allowed vals?
414   */
415   if (val == SCM_EOL || val == SCM_BOOL_F)
416     return ok;
417
418   if (!scm_is_symbol (sym))
419 #if 0
420     return false;
421 #else
422   /*
423     This is used for autoBeamSettings.
424
425     TODO: deprecate the use of \override and \revert for
426     autoBeamSettings?
427
428     or use a symbol autoBeamSettingS?
429   */
430   return true;
431 #endif
432
433   SCM type = scm_object_property (sym, type_symbol);
434
435   if (type != SCM_EOL && !ly_is_procedure (type))
436     {
437       warning (_f ("can't find property type-check for `%s' (%s).",
438                    ly_symbol2string (sym).c_str (),
439                    ly_symbol2string (type_symbol).c_str ())
440                + "  " + _ ("perhaps a typing error?"));
441
442       /* Be strict when being anal :) */
443       if (do_internal_type_checking_global)
444         abort ();
445
446       warning (_ ("doing assignment anyway"));
447     }
448   else
449     {
450       if (val != SCM_EOL
451           && ly_is_procedure (type)
452           && scm_call_1 (type, val) == SCM_BOOL_F)
453         {
454           ok = false;
455           SCM typefunc = ly_lily_module_constant ("type-name");
456           SCM type_name = scm_call_1 (typefunc, type);
457
458           warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
459                        ly_symbol2string (sym).c_str (),
460                        print_scm_val (val),
461                        ly_scm2string (type_name).c_str ()));
462           progress_indication ("\n");
463         }
464     }
465   return ok;
466 }
467
468 /* some SCM abbrevs
469
470 zijn deze nou handig?
471 zijn ze er al in scheme, maar heten ze anders? */
472
473 /* Remove doubles from (sorted) list */
474 SCM
475 ly_unique (SCM list)
476 {
477   SCM unique = SCM_EOL;
478   for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
479     {
480       if (!scm_is_pair (scm_cdr (i))
481           || !ly_is_equal (scm_car (i), scm_cadr (i)))
482         unique = scm_cons (scm_car (i), unique);
483     }
484   return scm_reverse_x (unique, SCM_EOL);
485 }
486
487 static int
488 scm_default_compare (void const *a, void const *b)
489 {
490   SCM pa = *(SCM *) a;
491   SCM pb = *(SCM *) b;
492   if (pa == pb)
493     return 0;
494   return pa < pb ? -1 : 1;
495 }
496
497 /*  Modify LST in place: qsort it.
498
499 FIXME: unused, junk? */
500 SCM
501 ly_list_qsort_uniq_x (SCM lst)
502 {
503   int len = scm_ilength (lst);
504   SCM *arr = new SCM[len];
505   int k = 0;
506   for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
507     arr[k++] = scm_car (s);
508
509   assert (k == len);
510   qsort (arr, len, sizeof (SCM), &scm_default_compare);
511
512   SCM *tail = &lst;
513   for (int i = 0; i < len; i++)
514     if (!i || arr[i] != arr[i - 1])
515       {
516         SCM_SETCAR (*tail, arr[i]);
517         tail = SCM_CDRLOC (*tail);
518       }
519
520   *tail = SCM_EOL;
521   delete[] arr;
522
523   return lst;
524 }
525
526 /* tail add */
527 SCM
528 ly_snoc (SCM s, SCM list)
529 {
530   return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
531 }
532
533 /* Split list at member s, removing s.
534    Return (BEFORE . AFTER)  */
535 SCM
536 ly_split_list (SCM s, SCM list)
537 {
538   SCM before = SCM_EOL;
539   SCM after = list;
540   for (; scm_is_pair (after);)
541     {
542       SCM i = scm_car (after);
543       after = scm_cdr (after);
544       if (ly_is_equal (i, s))
545         break;
546       before = scm_cons (i, before);
547     }
548   return scm_cons (scm_reverse_x (before, SCM_EOL), after);
549 }
550
551 void
552 taint (SCM *)
553 {
554   /*
555     nop.
556   */
557 }
558
559 /*
560   display stuff without using stack
561 */
562 SCM
563 display_list (SCM s)
564 {
565   SCM p = scm_current_output_port ();
566
567   scm_puts ("(", p);
568   for (; scm_is_pair (s); s = scm_cdr (s))
569     {
570       scm_display (scm_car (s), p);
571       scm_puts (" ", p);
572     }
573   scm_puts (")", p);
574   return SCM_UNSPECIFIED;
575 }
576
577 Slice
578 int_list_to_slice (SCM l)
579 {
580   Slice s;
581   s.set_empty ();
582   for (; scm_is_pair (l); l = scm_cdr (l))
583     if (scm_is_number (scm_car (l)))
584       s.add_point (scm_to_int (scm_car (l)));
585   return s;
586 }
587
588 /* Return I-th element, or last elt L. If I < 0, then we take the first
589    element.
590
591    PRE: length (L) > 0  */
592 SCM
593 robust_list_ref (int i, SCM l)
594 {
595   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
596     l = scm_cdr (l);
597   return scm_car (l);
598 }
599
600 Real
601 robust_scm2double (SCM k, double x)
602 {
603   if (scm_is_number (k))
604     x = scm_to_double (k);
605   return x;
606 }
607
608 Direction
609 robust_scm2dir (SCM d, Direction def)
610 {
611   if (is_direction (d))
612     def = to_dir (d);
613   return def;
614 }
615
616 Interval
617 robust_scm2interval (SCM k, Drul_array<Real> v)
618 {
619   Interval i;
620   i[LEFT] = v[LEFT];
621   i[RIGHT] = v[RIGHT];
622   if (is_number_pair (k))
623     i = ly_scm2interval (k);
624   return i;
625 }
626
627 Drul_array<Real>
628 robust_scm2drul (SCM k, Drul_array<Real> v)
629 {
630   if (is_number_pair (k))
631     v = ly_scm2interval (k);
632   return v;
633 }
634
635 Drul_array<bool>
636 robust_scm2booldrul (SCM k, Drul_array<bool> def)
637 {
638   if (scm_is_pair (k))
639     {
640       def[LEFT] = to_boolean (scm_car (k));
641       def[RIGHT] = to_boolean (scm_cdr (k));
642     }
643   return def;
644 }
645
646 Offset
647 robust_scm2offset (SCM k, Offset o)
648 {
649   if (is_number_pair (k))
650     o = ly_scm2offset (k);
651   return o;
652 }
653
654 string
655 robust_scm2string (SCM k, string s)
656 {
657   if (scm_is_string (k))
658     s = ly_scm2string (k);
659   return s;
660 }
661
662 int
663 robust_scm2int (SCM k, int o)
664 {
665   if (scm_integer_p (k) == SCM_BOOL_T)
666     o = scm_to_int (k);
667   return o;
668 }
669
670 SCM
671 alist_to_hashq (SCM alist)
672 {
673   int i = scm_ilength (alist);
674   if (i < 0)
675     return scm_c_make_hash_table (0);
676
677   SCM tab = scm_c_make_hash_table (i);
678   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
679     {
680       SCM pt = scm_cdar (s);
681       scm_hashq_set_x (tab, scm_caar (s), pt);
682     }
683   return tab;
684 }
685
686 bool
687 alist_equal_p (SCM a, SCM b)
688 {
689   for (SCM s = a;
690        scm_is_pair (s); s = scm_cdr (s))
691     {
692       SCM key = scm_caar (s);
693       SCM val = scm_cdar (s);
694       SCM l = scm_assoc (key, b);
695
696       if (l == SCM_BOOL_F
697           || !ly_is_equal (scm_cdr (l), val))
698
699         return false;
700     }
701   return true;
702 }
703
704 SCM
705 ly_alist_vals (SCM alist)
706 {
707   SCM x = SCM_EOL;
708   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
709     x = scm_cons (scm_cdar (p), x);
710   return x;
711 }
712
713 SCM
714 ly_hash2alist (SCM tab)
715 {
716   SCM func = ly_lily_module_constant ("hash-table->alist");
717   return scm_call_1 (func, tab);
718 }
719
720 int
721 procedure_arity (SCM proc)
722 {
723   assert (ly_is_procedure (proc));
724   SCM arity = scm_procedure_property (proc,
725                                       ly_symbol2scm ("arity"));
726
727   SCM fixed = scm_car (arity);
728   return scm_to_int (fixed);
729 }
730
731 string
732 mangle_cxx_identifier (string cxx_id)
733 {
734   if (cxx_id.substr (0, 3) == "ly_")
735     cxx_id = cxx_id.replace (0, 3, "ly:");
736   else
737     {
738       cxx_id = String_convert::to_lower (cxx_id);
739       cxx_id = "ly:" + cxx_id;
740     }
741   if (cxx_id.substr (cxx_id.length () - 2) == "_p")
742     cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "?");
743   else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
744     cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "!");
745
746   cxx_id = replace_all (cxx_id, '_', '-');
747   return cxx_id;
748 }
749