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