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