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