]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/lily-guile.cc
* lily/modified-font-metric.cc (text_dimension): try
[lilypond.git] / lily / lily-guile.cc
index 92143ae7c24f5b1299790552b07ae1623226bae4..aa1161a37dcbe0c61a8cd2df26ebf0a07a303d4b 100644 (file)
@@ -7,34 +7,32 @@
                  Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
 
+#include "lily-guile.hh"
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <libintl.h>           // gettext on macos x
+#include <cstdio>
+#include <cstdlib>
 #include <math.h>   /* isinf */
-#include <string.h> /* strdup, strchr */
-#include <ctype.h>
+#include <cstring> /* strdup, strchr */
+#include <cctype>
+
+#include <libintl.h>           // gettext on macos x
 
-#include "lily-proto.hh"
 #include "version.hh"
 
 /* MacOS S fix:
    source-file.hh includes cmath which undefines isinf and isnan
-
-   FIXME: #ifdef MACOS_X?
 */
+#ifdef __APPLE__
 inline int my_isinf (Real r) { return isinf (r); }
 inline int my_isnan (Real r) { return isnan (r); }
-
+#endif
 
 #include "libc-extension.hh"
-#include "lily-guile.hh"
 #include "main.hh"
 #include "file-path.hh"
 #include "warn.hh"
 #include "direction.hh"
 #include "offset.hh"
-#include "interval.hh"
 #include "pitch.hh"
 #include "dimensions.hh"
 #include "source-file.hh"
@@ -42,11 +40,23 @@ inline int my_isnan (Real r) { return isnan (r); }
 // #define TEST_GC
 
 SCM
-ly_last (SCM list)
+ly_to_symbol (SCM scm)
 {
-  return ly_car (scm_last_pair (list));
+  return scm_string_to_symbol (ly_to_string (scm));
 }
 
+SCM
+ly_to_string (SCM scm)
+{
+  return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
+                    scm_makfrom0str ("~S"), scm);
+}
+
+SCM
+ly_last (SCM list)
+{
+  return scm_car (scm_last_pair (list));
+}
 
 SCM
 ly_write2scm (SCM s)
@@ -63,7 +73,6 @@ ly_write2scm (SCM s)
   return scm_strport_to_string (port);
 }
 
-
 SCM
 ly_quote_scm (SCM s)
 {
@@ -81,22 +90,28 @@ ly_symbol2string (SCM s)
 }
 
 String
-gulp_file_to_string (String fn)
+gulp_file_to_string (String fn, bool must_exist)
 {
   String s = global_path.find (fn);
   if (s == "")
     {
-      String e = _f ("can't find file: `%s'", fn);
-      e += " ";
-      e += _f ("(load path: `%s')", global_path.to_string ());
-      error (e);
+      if (must_exist)
+       {
+         String e = _f ("can't find file: `%s'", fn);
+         e += " ";
+         e += _f ("(load path: `%s')", global_path.to_string ());
+         error (e);
+         /* unreachable */
+       }
+      return s;
     }
-  else if (verbose_global_b)
+
+  if (verbose_global_b)
     progress_indication ("[" + s);
 
   int n;
-  char * str = gulp_file (s, &n);
-  String result (str);
+  char *str = gulp_file (s, &n);
+  String result ((Byte*) str, n);
   delete[] str;
   
   if (verbose_global_b)
@@ -111,7 +126,8 @@ LY_DEFINE (ly_gulp_file, "ly:gulp-file",
           "The file is looked up using the search path.")
 {
   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
-  return scm_makfrom0str (gulp_file_to_string (ly_scm2string (name)).to_str0 ());
+  String contents = gulp_file_to_string (ly_scm2string (name), true);
+  return scm_from_locale_stringn (contents.get_str0 (), contents.length ());
 }
 
 
@@ -128,6 +144,7 @@ ly_display_scm (SCM s)
 String
 ly_scm2string (SCM str)
 {
+  assert (scm_is_string (str));
   return String ((Byte*)scm_i_string_chars (str),
                 (int) scm_i_string_length (str));
 }
@@ -156,7 +173,7 @@ index_get_cell (SCM s, Direction d)
 {
   
   assert (d);
-  return (d == LEFT) ? ly_car (s) : ly_cdr (s);
+  return (d == LEFT) ? scm_car (s) : scm_cdr (s);
 }
 
 SCM
@@ -213,7 +230,7 @@ bool
 is_number_pair (SCM p)
 {
   return scm_is_pair (p)
-    && scm_is_number (ly_car (p)) && scm_is_number (ly_cdr (p));
+    && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
 }
 
 typedef void (*Void_fptr) ();
@@ -230,7 +247,7 @@ void add_scm_init_func (void (*f) ())
 void
 ly_init_ly_module (void *)
 {
-  for (int i=scm_init_funcs_->size () ; i--;)
+  for (int i = scm_init_funcs_->size () ; i--;)
     (scm_init_funcs_->elem (i)) ();
 
   if (verbose_global_b)
@@ -265,10 +282,11 @@ is_direction (SCM s)
   return false;
 }
 
-LY_DEFINE(ly_assoc_get, "ly:assoc-get",
-         2, 1, 0,
-         (SCM key, SCM alist, SCM default_value),
-         "Return value if KEY in ALIST, else DEFAULT-VALUE (or #f if not specified).")
+LY_DEFINE (ly_assoc_get, "ly:assoc-get",
+          2, 1, 0,
+          (SCM key, SCM alist, SCM default_value),
+          "Return value if KEY in ALIST, else DEFAULT-VALUE "
+          "(or #f if not specified).")
 {
   SCM handle = scm_assoc (key, alist);
 
@@ -276,7 +294,7 @@ LY_DEFINE(ly_assoc_get, "ly:assoc-get",
     default_value = SCM_BOOL_F;
   
   if (scm_is_pair (handle))
-    return ly_cdr (handle);
+    return scm_cdr (handle);
   else
     return default_value;
 }
@@ -287,7 +305,7 @@ is_axis (SCM s)
   if (scm_is_number (s))
     {
       int i = scm_to_int (s);
-      return i== 0 || i == 1;
+      return i == 0 || i == 1;
     }
   return false;
 }
@@ -301,14 +319,14 @@ to_dir (SCM s)
 Interval
 ly_scm2interval (SCM p)
 {
-  return Interval (scm_to_double (ly_car (p)), scm_to_double (ly_cdr (p)));
+  return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
 }
 
 Drul_array<Real>
 ly_scm2realdrul (SCM p)
 {
-  return Drul_array<Real> (scm_to_double (ly_car (p)),
-                          scm_to_double (ly_cdr (p)));
+  return Drul_array<Real> (scm_to_double (scm_car (p)),
+                          scm_to_double (scm_cdr (p)));
 }
 
 SCM
@@ -339,7 +357,7 @@ appendable_list_append (SCM l, SCM elt)
 {
   SCM newcons = scm_cons (elt, SCM_EOL);
   
-  scm_set_cdr_x (ly_car (l), newcons);      
+  scm_set_cdr_x (scm_car (l), newcons);      
   scm_set_car_x (l, newcons);
 }
 
@@ -352,8 +370,8 @@ ly_offset2scm (Offset o)
 Offset
 ly_scm2offset (SCM s)
 {
-  return Offset (scm_to_double (ly_car (s)),
-                scm_to_double (ly_cdr (s)));
+  return Offset (scm_to_double (scm_car (s)),
+                scm_to_double (scm_cdr (s)));
 }
 
 LY_DEFINE (ly_number2string, "ly:number->string",
@@ -367,8 +385,11 @@ LY_DEFINE (ly_number2string, "ly:number->string",
   if (scm_exact_p (s) == SCM_BOOL_F)
     {
       Real r (scm_to_double (s));
-
+#ifdef __APPLE__
       if (my_isinf (r) || my_isnan (r))
+#else
+      if (isinf (r) || isnan (r))
+#endif
        {
          programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
          r = 0.0;
@@ -411,12 +432,12 @@ SCM
 ly_deep_copy (SCM src)
 {
   if (scm_is_pair (src))
-    return scm_cons (ly_deep_copy (ly_car (src)), ly_deep_copy (ly_cdr (src)));
+    return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
   else if (ly_c_vector_p (src))
     {
       int len = SCM_VECTOR_LENGTH (src);
       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
-      for (int i  =0 ; i < len ; i++)
+      for (int i = 0 ;i < len ; i++)
        {
          SCM si = scm_int2num (i);
          scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si))); 
@@ -425,19 +446,31 @@ ly_deep_copy (SCM src)
   return src;
 }
 
-
-
+SCM
+ly_chain_assoc_get (SCM key, SCM achain, SCM dfault)
+{
+  if (scm_is_pair (achain))
+    {
+      SCM handle = scm_assoc (key, scm_car (achain));
+      if (scm_is_pair (handle))
+       return scm_cdr (handle);
+      else
+       return ly_chain_assoc (key, scm_cdr (achain));
+    }
+  else
+    return dfault;
+}
 
 SCM
-ly_assoc_chain (SCM key, SCM achain)
+ly_chain_assoc (SCM key, SCM achain)
 {
   if (scm_is_pair (achain))
     {
-      SCM handle = scm_assoc (key, ly_car (achain));
+      SCM handle = scm_assoc (key, scm_car (achain));
       if (scm_is_pair (handle))
        return handle;
       else
-       return ly_assoc_chain (key, ly_cdr (achain));
+       return ly_chain_assoc (key, scm_cdr (achain));
     }
   else
     return SCM_BOOL_F;
@@ -467,11 +500,11 @@ ly_assoc_cdr (SCM key, SCM alist)
 {
   if (scm_is_pair (alist))
     {
-      SCM trykey = ly_caar (alist);
-      if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, ly_cdr (trykey))))
-       return ly_car (alist);
+      SCM trykey = scm_caar (alist);
+      if (scm_is_pair (trykey) && to_boolean (scm_equal_p (key, scm_cdr (trykey))))
+       return scm_car (alist);
       else
-       return ly_assoc_cdr (key, ly_cdr (alist));
+       return ly_assoc_cdr (key, scm_cdr (alist));
     }
   return SCM_BOOL_F;
 }
@@ -519,7 +552,7 @@ ly_truncate_list (int k, SCM lst)
     {
       SCM s = lst;
       k--;
-      for (; scm_is_pair (s) && k--; s = ly_cdr (s))
+      for (; scm_is_pair (s) && k--; s = scm_cdr (s))
        ;
 
       if (scm_is_pair (s))
@@ -591,7 +624,7 @@ type_check_assignment (SCM sym, SCM val,  SCM type_symbol)
        {
          SCM errport = scm_current_error_port ();
          ok = false;
-         SCM typefunc = ly_scheme_function ("type-name");
+         SCM typefunc = ly_lily_module_constant ("type-name");
          SCM type_name = scm_call_1 (typefunc, type);
 
         
@@ -618,11 +651,11 @@ SCM
 ly_unique (SCM list)
 {
   SCM unique = SCM_EOL;
-  for (SCM i = list; scm_is_pair (i); i = ly_cdr (i))
+  for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
     {
-      if (!scm_is_pair (ly_cdr (i))
-         || !ly_c_equal_p (ly_car (i), ly_cadr (i)))
-       unique = scm_cons (ly_car (i), unique);
+      if (!scm_is_pair (scm_cdr (i))
+         || !ly_c_equal_p (scm_car (i), scm_cadr (i)))
+       unique = scm_cons (scm_car (i), unique);
     }
   return scm_reverse_x (unique, SCM_EOL);
 }
@@ -645,8 +678,8 @@ ly_list_qsort_uniq_x (SCM lst)
   int len = scm_ilength (lst);
   SCM *arr = new SCM[len];
   int k = 0;
-  for (SCM s = lst; SCM_NNULLP (s); s = ly_cdr (s))
-    arr[k++] = ly_car (s);
+  for (SCM s = lst; SCM_NNULLP (s); s = scm_cdr (s))
+    arr[k++] = scm_car (s);
 
   assert (k == len);
   qsort (arr, len, sizeof (SCM), &scm_default_compare);
@@ -682,8 +715,8 @@ ly_split_list (SCM s, SCM list)
   SCM after = list;
   for (; scm_is_pair (after);)
     {
-      SCM i = ly_car (after);
-      after = ly_cdr (after);
+      SCM i = scm_car (after);
+      after = scm_cdr (after);
       if (ly_c_equal_p (i, s))
        break;
       before = scm_cons (i, before);
@@ -710,9 +743,9 @@ display_list (SCM s)
   SCM p = scm_current_output_port ();
 
   scm_puts ("(", p);
-  for (; scm_is_pair (s); s =ly_cdr (s))
+  for (; scm_is_pair (s); s = scm_cdr (s))
     {
-      scm_display (ly_car (s), p);
+      scm_display (scm_car (s), p);
       scm_puts (" ", p);      
     }
   scm_puts (")", p);
@@ -724,9 +757,9 @@ int_list_to_slice (SCM l)
 {
   Slice s;
   s.set_empty ();
-  for (; scm_is_pair (l); l = ly_cdr (l))
-    if (scm_is_number (ly_car (l)))
-      s.add_point (scm_to_int (ly_car (l))); 
+  for (; scm_is_pair (l); l = scm_cdr (l))
+    if (scm_is_number (scm_car (l)))
+      s.add_point (scm_to_int (scm_car (l))); 
   return s;
 }
 
@@ -737,9 +770,9 @@ int_list_to_slice (SCM l)
 SCM
 robust_list_ref (int i, SCM l)
 {
-  while (i-- > 0 && scm_is_pair (ly_cdr (l)))
-    l = ly_cdr (l);
-  return ly_car (l);
+  while (i-- > 0 && scm_is_pair (scm_cdr (l)))
+    l = scm_cdr (l);
+  return scm_car (l);
 }
 
 Real
@@ -793,15 +826,14 @@ alist_to_hashq (SCM alist)
     return scm_c_make_hash_table (0);
          
   SCM tab = scm_c_make_hash_table (i);
-  for (SCM s = alist; scm_is_pair (s); s = ly_cdr (s))
+  for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
     {
-      SCM pt = ly_cdar (s);
-      scm_hashq_set_x (tab, ly_caar (s), pt);
+      SCM pt = scm_cdar (s);
+      scm_hashq_set_x (tab, scm_caar (s), pt);
     }
   return tab; 
 }
 
-#if 1
 /*
   Debugging mem leaks:
  */
@@ -811,24 +843,6 @@ LY_DEFINE (ly_protects, "ly:protects",
 {
   return scm_protects;
 }
-#endif
-
-
-#if HAVE_PANGO_FC_FONT_MAP_ADD_DECODER_FIND_FUNC
-
-#include "pangofc-afm-decoder.hh"
-
-LY_DEFINE (ly_pango_add_afm_decoder, "ly:pango-add-afm-decoder",
-          1, 0, 0, (SCM font_family),
-          "Add pango afm decoder for FONT-FAMILY.")
-{
-  SCM_ASSERT_TYPE (scm_is_string (font_family), font_family, SCM_ARG1,
-                  __FUNCTION__, "font_family");
-  pango_fc_afm_add_decoder (ly_scm2newstr (font_family, 0));
-  return SCM_UNSPECIFIED;
-}
-
-#endif
 
 LY_DEFINE (ly_gettext, "ly:gettext",
           1, 0, 0, (SCM string),