]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
* flower
[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     {
204       initialize_kpathsea ();
205     }
206 #endif
207
208   scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
209 }
210
211 SCM global_lily_module;
212
213 void
214 ly_c_init_guile ()
215 {
216   global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
217   scm_c_use_module ("lily");
218 }
219
220 unsigned int
221 ly_scm_hash (SCM s)
222 {
223   return scm_ihashv (s, ~1u);
224 }
225
226 bool
227 is_direction (SCM s)
228 {
229   if (scm_is_number (s))
230     {
231       int i = scm_to_int (s);
232       return i>= -1 && i <= 1;
233     }
234   return false;
235 }
236
237 bool
238 is_axis (SCM s)
239 {
240   if (scm_is_number (s))
241     {
242       int i = scm_to_int (s);
243       return i == 0 || i == 1;
244     }
245   return false;
246 }
247
248 Direction
249 to_dir (SCM s)
250 {
251   return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
252 }
253
254 Interval
255 ly_scm2interval (SCM p)
256 {
257   return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
258 }
259
260 Drul_array<Real>
261 ly_scm2realdrul (SCM p)
262 {
263   return Drul_array<Real> (scm_to_double (scm_car (p)),
264                            scm_to_double (scm_cdr (p)));
265 }
266
267 SCM
268 ly_interval2scm (Drul_array<Real> i)
269 {
270   return scm_cons (scm_make_real (i[LEFT]), scm_make_real (i[RIGHT]));
271 }
272
273 bool
274 to_boolean (SCM s)
275 {
276   return scm_is_bool (s) && ly_scm2bool (s);
277 }
278
279 /* Appendable list L: the cdr contains the list, the car the last cons
280    in the list.  */
281 SCM
282 appendable_list ()
283 {
284   SCM s = scm_cons (SCM_EOL, SCM_EOL);
285   scm_set_car_x (s, s);
286
287   return s;
288 }
289
290 void
291 appendable_list_append (SCM l, SCM elt)
292 {
293   SCM newcons = scm_cons (elt, SCM_EOL);
294
295   scm_set_cdr_x (scm_car (l), newcons);
296   scm_set_car_x (l, newcons);
297 }
298
299 SCM
300 ly_offset2scm (Offset o)
301 {
302   return scm_cons (scm_make_real (o[X_AXIS]), scm_make_real (o[Y_AXIS]));
303 }
304
305 Offset
306 ly_scm2offset (SCM s)
307 {
308   return Offset (scm_to_double (scm_car (s)),
309                  scm_to_double (scm_cdr (s)));
310 }
311
312 SCM
313 ly_deep_copy (SCM src)
314 {
315   if (scm_is_pair (src))
316     return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
317   else if (scm_is_vector (src))
318     {
319       int len = scm_c_vector_length (src);
320       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
321       for (int i = 0;i < len; i++)
322         {
323           SCM si = scm_int2num (i);
324           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
325         }
326     }
327   return src;
328 }
329
330 SCM
331 ly_chain_assoc_get (SCM key, SCM achain, SCM dfault)
332 {
333   if (scm_is_pair (achain))
334     {
335       SCM handle = scm_assoc (key, scm_car (achain));
336       if (scm_is_pair (handle))
337         return scm_cdr (handle);
338       else
339         return ly_chain_assoc (key, scm_cdr (achain));
340     }
341   else
342     return dfault;
343 }
344
345 SCM
346 ly_chain_assoc (SCM key, SCM achain)
347 {
348   if (scm_is_pair (achain))
349     {
350       SCM handle = scm_assoc (key, scm_car (achain));
351       if (scm_is_pair (handle))
352         return handle;
353       else
354         return ly_chain_assoc (key, scm_cdr (achain));
355     }
356   else
357     return SCM_BOOL_F;
358 }
359
360 /* looks the key up in the cdrs of the alist-keys
361    - ignoring the car and ignoring non-pair keys.
362    Returns first match found, i.e.
363
364    alist = ((1 . 10)
365    ((1 . 2) . 11)
366    ((2 . 1) . 12)
367    ((3 . 0) . 13)
368    ((4 . 1) . 14) )
369
370    I would like (ly_assoc_cdr 1) to return 12 - because it's the first
371    element with the cdr of the key = 1.  In other words (alloc_cdr key)
372    corresponds to call
373
374    (alloc (anything . key))
375 */
376 SCM
377 ly_assoc_cdr (SCM key, SCM alist)
378 {
379   if (scm_is_pair (alist))
380     {
381       SCM trykey = scm_caar (alist);
382       if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
383         return scm_car (alist);
384       else
385         return ly_assoc_cdr (key, scm_cdr (alist));
386     }
387   return SCM_BOOL_F;
388 }
389
390 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
391    i.e. \n and ' ' can be used interchangeably as separators.  */
392 SCM
393 parse_symbol_list (char const *lst)
394 {
395   char *s = strdup (lst);
396   char *orig = s;
397   SCM create_list = SCM_EOL;
398
399   char *e = s + strlen (s) - 1;
400   while (e >= s && isspace (*e))
401     *e-- = 0;
402
403   for (char *p = s; *p; p++)
404     if (*p == '\n')
405       *p = ' ';
406   
407   if (!s[0])
408     s = 0;
409   
410   while (s)
411     {
412       char *next = strchr (s, ' ');
413       if (next)
414         *next++ = 0;
415
416       create_list = scm_cons (ly_symbol2scm (s), create_list);
417       s = next;
418     }
419
420   free (orig);
421   return create_list;
422 }
423
424 SCM
425 ly_truncate_list (int k, SCM lst)
426 {
427   if (k == 0)
428     lst = SCM_EOL;
429   else
430     {
431       SCM s = lst;
432       k--;
433       for (; scm_is_pair (s) && k--; s = scm_cdr (s))
434         ;
435
436       if (scm_is_pair (s))
437         scm_set_cdr_x (s, SCM_EOL);
438     }
439   return lst;
440 }
441
442 String
443 print_scm_val (SCM val)
444 {
445   String realval = ly_scm2string (ly_write2scm (val));
446   if (realval.length () > 200)
447     realval = realval.left_string (100)
448       + "\n :\n :\n"
449       + realval.right_string (100);
450   return realval;
451 }
452
453 bool
454 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
455 {
456   bool ok = true;
457
458   /*
459     Always succeeds.
460
461
462     TODO: should remove #f from allowed vals?
463   */
464   if (val == SCM_EOL || val == SCM_BOOL_F)
465     return ok;
466
467   if (!scm_is_symbol (sym))
468 #if 0
469     return false;
470 #else
471   /*
472     This is used for autoBeamSettings.
473
474     TODO: deprecate the use of \override and \revert for
475     autoBeamSettings?
476
477     or use a symbol autoBeamSettingS?
478   */
479   return true;
480 #endif
481
482   SCM type = scm_object_property (sym, type_symbol);
483
484   if (type != SCM_EOL && !ly_c_procedure_p (type))
485     {
486       warning (_f ("Can't find property type-check for `%s' (%s).",
487                    ly_symbol2string (sym).to_str0 (),
488                    ly_symbol2string (type_symbol).to_str0 ())
489                + "  " + _ ("Perhaps you made a typing error?"));
490
491       /* Be strict when being anal :) */
492       if (do_internal_type_checking_global)
493         abort ();
494
495       warning (_ ("Doing assignment anyway."));
496     }
497   else
498     {
499       if (val != SCM_EOL
500           && ly_c_procedure_p (type)
501           && scm_call_1 (type, val) == SCM_BOOL_F)
502         {
503           SCM errport = scm_current_error_port ();
504           ok = false;
505           SCM typefunc = ly_lily_module_constant ("type-name");
506           SCM type_name = scm_call_1 (typefunc, type);
507
508           scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
509                         ly_symbol2string (sym).to_str0 (),
510                         print_scm_val (val),
511                         ly_scm2string (type_name).to_str0 ()).to_str0 (),
512                     errport);
513           scm_puts ("\n", errport);
514         }
515     }
516   return ok;
517 }
518
519 /* some SCM abbrevs
520
521 zijn deze nou handig?
522 zijn ze er al in scheme, maar heten ze anders? */
523
524 /* Remove doubles from (sorted) list */
525 SCM
526 ly_unique (SCM list)
527 {
528   SCM unique = SCM_EOL;
529   for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
530     {
531       if (!scm_is_pair (scm_cdr (i))
532           || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
533         unique = scm_cons (scm_car (i), unique);
534     }
535   return scm_reverse_x (unique, SCM_EOL);
536 }
537
538 static int
539 scm_default_compare (void const *a, void const *b)
540 {
541   SCM pa = *(SCM *) a;
542   SCM pb = *(SCM *) b;
543   if (pa == pb)
544     return 0;
545   return pa < pb ? -1 : 1;
546 }
547
548 /*  Modify LST in place: qsort it.  */
549 SCM
550 ly_list_qsort_uniq_x (SCM lst)
551 {
552   int len = scm_ilength (lst);
553   SCM *arr = new SCM[len];
554   int k = 0;
555   for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
556     arr[k++] = scm_car (s);
557
558   assert (k == len);
559   qsort (arr, len, sizeof (SCM), &scm_default_compare);
560
561   SCM *tail = &lst;
562   for (int i = 0; i < len; i++)
563     if (!i || arr[i] != arr[i - 1])
564       {
565         SCM_SETCAR (*tail, arr[i]);
566         tail = SCM_CDRLOC (*tail);
567       }
568
569   *tail = SCM_EOL;
570   delete[] arr;
571
572   return lst;
573 }
574
575 /* tail add */
576 SCM
577 ly_snoc (SCM s, SCM list)
578 {
579   return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
580 }
581
582 /* Split list at member s, removing s.
583    Return (BEFORE . AFTER)  */
584 SCM
585 ly_split_list (SCM s, SCM list)
586 {
587   SCM before = SCM_EOL;
588   SCM after = list;
589   for (; scm_is_pair (after);)
590     {
591       SCM i = scm_car (after);
592       after = scm_cdr (after);
593       if (ly_c_equal_p (i, s))
594         break;
595       before = scm_cons (i, before);
596     }
597   return scm_cons (scm_reverse_x (before, SCM_EOL), after);
598
599 }
600
601 void
602 taint (SCM *)
603 {
604   /*
605     nop.
606   */
607 }
608
609 /*
610   display stuff without using stack
611 */
612 SCM
613 display_list (SCM s)
614 {
615   SCM p = scm_current_output_port ();
616
617   scm_puts ("(", p);
618   for (; scm_is_pair (s); s = scm_cdr (s))
619     {
620       scm_display (scm_car (s), p);
621       scm_puts (" ", p);
622     }
623   scm_puts (")", p);
624   return SCM_UNSPECIFIED;
625 }
626
627 Slice
628 int_list_to_slice (SCM l)
629 {
630   Slice s;
631   s.set_empty ();
632   for (; scm_is_pair (l); l = scm_cdr (l))
633     if (scm_is_number (scm_car (l)))
634       s.add_point (scm_to_int (scm_car (l)));
635   return s;
636 }
637
638 /* Return I-th element, or last elt L. If I < 0, then we take the first
639    element.
640
641    PRE: length (L) > 0  */
642 SCM
643 robust_list_ref (int i, SCM l)
644 {
645   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
646     l = scm_cdr (l);
647   return scm_car (l);
648 }
649
650 Real
651 robust_scm2double (SCM k, double x)
652 {
653   if (scm_is_number (k))
654     x = scm_to_double (k);
655   return x;
656 }
657
658 Interval
659 robust_scm2interval (SCM k, Drul_array<Real> v)
660 {
661   Interval i;
662   i[LEFT]= v[LEFT];
663   i[RIGHT]= v[RIGHT];
664   if (is_number_pair (k))
665     i = ly_scm2interval (k);
666   return i;
667 }
668
669 Drul_array<Real>
670 robust_scm2drul (SCM k, Drul_array<Real> v)
671 {
672   if (is_number_pair (k))
673     v = ly_scm2interval (k);
674   return v;
675 }
676
677 Offset
678 robust_scm2offset (SCM k, Offset o)
679 {
680   if (is_number_pair (k))
681     o = ly_scm2offset (k);
682   return o;
683 }
684
685 int
686 robust_scm2int (SCM k, int o)
687 {
688   if (scm_integer_p (k) == SCM_BOOL_T)
689     o = scm_to_int (k);
690   return o;
691 }
692
693 SCM
694 alist_to_hashq (SCM alist)
695 {
696   int i = scm_ilength (alist);
697   if (i < 0)
698     return scm_c_make_hash_table (0);
699
700   SCM tab = scm_c_make_hash_table (i);
701   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
702     {
703       SCM pt = scm_cdar (s);
704       scm_hashq_set_x (tab, scm_caar (s), pt);
705     }
706   return tab;
707 }
708
709 bool
710 alist_equal_p (SCM a, SCM b)
711 {
712   for (SCM s = a;
713        scm_is_pair (s); s = scm_cdr (s))
714     {
715       SCM key = scm_caar (s);
716       SCM val = scm_cdar (s);
717       SCM l = scm_assoc (key, b);
718
719       if (l == SCM_BOOL_F
720           || !ly_c_equal_p (scm_cdr (l), val))
721
722         return false;
723     }
724   return true;
725 }
726
727 SCM
728 ly_alist_vals (SCM alist)
729 {
730   SCM x = SCM_EOL;
731   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
732     {
733       x = scm_cons (scm_cdar (p), x);
734     }
735   return x;
736 }
737
738 SCM
739 ly_hash2alist (SCM tab)
740 {
741   SCM func = ly_lily_module_constant ("hash-table->alist");
742   return scm_call_1 (func, tab);
743 }
744