]> 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 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   assert (d);
149   return (d == LEFT) ? ly_car (s) : ly_cdr (s);
150 }
151
152 SCM
153 index_set_cell (SCM s, Direction d, SCM v)
154 {
155   if (d == LEFT)
156     gh_set_car_x (s, v);
157   else if (d == RIGHT)
158     gh_set_cdr_x (s, v);
159   return s;
160 }
161   
162 LY_DEFINE(ly_warning,"ly-warn", 1, 0, 0,
163   (SCM str),"Scheme callable function to issue the warning @code{msg}.
164 ")
165 {
166   assert (gh_string_p (str));
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 (Direction) gh_scm2int (s);
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 SCM
320 ly_type (SCM exp)
321 {
322   char const  * cp = "unknown";
323   if (gh_number_p (exp))
324     {
325       cp = "number";
326     }
327   else if (gh_string_p (exp))
328     {
329       cp = "string";
330     }
331   else if (gh_procedure_p (exp))
332     {
333       cp = "procedure";
334     }
335   else if (gh_boolean_p (exp))
336     {
337       cp = "boolean";
338     }
339   else if (gh_pair_p (exp))
340     {
341       cp = "list";
342     }
343
344   return scm_makfrom0str (cp);
345 }
346
347 /*
348   convert without too many decimals, and leave  a space at the end.
349  */
350    
351    
352 LY_DEFINE(ly_number2string,  "ly-number->string", 1, 0,0,
353           (SCM s),
354           " converts @var{num} to a string without generating many decimals. It
355 leaves a space at the end.
356 ")
357 {
358   assert (gh_number_p (s));
359
360   char str[400];                        // ugh.
361
362   if (scm_exact_p (s) == SCM_BOOL_F)
363     {
364       Real r (gh_scm2double (s));
365
366       if (isinf (r) || isnan (r))
367         {
368           programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
369           r = 0.0;
370         }
371
372       sprintf (str, "%8.4f ", r);
373     }
374   else
375     {
376       sprintf (str, "%d ", gh_scm2int (s));
377     }
378
379   return scm_makfrom0str (str);
380 }
381
382 /*
383   Undef this to see if GUILE GC is causing too many swaps.
384  */
385
386 // #define TEST_GC
387
388 #ifdef TEST_GC
389 #include <libguile/gc.h>
390
391 static void *
392 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
393 {
394    fprintf (stderr, "entering sweep\n");
395 }
396
397 static void *
398 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
399 {
400    fprintf (stderr, "leaving sweep\n");
401 }
402 #endif
403
404
405 #include "version.hh"
406 LY_DEFINE(ly_version,  "ly-version", 0, 0, 0, (),
407           "Return the current lilypond version as a list, e.g.
408 @code{(1 3 127 uu1)}. 
409 ")
410 {
411   char const* vs =  "\' (" MAJOR_VERSION " " MINOR_VERSION " "  PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
412   
413   return gh_eval_str ((char*)vs);
414 }
415
416 LY_DEFINE(ly_unit,  "ly-unit", 0, 0, 0, (),
417           "Return the unit used for lengths as a string.")
418 {
419   return scm_makfrom0str (INTERNAL_UNIT);
420 }
421
422 LY_DEFINE(ly_verbose,  "ly-verbose", 0, 0, 0, (),
423   "Return whether lilypond is being run in verbose mode.")
424 {
425   return gh_bool2scm (verbose_global_b);
426 }
427
428 static void
429 init_functions ()
430 {
431 #ifdef TEST_GC 
432   scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
433   scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
434 #endif
435 }
436
437 ADD_SCM_INIT_FUNC (funcs, init_functions);
438
439 SCM
440 ly_deep_copy (SCM src)
441 {
442   if (gh_pair_p (src))
443     {
444       return gh_cons (ly_deep_copy (ly_car (src)), ly_deep_copy (ly_cdr (src)));
445     }
446   else if (gh_vector_p (src))
447     {
448       int  l = SCM_VECTOR_LENGTH (src);
449       SCM nv = scm_c_make_vector (l, SCM_UNDEFINED);
450       for (int i  =0 ; i< l ; i++)
451         {
452           SCM si = gh_int2scm (i);
453           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si))); 
454         }
455     }
456   else
457     return src;
458
459   return src;
460 }
461
462
463
464
465 SCM
466 ly_assoc_chain (SCM key, SCM achain)
467 {
468   if (gh_pair_p (achain))
469     {
470       SCM handle = scm_assoc (key, ly_car (achain));
471       if (gh_pair_p (handle))
472         return handle;
473       else
474         return ly_assoc_chain (key, ly_cdr (achain));
475     }
476   else
477     return SCM_BOOL_F;
478 }
479
480 /* looks the key up in the cdrs of the alist-keys
481    - ignoring the car and ignoring non-pair keys.
482    Returns first match found, i.e.
483
484    alist = ((1 . 10)
485                    ((1 . 2) . 11)
486                    ((2 . 1) . 12)
487                    ((3 . 0) . 13)
488                    ((4 . 1) . 14) )
489
490 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
491 element with the cdr of the key = 1.  In other words (alloc_cdr key)
492 corresponds to call
493
494 (alloc (anything . key))
495
496
497
498 */
499 SCM
500 ly_assoc_cdr (SCM key, SCM alist)
501 {
502   if (gh_pair_p (alist)) {
503     SCM trykey = ly_caar(alist);
504     if(gh_pair_p(trykey) && to_boolean(scm_equal_p(key,ly_cdr(trykey))))
505       return ly_car(alist);
506     else
507       return ly_assoc_cdr (key, ly_cdr (alist));
508   }
509   else
510     return SCM_BOOL_F;
511 }
512
513 /*
514   LIST has the form "sym1 sym2 sym3\nsym4\nsym5"
515
516   i.e. \n and ' ' can be used interchangeably as separators.
517  */
518 SCM
519 parse_symbol_list (const char * list)
520 {
521   char * s = strdup (list);
522   char *orig = s;
523   SCM create_list = SCM_EOL;
524
525   for (char * p = s; *p; p++)
526     {
527       if (*p == '\n')
528         *p = ' ' ;
529     }
530   
531   if (!s[0] )
532     s = 0;
533
534
535   
536   while (s)
537     {
538       char *next = strchr (s, ' ');
539       if (next)
540         *next++ = 0;
541
542       create_list = gh_cons (ly_symbol2scm (s), create_list);
543       s = next;
544     }
545
546   free (orig);
547   return create_list;
548 }
549
550
551 SCM
552 ly_truncate_list (int k, SCM l )
553 {
554   if (k == 0)
555     {
556       l = SCM_EOL;
557     }
558   else
559     {
560       SCM s = l;
561       k--;
562       for (; gh_pair_p (s) && k--; s = ly_cdr (s))
563         ;
564
565       if (gh_pair_p (s))
566         {
567           gh_set_cdr_x (s, SCM_EOL);
568         }
569     }
570   return l;
571 }
572
573
574 String
575 print_scm_val (SCM val)
576 {
577   String realval = ly_scm2string (ly_write2scm (val));
578   if (realval.length () > 200)
579     realval = realval.left_string (100) + "\n :\n :\n" + realval.right_string (100);
580   
581   return realval;        
582 }
583
584 bool
585 type_check_assignment (SCM sym, SCM val,  SCM type_symbol) 
586 {
587   bool ok = true;
588
589   /*
590     Always succeeds.
591
592
593     TODO: should remove #f from allowed vals?
594    */
595   if (val == SCM_EOL || val == SCM_BOOL_F)
596     return ok;
597
598   
599   SCM type = SCM_EOL;
600
601   if (gh_symbol_p (sym))
602     type = scm_object_property (sym, type_symbol);
603
604   if (type != SCM_EOL && !gh_procedure_p (type))
605       {
606         warning (_f ("Can't find property type-check for `%s' (%s).  Perhaps you made a typing error? Doing assignment anyway.",
607                      ly_symbol2string (sym).to_str0 (),
608                      ly_symbol2string (type_symbol).to_str0 ()
609
610                      ));
611       }
612   else
613     {
614       if (val != SCM_EOL
615           && gh_procedure_p (type)
616           && gh_call1 (type, val) == SCM_BOOL_F)
617         {
618           SCM errport = scm_current_error_port ();
619           ok = false;
620           SCM typefunc = scm_primitive_eval (ly_symbol2scm ("type-name"));
621           SCM type_name = gh_call1 (typefunc, type);
622
623          
624           scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
625                         ly_symbol2string (sym).to_str0 (),
626                         print_scm_val (val),
627                         ly_scm2string (type_name).to_str0 ()).to_str0 (),
628                     errport);
629           scm_puts ("\n", errport);                   
630         }
631     }
632   return ok;
633 }
634
635
636 /* some SCM abbrevs
637
638    zijn deze nou handig?
639    zijn ze er al in scheme, maar heten ze anders? */
640
641
642 /* Remove doubles from (sorted) list */
643 SCM
644 ly_unique (SCM list)
645 {
646   SCM unique = SCM_EOL;
647   for (SCM i = list; gh_pair_p (i); i = ly_cdr (i))
648     {
649       if (!gh_pair_p (ly_cdr (i))
650           || !gh_equal_p (ly_car (i), ly_cadr (i)))
651         unique = gh_cons (ly_car (i), unique);
652     }
653   return scm_reverse_x (unique, SCM_EOL);
654 }
655
656 /* tail add */
657 SCM
658 ly_snoc (SCM s, SCM list)
659 {
660   return gh_append2 (list, scm_list_n (s, SCM_UNDEFINED));
661 }
662
663
664 /* Split list at member s, removing s.
665    Return (BEFORE . AFTER) */
666 SCM
667 ly_split_list (SCM s, SCM list)
668 {
669   SCM before = SCM_EOL;
670   SCM after = list;
671   for (; gh_pair_p (after);)
672     {
673       SCM i = ly_car (after);
674       after = ly_cdr (after);
675       if (gh_equal_p (i, s))
676         break;
677       before = gh_cons (i, before);
678     }
679   return gh_cons ( scm_reverse_x (before, SCM_EOL),  after);
680   
681 }
682
683
684 void
685 taint (SCM *)
686 {
687   /*
688     nop.
689    */
690 }
691
692 /*
693   display stuff without using stack
694  */
695 SCM
696 display_list (SCM s)
697 {
698   SCM p = scm_current_output_port();
699
700   scm_puts ("(", p);
701   for (; gh_pair_p(s); s =gh_cdr(s))
702     {
703       scm_display (gh_car(s), p);
704       scm_puts (" ", p);      
705     }
706   scm_puts (")", p);
707   return SCM_UNSPECIFIED;
708 }
709
710 Slice
711 int_list_to_slice (SCM l)
712 {
713   Slice s;
714   s.set_empty ();
715   for (; gh_pair_p (l); l = gh_cdr (l))
716     {
717       if (gh_number_p (gh_car (l)))
718         s.add_point (gh_scm2int (gh_car (l))); 
719     }
720
721   return s;
722 }
723
724
725
726
727 /*
728   Return I-th element, or last elt L. If I < 0, then we take the first
729   element.
730
731   PRE: length (L) > 0
732  */
733 SCM
734 robust_list_ref(int i, SCM l)
735 {
736   while (i-- > 0 && gh_pair_p (gh_cdr(l)))
737     l = gh_cdr (l);
738
739   return gh_car(l);
740 }