]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
* lily/pango-select.cc (select_pango_font): use ::find_pango_font,
[lilypond.git] / lily / lily-guile.cc
1 /*
2   lily-guile.cc -- implement assorted SCM interface functions
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7                  Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9
10
11 #include <cstdio>
12 #include <cstdlib>
13 #include <cstring> /* strdup, strchr */
14 #include <cctype>
15 #include <libintl.h>            // gettext on macos x
16
17 #include "version.hh"
18 #include "lily-guile.hh"
19 #include "libc-extension.hh"
20 #include "main.hh"
21 #include "file-path.hh"
22 #include "warn.hh"
23 #include "direction.hh"
24 #include "offset.hh"
25 #include "pitch.hh"
26 #include "dimensions.hh"
27 #include "source-file.hh"
28 #include "misc.hh"
29
30 // #define TEST_GC
31
32 SCM
33 ly_to_symbol (SCM scm)
34 {
35   return scm_string_to_symbol (ly_to_string (scm));
36 }
37
38 SCM
39 ly_to_string (SCM scm)
40 {
41   return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
42                      scm_makfrom0str ("~S"), scm);
43 }
44
45 SCM
46 ly_last (SCM list)
47 {
48   return scm_car (scm_last_pair (list));
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   scm_call_2 (write, s, port);
63   return scm_strport_to_string (port);
64 }
65
66 SCM
67 ly_quote_scm (SCM s)
68 {
69   return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
70 }
71
72 String
73 ly_symbol2string (SCM s)
74 {
75   /*
76     Ugh. this is not very efficient.
77    */
78   SCM str = scm_symbol_to_string (s);
79   return ly_scm2string (str);
80 }
81
82 String
83 gulp_file_to_string (String fn, bool must_exist)
84 {
85   String s = global_path.find (fn);
86   if (s == "")
87     {
88       if (must_exist)
89         {
90           String e = _f ("can't find file: `%s'", fn);
91           e += " ";
92           e += _f ("(load path: `%s')", global_path.to_string ());
93           error (e);
94           /* unreachable */
95         }
96       return s;
97     }
98
99   if (be_verbose_global)
100     progress_indication ("[" + s);
101
102   int n;
103   char *str = gulp_file (s, &n);
104   String result ((Byte*) str, n);
105   delete[] str;
106   
107   if (be_verbose_global)
108     progress_indication ("]");
109
110   return result;
111 }
112
113
114
115 extern "C" {
116   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
117 void
118 ly_display_scm (SCM s)
119 {
120   scm_display (s, scm_current_output_port ());
121   scm_newline (scm_current_output_port ());
122 }
123 };
124
125 String
126 ly_scm2string (SCM str)
127 {
128   assert (scm_is_string (str));
129   return String ((Byte*)scm_i_string_chars (str),
130                  (int) scm_i_string_length (str));
131 }
132
133 char *
134 ly_scm2newstr (SCM str, size_t *lenp)
135 {
136   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
137
138   size_t len = SCM_STRING_LENGTH (str);
139   if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
140     {
141       memcpy (new_str, scm_i_string_chars (str), len);
142       new_str[len] = '\0';
143
144       if (lenp)
145         *lenp = len;
146       
147       return new_str;
148     }
149   return 0;
150 }
151
152 SCM
153 index_get_cell (SCM s, Direction d)
154 {
155   
156   assert (d);
157   return (d == LEFT) ? scm_car (s) : scm_cdr (s);
158 }
159
160 SCM
161 index_set_cell (SCM s, Direction d, SCM v)
162 {
163   if (d == LEFT)
164     scm_set_car_x (s, v);
165   else if (d == RIGHT)
166     scm_set_cdr_x (s, v);
167   return s;
168 }
169   
170 bool
171 is_number_pair (SCM p)
172 {
173   return scm_is_pair (p)
174     && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
175 }
176
177 typedef void (*Void_fptr) ();
178 Array<Void_fptr> *scm_init_funcs_;
179
180 void add_scm_init_func (void (*f) ())
181 {
182   if (!scm_init_funcs_)
183     scm_init_funcs_ = new Array<Void_fptr>;
184
185   scm_init_funcs_->push (f);
186 }
187
188 #if KPATHSEA
189 extern "C" {
190   void initialize_kpathsea ();
191 }
192 #endif
193     
194 void
195 ly_init_ly_module (void *)
196 {
197   for (int i = scm_init_funcs_->size () ; i--;)
198     (scm_init_funcs_->elem (i)) ();
199
200   if (be_verbose_global)
201     progress_indication ("\n");
202
203 #if KPATHSEA
204   initialize_kpathsea ();
205 #endif
206   
207   scm_primitive_load_path (scm_makfrom0str ("lily.scm"));
208 }
209
210 SCM global_lily_module;
211
212 void
213 ly_c_init_guile ()
214 {
215   global_lily_module = scm_c_define_module ("lily", ly_init_ly_module, 0);
216   scm_c_use_module ("lily");
217 }
218
219 unsigned int
220 ly_scm_hash (SCM s)
221 {
222   return scm_ihashv (s, ~1u);
223 }
224
225 bool
226 is_direction (SCM s)
227 {
228   if (scm_is_number (s))
229     {
230       int i = scm_to_int (s);
231       return i>= -1 && i <= 1; 
232     }
233   return false;
234 }
235
236 bool
237 is_axis (SCM s)
238 {
239   if (scm_is_number (s))
240     {
241       int i = scm_to_int (s);
242       return i == 0 || i == 1;
243     }
244   return false;
245 }
246
247 Direction
248 to_dir (SCM s)
249 {
250   return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
251 }
252
253 Interval
254 ly_scm2interval (SCM p)
255 {
256   return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
257 }
258
259 Drul_array<Real>
260 ly_scm2realdrul (SCM p)
261 {
262   return Drul_array<Real> (scm_to_double (scm_car (p)),
263                            scm_to_double (scm_cdr (p)));
264 }
265
266 SCM
267 ly_interval2scm (Drul_array<Real> i)
268 {
269   return scm_cons (scm_make_real (i[LEFT]), scm_make_real (i[RIGHT]));
270 }
271
272 bool
273 to_boolean (SCM s)
274 {
275   return scm_is_bool (s) && ly_scm2bool (s);
276 }
277
278 /* Appendable list L: the cdr contains the list, the car the last cons
279    in the list.  */
280 SCM
281 appendable_list ()
282 {
283   SCM s = scm_cons (SCM_EOL, SCM_EOL);
284   scm_set_car_x (s, s);
285   
286   return s;
287 }
288
289 void
290 appendable_list_append (SCM l, SCM elt)
291 {
292   SCM newcons = scm_cons (elt, SCM_EOL);
293   
294   scm_set_cdr_x (scm_car (l), newcons);      
295   scm_set_car_x (l, newcons);
296 }
297
298 SCM
299 ly_offset2scm (Offset o)
300 {
301   return scm_cons (scm_make_real (o[X_AXIS]), scm_make_real (o[Y_AXIS]));
302 }
303
304 Offset
305 ly_scm2offset (SCM s)
306 {
307   return Offset (scm_to_double (scm_car (s)),
308                  scm_to_double (scm_cdr (s)));
309 }
310
311 SCM
312 ly_deep_copy (SCM src)
313 {
314   if (scm_is_pair (src))
315     return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
316   else if (scm_is_vector (src))
317     {
318       int len = scm_c_vector_length (src);
319       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
320       for (int i = 0 ;i < len ; i++)
321         {
322           SCM si = scm_int2num (i);
323           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si))); 
324         }
325     }
326   return src;
327 }
328
329 SCM
330 ly_chain_assoc_get (SCM key, SCM achain, SCM dfault)
331 {
332   if (scm_is_pair (achain))
333     {
334       SCM handle = scm_assoc (key, scm_car (achain));
335       if (scm_is_pair (handle))
336         return scm_cdr (handle);
337       else
338         return ly_chain_assoc (key, scm_cdr (achain));
339     }
340   else
341     return dfault;
342 }
343
344 SCM
345 ly_chain_assoc (SCM key, SCM achain)
346 {
347   if (scm_is_pair (achain))
348     {
349       SCM handle = scm_assoc (key, scm_car (achain));
350       if (scm_is_pair (handle))
351         return handle;
352       else
353         return ly_chain_assoc (key, scm_cdr (achain));
354     }
355   else
356     return SCM_BOOL_F;
357 }
358
359 /* looks the key up in the cdrs of the alist-keys
360    - ignoring the car and ignoring non-pair keys.
361    Returns first match found, i.e.
362
363    alist = ((1 . 10)
364                    ((1 . 2) . 11)
365                    ((2 . 1) . 12)
366                    ((3 . 0) . 13)
367                    ((4 . 1) . 14) )
368
369 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
370 element with the cdr of the key = 1.  In other words (alloc_cdr key)
371 corresponds to call
372
373 (alloc (anything . key))
374
375
376
377 */
378 SCM
379 ly_assoc_cdr (SCM key, SCM alist)
380 {
381   if (scm_is_pair (alist))
382     {
383       SCM trykey = scm_caar (alist);
384       if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
385         return scm_car (alist);
386       else
387         return ly_assoc_cdr (key, scm_cdr (alist));
388     }
389   return SCM_BOOL_F;
390 }
391
392 /* LST has the form "sym1 sym2 sym3\nsym4\nsym5"
393    i.e. \n and ' ' can be used interchangeably as separators.  */
394 SCM
395 parse_symbol_list (char const *lst)
396 {
397   char *s = strdup (lst);
398   char *orig = s;
399   SCM create_list = SCM_EOL;
400
401   char * e = s + strlen (s) - 1;
402   while (e >= s && isspace (*e))
403     *e-- = 0;
404
405   for (char * p = s; *p; p++)
406     if (*p == '\n')
407       *p = ' ';
408   
409   if (!s[0])
410     s = 0;
411   
412   while (s)
413     {
414       char *next = strchr (s, ' ');
415       if (next)
416         *next++ = 0;
417
418       create_list = scm_cons (ly_symbol2scm (s), create_list);
419       s = next;
420     }
421
422   free (orig);
423   return create_list;
424 }
425
426 SCM
427 ly_truncate_list (int k, SCM lst)
428 {
429   if (k == 0)
430     lst = SCM_EOL;
431   else
432     {
433       SCM s = lst;
434       k--;
435       for (; scm_is_pair (s) && k--; s = scm_cdr (s))
436         ;
437
438       if (scm_is_pair (s))
439         scm_set_cdr_x (s, SCM_EOL);
440     }
441   return lst;
442 }
443
444 String
445 print_scm_val (SCM val)
446 {
447   String realval = ly_scm2string (ly_write2scm (val));
448   if (realval.length () > 200)
449     realval = realval.left_string (100)
450       + "\n :\n :\n"
451       + realval.right_string (100);
452   return realval;        
453 }
454
455 bool
456 type_check_assignment (SCM sym, SCM val,  SCM type_symbol) 
457 {
458   bool ok = true;
459
460   /*
461     Always succeeds.
462
463
464     TODO: should remove #f from allowed vals?
465    */
466   if (val == SCM_EOL || val == SCM_BOOL_F)
467     return ok;
468
469   if (!scm_is_symbol (sym))
470 #if 0
471     return false;
472 #else
473   /*
474     This is used for autoBeamSettings.
475
476     TODO: deprecate the use of \override and \revert for
477     autoBeamSettings?
478
479     or use a symbol autoBeamSettingS?  
480    */
481   return true; 
482 #endif
483   
484   SCM type = scm_object_property (sym, type_symbol);
485
486   if (type != SCM_EOL && !ly_c_procedure_p (type))
487       {
488         warning (_f ("Can't find property type-check for `%s' (%s).",
489                      ly_symbol2string (sym).to_str0 (),
490                      ly_symbol2string (type_symbol).to_str0 ())
491                  + "  " + _ ("Perhaps you made a typing error?"));
492
493         /* Be strict when being anal :) */
494         if (do_internal_type_checking_global)
495           abort ();
496         
497         warning (_ ("Doing assignment anyway."));
498       }
499   else
500     {
501       if (val != SCM_EOL
502           && ly_c_procedure_p (type)
503           && scm_call_1 (type, val) == SCM_BOOL_F)
504         {
505           SCM errport = scm_current_error_port ();
506           ok = false;
507           SCM typefunc = ly_lily_module_constant ("type-name");
508           SCM type_name = scm_call_1 (typefunc, type);
509
510          
511           scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
512                         ly_symbol2string (sym).to_str0 (),
513                         print_scm_val (val),
514                         ly_scm2string (type_name).to_str0 ()).to_str0 (),
515                     errport);
516           scm_puts ("\n", errport);                   
517         }
518     }
519   return ok;
520 }
521
522
523 /* some SCM abbrevs
524
525    zijn deze nou handig?
526    zijn ze er al in scheme, maar heten ze anders? */
527
528
529 /* Remove doubles from (sorted) list */
530 SCM
531 ly_unique (SCM list)
532 {
533   SCM unique = SCM_EOL;
534   for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
535     {
536       if (!scm_is_pair (scm_cdr (i))
537           || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
538         unique = scm_cons (scm_car (i), unique);
539     }
540   return scm_reverse_x (unique, SCM_EOL);
541 }
542
543
544 static int
545 scm_default_compare (void const *a, void const *b)
546 {
547   SCM pa = *(SCM*) a;
548   SCM pb = *(SCM*) b;
549   if (pa == pb)
550     return 0;
551   return pa < pb ? -1 : 1;
552 }
553
554 /*  Modify LST in place: qsort it.  */
555 SCM
556 ly_list_qsort_uniq_x (SCM lst)
557 {
558   int len = scm_ilength (lst);
559   SCM *arr = new SCM[len];
560   int k = 0;
561   for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
562     arr[k++] = scm_car (s);
563
564   assert (k == len);
565   qsort (arr, len, sizeof (SCM), &scm_default_compare);
566
567   SCM *tail = &lst;
568   for (int i = 0; i < len; i++)
569     if (!i || arr[i] != arr[i - 1])
570       {
571         SCM_SETCAR (*tail, arr[i]);
572         tail = SCM_CDRLOC (*tail);
573       }
574
575   *tail = SCM_EOL;
576   delete[] arr;
577
578   return lst; 
579 }
580
581
582 /* tail add */
583 SCM
584 ly_snoc (SCM s, SCM list)
585 {
586   return ly_append2 (list, scm_list_n (s, SCM_UNDEFINED));
587 }
588
589 /* Split list at member s, removing s.
590    Return (BEFORE . AFTER)  */
591 SCM
592 ly_split_list (SCM s, SCM list)
593 {
594   SCM before = SCM_EOL;
595   SCM after = list;
596   for (; scm_is_pair (after);)
597     {
598       SCM i = scm_car (after);
599       after = scm_cdr (after);
600       if (ly_c_equal_p (i, s))
601         break;
602       before = scm_cons (i, before);
603     }
604   return scm_cons ( scm_reverse_x (before, SCM_EOL),  after);
605   
606 }
607
608
609 void
610 taint (SCM *)
611 {
612   /*
613     nop.
614    */
615 }
616
617 /*
618   display stuff without using stack
619  */
620 SCM
621 display_list (SCM s)
622 {
623   SCM p = scm_current_output_port ();
624
625   scm_puts ("(", p);
626   for (; scm_is_pair (s); s = scm_cdr (s))
627     {
628       scm_display (scm_car (s), p);
629       scm_puts (" ", p);      
630     }
631   scm_puts (")", p);
632   return SCM_UNSPECIFIED;
633 }
634
635 Slice
636 int_list_to_slice (SCM l)
637 {
638   Slice s;
639   s.set_empty ();
640   for (; scm_is_pair (l); l = scm_cdr (l))
641     if (scm_is_number (scm_car (l)))
642       s.add_point (scm_to_int (scm_car (l))); 
643   return s;
644 }
645
646 /* Return I-th element, or last elt L. If I < 0, then we take the first
647    element.
648    
649    PRE: length (L) > 0  */
650 SCM
651 robust_list_ref (int i, SCM l)
652 {
653   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
654     l = scm_cdr (l);
655   return scm_car (l);
656 }
657
658 Real
659 robust_scm2double (SCM k, double x)
660 {
661   if (scm_is_number (k))
662     x = scm_to_double (k);
663   return x;
664 }
665
666 Interval
667 robust_scm2interval (SCM k, Drul_array<Real> v)
668 {
669   Interval i;
670   i[LEFT]= v[LEFT];
671   i[RIGHT]= v[RIGHT];
672   if (is_number_pair (k))
673     i = ly_scm2interval (k);
674   return i;
675 }
676
677 Drul_array<Real>
678 robust_scm2drul (SCM k, Drul_array<Real> v)
679 {
680   if (is_number_pair (k))
681     v = ly_scm2interval (k);
682   return v;
683 }
684
685 Offset
686 robust_scm2offset (SCM k, Offset o)
687 {
688   if (is_number_pair (k))
689     o = ly_scm2offset (k);
690   return o;
691 }
692
693 int
694 robust_scm2int (SCM k, int o)
695 {
696   if (scm_integer_p (k) == SCM_BOOL_T)
697     o = scm_to_int (k);
698   return o;
699 }
700
701 SCM
702 alist_to_hashq (SCM alist)
703 {
704   int i = scm_ilength (alist);
705   if (i < 0)
706     return scm_c_make_hash_table (0);
707           
708   SCM tab = scm_c_make_hash_table (i);
709   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
710     {
711       SCM pt = scm_cdar (s);
712       scm_hashq_set_x (tab, scm_caar (s), pt);
713     }
714   return tab; 
715 }
716
717
718 bool
719 alist_equal_p (SCM a, SCM b)
720 {
721   for (SCM s = a;
722        scm_is_pair (s); s = scm_cdr (s))
723     {
724       SCM key = scm_caar (s);
725       SCM val = scm_cdar (s);
726       SCM l = scm_assoc (key, b);
727
728       if (l == SCM_BOOL_F
729           || !ly_c_equal_p ( scm_cdr (l), val))
730
731         return false;
732     }
733   return true;
734 }
735
736
737
738 SCM
739 ly_alist_vals (SCM alist)
740 {
741   SCM x = SCM_EOL;
742   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
743     {
744       x = scm_cons (scm_cdar (p), x);
745     }
746   return x;
747 }
748
749 SCM
750 ly_hash2alist (SCM tab)
751 {
752   SCM func = ly_lily_module_constant ("hash-table->alist");
753   return scm_call_1 (func, tab);
754 }
755
756
757