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