]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
* VERSION: 1.5.72 released
[lilypond.git] / lily / lily-guile.cc
1 /*
2   lily-guile.cc -- implement assorted guile functions
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2002 Jan Nieuwenhuizen <janneke@gnu.org>
7
8   Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 */
10
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h>   /* isinf */
15 #include <string.h> /* strdup, strchr */
16 #include "libc-extension.hh"
17 #include "lily-guile.hh"
18 #include "main.hh"
19 #include "simple-file-storage.hh"
20 #include "file-path.hh"
21 #include "warn.hh"
22 #include "direction.hh"
23 #include "offset.hh"
24 #include "interval.hh"
25 #include "pitch.hh"
26 #include "dimensions.hh"
27
28
29 // #define TEST_GC
30
31 #ifdef PARANOID
32 #include <libguile/gc.h>
33 #undef gh_pair_p
34 bool
35 ly_pair_p (SCM x)
36 {
37 #if 0
38   assert (!SCM_CONSP (x) || (*(scm_t_bits*) SCM2PTR (SCM_CAR (x))) != scm_tc_free_cell);
39   assert (!SCM_CONSP (x) || (*(scm_t_bits*) SCM2PTR (SCM_CDR (x))) != scm_tc_free_cell);
40 #elif GUILE_MINOR_VERSION < 5
41   assert (!SCM_CONSP (x) || !SCM_FREEP (SCM_CAR (x)));
42   assert (!SCM_CONSP (x) || !SCM_FREEP (SCM_CDR (x)));
43 #else
44   assert (!SCM_CONSP (x) || !SCM_FREE_CELL_P (SCM_CAR (x)));
45   assert (!SCM_CONSP (x) || !SCM_FREE_CELL_P (SCM_CDR (x)));
46 #endif  
47   //return SCM_NFALSEP (scm_pair_p (x));
48   return gh_pair_p (x); 
49 }
50 #define gh_pair_p ly_pair_p
51 #endif
52
53 SCM
54 ly_last (SCM list)
55 {
56   return ly_car (scm_last_pair (list));
57 }
58
59
60 SCM
61 ly_write2scm (SCM s)
62 {
63   SCM port = scm_mkstrport (SCM_INUM0, 
64                             scm_make_string (SCM_INUM0, SCM_UNDEFINED),
65                             SCM_OPN | SCM_WRTNG,
66                             "ly_write2string");
67   //  SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
68   SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
69   
70   // scm_apply (write, port, SCM_EOL);
71   gh_call2 (write, s, port);
72   return scm_strport_to_string (port);
73 }
74
75
76 SCM
77 ly_quote_scm (SCM s)
78 {
79   return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
80 }
81
82
83
84 String
85 ly_symbol2string (SCM s)
86 {
87   assert (gh_symbol_p (s));
88   return String ((Byte*)SCM_STRING_CHARS (s), (int) SCM_STRING_LENGTH (s));
89 }
90
91
92 String
93 gulp_file_to_string (String fn)
94 {
95   String s = global_path.find (fn);
96   if (s == "")
97     {
98       String e = _f ("can't find file: `%s'", fn);
99       e += " ";
100       e += _f ("(load path: `%s')", global_path.string ());
101       error (e);
102     }
103   else if (verbose_global_b)
104     progress_indication ("[" + s);
105
106
107   Simple_file_storage f (s);
108   String result (f.to_str0 ());
109   if (verbose_global_b)
110     progress_indication ("]");
111   return result;
112 }
113
114 LY_DEFINE(ly_gulp_file, "ly-gulp-file", 1,0, 0,
115           (SCM name),
116           "Read the file named @var{name}, and return its contents in a string. The
117 file is looked up using the lilypond search path.
118
119 ")
120 {
121   return scm_makfrom0str (gulp_file_to_string (ly_scm2string (name)).to_str0 ());
122 }
123
124
125 extern "C" {
126   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
127 void
128 ly_display_scm (SCM s)
129 {
130   gh_display (s);
131   gh_newline ();
132 }
133 };
134
135 String
136 ly_scm2string (SCM s)
137 {
138   assert (gh_string_p (s));
139
140   char *p = SCM_STRING_CHARS(s);
141   String r (p);
142   return r;
143 }
144
145 SCM
146 index_get_cell (SCM s, Direction d)
147 {
148   
149   assert (d);
150   return (d == LEFT) ? ly_car (s) : ly_cdr (s);
151 }
152
153 SCM
154 index_set_cell (SCM s, Direction d, SCM v)
155 {
156   if (d == LEFT)
157     gh_set_car_x (s, v);
158   else if (d == RIGHT)
159     gh_set_cdr_x (s, v);
160   return s;
161 }
162   
163 LY_DEFINE(ly_warning,"ly-warn", 1, 0, 0,
164   (SCM str),"Scheme callable function to issue the warning @code{msg}.")
165 {
166   SCM_ASSERT_TYPE (gh_string_p (str), str, SCM_ARG1, __FUNCTION__, "string");
167   warning ("lily-guile: " + ly_scm2string (str));
168   return SCM_BOOL_T;
169 }
170
171 LY_DEFINE(ly_isdir,  "dir?", 1,0, 0,  (SCM s),
172           "type predicate. A direction is a -1, 0 or 1, where -1 represents left or
173 down and 1 represents right or up.
174 ")
175 {
176   if (gh_number_p (s))
177     {
178       int i = gh_scm2int (s);
179       return (i>= -1 && i <= 1)  ? SCM_BOOL_T : SCM_BOOL_F; 
180     }
181   return SCM_BOOL_F;
182 }
183
184 bool
185 ly_number_pair_p (SCM p)
186 {
187   return gh_pair_p (p) && gh_number_p (ly_car (p)) && gh_number_p (ly_cdr (p));
188 }
189
190 typedef void (*Void_fptr) ();
191 Array<Void_fptr> *scm_init_funcs_;
192
193 void add_scm_init_func (void (*f) ())
194 {
195   if (!scm_init_funcs_)
196     scm_init_funcs_ = new Array<Void_fptr>;
197
198   scm_init_funcs_->push (f);
199 }
200
201 extern void init_cxx_function_smobs ();
202
203
204 void
205 ly_init_guile ()
206 {
207   SCM last_mod = scm_current_module ();
208   scm_set_current_module (scm_c_resolve_module ("guile"));
209   
210   init_cxx_function_smobs ();
211   for (int i=scm_init_funcs_->size () ; i--;)
212     (scm_init_funcs_->elem (i)) ();
213
214   if (verbose_global_b)
215     progress_indication ("\n");
216
217
218   scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
219
220   scm_set_current_module (last_mod);
221 }
222
223 unsigned int ly_scm_hash (SCM s)
224 {
225   return scm_ihashv (s, ~1u);
226 }
227
228
229
230 bool
231 ly_dir_p (SCM s)
232 {
233   if (gh_number_p (s))
234     {
235       int i = gh_scm2int (s);
236       return i>= -1 && i <= 1; 
237     }
238   return false;
239 }
240
241
242 bool
243 ly_axis_p (SCM s)
244 {
245   if (gh_number_p (s))
246     {
247       int i = gh_scm2int (s);
248       return i== 0 || i == 1;
249     }
250   return false;
251 }
252
253
254 Direction
255 to_dir (SCM s)
256 {
257   return SCM_INUMP (s) ?  (Direction) gh_scm2int (s) : CENTER;
258 }
259
260 Interval
261 ly_scm2interval (SCM p)
262 {
263   return  Interval (gh_scm2double (ly_car (p)),
264                     gh_scm2double (ly_cdr (p)));
265 }
266
267 SCM
268 ly_interval2scm (Drul_array<Real> i)
269 {
270   return gh_cons (gh_double2scm (i[LEFT]),
271                   gh_double2scm (i[RIGHT]));
272 }
273
274
275
276
277 bool
278 to_boolean (SCM s)
279 {
280   return gh_boolean_p (s) && gh_scm2bool (s);
281 }
282
283 /*
284   Appendable list L: the cdr contains the list, the car the last cons
285   in the list.
286  */
287 SCM
288 appendable_list ()
289 {
290   SCM s = gh_cons (SCM_EOL, SCM_EOL);
291   gh_set_car_x (s, s);
292   
293   return s;
294 }
295
296 void
297 appendable_list_append (SCM l, SCM elt)
298 {
299   SCM newcons = gh_cons (elt, SCM_EOL);
300   
301   gh_set_cdr_x (ly_car (l), newcons);      
302   gh_set_car_x (l, newcons);
303 }
304
305
306 SCM
307 ly_offset2scm (Offset o)
308 {
309   return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm (o[Y_AXIS]));
310 }
311
312 Offset
313 ly_scm2offset (SCM s)
314 {
315   return Offset (gh_scm2double (ly_car (s)),
316                  gh_scm2double (ly_cdr (s)));
317 }
318
319
320 /*
321   convert without too many decimals, and leave  a space at the end.
322  */
323    
324    
325 LY_DEFINE(ly_number2string,  "ly-number->string", 1, 0,0,
326           (SCM s),
327           " converts @var{num} to a string without generating many decimals. It
328 leaves a space at the end.
329 ")
330 {
331   SCM_ASSERT_TYPE (gh_number_p (s), s, SCM_ARG1, __FUNCTION__, "number");
332
333   char str[400];                        // ugh.
334
335   if (scm_exact_p (s) == SCM_BOOL_F)
336     {
337       Real r (gh_scm2double (s));
338
339       if (isinf (r) || isnan (r))
340         {
341           programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
342           r = 0.0;
343         }
344
345       sprintf (str, "%8.4f ", r);
346     }
347   else
348     {
349       sprintf (str, "%d ", gh_scm2int (s));
350     }
351
352   return scm_makfrom0str (str);
353 }
354
355 /*
356   Undef this to see if GUILE GC is causing too many swaps.
357  */
358
359 // #define TEST_GC
360
361 #ifdef TEST_GC
362 #include <libguile/gc.h>
363
364 static void *
365 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
366 {
367    fprintf (stderr, "entering sweep\n");
368 }
369
370 static void *
371 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
372 {
373    fprintf (stderr, "leaving sweep\n");
374 }
375 #endif
376
377
378 #include "version.hh"
379 LY_DEFINE(ly_version,  "ly-version", 0, 0, 0, (),
380           "Return the current lilypond version as a list, e.g.
381 @code{(1 3 127 uu1)}. 
382 ")
383 {
384   char const* vs =  "\' (" MAJOR_VERSION " " MINOR_VERSION " "  PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
385   
386   return gh_eval_str ((char*)vs);
387 }
388
389 LY_DEFINE(ly_unit,  "ly-unit", 0, 0, 0, (),
390           "Return the unit used for lengths as a string.")
391 {
392   return scm_makfrom0str (INTERNAL_UNIT);
393 }
394
395 LY_DEFINE(ly_verbose,  "ly-verbose", 0, 0, 0, (),
396   "Return whether lilypond is being run in verbose mode.")
397 {
398   return gh_bool2scm (verbose_global_b);
399 }
400
401 static void
402 init_functions ()
403 {
404 #ifdef TEST_GC 
405   scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
406   scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
407 #endif
408 }
409
410 ADD_SCM_INIT_FUNC (funcs, init_functions);
411
412 SCM
413 ly_deep_copy (SCM src)
414 {
415   if (gh_pair_p (src))
416     {
417       return gh_cons (ly_deep_copy (ly_car (src)), ly_deep_copy (ly_cdr (src)));
418     }
419   else if (gh_vector_p (src))
420     {
421       int  l = SCM_VECTOR_LENGTH (src);
422       SCM nv = scm_c_make_vector (l, SCM_UNDEFINED);
423       for (int i  =0 ; i< l ; i++)
424         {
425           SCM si = gh_int2scm (i);
426           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si))); 
427         }
428     }
429   else
430     return src;
431
432   return src;
433 }
434
435
436
437
438 SCM
439 ly_assoc_chain (SCM key, SCM achain)
440 {
441   if (gh_pair_p (achain))
442     {
443       SCM handle = scm_assoc (key, ly_car (achain));
444       if (gh_pair_p (handle))
445         return handle;
446       else
447         return ly_assoc_chain (key, ly_cdr (achain));
448     }
449   else
450     return SCM_BOOL_F;
451 }
452
453 /* looks the key up in the cdrs of the alist-keys
454    - ignoring the car and ignoring non-pair keys.
455    Returns first match found, i.e.
456
457    alist = ((1 . 10)
458                    ((1 . 2) . 11)
459                    ((2 . 1) . 12)
460                    ((3 . 0) . 13)
461                    ((4 . 1) . 14) )
462
463 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
464 element with the cdr of the key = 1.  In other words (alloc_cdr key)
465 corresponds to call
466
467 (alloc (anything . key))
468
469
470
471 */
472 SCM
473 ly_assoc_cdr (SCM key, SCM alist)
474 {
475   if (gh_pair_p (alist)) {
476     SCM trykey = ly_caar(alist);
477     if(gh_pair_p(trykey) && to_boolean(scm_equal_p(key,ly_cdr(trykey))))
478       return ly_car(alist);
479     else
480       return ly_assoc_cdr (key, ly_cdr (alist));
481   }
482   else
483     return SCM_BOOL_F;
484 }
485
486 /*
487   LIST has the form "sym1 sym2 sym3\nsym4\nsym5"
488
489   i.e. \n and ' ' can be used interchangeably as separators.
490  */
491 SCM
492 parse_symbol_list (const char * list)
493 {
494   char * s = strdup (list);
495   char *orig = s;
496   SCM create_list = SCM_EOL;
497
498   for (char * p = s; *p; p++)
499     {
500       if (*p == '\n')
501         *p = ' ' ;
502     }
503   
504   if (!s[0] )
505     s = 0;
506
507
508   
509   while (s)
510     {
511       char *next = strchr (s, ' ');
512       if (next)
513         *next++ = 0;
514
515       create_list = gh_cons (ly_symbol2scm (s), create_list);
516       s = next;
517     }
518
519   free (orig);
520   return create_list;
521 }
522
523
524 SCM
525 ly_truncate_list (int k, SCM l )
526 {
527   if (k == 0)
528     {
529       l = SCM_EOL;
530     }
531   else
532     {
533       SCM s = l;
534       k--;
535       for (; gh_pair_p (s) && k--; s = ly_cdr (s))
536         ;
537
538       if (gh_pair_p (s))
539         {
540           gh_set_cdr_x (s, SCM_EOL);
541         }
542     }
543   return l;
544 }
545
546
547 String
548 print_scm_val (SCM val)
549 {
550   String realval = ly_scm2string (ly_write2scm (val));
551   if (realval.length () > 200)
552     realval = realval.left_string (100) + "\n :\n :\n" + realval.right_string (100);
553   
554   return realval;        
555 }
556
557 bool
558 type_check_assignment (SCM sym, SCM val,  SCM type_symbol) 
559 {
560   bool ok = true;
561
562   /*
563     Always succeeds.
564
565
566     TODO: should remove #f from allowed vals?
567    */
568   if (val == SCM_EOL || val == SCM_BOOL_F)
569     return ok;
570
571   
572   SCM type = SCM_EOL;
573
574   if (gh_symbol_p (sym))
575     type = scm_object_property (sym, type_symbol);
576
577   if (type != SCM_EOL && !gh_procedure_p (type))
578       {
579         warning (_f ("Can't find property type-check for `%s' (%s).  Perhaps you made a typing error? Doing assignment anyway.",
580                      ly_symbol2string (sym).to_str0 (),
581                      ly_symbol2string (type_symbol).to_str0 ()
582
583                      ));
584       }
585   else
586     {
587       if (val != SCM_EOL
588           && gh_procedure_p (type)
589           && gh_call1 (type, val) == SCM_BOOL_F)
590         {
591           SCM errport = scm_current_error_port ();
592           ok = false;
593           SCM typefunc = scm_primitive_eval (ly_symbol2scm ("type-name"));
594           SCM type_name = gh_call1 (typefunc, type);
595
596          
597           scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
598                         ly_symbol2string (sym).to_str0 (),
599                         print_scm_val (val),
600                         ly_scm2string (type_name).to_str0 ()).to_str0 (),
601                     errport);
602           scm_puts ("\n", errport);                   
603         }
604     }
605   return ok;
606 }
607
608
609 /* some SCM abbrevs
610
611    zijn deze nou handig?
612    zijn ze er al in scheme, maar heten ze anders? */
613
614
615 /* Remove doubles from (sorted) list */
616 SCM
617 ly_unique (SCM list)
618 {
619   SCM unique = SCM_EOL;
620   for (SCM i = list; gh_pair_p (i); i = ly_cdr (i))
621     {
622       if (!gh_pair_p (ly_cdr (i))
623           || !gh_equal_p (ly_car (i), ly_cadr (i)))
624         unique = gh_cons (ly_car (i), unique);
625     }
626   return scm_reverse_x (unique, SCM_EOL);
627 }
628
629 /* tail add */
630 SCM
631 ly_snoc (SCM s, SCM list)
632 {
633   return gh_append2 (list, scm_list_n (s, SCM_UNDEFINED));
634 }
635
636
637 /* Split list at member s, removing s.
638    Return (BEFORE . AFTER) */
639 SCM
640 ly_split_list (SCM s, SCM list)
641 {
642   SCM before = SCM_EOL;
643   SCM after = list;
644   for (; gh_pair_p (after);)
645     {
646       SCM i = ly_car (after);
647       after = ly_cdr (after);
648       if (gh_equal_p (i, s))
649         break;
650       before = gh_cons (i, before);
651     }
652   return gh_cons ( scm_reverse_x (before, SCM_EOL),  after);
653   
654 }
655
656
657 void
658 taint (SCM *)
659 {
660   /*
661     nop.
662    */
663 }
664
665 /*
666   display stuff without using stack
667  */
668 SCM
669 display_list (SCM s)
670 {
671   SCM p = scm_current_output_port();
672
673   scm_puts ("(", p);
674   for (; gh_pair_p(s); s =gh_cdr(s))
675     {
676       scm_display (gh_car(s), p);
677       scm_puts (" ", p);      
678     }
679   scm_puts (")", p);
680   return SCM_UNSPECIFIED;
681 }
682
683 Slice
684 int_list_to_slice (SCM l)
685 {
686   Slice s;
687   s.set_empty ();
688   for (; gh_pair_p (l); l = gh_cdr (l))
689     {
690       if (gh_number_p (gh_car (l)))
691         s.add_point (gh_scm2int (gh_car (l))); 
692     }
693
694   return s;
695 }
696
697
698
699
700 /*
701   Return I-th element, or last elt L. If I < 0, then we take the first
702   element.
703
704   PRE: length (L) > 0
705  */
706 SCM
707 robust_list_ref(int i, SCM l)
708 {
709   while (i-- > 0 && gh_pair_p (gh_cdr(l)))
710     l = gh_cdr (l);
711
712   return gh_car(l);
713 }