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