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