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