]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
Merge with master
[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--2007 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
34 /*
35   symbols/strings.
36  */
37 SCM
38 ly_write2scm (SCM s)
39 {
40   SCM port = scm_mkstrport (SCM_INUM0,
41                             scm_make_string (SCM_INUM0, SCM_UNDEFINED),
42                             SCM_OPN | SCM_WRTNG,
43                             "ly_write2string");
44   //  SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
45   SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
46
47   // scm_apply (write, port, SCM_EOL);
48   scm_call_2 (write, s, port);
49   return scm_strport_to_string (port);
50 }
51
52 SCM
53 ly_quote_scm (SCM s)
54 {
55   return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
56 }
57
58 string
59 ly_symbol2string (SCM s)
60 {
61   /*
62     Ugh. this is not very efficient.
63   */
64   SCM str = scm_symbol_to_string (s);
65   return ly_scm2string (str);
66 }
67
68 string
69 gulp_file_to_string (string fn, bool must_exist, int size)
70 {
71   string s = global_path.find (fn);
72   if (s == "")
73     {
74       if (must_exist)
75         {
76           string e = _f ("cannot find file: `%s'", fn);
77           e += " ";
78           e += _f ("(load path: `%s')", global_path.to_string ());
79           error (e);
80           /* unreachable */
81         }
82       return s;
83     }
84
85   if (be_verbose_global)
86     progress_indication ("[" + s);
87
88   vector<char> chars = gulp_file (s, size);
89   string result (&chars[0], chars.size ());
90
91   if (be_verbose_global)
92     progress_indication ("]");
93
94   return result;
95 }
96
97 extern "C" {
98   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
99   void
100   ly_display_scm (SCM s)
101   {
102     scm_display (s, scm_current_output_port ());
103     scm_newline (scm_current_output_port ());
104   }
105 };
106
107 /*
108   STRINGS
109  */
110 string
111 ly_scm2string (SCM str)
112 {
113   assert (scm_is_string (str));
114   return string (scm_i_string_chars (str),
115                  (int) scm_i_string_length (str));
116 }
117
118 SCM
119 ly_string2scm (string const &str)
120 {
121   return scm_from_locale_stringn (str.c_str(),
122                                   str.length ());
123 }
124
125
126 char *
127 ly_scm2newstr (SCM str, size_t *lenp)
128 {
129   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
130
131   size_t len = scm_i_string_length (str);
132   if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
133     {
134       memcpy (new_str, scm_i_string_chars (str), len);
135       new_str[len] = '\0';
136
137       if (lenp)
138         *lenp = len;
139
140       return new_str;
141     }
142   return 0;
143 }
144
145
146 /*
147   PAIRS
148 */
149 SCM
150 index_get_cell (SCM s, Direction d)
151 {
152
153   assert (d);
154   return (d == LEFT) ? scm_car (s) : scm_cdr (s);
155 }
156
157 SCM
158 index_set_cell (SCM s, Direction d, SCM v)
159 {
160   if (d == LEFT)
161     scm_set_car_x (s, v);
162   else if (d == RIGHT)
163     scm_set_cdr_x (s, v);
164   return s;
165 }
166
167 bool
168 is_number_pair (SCM p)
169 {
170   return scm_is_pair (p)
171     && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
172 }
173
174
175 unsigned int
176 ly_scm_hash (SCM s)
177 {
178   return scm_ihashv (s, ~1u);
179 }
180
181
182 bool
183 is_axis (SCM s)
184 {
185   if (scm_is_number (s))
186     {
187       int i = scm_to_int (s);
188       return i == 0 || i == 1;
189     }
190   return false;
191 }
192
193 bool
194 to_boolean (SCM s)
195 {
196   return scm_is_bool (s) && ly_scm2bool (s);
197 }
198
199 /*
200   DIRECTIONS
201  */
202 Direction
203 to_dir (SCM s)
204 {
205   return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
206 }
207
208 Direction
209 robust_scm2dir (SCM d, Direction def)
210 {
211   if (is_direction (d))
212     def = to_dir (d);
213   return def;
214 }
215
216 bool
217 is_direction (SCM s)
218 {
219   if (scm_is_number (s))
220     {
221       int i = scm_to_int (s);
222       return i >= -1 && i <= 1;
223     }
224   return false;
225 }
226
227 /*
228   INTERVALS
229  */
230 Interval
231 ly_scm2interval (SCM p)
232 {
233   return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
234 }
235
236 Drul_array<Real>
237 ly_scm2realdrul (SCM p)
238 {
239   return Drul_array<Real> (scm_to_double (scm_car (p)),
240                            scm_to_double (scm_cdr (p)));
241 }
242
243 SCM
244 ly_interval2scm (Drul_array<Real> i)
245 {
246   return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
247 }
248
249
250 Interval
251 robust_scm2interval (SCM k, Drul_array<Real> v)
252 {
253   Interval i;
254   i[LEFT] = v[LEFT];
255   i[RIGHT] = v[RIGHT];
256   if (is_number_pair (k))
257     i = ly_scm2interval (k);
258   return i;
259 }
260
261 Drul_array<Real>
262 robust_scm2drul (SCM k, Drul_array<Real> v)
263 {
264   if (is_number_pair (k))
265     v = ly_scm2interval (k);
266   return v;
267 }
268
269 Drul_array<bool>
270 robust_scm2booldrul (SCM k, Drul_array<bool> def)
271 {
272   if (scm_is_pair (k))
273     {
274       def[LEFT] = to_boolean (scm_car (k));
275       def[RIGHT] = to_boolean (scm_cdr (k));
276     }
277   return def;
278 }
279
280 /*
281   OFFSET
282 */
283 SCM
284 ly_offset2scm (Offset o)
285 {
286   return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
287 }
288
289 Offset
290 ly_scm2offset (SCM s)
291 {
292   return Offset (scm_to_double (scm_car (s)),
293                  scm_to_double (scm_cdr (s)));
294 }
295
296 Offset
297 robust_scm2offset (SCM k, Offset o)
298 {
299   if (is_number_pair (k))
300     o = ly_scm2offset (k);
301   return o;
302 }
303 SCM
304 ly_offsets2scm (vector<Offset> os)
305 {
306   SCM l = SCM_EOL;
307   SCM *tail = &l;
308   for (vsize i = 0; i < os.size (); i++)
309     {
310       *tail = scm_cons (ly_offset2scm (os[i]), SCM_EOL);
311       tail = SCM_CDRLOC(*tail);
312     }
313   return l;
314 }
315
316 vector<Offset>
317 ly_scm2offsets (SCM s)
318 {
319   vector<Offset> os;
320   for (; scm_is_pair (s); s = scm_cdr (s))
321     os.push_back (ly_scm2offset (scm_car (s)));
322   return os;
323 }
324
325
326
327
328 /*
329   ALIST
330 */
331
332 bool
333 alist_equal_p (SCM a, SCM b)
334 {
335   for (SCM s = a;
336        scm_is_pair (s); s = scm_cdr (s))
337     {
338       SCM key = scm_caar (s);
339       SCM val = scm_cdar (s);
340       SCM l = scm_assoc (key, b);
341
342       if (l == SCM_BOOL_F
343           || !ly_is_equal (scm_cdr (l), val))
344
345         return false;
346     }
347   return true;
348 }
349
350 SCM
351 ly_alist_vals (SCM alist)
352 {
353   SCM x = SCM_EOL;
354   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
355     x = scm_cons (scm_cdar (p), x);
356   return x;
357 }
358
359 /*
360   LISTS
361  */
362
363 /* Return I-th element, or last elt L. If I < 0, then we take the first
364    element.
365
366    PRE: length (L) > 0  */
367 SCM
368 robust_list_ref (int i, SCM l)
369 {
370   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
371     l = scm_cdr (l);
372   return scm_car (l);
373 }
374
375
376 SCM
377 ly_deep_copy (SCM src)
378 {
379   if (scm_is_pair (src))
380     return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
381   else if (scm_is_vector (src))
382     {
383       int len = scm_c_vector_length (src);
384       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
385       for (int i = 0;i < len; i++)
386         {
387           SCM si = scm_from_int (i);
388           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
389         }
390     }
391   return src;
392 }
393
394 string
395 print_scm_val (SCM val)
396 {
397   string realval = ly_scm2string (ly_write2scm (val));
398   if (realval.length () > 200)
399     realval = realval.substr (0, 100)
400       + "\n :\n :\n"
401       + realval.substr (realval.length () - 100);
402   return realval;
403 }
404
405 bool
406 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
407 {
408   bool ok = true;
409
410   /*
411     Always succeeds.
412
413
414     TODO: should remove #f from allowed vals?
415   */
416   if (val == SCM_EOL || val == SCM_BOOL_F)
417     return ok;
418
419   if (!scm_is_symbol (sym))
420 #if 0
421     return false;
422 #else
423   /*
424     This is used for autoBeamSettings.
425
426     TODO: deprecate the use of \override and \revert for
427     autoBeamSettings?
428
429     or use a symbol autoBeamSettingS?
430   */
431   return true;
432 #endif
433
434   SCM type = scm_object_property (sym, type_symbol);
435
436   if (type != SCM_EOL && !ly_is_procedure (type))
437     {
438       warning (_f ("cannot find property type-check for `%s' (%s).",
439                    ly_symbol2string (sym).c_str (),
440                    ly_symbol2string (type_symbol).c_str ())
441                + "  " + _ ("perhaps a typing error?"));
442
443       /* Be strict when being anal :) */
444       if (do_internal_type_checking_global)
445         scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
446                                                                  sym, val));
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
490 /* Split list at member s, removing s.
491    Return (BEFORE . AFTER)  */
492 SCM
493 ly_split_list (SCM s, SCM list)
494 {
495   SCM before = SCM_EOL;
496   SCM after = list;
497   for (; scm_is_pair (after);)
498     {
499       SCM i = scm_car (after);
500       after = scm_cdr (after);
501       if (ly_is_equal (i, s))
502         break;
503       before = scm_cons (i, before);
504     }
505   return scm_cons (scm_reverse_x (before, SCM_EOL), after);
506 }
507
508 void
509 taint (SCM *)
510 {
511   /*
512     nop.
513   */
514 }
515
516 /*
517   display stuff without using stack
518 */
519 SCM
520 display_list (SCM s)
521 {
522   SCM p = scm_current_output_port ();
523
524   scm_puts ("(", p);
525   for (; scm_is_pair (s); s = scm_cdr (s))
526     {
527       scm_display (scm_car (s), p);
528       scm_puts (" ", p);
529     }
530   scm_puts (")", p);
531   return SCM_UNSPECIFIED;
532 }
533
534 Slice
535 int_list_to_slice (SCM l)
536 {
537   Slice s;
538   s.set_empty ();
539   for (; scm_is_pair (l); l = scm_cdr (l))
540     if (scm_is_number (scm_car (l)))
541       s.add_point (scm_to_int (scm_car (l)));
542   return s;
543 }
544
545 Real
546 robust_scm2double (SCM k, double x)
547 {
548   if (scm_is_number (k))
549     x = scm_to_double (k);
550   return x;
551 }
552
553
554 string
555 robust_scm2string (SCM k, string s)
556 {
557   if (scm_is_string (k))
558     s = ly_scm2string (k);
559   return s;
560 }
561
562 int
563 robust_scm2int (SCM k, int o)
564 {
565   if (scm_integer_p (k) == SCM_BOOL_T)
566     o = scm_to_int (k);
567   return o;
568 }
569
570
571 SCM
572 ly_rational2scm (Rational r)
573 {
574   return scm_divide (scm_from_int (r.numerator ()), scm_from_int (r.denominator ()));
575 }
576
577
578 Rational
579 ly_scm2rational (SCM r)
580 {
581   return Rational (scm_to_int (scm_numerator (r)),
582                    scm_to_int (scm_denominator (r)));
583 }
584
585
586 SCM
587 alist_to_hashq (SCM alist)
588 {
589   int i = scm_ilength (alist);
590   if (i < 0)
591     return scm_c_make_hash_table (0);
592
593   SCM tab = scm_c_make_hash_table (i);
594   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
595     {
596       SCM pt = scm_cdar (s);
597       scm_hashq_set_x (tab, scm_caar (s), pt);
598     }
599   return tab;
600 }
601
602 SCM
603 ly_hash2alist (SCM tab)
604 {
605   SCM func = ly_lily_module_constant ("hash-table->alist");
606   return scm_call_1 (func, tab);
607 }
608
609
610 /*
611   C++ interfacing.
612  */
613
614 string
615 mangle_cxx_identifier (string cxx_id)
616 {
617   if (cxx_id.substr (0, 3) == "ly_")
618     cxx_id = cxx_id.replace (0, 3, "ly:");
619   else
620     {
621       cxx_id = String_convert::to_lower (cxx_id);
622       cxx_id = "ly:" + cxx_id;
623     }
624   if (cxx_id.substr (cxx_id.length () - 2) == "_p")
625     cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "?");
626   else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
627     cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "!");
628
629   cxx_id = replace_all (cxx_id, '_', '-');
630   return cxx_id;
631 }
632
633
634
635 SCM
636 ly_string_array_to_scm (vector<string> a)
637 {
638   SCM s = SCM_EOL;
639   for (vsize i = a.size (); i ; i--)
640     s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
641   return s;
642 }
643
644 /* SYMBOLS is a whitespace separated list.  */
645 SCM
646 parse_symbol_list (char const *symbols)
647 {
648   while (isspace (*symbols))
649     *symbols++;
650   string s = symbols;
651   replace_all (s, '\n', ' ');
652   replace_all (s, '\t', ' ');
653   return ly_string_array_to_scm (string_split (s, ' '));
654 }
655
656
657 bool
658 ly_is_fraction (SCM x)
659 {
660   return SCM_FRACTIONP(x);
661 }
662
663 struct ly_t_double_cell
664 {
665   SCM a;
666   SCM b;
667   SCM c;
668   SCM d;
669 };
670
671 /* inserts at front, removing duplicates */
672 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
673 {
674   return scm_acons (key, val, scm_assoc_remove_x (alist, key));
675 }
676