]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
Optimizes note-heads.cc and introduces robust_symbol2string.
[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   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_number (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)), scm_to_double (scm_cdr (p)));
235 }
236
237 Drul_array<Real>
238 ly_scm2realdrul (SCM p)
239 {
240   return Drul_array<Real> (scm_to_double (scm_car (p)),
241                            scm_to_double (scm_cdr (p)));
242 }
243
244 SCM
245 ly_interval2scm (Drul_array<Real> i)
246 {
247   return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
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   ALIST
327 */
328
329 bool
330 alist_equal_p (SCM a, SCM b)
331 {
332   for (SCM s = a;
333        scm_is_pair (s); s = scm_cdr (s))
334     {
335       SCM key = scm_caar (s);
336       SCM val = scm_cdar (s);
337       SCM l = scm_assoc (key, b);
338
339       if (l == SCM_BOOL_F
340           || !ly_is_equal (scm_cdr (l), val))
341
342         return false;
343     }
344   return true;
345 }
346
347 SCM
348 ly_alist_vals (SCM alist)
349 {
350   SCM x = SCM_EOL;
351   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
352     x = scm_cons (scm_cdar (p), x);
353   return x;
354 }
355
356 /*
357   LISTS
358  */
359
360 /* Return I-th element, or last elt L. If I < 0, then we take the first
361    element.
362
363    PRE: length (L) > 0  */
364 SCM
365 robust_list_ref (int i, SCM l)
366 {
367   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
368     l = scm_cdr (l);
369   return scm_car (l);
370 }
371
372 SCM
373 ly_deep_copy (SCM src)
374 {
375   if (scm_is_pair (src))
376     return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
377   else if (scm_is_vector (src))
378     {
379       int len = scm_c_vector_length (src);
380       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
381       for (int i = 0; i < len; i++)
382         {
383           SCM si = scm_from_int (i);
384           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
385         }
386     }
387   return src;
388 }
389
390 string
391 print_scm_val (SCM val)
392 {
393   string realval = ly_scm_write_string (val);
394   if (realval.length () > 200)
395     realval = realval.substr (0, 100)
396               + "\n :\n :\n"
397               + realval.substr (realval.length () - 100);
398   return realval;
399 }
400
401 bool
402 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
403 {
404   bool ok = true;
405
406   /*
407     Always succeeds.
408
409
410     TODO: should remove #f from allowed vals?
411   */
412   if (val == SCM_EOL || val == SCM_BOOL_F)
413     return ok;
414
415   if (!scm_is_symbol (sym))
416 #if 0
417     return false;
418 #else
419     /*
420       This is used for autoBeamSettings.
421
422       TODO: deprecate the use of \override and \revert for
423       autoBeamSettings?
424
425       or use a symbol autoBeamSettingS?
426     */
427     return true;
428 #endif
429
430   SCM type = scm_object_property (sym, type_symbol);
431
432   if (type != SCM_EOL && !ly_is_procedure (type))
433     {
434       warning (_f ("cannot find property type-check for `%s' (%s).",
435                    ly_symbol2string (sym).c_str (),
436                    ly_symbol2string (type_symbol).c_str ())
437                + "  " + _ ("perhaps a typing error?"));
438
439       /* Be strict when being anal :) */
440       if (do_internal_type_checking_global)
441         scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
442                                                                  sym, val));
443
444       warning (_ ("doing assignment anyway"));
445     }
446   else
447     {
448       if (val != SCM_EOL
449           && ly_is_procedure (type)
450           && scm_call_1 (type, val) == SCM_BOOL_F)
451         {
452           ok = false;
453           SCM typefunc = ly_lily_module_constant ("type-name");
454           SCM type_name = scm_call_1 (typefunc, type);
455
456           warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
457                        ly_symbol2string (sym).c_str (),
458                        print_scm_val (val),
459                        ly_scm2string (type_name).c_str ()));
460           progress_indication ("\n");
461         }
462     }
463   return ok;
464 }
465
466 /* some SCM abbrevs
467
468 zijn deze nou handig?
469 zijn ze er al in scheme, maar heten ze anders? */
470
471 /* Remove doubles from (sorted) list */
472 SCM
473 ly_unique (SCM list)
474 {
475   SCM unique = SCM_EOL;
476   for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
477     {
478       if (!scm_is_pair (scm_cdr (i))
479           || !ly_is_equal (scm_car (i), scm_cadr (i)))
480         unique = scm_cons (scm_car (i), unique);
481     }
482   return scm_reverse_x (unique, SCM_EOL);
483 }
484
485 /* Split list at member s, removing s.
486    Return (BEFORE . AFTER)  */
487 SCM
488 ly_split_list (SCM s, SCM list)
489 {
490   SCM before = SCM_EOL;
491   SCM after = list;
492   for (; scm_is_pair (after);)
493     {
494       SCM i = scm_car (after);
495       after = scm_cdr (after);
496       if (ly_is_equal (i, s))
497         break;
498       before = scm_cons (i, before);
499     }
500   return scm_cons (scm_reverse_x (before, SCM_EOL), after);
501 }
502
503 void
504 taint (SCM *)
505 {
506   /*
507     nop.
508   */
509 }
510
511 /*
512   display stuff without using stack
513 */
514 SCM
515 display_list (SCM s)
516 {
517   SCM p = scm_current_output_port ();
518
519   scm_puts ("(", p);
520   for (; scm_is_pair (s); s = scm_cdr (s))
521     {
522       scm_display (scm_car (s), p);
523       scm_puts (" ", p);
524     }
525   scm_puts (")", p);
526   return SCM_UNSPECIFIED;
527 }
528
529 Slice
530 int_list_to_slice (SCM l)
531 {
532   Slice s;
533   s.set_empty ();
534   for (; scm_is_pair (l); l = scm_cdr (l))
535     if (scm_is_number (scm_car (l)))
536       s.add_point (scm_to_int (scm_car (l)));
537   return s;
538 }
539
540 Real
541 robust_scm2double (SCM k, double x)
542 {
543   if (scm_is_number (k))
544     x = scm_to_double (k);
545   return x;
546 }
547
548 string
549 robust_scm2string (SCM k, string s)
550 {
551   if (scm_is_string (k))
552     s = ly_scm2string (k);
553   return s;
554 }
555
556 int
557 robust_scm2int (SCM k, int o)
558 {
559   if (scm_integer_p (k) == SCM_BOOL_T)
560     o = scm_to_int (k);
561   return o;
562 }
563
564 SCM
565 ly_rational2scm (Rational r)
566 {
567   return scm_divide (scm_from_int64 (r.numerator ()),
568                      scm_from_int64 (r.denominator ()));
569 }
570
571 Rational
572 ly_scm2rational (SCM r)
573 {
574   return Rational (scm_to_int64 (scm_numerator (r)),
575                    scm_to_int64 (scm_denominator (r)));
576 }
577
578 Rational
579 robust_scm2rational (SCM n, Rational rat)
580 {
581   if (ly_is_fraction (n))
582     return ly_scm2rational (n);
583   else
584     return rat;
585 }
586
587 SCM
588 alist_to_hashq (SCM alist)
589 {
590   int i = scm_ilength (alist);
591   if (i < 0)
592     return scm_c_make_hash_table (0);
593
594   SCM tab = scm_c_make_hash_table (i);
595   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
596     {
597       SCM pt = scm_cdar (s);
598       scm_hashq_set_x (tab, scm_caar (s), pt);
599     }
600   return tab;
601 }
602
603 SCM
604 ly_hash2alist (SCM tab)
605 {
606   SCM func = ly_lily_module_constant ("hash-table->alist");
607   return scm_call_1 (func, tab);
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, 2, "?");
626   else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
627     cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "!");
628
629   replace_all (&cxx_id, "_less?", "<?");
630   replace_all (&cxx_id, "_2_", "->");
631   replace_all (&cxx_id, "__", "::");
632   replace_all (&cxx_id, '_', '-');
633
634   return cxx_id;
635 }
636
637 SCM
638 ly_string_array_to_scm (vector<string> a)
639 {
640   SCM s = SCM_EOL;
641   for (vsize i = a.size (); i; i--)
642     s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
643   return s;
644 }
645
646 /* SYMBOLS is a whitespace separated list.  */
647 SCM
648 parse_symbol_list (char const *symbols)
649 {
650   while (isspace (*symbols))
651     symbols++;
652   string s = symbols;
653   replace_all (&s, '\n', ' ');
654   replace_all (&s, '\t', ' ');
655   replace_all (&s, "  ", " ");
656   return ly_string_array_to_scm (string_split (s, ' '));
657 }
658
659 /* GDB debugging. */
660 struct ly_t_double_cell
661 {
662   SCM a;
663   SCM b;
664   SCM c;
665   SCM d;
666 };
667
668 /* inserts at front, removing duplicates */
669 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
670 {
671   return scm_acons (key, val, scm_assoc_remove_x (alist, key));
672 }
673