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