]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
(midi_error): take two arguments.
[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
197 void
198 ly_init_ly_module (void *data)
199 {
200   for (int i=scm_init_funcs_->size () ; i--;)
201     (scm_init_funcs_->elem (i)) ();
202
203   if (verbose_global_b)
204     progress_indication ("\n");
205   scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
206 }
207
208
209 SCM lily_module ;
210
211 void
212 ly_init_guile ()
213 {
214   lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
215   scm_c_use_module ("lily");
216 }
217
218 unsigned int ly_scm_hash (SCM s)
219 {
220   return scm_ihashv (s, ~1u);
221 }
222
223
224
225 bool
226 ly_dir_p (SCM s)
227 {
228   if (gh_number_p (s))
229     {
230       int i = gh_scm2int (s);
231       return i>= -1 && i <= 1; 
232     }
233   return false;
234 }
235
236
237 bool
238 ly_axis_p (SCM s)
239 {
240   if (gh_number_p (s))
241     {
242       int i = gh_scm2int (s);
243       return i== 0 || i == 1;
244     }
245   return false;
246 }
247
248
249 Direction
250 to_dir (SCM s)
251 {
252   return SCM_INUMP (s) ?  (Direction) gh_scm2int (s) : CENTER;
253 }
254
255 Interval
256 ly_scm2interval (SCM p)
257 {
258   return  Interval (gh_scm2double (ly_car (p)),
259                     gh_scm2double (ly_cdr (p)));
260 }
261
262 SCM
263 ly_interval2scm (Drul_array<Real> i)
264 {
265   return gh_cons (gh_double2scm (i[LEFT]),
266                   gh_double2scm (i[RIGHT]));
267 }
268
269
270
271
272 bool
273 to_boolean (SCM s)
274 {
275   return gh_boolean_p (s) && gh_scm2bool (s);
276 }
277
278 /*
279   Appendable list L: the cdr contains the list, the car the last cons
280   in the list.
281  */
282 SCM
283 appendable_list ()
284 {
285   SCM s = gh_cons (SCM_EOL, SCM_EOL);
286   gh_set_car_x (s, s);
287   
288   return s;
289 }
290
291 void
292 appendable_list_append (SCM l, SCM elt)
293 {
294   SCM newcons = gh_cons (elt, SCM_EOL);
295   
296   gh_set_cdr_x (ly_car (l), newcons);      
297   gh_set_car_x (l, newcons);
298 }
299
300
301 SCM
302 ly_offset2scm (Offset o)
303 {
304   return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm (o[Y_AXIS]));
305 }
306
307 Offset
308 ly_scm2offset (SCM s)
309 {
310   return Offset (gh_scm2double (ly_car (s)),
311                  gh_scm2double (ly_cdr (s)));
312 }
313
314    
315 LY_DEFINE(ly_number2string,  "ly-number->string", 1, 0,0,
316           (SCM s),
317           " converts @var{num} to a string without generating many decimals. It
318 leaves a space at the end.
319 ")
320 {
321   SCM_ASSERT_TYPE (gh_number_p (s), s, SCM_ARG1, __FUNCTION__, "number");
322
323   char str[400];                        // ugh.
324
325   if (scm_exact_p (s) == SCM_BOOL_F)
326     {
327       Real r (gh_scm2double (s));
328
329       if (my_isinf (r) || my_isnan (r))
330         {
331           programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
332           r = 0.0;
333         }
334
335       sprintf (str, "%8.4f ", r);
336     }
337   else
338     {
339       sprintf (str, "%d ", gh_scm2int (s));
340     }
341
342   return scm_makfrom0str (str);
343 }
344
345 /*
346   Undef this to see if GUILE GC is causing too many swaps.
347  */
348
349 //#define TEST_GC
350
351 #ifdef TEST_GC
352 #include <libguile/gc.h>
353
354 static void *
355 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
356 {
357    fprintf (stderr, "entering sweep\n");
358 }
359
360 static void *
361 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
362 {
363    fprintf (stderr, "leaving sweep\n");
364 }
365 #endif
366
367
368 #include "version.hh"
369 LY_DEFINE(ly_version,  "ly-version", 0, 0, 0, (),
370           "Return the current lilypond version as a list, e.g.
371 @code{(1 3 127 uu1)}. 
372 ")
373 {
374   char const* vs =  "\' (" MAJOR_VERSION " " MINOR_VERSION " "  PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
375   
376   return gh_eval_str ((char*)vs);
377 }
378
379 LY_DEFINE(ly_unit,  "ly-unit", 0, 0, 0, (),
380           "Return the unit used for lengths as a string.")
381 {
382   return scm_makfrom0str (INTERNAL_UNIT);
383 }
384
385 LY_DEFINE(ly_verbose,  "ly-verbose", 0, 0, 0, (),
386   "Return whether lilypond is being run in verbose mode.")
387 {
388   return gh_bool2scm (verbose_global_b);
389 }
390
391 static void
392 init_functions ()
393 {
394 #ifdef TEST_GC 
395   scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
396   scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
397 #endif
398 }
399
400 ADD_SCM_INIT_FUNC (funcs, init_functions);
401
402 SCM
403 ly_deep_copy (SCM src)
404 {
405   if (gh_pair_p (src))
406     {
407       return gh_cons (ly_deep_copy (ly_car (src)), ly_deep_copy (ly_cdr (src)));
408     }
409   else if (gh_vector_p (src))
410     {
411       int  l = SCM_VECTOR_LENGTH (src);
412       SCM nv = scm_c_make_vector (l, SCM_UNDEFINED);
413       for (int i  =0 ; i< l ; i++)
414         {
415           SCM si = gh_int2scm (i);
416           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si))); 
417         }
418     }
419   else
420     return src;
421
422   return src;
423 }
424
425
426
427
428 SCM
429 ly_assoc_chain (SCM key, SCM achain)
430 {
431   if (gh_pair_p (achain))
432     {
433       SCM handle = scm_assoc (key, ly_car (achain));
434       if (gh_pair_p (handle))
435         return handle;
436       else
437         return ly_assoc_chain (key, ly_cdr (achain));
438     }
439   else
440     return SCM_BOOL_F;
441 }
442
443 /* looks the key up in the cdrs of the alist-keys
444    - ignoring the car and ignoring non-pair keys.
445    Returns first match found, i.e.
446
447    alist = ((1 . 10)
448                    ((1 . 2) . 11)
449                    ((2 . 1) . 12)
450                    ((3 . 0) . 13)
451                    ((4 . 1) . 14) )
452
453 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
454 element with the cdr of the key = 1.  In other words (alloc_cdr key)
455 corresponds to call
456
457 (alloc (anything . key))
458
459
460
461 */
462 SCM
463 ly_assoc_cdr (SCM key, SCM alist)
464 {
465   if (gh_pair_p (alist)) {
466     SCM trykey = ly_caar(alist);
467     if(gh_pair_p(trykey) && to_boolean(scm_equal_p(key,ly_cdr(trykey))))
468       return ly_car(alist);
469     else
470       return ly_assoc_cdr (key, ly_cdr (alist));
471   }
472   else
473     return SCM_BOOL_F;
474 }
475
476 /*
477   LIST has the form "sym1 sym2 sym3\nsym4\nsym5"
478
479   i.e. \n and ' ' can be used interchangeably as separators.
480  */
481 SCM
482 parse_symbol_list (const char * list)
483 {
484   char * s = strdup (list);
485   char *orig = s;
486   SCM create_list = SCM_EOL;
487
488   for (char * p = s; *p; p++)
489     {
490       if (*p == '\n')
491         *p = ' ' ;
492     }
493   
494   if (!s[0] )
495     s = 0;
496
497
498   
499   while (s)
500     {
501       char *next = strchr (s, ' ');
502       if (next)
503         *next++ = 0;
504
505       create_list = gh_cons (ly_symbol2scm (s), create_list);
506       s = next;
507     }
508
509   free (orig);
510   return create_list;
511 }
512
513
514 SCM
515 ly_truncate_list (int k, SCM l )
516 {
517   if (k == 0)
518     {
519       l = SCM_EOL;
520     }
521   else
522     {
523       SCM s = l;
524       k--;
525       for (; gh_pair_p (s) && k--; s = ly_cdr (s))
526         ;
527
528       if (gh_pair_p (s))
529         {
530           gh_set_cdr_x (s, SCM_EOL);
531         }
532     }
533   return l;
534 }
535
536
537 String
538 print_scm_val (SCM val)
539 {
540   String realval = ly_scm2string (ly_write2scm (val));
541   if (realval.length () > 200)
542     realval = realval.left_string (100) + "\n :\n :\n" + realval.right_string (100);
543   
544   return realval;        
545 }
546
547 bool
548 type_check_assignment (SCM sym, SCM val,  SCM type_symbol) 
549 {
550   bool ok = true;
551
552   /*
553     Always succeeds.
554
555
556     TODO: should remove #f from allowed vals?
557    */
558   if (val == SCM_EOL || val == SCM_BOOL_F)
559     return ok;
560
561   
562   SCM type = SCM_EOL;
563
564   if (gh_symbol_p (sym))
565     type = scm_object_property (sym, type_symbol);
566
567   if (type != SCM_EOL && !gh_procedure_p (type))
568       {
569         warning (_f ("Can't find property type-check for `%s' (%s).",
570                      ly_symbol2string (sym).to_str0 (),
571                      ly_symbol2string (type_symbol).to_str0 ())
572                  + "  " + _ ("Perhaps you made a typing error?"));
573
574         /* Be strict when being anal :) */
575         if (internal_type_checking_global_b)
576           abort ();
577         
578         warning (_ ("Doing assignment anyway."));
579       }
580   else
581     {
582       if (val != SCM_EOL
583           && gh_procedure_p (type)
584           && gh_call1 (type, val) == SCM_BOOL_F)
585         {
586           SCM errport = scm_current_error_port ();
587           ok = false;
588           SCM typefunc = scm_primitive_eval (ly_symbol2scm ("type-name"));
589           SCM type_name = gh_call1 (typefunc, type);
590
591          
592           scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
593                         ly_symbol2string (sym).to_str0 (),
594                         print_scm_val (val),
595                         ly_scm2string (type_name).to_str0 ()).to_str0 (),
596                     errport);
597           scm_puts ("\n", errport);                   
598         }
599     }
600   return ok;
601 }
602
603
604 /* some SCM abbrevs
605
606    zijn deze nou handig?
607    zijn ze er al in scheme, maar heten ze anders? */
608
609
610 /* Remove doubles from (sorted) list */
611 SCM
612 ly_unique (SCM list)
613 {
614   SCM unique = SCM_EOL;
615   for (SCM i = list; gh_pair_p (i); i = ly_cdr (i))
616     {
617       if (!gh_pair_p (ly_cdr (i))
618           || !gh_equal_p (ly_car (i), ly_cadr (i)))
619         unique = gh_cons (ly_car (i), unique);
620     }
621   return scm_reverse_x (unique, SCM_EOL);
622 }
623
624 /* tail add */
625 SCM
626 ly_snoc (SCM s, SCM list)
627 {
628   return gh_append2 (list, scm_list_n (s, SCM_UNDEFINED));
629 }
630
631
632 /* Split list at member s, removing s.
633    Return (BEFORE . AFTER) */
634 SCM
635 ly_split_list (SCM s, SCM list)
636 {
637   SCM before = SCM_EOL;
638   SCM after = list;
639   for (; gh_pair_p (after);)
640     {
641       SCM i = ly_car (after);
642       after = ly_cdr (after);
643       if (gh_equal_p (i, s))
644         break;
645       before = gh_cons (i, before);
646     }
647   return gh_cons ( scm_reverse_x (before, SCM_EOL),  after);
648   
649 }
650
651
652 void
653 taint (SCM *)
654 {
655   /*
656     nop.
657    */
658 }
659
660 /*
661   display stuff without using stack
662  */
663 SCM
664 display_list (SCM s)
665 {
666   SCM p = scm_current_output_port();
667
668   scm_puts ("(", p);
669   for (; gh_pair_p(s); s =gh_cdr(s))
670     {
671       scm_display (gh_car(s), p);
672       scm_puts (" ", p);      
673     }
674   scm_puts (")", p);
675   return SCM_UNSPECIFIED;
676 }
677
678 Slice
679 int_list_to_slice (SCM l)
680 {
681   Slice s;
682   s.set_empty ();
683   for (; gh_pair_p (l); l = gh_cdr (l))
684     {
685       if (gh_number_p (gh_car (l)))
686         s.add_point (gh_scm2int (gh_car (l))); 
687     }
688
689   return s;
690 }
691
692
693
694
695 /*
696   Return I-th element, or last elt L. If I < 0, then we take the first
697   element.
698
699   PRE: length (L) > 0
700  */
701 SCM
702 robust_list_ref(int i, SCM l)
703 {
704   while (i-- > 0 && gh_pair_p (gh_cdr(l)))
705     l = gh_cdr (l);
706
707   return gh_car(l);
708 }
709