]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
* lily/tie-column.cc (set_manual_tie_configuration): new function.
[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 using namespace std;
17
18 #include "config.hh"
19
20 #include "dimensions.hh"
21 #include "direction.hh"
22 #include "file-path.hh"
23 #include "international.hh"
24 #include "libc-extension.hh"
25 #include "main.hh"
26 #include "misc.hh"
27 #include "offset.hh"
28 #include "pitch.hh"
29 #include "string-convert.hh"
30 #include "source-file.hh"
31 #include "version.hh"
32 #include "warn.hh"
33
34 // #define TEST_GC
35
36 SCM
37 ly_to_symbol (SCM scm)
38 {
39   return scm_string_to_symbol (ly_to_string (scm));
40 }
41
42 SCM
43 ly_to_string (SCM scm)
44 {
45   return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
46
47                      scm_makfrom0str ("~S"), scm);
48 }
49
50 SCM
51 ly_last (SCM list)
52 {
53   return scm_car (scm_last_pair (list));
54 }
55
56 SCM
57 ly_write2scm (SCM s)
58 {
59   SCM port = scm_mkstrport (SCM_INUM0,
60                             scm_make_string (SCM_INUM0, SCM_UNDEFINED),
61                             SCM_OPN | SCM_WRTNG,
62                             "ly_write2string");
63   //  SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
64   SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
65
66   // scm_apply (write, port, SCM_EOL);
67   scm_call_2 (write, s, port);
68   return scm_strport_to_string (port);
69 }
70
71 SCM
72 ly_quote_scm (SCM s)
73 {
74   return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
75 }
76
77 String
78 ly_symbol2string (SCM s)
79 {
80   /*
81     Ugh. this is not very efficient.
82   */
83   SCM str = scm_symbol_to_string (s);
84   return ly_scm2string (str);
85 }
86
87 String
88 gulp_file_to_string (String fn, bool must_exist, int size)
89 {
90   String s = global_path.find (fn);
91   if (s == "")
92     {
93       if (must_exist)
94         {
95           String e = _f ("can't find file: `%s'", fn);
96           e += " ";
97           e += _f ("(load path: `%s')", global_path.to_string ());
98           error (e);
99           /* unreachable */
100         }
101       return s;
102     }
103
104   if (be_verbose_global)
105     progress_indication ("[" + s);
106
107   int n = size;
108   char *str = gulp_file (s, &n);
109   String result ((Byte *) str, n);
110   delete[] str;
111
112   if (be_verbose_global)
113     progress_indication ("]");
114
115   return result;
116 }
117
118 extern "C" {
119   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
120   void
121   ly_display_scm (SCM s)
122   {
123     scm_display (s, scm_current_output_port ());
124     scm_newline (scm_current_output_port ());
125   }
126 };
127
128 String
129 ly_scm2string (SCM str)
130 {
131   assert (scm_is_string (str));
132   return String ((Byte *)scm_i_string_chars (str),
133                  (int) scm_i_string_length (str));
134 }
135
136 char *
137 ly_scm2newstr (SCM str, size_t *lenp)
138 {
139   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
140
141   size_t len = scm_i_string_length (str);
142   if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
143     {
144       memcpy (new_str, scm_i_string_chars (str), len);
145       new_str[len] = '\0';
146
147       if (lenp)
148         *lenp = len;
149
150       return new_str;
151     }
152   return 0;
153 }
154
155 SCM
156 index_get_cell (SCM s, Direction d)
157 {
158
159   assert (d);
160   return (d == LEFT) ? scm_car (s) : scm_cdr (s);
161 }
162
163 SCM
164 index_set_cell (SCM s, Direction d, SCM v)
165 {
166   if (d == LEFT)
167     scm_set_car_x (s, v);
168   else if (d == RIGHT)
169     scm_set_cdr_x (s, v);
170   return s;
171 }
172
173 bool
174 is_number_pair (SCM p)
175 {
176   return scm_is_pair (p)
177     && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
178 }
179
180 typedef void (*Void_fptr) ();
181 Array<Void_fptr> *scm_init_funcs_;
182
183 void add_scm_init_func (void (*f) ())
184 {
185   if (!scm_init_funcs_)
186     scm_init_funcs_ = new Array<Void_fptr>;
187
188   scm_init_funcs_->push (f);
189 }
190
191 #if KPATHSEA
192 extern "C" {
193   void initialize_kpathsea ();
194 }
195 #endif
196
197 void
198 ly_init_ly_module (void *)
199 {
200   for (int i = scm_init_funcs_->size (); i--;)
201     (scm_init_funcs_->elem (i)) ();
202
203   if (be_verbose_global)
204     progress_indication ("\n");
205
206 #if KPATHSEA
207   if (is_TeX_format_global)
208     initialize_kpathsea ();
209 #endif
210
211   scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
212 }
213
214 SCM global_lily_module;
215
216 void
217 ly_c_init_guile ()
218 {
219   global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
220   scm_c_use_module ("lily");
221 }
222
223 unsigned int
224 ly_scm_hash (SCM s)
225 {
226   return scm_ihashv (s, ~1u);
227 }
228
229 bool
230 is_direction (SCM s)
231 {
232   if (scm_is_number (s))
233     {
234       int i = scm_to_int (s);
235       return i >= -1 && i <= 1;
236     }
237   return false;
238 }
239
240 bool
241 is_axis (SCM s)
242 {
243   if (scm_is_number (s))
244     {
245       int i = scm_to_int (s);
246       return i == 0 || i == 1;
247     }
248   return false;
249 }
250
251 Direction
252 to_dir (SCM s)
253 {
254   return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
255 }
256
257 Interval
258 ly_scm2interval (SCM p)
259 {
260   return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
261 }
262
263 Drul_array<Real>
264 ly_scm2realdrul (SCM p)
265 {
266   return Drul_array<Real> (scm_to_double (scm_car (p)),
267                            scm_to_double (scm_cdr (p)));
268 }
269
270 SCM
271 ly_interval2scm (Drul_array<Real> i)
272 {
273   return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
274 }
275
276 bool
277 to_boolean (SCM s)
278 {
279   return scm_is_bool (s) && ly_scm2bool (s);
280 }
281
282 /* Appendable list L: the cdr contains the list, the car the last cons
283    in the list.  */
284 SCM
285 appendable_list ()
286 {
287   SCM s = scm_cons (SCM_EOL, SCM_EOL);
288   scm_set_car_x (s, s);
289
290   return s;
291 }
292
293 void
294 appendable_list_append (SCM l, SCM elt)
295 {
296   SCM newcons = scm_cons (elt, SCM_EOL);
297
298   scm_set_cdr_x (scm_car (l), newcons);
299   scm_set_car_x (l, newcons);
300 }
301
302 SCM
303 ly_offset2scm (Offset o)
304 {
305   return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
306 }
307
308 Offset
309 ly_scm2offset (SCM s)
310 {
311   return Offset (scm_to_double (scm_car (s)),
312                  scm_to_double (scm_cdr (s)));
313 }
314
315 SCM
316 ly_deep_copy (SCM src)
317 {
318   if (scm_is_pair (src))
319     return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
320   else if (scm_is_vector (src))
321     {
322       int len = scm_c_vector_length (src);
323       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
324       for (int i = 0;i < len; i++)
325         {
326           SCM si = scm_from_int (i);
327           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
328         }
329     }
330   return src;
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     x = scm_cons (scm_cdar (p), x);
690   return x;
691 }
692
693 SCM
694 ly_hash2alist (SCM tab)
695 {
696   SCM func = ly_lily_module_constant ("hash-table->alist");
697   return scm_call_1 (func, tab);
698 }
699