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