+/**
+ * g_markup_parse_context_get_user_data:
+ * @context: a #GMarkupParseContext
+ *
+ * Returns the user_data associated with @context.
+ *
+ * This will either be the user_data that was provided to
+ * g_markup_parse_context_new() or to the most recent call
+ * of g_markup_parse_context_push().
+ *
+ * Returns: the provided user_data. The returned data belongs to
+ * the markup context and will be freed when
+ * g_markup_parse_context_free() is called.
+ *
+ * Since: 2.18
+ */
+gpointer
+g_markup_parse_context_get_user_data (GMarkupParseContext *context)
+{
+ return context->user_data;
+}
+
+/**
+ * g_markup_parse_context_push:
+ * @context: a #GMarkupParseContext
+ * @parser: a #GMarkupParser
+ * @user_data: user data to pass to #GMarkupParser functions
+ *
+ * Temporarily redirects markup data to a sub-parser.
+ *
+ * This function may only be called from the start_element handler of
+ * a #GMarkupParser. It must be matched with a corresponding call to
+ * g_markup_parse_context_pop() in the matching end_element handler
+ * (except in the case that the parser aborts due to an error).
+ *
+ * All tags, text and other data between the matching tags is
+ * redirected to the subparser given by @parser. @user_data is used
+ * as the user_data for that parser. @user_data is also passed to the
+ * error callback in the event that an error occurs. This includes
+ * errors that occur in subparsers of the subparser.
+ *
+ * The end tag matching the start tag for which this call was made is
+ * handled by the previous parser (which is given its own user_data)
+ * which is why g_markup_parse_context_pop() is provided to allow "one
+ * last access" to the @user_data provided to this function. In the
+ * case of error, the @user_data provided here is passed directly to
+ * the error callback of the subparser and g_markup_parse_context_pop()
+ * should not be called. In either case, if @user_data was allocated
+ * then it ought to be freed from both of these locations.
+ *
+ * This function is not intended to be directly called by users
+ * interested in invoking subparsers. Instead, it is intended to be
+ * used by the subparsers themselves to implement a higher-level
+ * interface.
+ *
+ * As an example, see the following implementation of a simple
+ * parser that counts the number of tags encountered.
+ *
+ * |[<!-- language="C" -->
+ * typedef struct
+ * {
+ * gint tag_count;
+ * } CounterData;
+ *
+ * static void
+ * counter_start_element (GMarkupParseContext *context,
+ * const gchar *element_name,
+ * const gchar **attribute_names,
+ * const gchar **attribute_values,
+ * gpointer user_data,
+ * GError **error)
+ * {
+ * CounterData *data = user_data;
+ *
+ * data->tag_count++;
+ * }
+ *
+ * static void
+ * counter_error (GMarkupParseContext *context,
+ * GError *error,
+ * gpointer user_data)
+ * {
+ * CounterData *data = user_data;
+ *
+ * g_slice_free (CounterData, data);
+ * }
+ *
+ * static GMarkupParser counter_subparser =
+ * {
+ * counter_start_element,
+ * NULL,
+ * NULL,
+ * NULL,
+ * counter_error
+ * };
+ * ]|
+ *
+ * In order to allow this parser to be easily used as a subparser, the
+ * following interface is provided:
+ *
+ * |[<!-- language="C" -->
+ * void
+ * start_counting (GMarkupParseContext *context)
+ * {
+ * CounterData *data = g_slice_new (CounterData);
+ *
+ * data->tag_count = 0;
+ * g_markup_parse_context_push (context, &counter_subparser, data);
+ * }
+ *
+ * gint
+ * end_counting (GMarkupParseContext *context)
+ * {
+ * CounterData *data = g_markup_parse_context_pop (context);
+ * int result;
+ *
+ * result = data->tag_count;
+ * g_slice_free (CounterData, data);
+ *
+ * return result;
+ * }
+ * ]|
+ *
+ * The subparser would then be used as follows:
+ *
+ * |[<!-- language="C" -->
+ * static void start_element (context, element_name, ...)
+ * {
+ * if (strcmp (element_name, "count-these") == 0)
+ * start_counting (context);
+ *
+ * // else, handle other tags...
+ * }
+ *
+ * static void end_element (context, element_name, ...)
+ * {
+ * if (strcmp (element_name, "count-these") == 0)
+ * g_print ("Counted %d tags\n", end_counting (context));
+ *
+ * // else, handle other tags...
+ * }
+ * ]|
+ *
+ * Since: 2.18
+ **/
+void
+g_markup_parse_context_push (GMarkupParseContext *context,
+ const GMarkupParser *parser,
+ gpointer user_data)
+{
+ GMarkupRecursionTracker *tracker;
+
+ tracker = g_slice_new (GMarkupRecursionTracker);
+ tracker->prev_element = context->subparser_element;
+ tracker->prev_parser = context->parser;
+ tracker->prev_user_data = context->user_data;
+
+ context->subparser_element = current_element (context);
+ context->parser = parser;
+ context->user_data = user_data;
+
+ context->subparser_stack = g_slist_prepend (context->subparser_stack,
+ tracker);
+}
+
+/**
+ * g_markup_parse_context_pop:
+ * @context: a #GMarkupParseContext
+ *
+ * Completes the process of a temporary sub-parser redirection.
+ *
+ * This function exists to collect the user_data allocated by a
+ * matching call to g_markup_parse_context_push(). It must be called
+ * in the end_element handler corresponding to the start_element
+ * handler during which g_markup_parse_context_push() was called.
+ * You must not call this function from the error callback -- the
+ * @user_data is provided directly to the callback in that case.
+ *
+ * This function is not intended to be directly called by users
+ * interested in invoking subparsers. Instead, it is intended to
+ * be used by the subparsers themselves to implement a higher-level
+ * interface.
+ *
+ * Returns: the user data passed to g_markup_parse_context_push()
+ *
+ * Since: 2.18
+ */
+gpointer
+g_markup_parse_context_pop (GMarkupParseContext *context)
+{
+ gpointer user_data;
+
+ if (!context->awaiting_pop)
+ possibly_finish_subparser (context);
+
+ g_assert (context->awaiting_pop);
+
+ context->awaiting_pop = FALSE;
+
+ /* valgrind friendliness */
+ user_data = context->held_user_data;
+ context->held_user_data = NULL;
+
+ return user_data;
+}
+