From 53d5bd8ec65e2caa9febf487bd492b68365752b0 Mon Sep 17 00:00:00 2001
From: Han-Wen Nienhuys <hanwen@xs4all.nl>
Date: Thu, 18 Jan 2007 13:11:47 +0100
Subject: [PATCH] robust_scm2fraction

---
 lily/accidental.cc                     |  7 +++++--
 lily/ambitus-engraver.cc               |  2 +-
 lily/include/lily-guile.hh             |  5 ++++-
 lily/include/tie-formatting-problem.hh |  1 +
 lily/key-engraver.cc                   |  2 +-
 lily/lily-guile.cc                     | 24 +++++++++++++-----------
 6 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/lily/accidental.cc b/lily/accidental.cc
index 46db4f4f2f..3bfd1b816d 100644
--- a/lily/accidental.cc
+++ b/lily/accidental.cc
@@ -66,13 +66,16 @@ Accidental_interface::accurate_boxes (Grob *me, Grob **common)
   vector<Box> boxes;
 
   bool parens = to_boolean (me->get_property ("parenthesized"));
+  if (!me->is_live ())
+    return boxes;
 
   SCM scm_style = me->get_property ("style");
   if (!scm_is_symbol (scm_style)
       && !to_boolean (me->get_property ("restore-first"))
       && !parens)
     {
-      Rational alteration = ly_scm2rational (me->get_property ("alteration"));
+      Rational alteration
+	= robust_scm2rational (me->get_property ("alteration"), 0);
       if (alteration == FLAT_ALTERATION)
 	{
 	  Box stem = b;
@@ -151,7 +154,7 @@ Accidental_interface::print (SCM smob)
   if (!scm_is_string (glyph_name))
     {
       me->warning (_f ("Could not find glyph-name for alteration %s",
-		       ly_scm2rational (alt).to_string ().c_str ()));
+		       ly_scm_write_string (alt).c_str ()));
       return SCM_EOL;
     }
   
diff --git a/lily/ambitus-engraver.cc b/lily/ambitus-engraver.cc
index 0e4e2d949b..e2fdcc9595 100644
--- a/lily/ambitus-engraver.cc
+++ b/lily/ambitus-engraver.cc
@@ -149,7 +149,7 @@ Ambitus_engraver::finalize ()
 				start_key_sig_);
 
 	  Rational sig_alter = (handle != SCM_BOOL_F)
-	    ? ly_scm2rational (scm_cdr (handle)) : Rational (0);
+	    ? robust_scm2rational (scm_cdr (handle), Rational (0)) : Rational (0);
 
 	  if (sig_alter == p.get_alteration ())
 	    {
diff --git a/lily/include/lily-guile.hh b/lily/include/lily-guile.hh
index 6eef555bf5..6f21a29b03 100644
--- a/lily/include/lily-guile.hh
+++ b/lily/include/lily-guile.hh
@@ -32,7 +32,7 @@
     A ly_B2A (B b);  */
 
 SCM ly_last (SCM list);
-SCM ly_write2scm (SCM s);
+string ly_scm_write_string (SCM s);
 SCM ly_deep_copy (SCM);
 SCM ly_truncate_list (int k, SCM lst);
 
@@ -65,6 +65,7 @@ Drul_array<bool> robust_scm2booldrul (SCM, Drul_array<bool>);
 Interval robust_scm2interval (SCM, Drul_array<Real>);
 Offset robust_scm2offset (SCM, Offset);
 string robust_scm2string (SCM, string);
+Rational robust_scm2rational (SCM, Rational);
 
   
 SCM ly_quote_scm (SCM s);
@@ -81,6 +82,8 @@ SCM ly_hash2alist (SCM tab);
 SCM ly_hash_table_keys (SCM tab);
 
 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val);
+inline bool ly_is_fraction (SCM x) { return SCM_FRACTIONP(x) || scm_is_integer (x); }
+    
 inline bool ly_is_list (SCM x) { return SCM_NFALSEP (scm_list_p (x)); }
 inline bool ly_is_procedure (SCM x) { return SCM_NFALSEP (scm_procedure_p (x)); }
 inline bool ly_is_port (SCM x) { return SCM_NFALSEP (scm_port_p (x)); }
diff --git a/lily/include/tie-formatting-problem.hh b/lily/include/tie-formatting-problem.hh
index ebee98877f..50f1746b86 100644
--- a/lily/include/tie-formatting-problem.hh
+++ b/lily/include/tie-formatting-problem.hh
@@ -31,6 +31,7 @@ struct Tie_configuration_variation
 typedef map < Tuple<int, 2>, Skyline> Chord_outline_map;
 typedef map < Tuple<int, 2>, Box> Column_extent_map;
 typedef map <int, Slice> Position_extent_map;
+
 class Tie_formatting_problem
 {
   Chord_outline_map chord_outlines_;
diff --git a/lily/key-engraver.cc b/lily/key-engraver.cc
index c086f3d250..36199d7041 100644
--- a/lily/key-engraver.cc
+++ b/lily/key-engraver.cc
@@ -77,7 +77,7 @@ Key_engraver::create_key (bool is_default)
 	  for (SCM s = last; scm_is_pair (s); s = scm_cdr (s))
 	    {
 	      SCM new_alter_pair = scm_assoc (scm_caar (s), key);
-	      Rational old_alter = ly_scm2rational (scm_cdar (s));
+	      Rational old_alter = robust_scm2rational (scm_cdar (s), 0);
 	      if (new_alter_pair == SCM_BOOL_F
 		  || extranatural
 		  && (ly_scm2rational (scm_cdr (new_alter_pair)) - old_alter)*old_alter < Rational (0))
diff --git a/lily/lily-guile.cc b/lily/lily-guile.cc
index a1a635961c..976b19e47c 100644
--- a/lily/lily-guile.cc
+++ b/lily/lily-guile.cc
@@ -34,8 +34,8 @@ using namespace std;
 /*
   symbols/strings.
  */
-SCM
-ly_write2scm (SCM s)
+string
+ly_scm_write_string (SCM s)
 {
   SCM port = scm_mkstrport (SCM_INUM0,
 			    scm_make_string (SCM_INUM0, SCM_UNDEFINED),
@@ -46,7 +46,7 @@ ly_write2scm (SCM s)
 
   // scm_apply (write, port, SCM_EOL);
   scm_call_2 (write, s, port);
-  return scm_strport_to_string (port);
+  return ly_scm2string (scm_strport_to_string (port));
 }
 
 SCM
@@ -394,7 +394,7 @@ ly_deep_copy (SCM src)
 string
 print_scm_val (SCM val)
 {
-  string realval = ly_scm2string (ly_write2scm (val));
+  string realval = ly_scm_write_string (val);
   if (realval.length () > 200)
     realval = realval.substr (0, 100)
       + "\n :\n :\n"
@@ -582,6 +582,14 @@ ly_scm2rational (SCM r)
 		   scm_to_int (scm_denominator (r)));
 }
 
+Rational
+robust_scm2rational (SCM n, Rational rat)
+{
+  if (ly_is_fraction (n))
+    return ly_scm2rational (n);
+  else
+    return rat;
+}
 
 SCM
 alist_to_hashq (SCM alist)
@@ -653,13 +661,7 @@ parse_symbol_list (char const *symbols)
   return ly_string_array_to_scm (string_split (s, ' '));
 }
 
-
-bool
-ly_is_fraction (SCM x)
-{
-  return SCM_FRACTIONP(x);
-}
-
+/* GDB debugging. */
 struct ly_t_double_cell
 {
   SCM a;
-- 
2.39.5