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