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