- if (!is_markup (markup))
- programming_error ("markup head has no markup signature");
-
- /* 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)
+
+ /* Check for non-terminating markups, e.g. recursive calls with
+ * changing arguments */
+ SCM opt_depth = ly_get_option (ly_symbol2scm ("max-markup-depth"));
+ size_t max_depth = robust_scm2int (opt_depth, 1024);
+
+ // Don't use SCM_F_DYNWIND_REWINDABLE since it may be expensive
+ // without any obvious use for retaining continuations into
+ // markup expansion
+ scm_dynwind_begin ((scm_t_dynwind_flags)0);
+ // scm_dynwind_rewind_handler (markup_up_depth, 0, SCM_F_WIND_EXPLICITLY);
+ markup_up_depth (0);
+ scm_dynwind_unwind_handler (markup_down_depth, 0, SCM_F_WIND_EXPLICITLY);
+ if (markup_depth > max_depth)