]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
* scm/ps-to-png.scm: remove dir-re 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
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, int size)
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 = size;
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 /* 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)
355           && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
356         return scm_car (alist);
357       return ly_assoc_cdr (key, scm_cdr (alist));
358     }
359   return SCM_BOOL_F;
360 }
361
362 SCM
363 ly_string_array_to_scm (Array<String> a)
364 {
365   SCM s = SCM_EOL;
366   for (int i = a.size () - 1; i >= 0; i--)
367     s = scm_cons (ly_symbol2scm (a[i].to_str0 ()), s);
368   return s;
369 }
370
371 /* SYMBOLS is a whitespace separated list.  */
372 SCM
373 parse_symbol_list (char const *symbols)
374 {
375   while (isspace (*symbols))
376     *symbols++;
377   String s = symbols;
378   s.substitute ('\n', ' ');
379   s.substitute ('\t', ' ');
380   return ly_string_array_to_scm (String_convert::split (s, ' '));
381 }
382
383 SCM
384 ly_truncate_list (int k, SCM lst)
385 {
386   if (k == 0)
387     lst = SCM_EOL;
388   else
389     {
390       SCM s = lst;
391       k--;
392       for (; scm_is_pair (s) && k--; s = scm_cdr (s))
393         ;
394
395       if (scm_is_pair (s))
396         scm_set_cdr_x (s, SCM_EOL);
397     }
398   return lst;
399 }
400
401 String
402 print_scm_val (SCM val)
403 {
404   String realval = ly_scm2string (ly_write2scm (val));
405   if (realval.length () > 200)
406     realval = realval.left_string (100)
407       + "\n :\n :\n"
408       + realval.right_string (100);
409   return realval;
410 }
411
412 bool
413 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
414 {
415   bool ok = true;
416
417   /*
418     Always succeeds.
419
420
421     TODO: should remove #f from allowed vals?
422   */
423   if (val == SCM_EOL || val == SCM_BOOL_F)
424     return ok;
425
426   if (!scm_is_symbol (sym))
427 #if 0
428     return false;
429 #else
430   /*
431     This is used for autoBeamSettings.
432
433     TODO: deprecate the use of \override and \revert for
434     autoBeamSettings?
435
436     or use a symbol autoBeamSettingS?
437   */
438   return true;
439 #endif
440
441   SCM type = scm_object_property (sym, type_symbol);
442
443   if (type != SCM_EOL && !ly_is_procedure (type))
444     {
445       warning (_f ("can't find property type-check for `%s' (%s).",
446                    ly_symbol2string (sym).to_str0 (),
447                    ly_symbol2string (type_symbol).to_str0 ())
448                + "  " + _ ("perhaps a typing error?"));
449
450       /* Be strict when being anal :) */
451       if (do_internal_type_checking_global)
452         abort ();
453
454       warning (_ ("doing assignment anyway"));
455     }
456   else
457     {
458       if (val != SCM_EOL
459           && ly_is_procedure (type)
460           && scm_call_1 (type, val) == SCM_BOOL_F)
461         {
462           ok = false;
463           SCM typefunc = ly_lily_module_constant ("type-name");
464           SCM type_name = scm_call_1 (typefunc, type);
465
466           message (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
467                        ly_symbol2string (sym).to_str0 (),
468                        print_scm_val (val),
469                        ly_scm2string (type_name).to_str0 ()));
470           progress_indication ("\n");
471         }
472     }
473   return ok;
474 }
475
476 /* some SCM abbrevs
477
478 zijn deze nou handig?
479 zijn ze er al in scheme, maar heten ze anders? */
480
481 /* Remove doubles from (sorted) list */
482 SCM
483 ly_unique (SCM list)
484 {
485   SCM unique = SCM_EOL;
486   for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
487     {
488       if (!scm_is_pair (scm_cdr (i))
489           || !ly_is_equal (scm_car (i), scm_cadr (i)))
490         unique = scm_cons (scm_car (i), unique);
491     }
492   return scm_reverse_x (unique, SCM_EOL);
493 }
494
495 static int
496 scm_default_compare (void const *a, void const *b)
497 {
498   SCM pa = *(SCM *) a;
499   SCM pb = *(SCM *) b;
500   if (pa == pb)
501     return 0;
502   return pa < pb ? -1 : 1;
503 }
504
505 /*  Modify LST in place: qsort it.  */
506 SCM
507 ly_list_qsort_uniq_x (SCM lst)
508 {
509   int len = scm_ilength (lst);
510   SCM *arr = new SCM[len];
511   int k = 0;
512   for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
513     arr[k++] = scm_car (s);
514
515   assert (k == len);
516   qsort (arr, len, sizeof (SCM), &scm_default_compare);
517
518   SCM *tail = &lst;
519   for (int i = 0; i < len; i++)
520     if (!i || arr[i] != arr[i - 1])
521       {
522         SCM_SETCAR (*tail, arr[i]);
523         tail = SCM_CDRLOC (*tail);
524       }
525
526   *tail = SCM_EOL;
527   delete[] arr;
528
529   return lst;
530 }
531
532 /* tail add */
533 SCM
534 ly_snoc (SCM s, SCM list)
535 {
536   return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
537 }
538
539 /* Split list at member s, removing s.
540    Return (BEFORE . AFTER)  */
541 SCM
542 ly_split_list (SCM s, SCM list)
543 {
544   SCM before = SCM_EOL;
545   SCM after = list;
546   for (; scm_is_pair (after);)
547     {
548       SCM i = scm_car (after);
549       after = scm_cdr (after);
550       if (ly_is_equal (i, s))
551         break;
552       before = scm_cons (i, before);
553     }
554   return scm_cons (scm_reverse_x (before, SCM_EOL), after);
555 }
556
557 void
558 taint (SCM *)
559 {
560   /*
561     nop.
562   */
563 }
564
565 /*
566   display stuff without using stack
567 */
568 SCM
569 display_list (SCM s)
570 {
571   SCM p = scm_current_output_port ();
572
573   scm_puts ("(", p);
574   for (; scm_is_pair (s); s = scm_cdr (s))
575     {
576       scm_display (scm_car (s), p);
577       scm_puts (" ", p);
578     }
579   scm_puts (")", p);
580   return SCM_UNSPECIFIED;
581 }
582
583 Slice
584 int_list_to_slice (SCM l)
585 {
586   Slice s;
587   s.set_empty ();
588   for (; scm_is_pair (l); l = scm_cdr (l))
589     if (scm_is_number (scm_car (l)))
590       s.add_point (scm_to_int (scm_car (l)));
591   return s;
592 }
593
594 /* Return I-th element, or last elt L. If I < 0, then we take the first
595    element.
596
597    PRE: length (L) > 0  */
598 SCM
599 robust_list_ref (int i, SCM l)
600 {
601   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
602     l = scm_cdr (l);
603   return scm_car (l);
604 }
605
606 Real
607 robust_scm2double (SCM k, double x)
608 {
609   if (scm_is_number (k))
610     x = scm_to_double (k);
611   return x;
612 }
613
614 Interval
615 robust_scm2interval (SCM k, Drul_array<Real> v)
616 {
617   Interval i;
618   i[LEFT] = v[LEFT];
619   i[RIGHT] = v[RIGHT];
620   if (is_number_pair (k))
621     i = ly_scm2interval (k);
622   return i;
623 }
624
625 Drul_array<Real>
626 robust_scm2drul (SCM k, Drul_array<Real> v)
627 {
628   if (is_number_pair (k))
629     v = ly_scm2interval (k);
630   return v;
631 }
632
633 Offset
634 robust_scm2offset (SCM k, Offset o)
635 {
636   if (is_number_pair (k))
637     o = ly_scm2offset (k);
638   return o;
639 }
640
641 int
642 robust_scm2int (SCM k, int o)
643 {
644   if (scm_integer_p (k) == SCM_BOOL_T)
645     o = scm_to_int (k);
646   return o;
647 }
648
649 SCM
650 alist_to_hashq (SCM alist)
651 {
652   int i = scm_ilength (alist);
653   if (i < 0)
654     return scm_c_make_hash_table (0);
655
656   SCM tab = scm_c_make_hash_table (i);
657   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
658     {
659       SCM pt = scm_cdar (s);
660       scm_hashq_set_x (tab, scm_caar (s), pt);
661     }
662   return tab;
663 }
664
665 bool
666 alist_equal_p (SCM a, SCM b)
667 {
668   for (SCM s = a;
669        scm_is_pair (s); s = scm_cdr (s))
670     {
671       SCM key = scm_caar (s);
672       SCM val = scm_cdar (s);
673       SCM l = scm_assoc (key, b);
674
675       if (l == SCM_BOOL_F
676           || !ly_is_equal (scm_cdr (l), val))
677
678         return false;
679     }
680   return true;
681 }
682
683 SCM
684 ly_alist_vals (SCM alist)
685 {
686   SCM x = SCM_EOL;
687   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
688     x = scm_cons (scm_cdar (p), x);
689   return x;
690 }
691
692 SCM
693 ly_hash2alist (SCM tab)
694 {
695   SCM func = ly_lily_module_constant ("hash-table->alist");
696   return scm_call_1 (func, tab);
697 }
698