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