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