]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
Merge branch 'master' of git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond
[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--2007 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
38 SCM
39 ly_write2scm (SCM s)
40 {
41   SCM port = scm_mkstrport (SCM_INUM0,
42                             scm_make_string (SCM_INUM0, SCM_UNDEFINED),
43                             SCM_OPN | SCM_WRTNG,
44                             "ly_write2string");
45   //  SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
46   SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
47
48   // scm_apply (write, port, SCM_EOL);
49   scm_call_2 (write, s, port);
50   return scm_strport_to_string (port);
51 }
52
53 SCM
54 ly_quote_scm (SCM s)
55 {
56   return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
57 }
58
59 string
60 ly_symbol2string (SCM s)
61 {
62   /*
63     Ugh. this is not very efficient.
64   */
65   SCM str = scm_symbol_to_string (s);
66   return ly_scm2string (str);
67 }
68
69 string
70 gulp_file_to_string (string fn, bool must_exist, int size)
71 {
72   string s = global_path.find (fn);
73   if (s == "")
74     {
75       if (must_exist)
76         {
77           string e = _f ("cannot find file: `%s'", fn);
78           e += " ";
79           e += _f ("(load path: `%s')", global_path.to_string ());
80           error (e);
81           /* unreachable */
82         }
83       return s;
84     }
85
86   if (be_verbose_global)
87     progress_indication ("[" + s);
88
89   vector<char> chars = gulp_file (s, size);
90   string result (&chars[0], chars.size ());
91
92   if (be_verbose_global)
93     progress_indication ("]");
94
95   return result;
96 }
97
98 extern "C" {
99   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
100   void
101   ly_display_scm (SCM s)
102   {
103     scm_display (s, scm_current_output_port ());
104     scm_newline (scm_current_output_port ());
105   }
106 };
107
108 /*
109   STRINGS
110  */
111 string
112 ly_scm2string (SCM str)
113 {
114   assert (scm_is_string (str));
115   return string (scm_i_string_chars (str),
116                  (int) scm_i_string_length (str));
117 }
118
119 SCM
120 ly_string2scm (string const &str)
121 {
122   return scm_from_locale_stringn (str.c_str(),
123                                   str.length ());
124 }
125
126
127 char *
128 ly_scm2newstr (SCM str, size_t *lenp)
129 {
130   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
131
132   size_t len = scm_i_string_length (str);
133   if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
134     {
135       memcpy (new_str, scm_i_string_chars (str), len);
136       new_str[len] = '\0';
137
138       if (lenp)
139         *lenp = len;
140
141       return new_str;
142     }
143   return 0;
144 }
145
146
147 /*
148   PAIRS
149 */
150 SCM
151 index_get_cell (SCM s, Direction d)
152 {
153
154   assert (d);
155   return (d == LEFT) ? scm_car (s) : scm_cdr (s);
156 }
157
158 SCM
159 index_set_cell (SCM s, Direction d, SCM v)
160 {
161   if (d == LEFT)
162     scm_set_car_x (s, v);
163   else if (d == RIGHT)
164     scm_set_cdr_x (s, v);
165   return s;
166 }
167
168 bool
169 is_number_pair (SCM p)
170 {
171   return scm_is_pair (p)
172     && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
173 }
174
175
176 unsigned int
177 ly_scm_hash (SCM s)
178 {
179   return scm_ihashv (s, ~1u);
180 }
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
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
328
329 /*
330   ALIST
331 */
332
333 bool
334 alist_equal_p (SCM a, SCM b)
335 {
336   for (SCM s = a;
337        scm_is_pair (s); s = scm_cdr (s))
338     {
339       SCM key = scm_caar (s);
340       SCM val = scm_cdar (s);
341       SCM l = scm_assoc (key, b);
342
343       if (l == SCM_BOOL_F
344           || !ly_is_equal (scm_cdr (l), val))
345
346         return false;
347     }
348   return true;
349 }
350
351 SCM
352 ly_alist_vals (SCM alist)
353 {
354   SCM x = SCM_EOL;
355   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
356     x = scm_cons (scm_cdar (p), x);
357   return x;
358 }
359
360 /*
361   LISTS
362  */
363
364 /* Return I-th element, or last elt L. If I < 0, then we take the first
365    element.
366
367    PRE: length (L) > 0  */
368 SCM
369 robust_list_ref (int i, SCM l)
370 {
371   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
372     l = scm_cdr (l);
373   return scm_car (l);
374 }
375
376
377 SCM
378 ly_deep_copy (SCM src)
379 {
380   if (scm_is_pair (src))
381     return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
382   else if (scm_is_vector (src))
383     {
384       int len = scm_c_vector_length (src);
385       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
386       for (int i = 0;i < len; i++)
387         {
388           SCM si = scm_from_int (i);
389           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
390         }
391     }
392   return src;
393 }
394
395
396
397
398
399 string
400 print_scm_val (SCM val)
401 {
402   string realval = ly_scm2string (ly_write2scm (val));
403   if (realval.length () > 200)
404     realval = realval.substr (0, 100)
405       + "\n :\n :\n"
406       + realval.substr (realval.length () - 100);
407   return realval;
408 }
409
410 bool
411 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
412 {
413   bool ok = true;
414
415   /*
416     Always succeeds.
417
418
419     TODO: should remove #f from allowed vals?
420   */
421   if (val == SCM_EOL || val == SCM_BOOL_F)
422     return ok;
423
424   if (!scm_is_symbol (sym))
425 #if 0
426     return false;
427 #else
428   /*
429     This is used for autoBeamSettings.
430
431     TODO: deprecate the use of \override and \revert for
432     autoBeamSettings?
433
434     or use a symbol autoBeamSettingS?
435   */
436   return true;
437 #endif
438
439   SCM type = scm_object_property (sym, type_symbol);
440
441   if (type != SCM_EOL && !ly_is_procedure (type))
442     {
443       warning (_f ("cannot find property type-check for `%s' (%s).",
444                    ly_symbol2string (sym).c_str (),
445                    ly_symbol2string (type_symbol).c_str ())
446                + "  " + _ ("perhaps a typing error?"));
447
448       /* Be strict when being anal :) */
449       if (do_internal_type_checking_global)
450         scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
451                                                                  sym, val));
452
453       warning (_ ("doing assignment anyway"));
454     }
455   else
456     {
457       if (val != SCM_EOL
458           && ly_is_procedure (type)
459           && scm_call_1 (type, val) == SCM_BOOL_F)
460         {
461           ok = false;
462           SCM typefunc = ly_lily_module_constant ("type-name");
463           SCM type_name = scm_call_1 (typefunc, type);
464
465           warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
466                        ly_symbol2string (sym).c_str (),
467                        print_scm_val (val),
468                        ly_scm2string (type_name).c_str ()));
469           progress_indication ("\n");
470         }
471     }
472   return ok;
473 }
474
475 /* some SCM abbrevs
476
477 zijn deze nou handig?
478 zijn ze er al in scheme, maar heten ze anders? */
479
480 /* Remove doubles from (sorted) list */
481 SCM
482 ly_unique (SCM list)
483 {
484   SCM unique = SCM_EOL;
485   for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
486     {
487       if (!scm_is_pair (scm_cdr (i))
488           || !ly_is_equal (scm_car (i), scm_cadr (i)))
489         unique = scm_cons (scm_car (i), unique);
490     }
491   return scm_reverse_x (unique, SCM_EOL);
492 }
493
494
495 /* Split list at member s, removing s.
496    Return (BEFORE . AFTER)  */
497 SCM
498 ly_split_list (SCM s, SCM list)
499 {
500   SCM before = SCM_EOL;
501   SCM after = list;
502   for (; scm_is_pair (after);)
503     {
504       SCM i = scm_car (after);
505       after = scm_cdr (after);
506       if (ly_is_equal (i, s))
507         break;
508       before = scm_cons (i, before);
509     }
510   return scm_cons (scm_reverse_x (before, SCM_EOL), after);
511 }
512
513 void
514 taint (SCM *)
515 {
516   /*
517     nop.
518   */
519 }
520
521 /*
522   display stuff without using stack
523 */
524 SCM
525 display_list (SCM s)
526 {
527   SCM p = scm_current_output_port ();
528
529   scm_puts ("(", p);
530   for (; scm_is_pair (s); s = scm_cdr (s))
531     {
532       scm_display (scm_car (s), p);
533       scm_puts (" ", p);
534     }
535   scm_puts (")", p);
536   return SCM_UNSPECIFIED;
537 }
538
539 Slice
540 int_list_to_slice (SCM l)
541 {
542   Slice s;
543   s.set_empty ();
544   for (; scm_is_pair (l); l = scm_cdr (l))
545     if (scm_is_number (scm_car (l)))
546       s.add_point (scm_to_int (scm_car (l)));
547   return s;
548 }
549
550 Real
551 robust_scm2double (SCM k, double x)
552 {
553   if (scm_is_number (k))
554     x = scm_to_double (k);
555   return x;
556 }
557
558
559 string
560 robust_scm2string (SCM k, string s)
561 {
562   if (scm_is_string (k))
563     s = ly_scm2string (k);
564   return s;
565 }
566
567 int
568 robust_scm2int (SCM k, int o)
569 {
570   if (scm_integer_p (k) == SCM_BOOL_T)
571     o = scm_to_int (k);
572   return o;
573 }
574
575
576 SCM
577 ly_rational2scm (Rational r)
578 {
579   return scm_divide (scm_from_int (r.numerator ()), scm_from_int (r.denominator ()));
580 }
581
582
583 Rational
584 ly_scm2rational (SCM r)
585 {
586   return Rational (scm_to_int (scm_numerator (r)),
587                    scm_to_int (scm_denominator (r)));
588 }
589
590
591 SCM
592 alist_to_hashq (SCM alist)
593 {
594   int i = scm_ilength (alist);
595   if (i < 0)
596     return scm_c_make_hash_table (0);
597
598   SCM tab = scm_c_make_hash_table (i);
599   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
600     {
601       SCM pt = scm_cdar (s);
602       scm_hashq_set_x (tab, scm_caar (s), pt);
603     }
604   return tab;
605 }
606
607 SCM
608 ly_hash2alist (SCM tab)
609 {
610   SCM func = ly_lily_module_constant ("hash-table->alist");
611   return scm_call_1 (func, tab);
612 }
613
614
615 /*
616   C++ interfacing.
617  */
618
619 string
620 mangle_cxx_identifier (string cxx_id)
621 {
622   if (cxx_id.substr (0, 3) == "ly_")
623     cxx_id = cxx_id.replace (0, 3, "ly:");
624   else
625     {
626       cxx_id = String_convert::to_lower (cxx_id);
627       cxx_id = "ly:" + cxx_id;
628     }
629   if (cxx_id.substr (cxx_id.length () - 2) == "_p")
630     cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "?");
631   else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
632     cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "!");
633
634   cxx_id = replace_all (cxx_id, '_', '-');
635   return cxx_id;
636 }
637
638
639
640 SCM
641 ly_string_array_to_scm (vector<string> a)
642 {
643   SCM s = SCM_EOL;
644   for (vsize i = a.size (); i ; i--)
645     s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
646   return s;
647 }
648
649 /* SYMBOLS is a whitespace separated list.  */
650 SCM
651 parse_symbol_list (char const *symbols)
652 {
653   while (isspace (*symbols))
654     *symbols++;
655   string s = symbols;
656   replace_all (s, '\n', ' ');
657   replace_all (s, '\t', ' ');
658   return ly_string_array_to_scm (string_split (s, ' '));
659 }
660
661
662 bool
663 ly_is_fraction (SCM x)
664 {
665   return SCM_FRACTIONP(x);
666 }
667
668 struct ly_t_double_cell
669 {
670   SCM a;
671   SCM b;
672   SCM c;
673   SCM d;
674 };
675
676 /* inserts at front, removing duplicates */
677 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
678 {
679   return scm_acons (key, val, scm_assoc_remove_x (alist, key));
680 }
681