]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/text-interface.cc
Fix 380: Auto-detect all cyclic references in markups
[lilypond.git] / lily / text-interface.cc
index 8071df3cb856428af7aacac5aaa87028f9fecbb8..5bd13641d737d3bb25dcd08cb6cc3d411a581b8b 100644 (file)
@@ -101,7 +101,27 @@ Text_interface::interpret_markup (SCM layout_smob, SCM props, SCM markup)
       if (!is_markup (markup))
         programming_error ("markup head has no markup signature");
 
-      return scm_apply_2 (func, layout_smob, props, args);
+      /* Use a hare/tortoise algorithm to detect whether we are in a cycle,
+       * i.e. whether we have already encountered the same markup in the
+       * current branch of the markup tree structure. */
+      static vector<SCM> encountered_markups;
+      size_t depth = encountered_markups.size ();
+      if (depth > 0)
+        {
+          int slow = depth / 2;
+          if (ly_is_equal (encountered_markups[slow], markup))
+            {
+              string name = ly_symbol2string (scm_procedure_name (func));
+              // TODO: Also print the arguments of the markup!
+              non_fatal_error (_f("Cyclic markup detected: %s", name));
+              return Stencil().smobbed_copy ();
+            }
+        }
+
+      encountered_markups.push_back (markup);
+      SCM retval = scm_apply_2 (func, layout_smob, props, args);
+      encountered_markups.pop_back ();
+      return retval;
     }
   else
     {