]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
* flower/include/international.hh: Bugfix: include "string.hh".
[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 "lily-guile.hh"
11
12 #include <cstdio>
13 #include <cstdlib>
14 #include <cstring> /* strdup, strchr */
15 #include <cctype>
16
17 #include "config.hh"
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 "source-file.hh"
29 #include "version.hh"
30 #include "warn.hh"
31
32 // #define TEST_GC
33
34 SCM
35 ly_to_symbol (SCM scm)
36 {
37   return scm_string_to_symbol (ly_to_string (scm));
38 }
39
40 SCM
41 ly_to_string (SCM scm)
42 {
43   return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
44                      scm_makfrom0str ("~S"), scm);
45 }
46
47 SCM
48 ly_last (SCM list)
49 {
50   return scm_car (scm_last_pair (list));
51 }
52
53 SCM
54 ly_write2scm (SCM s)
55 {
56   SCM port = scm_mkstrport (SCM_INUM0,
57                             scm_make_string (SCM_INUM0, SCM_UNDEFINED),
58                             SCM_OPN | SCM_WRTNG,
59                             "ly_write2string");
60   //  SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
61   SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
62
63   // scm_apply (write, port, SCM_EOL);
64   scm_call_2 (write, s, port);
65   return scm_strport_to_string (port);
66 }
67
68 SCM
69 ly_quote_scm (SCM s)
70 {
71   return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
72 }
73
74 String
75 ly_symbol2string (SCM s)
76 {
77   /*
78     Ugh. this is not very efficient.
79   */
80   SCM str = scm_symbol_to_string (s);
81   return ly_scm2string (str);
82 }
83
84 String
85 gulp_file_to_string (String fn, bool must_exist)
86 {
87   String s = global_path.find (fn);
88   if (s == "")
89     {
90       if (must_exist)
91         {
92           String e = _f ("can't find file: `%s'", fn);
93           e += " ";
94           e += _f ("(load path: `%s')", global_path.to_string ());
95           error (e);
96           /* unreachable */
97         }
98       return s;
99     }
100
101   if (be_verbose_global)
102     progress_indication ("[" + s);
103
104   int n;
105   char *str = gulp_file (s, &n);
106   String result ((Byte *) str, n);
107   delete[] str;
108
109   if (be_verbose_global)
110     progress_indication ("]");
111
112   return result;
113 }
114
115 extern "C" {
116   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
117   void
118   ly_display_scm (SCM s)
119   {
120     scm_display (s, scm_current_output_port ());
121     scm_newline (scm_current_output_port ());
122   }
123 };
124
125 String
126 ly_scm2string (SCM str)
127 {
128   assert (scm_is_string (str));
129   return String ((Byte *)scm_i_string_chars (str),
130                  (int) scm_i_string_length (str));
131 }
132
133 char *
134 ly_scm2newstr (SCM str, size_t *lenp)
135 {
136   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
137
138   size_t len = SCM_STRING_LENGTH (str);
139   if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
140     {
141       memcpy (new_str, scm_i_string_chars (str), len);
142       new_str[len] = '\0';
143
144       if (lenp)
145         *lenp = len;
146       
147       return new_str;
148     }
149   return 0;
150 }
151
152 SCM
153 index_get_cell (SCM s, Direction d)
154 {
155   
156   assert (d);
157   return (d == LEFT) ? scm_car (s) : scm_cdr (s);
158 }
159
160 SCM
161 index_set_cell (SCM s, Direction d, SCM v)
162 {
163   if (d == LEFT)
164     scm_set_car_x (s, v);
165   else if (d == RIGHT)
166     scm_set_cdr_x (s, v);
167   return s;
168 }
169   
170 bool
171 is_number_pair (SCM p)
172 {
173   return scm_is_pair (p)
174     && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
175 }
176
177 typedef void (*Void_fptr) ();
178 Array<Void_fptr> *scm_init_funcs_;
179
180 void add_scm_init_func (void (*f) ())
181 {
182   if (!scm_init_funcs_)
183     scm_init_funcs_ = new Array<Void_fptr>;
184
185   scm_init_funcs_->push (f);
186 }
187
188 #if KPATHSEA
189 extern "C" {
190   void initialize_kpathsea ();
191 }
192 #endif
193
194 void
195 ly_init_ly_module (void *)
196 {
197   for (int i = scm_init_funcs_->size (); i--;)
198     (scm_init_funcs_->elem (i)) ();
199
200   if (be_verbose_global)
201     progress_indication ("\n");
202
203 #if KPATHSEA
204   if (is_TeX_format_global)
205     initialize_kpathsea ();
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
331
332 /* looks the key up in the cdrs of the alist-keys
333    - ignoring the car and ignoring non-pair keys.
334    Returns first match found, i.e.
335
336    alist = ((1 . 10)
337    ((1 . 2) . 11)
338    ((2 . 1) . 12)
339    ((3 . 0) . 13)
340    ((4 . 1) . 14) )
341
342    I would like (ly_assoc_cdr 1) to return 12 - because it's the first
343    element with the cdr of the key = 1.  In other words (alloc_cdr key)
344    corresponds to call
345
346    (alloc (anything . key))
347 */
348 SCM
349 ly_assoc_cdr (SCM key, SCM alist)
350 {
351   if (scm_is_pair (alist))
352     {
353       SCM trykey = scm_caar (alist);
354       if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
355         return scm_car (alist);
356       else
357         return ly_assoc_cdr (key, scm_cdr (alist));
358     }
359   return SCM_BOOL_F;
360 }
361
362 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
363    i.e. \n and ' ' can be used interchangeably as separators.  */
364 SCM
365 parse_symbol_list (char const *lst)
366 {
367   char *s = strdup (lst);
368   char *orig = s;
369   SCM create_list = SCM_EOL;
370
371   char *e = s + strlen (s) - 1;
372   while (e >= s && isspace (*e))
373     *e-- = 0;
374
375   for (char *p = s; *p; p++)
376     if (*p == '\n')
377       *p = ' ';
378   
379   if (!s[0])
380     s = 0;
381   
382   while (s)
383     {
384       char *next = strchr (s, ' ');
385       if (next)
386         *next++ = 0;
387
388       create_list = scm_cons (ly_symbol2scm (s), create_list);
389       s = next;
390     }
391
392   free (orig);
393   return create_list;
394 }
395
396 SCM
397 ly_truncate_list (int k, SCM lst)
398 {
399   if (k == 0)
400     lst = SCM_EOL;
401   else
402     {
403       SCM s = lst;
404       k--;
405       for (; scm_is_pair (s) && k--; s = scm_cdr (s))
406         ;
407
408       if (scm_is_pair (s))
409         scm_set_cdr_x (s, SCM_EOL);
410     }
411   return lst;
412 }
413
414 String
415 print_scm_val (SCM val)
416 {
417   String realval = ly_scm2string (ly_write2scm (val));
418   if (realval.length () > 200)
419     realval = realval.left_string (100)
420       + "\n :\n :\n"
421       + realval.right_string (100);
422   return realval;
423 }
424
425 bool
426 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
427 {
428   bool ok = true;
429
430   /*
431     Always succeeds.
432
433
434     TODO: should remove #f from allowed vals?
435   */
436   if (val == SCM_EOL || val == SCM_BOOL_F)
437     return ok;
438
439   if (!scm_is_symbol (sym))
440 #if 0
441     return false;
442 #else
443   /*
444     This is used for autoBeamSettings.
445
446     TODO: deprecate the use of \override and \revert for
447     autoBeamSettings?
448
449     or use a symbol autoBeamSettingS?
450   */
451   return true;
452 #endif
453
454   SCM type = scm_object_property (sym, type_symbol);
455
456   if (type != SCM_EOL && !ly_c_procedure_p (type))
457     {
458       warning (_f ("can't find property type-check for `%s' (%s).",
459                    ly_symbol2string (sym).to_str0 (),
460                    ly_symbol2string (type_symbol).to_str0 ())
461                + "  " + _ ("perhaps a typing error?"));
462
463       /* Be strict when being anal :) */
464       if (do_internal_type_checking_global)
465         abort ();
466
467       warning (_ ("doing assignment anyway"));
468     }
469   else
470     {
471       if (val != SCM_EOL
472           && ly_c_procedure_p (type)
473           && scm_call_1 (type, val) == SCM_BOOL_F)
474         {
475           ok = false;
476           SCM typefunc = ly_lily_module_constant ("type-name");
477           SCM type_name = scm_call_1 (typefunc, type);
478
479           message (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
480                        ly_symbol2string (sym).to_str0 (),
481                        print_scm_val (val),
482                        ly_scm2string (type_name).to_str0 ()));
483           progress_indication ("\n");
484         }
485     }
486   return ok;
487 }
488
489 /* some SCM abbrevs
490
491 zijn deze nou handig?
492 zijn ze er al in scheme, maar heten ze anders? */
493
494 /* Remove doubles from (sorted) list */
495 SCM
496 ly_unique (SCM list)
497 {
498   SCM unique = SCM_EOL;
499   for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
500     {
501       if (!scm_is_pair (scm_cdr (i))
502           || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
503         unique = scm_cons (scm_car (i), unique);
504     }
505   return scm_reverse_x (unique, SCM_EOL);
506 }
507
508 static int
509 scm_default_compare (void const *a, void const *b)
510 {
511   SCM pa = *(SCM *) a;
512   SCM pb = *(SCM *) b;
513   if (pa == pb)
514     return 0;
515   return pa < pb ? -1 : 1;
516 }
517
518 /*  Modify LST in place: qsort it.  */
519 SCM
520 ly_list_qsort_uniq_x (SCM lst)
521 {
522   int len = scm_ilength (lst);
523   SCM *arr = new SCM[len];
524   int k = 0;
525   for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
526     arr[k++] = scm_car (s);
527
528   assert (k == len);
529   qsort (arr, len, sizeof (SCM), &scm_default_compare);
530
531   SCM *tail = &lst;
532   for (int i = 0; i < len; i++)
533     if (!i || arr[i] != arr[i - 1])
534       {
535         SCM_SETCAR (*tail, arr[i]);
536         tail = SCM_CDRLOC (*tail);
537       }
538
539   *tail = SCM_EOL;
540   delete[] arr;
541
542   return lst;
543 }
544
545 /* tail add */
546 SCM
547 ly_snoc (SCM s, SCM list)
548 {
549   return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
550 }
551
552 /* Split list at member s, removing s.
553    Return (BEFORE . AFTER)  */
554 SCM
555 ly_split_list (SCM s, SCM list)
556 {
557   SCM before = SCM_EOL;
558   SCM after = list;
559   for (; scm_is_pair (after);)
560     {
561       SCM i = scm_car (after);
562       after = scm_cdr (after);
563       if (ly_c_equal_p (i, s))
564         break;
565       before = scm_cons (i, before);
566     }
567   return scm_cons (scm_reverse_x (before, SCM_EOL), after);
568 }
569
570 void
571 taint (SCM *)
572 {
573   /*
574     nop.
575   */
576 }
577
578 /*
579   display stuff without using stack
580 */
581 SCM
582 display_list (SCM s)
583 {
584   SCM p = scm_current_output_port ();
585
586   scm_puts ("(", p);
587   for (; scm_is_pair (s); s = scm_cdr (s))
588     {
589       scm_display (scm_car (s), p);
590       scm_puts (" ", p);
591     }
592   scm_puts (")", p);
593   return SCM_UNSPECIFIED;
594 }
595
596 Slice
597 int_list_to_slice (SCM l)
598 {
599   Slice s;
600   s.set_empty ();
601   for (; scm_is_pair (l); l = scm_cdr (l))
602     if (scm_is_number (scm_car (l)))
603       s.add_point (scm_to_int (scm_car (l)));
604   return s;
605 }
606
607 /* Return I-th element, or last elt L. If I < 0, then we take the first
608    element.
609
610    PRE: length (L) > 0  */
611 SCM
612 robust_list_ref (int i, SCM l)
613 {
614   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
615     l = scm_cdr (l);
616   return scm_car (l);
617 }
618
619 Real
620 robust_scm2double (SCM k, double x)
621 {
622   if (scm_is_number (k))
623     x = scm_to_double (k);
624   return x;
625 }
626
627 Interval
628 robust_scm2interval (SCM k, Drul_array<Real> v)
629 {
630   Interval i;
631   i[LEFT] = v[LEFT];
632   i[RIGHT] = v[RIGHT];
633   if (is_number_pair (k))
634     i = ly_scm2interval (k);
635   return i;
636 }
637
638 Drul_array<Real>
639 robust_scm2drul (SCM k, Drul_array<Real> v)
640 {
641   if (is_number_pair (k))
642     v = ly_scm2interval (k);
643   return v;
644 }
645
646 Offset
647 robust_scm2offset (SCM k, Offset o)
648 {
649   if (is_number_pair (k))
650     o = ly_scm2offset (k);
651   return o;
652 }
653
654 int
655 robust_scm2int (SCM k, int o)
656 {
657   if (scm_integer_p (k) == SCM_BOOL_T)
658     o = scm_to_int (k);
659   return o;
660 }
661
662 SCM
663 alist_to_hashq (SCM alist)
664 {
665   int i = scm_ilength (alist);
666   if (i < 0)
667     return scm_c_make_hash_table (0);
668
669   SCM tab = scm_c_make_hash_table (i);
670   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
671     {
672       SCM pt = scm_cdar (s);
673       scm_hashq_set_x (tab, scm_caar (s), pt);
674     }
675   return tab;
676 }
677
678 bool
679 alist_equal_p (SCM a, SCM b)
680 {
681   for (SCM s = a;
682        scm_is_pair (s); s = scm_cdr (s))
683     {
684       SCM key = scm_caar (s);
685       SCM val = scm_cdar (s);
686       SCM l = scm_assoc (key, b);
687
688       if (l == SCM_BOOL_F
689           || !ly_c_equal_p (scm_cdr (l), val))
690
691         return false;
692     }
693   return true;
694 }
695
696 SCM
697 ly_alist_vals (SCM alist)
698 {
699   SCM x = SCM_EOL;
700   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
701     {
702       x = scm_cons (scm_cdar (p), x);
703     }
704   return x;
705 }
706
707 SCM
708 ly_hash2alist (SCM tab)
709 {
710   SCM func = ly_lily_module_constant ("hash-table->alist");
711   return scm_call_1 (func, tab);
712 }
713