]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
Merge branch 'web-experimental' into lilypond/translation
[lilypond.git] / lily / lily-guile.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2011 Jan Nieuwenhuizen <janneke@gnu.org>
5   Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "lily-guile.hh"
22
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstring> /* strdup, strchr */
26 #include <cctype>
27
28 using namespace std;
29
30 #include "dimensions.hh"
31 #include "direction.hh"
32 #include "file-path.hh"
33 #include "international.hh"
34 #include "libc-extension.hh"
35 #include "main.hh"
36 #include "misc.hh"
37 #include "offset.hh"
38 #include "pitch.hh"
39 #include "string-convert.hh"
40 #include "source-file.hh"
41 #include "version.hh"
42 #include "warn.hh"
43
44
45 /*
46   symbols/strings.
47  */
48 string
49 ly_scm_write_string (SCM s)
50 {
51   SCM port = scm_mkstrport (SCM_INUM0,
52                             scm_make_string (SCM_INUM0, SCM_UNDEFINED),
53                             SCM_OPN | SCM_WRTNG,
54                             "ly_write2string");
55   //  SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
56   SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
57
58   // scm_apply (write, port, SCM_EOL);
59   scm_call_2 (write, s, port);
60   return ly_scm2string (scm_strport_to_string (port));
61 }
62
63 SCM
64 ly_quote_scm (SCM s)
65 {
66   return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
67 }
68
69 string
70 ly_symbol2string (SCM s)
71 {
72   /*
73     Ugh. this is not very efficient.
74   */
75   SCM str = scm_symbol_to_string (s);
76   return ly_scm2string (str);
77 }
78
79 string
80 gulp_file_to_string (string fn, bool must_exist, int size)
81 {
82   string s = global_path.find (fn);
83   if (s == "")
84     {
85       if (must_exist)
86         {
87           string e = _f ("cannot find file: `%s'", fn);
88           e += " ";
89           e += _f ("(load path: `%s')", global_path.to_string ());
90           error (e);
91           /* unreachable */
92         }
93       return s;
94     }
95
96   if (be_verbose_global)
97     progress_indication ("[" + s);
98
99   vector<char> chars = gulp_file (s, size);
100   string result (&chars[0], chars.size ());
101
102   if (be_verbose_global)
103     progress_indication ("]\n");
104
105   return result;
106 }
107
108 extern "C" {
109   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
110   void
111   ly_display_scm (SCM s)
112   {
113     scm_display (s, scm_current_output_port ());
114     scm_newline (scm_current_output_port ());
115   }
116 };
117
118 /*
119   STRINGS
120  */
121 string
122 ly_scm2string (SCM str)
123 {
124   assert (scm_is_string (str));
125   string result;
126   size_t len = scm_c_string_length (str);
127   if (len) {
128     result.resize(len);
129     scm_to_locale_stringbuf(str, &result.at(0), len);
130   }
131   return result;
132 }
133
134 SCM
135 ly_string2scm (string const &str)
136 {
137   return scm_from_locale_stringn (str.c_str (),
138                                   str.length ());
139 }
140
141 char *
142 ly_scm2str0 (SCM str)
143 {
144   return scm_to_locale_string (str);
145 }
146
147 /*
148   PAIRS
149 */
150 SCM 
151 index_get_cell (SCM s, Direction d)
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 bool
182 is_axis (SCM s)
183 {
184   if (scm_is_number (s))
185     {
186       int i = scm_to_int (s);
187       return i == 0 || i == 1;
188     }
189   return false;
190 }
191
192 bool
193 to_boolean (SCM s)
194 {
195   return scm_is_bool (s) && ly_scm2bool (s);
196 }
197
198 /*
199   DIRECTIONS
200  */
201 Direction
202 to_dir (SCM s)
203 {
204   return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
205 }
206
207 Direction
208 robust_scm2dir (SCM d, Direction def)
209 {
210   if (is_direction (d))
211     def = to_dir (d);
212   return def;
213 }
214
215 bool
216 is_direction (SCM s)
217 {
218   if (scm_is_number (s))
219     {
220       int i = scm_to_int (s);
221       return i >= -1 && i <= 1;
222     }
223   return false;
224 }
225
226 /*
227   INTERVALS
228  */
229 Interval
230 ly_scm2interval (SCM p)
231 {
232   return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
233 }
234
235 Drul_array<Real>
236 ly_scm2realdrul (SCM p)
237 {
238   return Drul_array<Real> (scm_to_double (scm_car (p)),
239                            scm_to_double (scm_cdr (p)));
240 }
241
242 SCM
243 ly_interval2scm (Drul_array<Real> i)
244 {
245   return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
246 }
247
248
249 Interval
250 robust_scm2interval (SCM k, Drul_array<Real> v)
251 {
252   Interval i;
253   i[LEFT] = v[LEFT];
254   i[RIGHT] = v[RIGHT];
255   if (is_number_pair (k))
256     i = ly_scm2interval (k);
257   return i;
258 }
259
260 Drul_array<Real>
261 robust_scm2drul (SCM k, Drul_array<Real> v)
262 {
263   if (is_number_pair (k))
264     v = ly_scm2interval (k);
265   return v;
266 }
267
268 Drul_array<bool>
269 robust_scm2booldrul (SCM k, Drul_array<bool> def)
270 {
271   if (scm_is_pair (k))
272     {
273       def[LEFT] = to_boolean (scm_car (k));
274       def[RIGHT] = to_boolean (scm_cdr (k));
275     }
276   return def;
277 }
278
279 /*
280   OFFSET
281 */
282 SCM
283 ly_offset2scm (Offset o)
284 {
285   return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
286 }
287
288 Offset
289 ly_scm2offset (SCM s)
290 {
291   return Offset (scm_to_double (scm_car (s)),
292                  scm_to_double (scm_cdr (s)));
293 }
294
295 Offset
296 robust_scm2offset (SCM k, Offset o)
297 {
298   if (is_number_pair (k))
299     o = ly_scm2offset (k);
300   return o;
301 }
302 SCM
303 ly_offsets2scm (vector<Offset> os)
304 {
305   SCM l = SCM_EOL;
306   SCM *tail = &l;
307   for (vsize i = 0; i < os.size (); i++)
308     {
309       *tail = scm_cons (ly_offset2scm (os[i]), SCM_EOL);
310       tail = SCM_CDRLOC (*tail);
311     }
312   return l;
313 }
314
315 vector<Offset>
316 ly_scm2offsets (SCM s)
317 {
318   vector<Offset> os;
319   for (; scm_is_pair (s); s = scm_cdr (s))
320     os.push_back (ly_scm2offset (scm_car (s)));
321   return os;
322 }
323
324
325
326
327 /*
328   ALIST
329 */
330
331 bool
332 alist_equal_p (SCM a, SCM b)
333 {
334   for (SCM s = a;
335        scm_is_pair (s); s = scm_cdr (s))
336     {
337       SCM key = scm_caar (s);
338       SCM val = scm_cdar (s);
339       SCM l = scm_assoc (key, b);
340
341       if (l == SCM_BOOL_F
342           || !ly_is_equal (scm_cdr (l), val))
343
344         return false;
345     }
346   return true;
347 }
348
349 SCM
350 ly_alist_vals (SCM alist)
351 {
352   SCM x = SCM_EOL;
353   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
354     x = scm_cons (scm_cdar (p), x);
355   return x;
356 }
357
358 /*
359   LISTS
360  */
361
362 /* Return I-th element, or last elt L. If I < 0, then we take the first
363    element.
364
365    PRE: length (L) > 0  */
366 SCM
367 robust_list_ref (int i, SCM l)
368 {
369   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
370     l = scm_cdr (l);
371   return scm_car (l);
372 }
373
374
375 SCM
376 ly_deep_copy (SCM src)
377 {
378   if (scm_is_pair (src))
379     return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
380   else if (scm_is_vector (src))
381     {
382       int len = scm_c_vector_length (src);
383       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
384       for (int i = 0;i < len; i++)
385         {
386           SCM si = scm_from_int (i);
387           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
388         }
389     }
390   return src;
391 }
392
393 string
394 print_scm_val (SCM val)
395 {
396   string realval = ly_scm_write_string (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 ("cannot 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         scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
445                                                                  sym, val));
446
447       warning (_ ("doing assignment anyway"));
448     }
449   else
450     {
451       if (val != SCM_EOL
452           && ly_is_procedure (type)
453           && scm_call_1 (type, val) == SCM_BOOL_F)
454         {
455           ok = false;
456           SCM typefunc = ly_lily_module_constant ("type-name");
457           SCM type_name = scm_call_1 (typefunc, type);
458
459           warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
460                        ly_symbol2string (sym).c_str (),
461                        print_scm_val (val),
462                        ly_scm2string (type_name).c_str ()));
463           progress_indication ("\n");
464         }
465     }
466   return ok;
467 }
468
469 /* some SCM abbrevs
470
471 zijn deze nou handig?
472 zijn ze er al in scheme, maar heten ze anders? */
473
474 /* Remove doubles from (sorted) list */
475 SCM
476 ly_unique (SCM list)
477 {
478   SCM unique = SCM_EOL;
479   for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
480     {
481       if (!scm_is_pair (scm_cdr (i))
482           || !ly_is_equal (scm_car (i), scm_cadr (i)))
483         unique = scm_cons (scm_car (i), unique);
484     }
485   return scm_reverse_x (unique, SCM_EOL);
486 }
487
488
489 /* Split list at member s, removing s.
490    Return (BEFORE . AFTER)  */
491 SCM
492 ly_split_list (SCM s, SCM list)
493 {
494   SCM before = SCM_EOL;
495   SCM after = list;
496   for (; scm_is_pair (after);)
497     {
498       SCM i = scm_car (after);
499       after = scm_cdr (after);
500       if (ly_is_equal (i, s))
501         break;
502       before = scm_cons (i, before);
503     }
504   return scm_cons (scm_reverse_x (before, SCM_EOL), after);
505 }
506
507 void
508 taint (SCM *)
509 {
510   /*
511     nop.
512   */
513 }
514
515 /*
516   display stuff without using stack
517 */
518 SCM
519 display_list (SCM s)
520 {
521   SCM p = scm_current_output_port ();
522
523   scm_puts ("(", p);
524   for (; scm_is_pair (s); s = scm_cdr (s))
525     {
526       scm_display (scm_car (s), p);
527       scm_puts (" ", p);
528     }
529   scm_puts (")", p);
530   return SCM_UNSPECIFIED;
531 }
532
533 Slice
534 int_list_to_slice (SCM l)
535 {
536   Slice s;
537   s.set_empty ();
538   for (; scm_is_pair (l); l = scm_cdr (l))
539     if (scm_is_number (scm_car (l)))
540       s.add_point (scm_to_int (scm_car (l)));
541   return s;
542 }
543
544 Real
545 robust_scm2double (SCM k, double x)
546 {
547   if (scm_is_number (k))
548     x = scm_to_double (k);
549   return x;
550 }
551
552
553 string
554 robust_scm2string (SCM k, string s)
555 {
556   if (scm_is_string (k))
557     s = ly_scm2string (k);
558   return s;
559 }
560
561 int
562 robust_scm2int (SCM k, int o)
563 {
564   if (scm_integer_p (k) == SCM_BOOL_T)
565     o = scm_to_int (k);
566   return o;
567 }
568
569
570 SCM
571 ly_rational2scm (Rational r)
572 {
573   return scm_divide (scm_from_int64 (r.numerator ()),
574                      scm_from_int64 (r.denominator ()));
575 }
576
577
578 Rational
579 ly_scm2rational (SCM r)
580 {
581   return Rational (scm_to_int64 (scm_numerator (r)),
582                    scm_to_int64 (scm_denominator (r)));
583 }
584
585 Rational
586 robust_scm2rational (SCM n, Rational rat)
587 {
588   if (ly_is_fraction (n))
589     return ly_scm2rational (n);
590   else
591     return rat;
592 }
593
594 SCM
595 alist_to_hashq (SCM alist)
596 {
597   int i = scm_ilength (alist);
598   if (i < 0)
599     return scm_c_make_hash_table (0);
600
601   SCM tab = scm_c_make_hash_table (i);
602   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
603     {
604       SCM pt = scm_cdar (s);
605       scm_hashq_set_x (tab, scm_caar (s), pt);
606     }
607   return tab;
608 }
609
610 SCM
611 ly_hash2alist (SCM tab)
612 {
613   SCM func = ly_lily_module_constant ("hash-table->alist");
614   return scm_call_1 (func, tab);
615 }
616
617
618 /*
619   C++ interfacing.
620  */
621
622 string
623 mangle_cxx_identifier (string cxx_id)
624 {
625   if (cxx_id.substr (0, 3) == "ly_")
626     cxx_id = cxx_id.replace (0, 3, "ly:");
627   else
628     {
629       cxx_id = String_convert::to_lower (cxx_id);
630       cxx_id = "ly:" + cxx_id;
631     }
632   if (cxx_id.substr (cxx_id.length () - 2) == "_p")
633     cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "?");
634   else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
635     cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "!");
636
637   replace_all (&cxx_id, "_less?", "<?");
638   replace_all (&cxx_id, "_2_", "->");
639   replace_all (&cxx_id, "__", "::");
640   replace_all (&cxx_id, '_', '-');
641
642
643   return cxx_id;
644 }
645
646
647
648 SCM
649 ly_string_array_to_scm (vector<string> a)
650 {
651   SCM s = SCM_EOL;
652   for (vsize i = a.size (); i ; i--)
653     s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
654   return s;
655 }
656
657 /* SYMBOLS is a whitespace separated list.  */
658 SCM
659 parse_symbol_list (char const *symbols)
660 {
661   while (isspace (*symbols))
662     symbols++;
663   string s = symbols;
664   replace_all (&s, '\n', ' ');
665   replace_all (&s, '\t', ' ');
666   replace_all (&s, "  ", " ");
667   return ly_string_array_to_scm (string_split (s, ' '));
668 }
669
670 /* GDB debugging. */
671 struct ly_t_double_cell
672 {
673   SCM a;
674   SCM b;
675   SCM c;
676   SCM d;
677 };
678
679 /* inserts at front, removing duplicates */
680 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
681 {
682   return scm_acons (key, val, scm_assoc_remove_x (alist, key));
683 }
684