+/* Iterate over the line and fill in display->cursors.
+ * It's a stripped copy of gtk_text_layout_get_line_display() */
+static void
+update_text_display_cursors (GtkTextLayout *layout,
+ GtkTextLine *line,
+ GtkTextLineDisplay *display)
+{
+ GtkTextLineSegment *seg;
+ GtkTextIter iter;
+ gint layout_byte_offset, buffer_byte_offset;
+ GSList *cursor_byte_offsets = NULL;
+ GSList *cursor_segs = NULL;
+ GSList *tmp_list1, *tmp_list2;
+
+ if (!display->cursors_invalid)
+ return;
+
+ display->cursors_invalid = FALSE;
+
+ /* Special-case optimization for completely
+ * invisible lines; makes it faster to deal
+ * with sequences of invisible lines.
+ */
+ if (totally_invisible_line (layout, line, &iter))
+ return;
+
+ /* Iterate over segments */
+ layout_byte_offset = 0; /* position in the layout text (includes preedit, does not include invisible text) */
+ buffer_byte_offset = 0; /* position in the buffer line */
+ seg = _gtk_text_iter_get_any_segment (&iter);
+ while (seg != NULL)
+ {
+ /* Displayable segments */
+ if (seg->type == >k_text_char_type ||
+ seg->type == >k_text_pixbuf_type ||
+ seg->type == >k_text_child_type)
+ {
+ gtk_text_layout_get_iter_at_line (layout, &iter, line,
+ buffer_byte_offset);
+
+ if (!_gtk_text_btree_char_is_invisible (&iter))
+ layout_byte_offset += seg->byte_count;
+
+ buffer_byte_offset += seg->byte_count;
+ }
+
+ /* Marks */
+ else if (seg->type == >k_text_right_mark_type ||
+ seg->type == >k_text_left_mark_type)
+ {
+ gint cursor_offset = 0;
+
+ /* At the insertion point, add the preedit string, if any */
+
+ if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
+ seg->body.mark.obj))
+ {
+ display->insert_index = layout_byte_offset;
+
+ if (layout->preedit_len > 0)
+ {
+ layout_byte_offset += layout->preedit_len;
+ /* DO NOT increment the buffer byte offset for preedit */
+ cursor_offset = layout->preedit_cursor - layout->preedit_len;
+ }
+ }
+
+ /* Display visible marks */
+
+ if (seg->body.mark.visible)
+ {
+ cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
+ GINT_TO_POINTER (layout_byte_offset + cursor_offset));
+ cursor_segs = g_slist_prepend (cursor_segs, seg);
+ }
+ }
+
+ /* Toggles */
+ else if (seg->type == >k_text_toggle_on_type ||
+ seg->type == >k_text_toggle_off_type)
+ {
+ }
+
+ else
+ g_error ("Unknown segment type: %s", seg->type->name);
+
+ seg = seg->next;
+ }
+
+ tmp_list1 = cursor_byte_offsets;
+ tmp_list2 = cursor_segs;
+ while (tmp_list1)
+ {
+ add_cursor (layout, display, tmp_list2->data,
+ GPOINTER_TO_INT (tmp_list1->data));
+ tmp_list1 = tmp_list1->next;
+ tmp_list2 = tmp_list2->next;
+ }
+ g_slist_free (cursor_byte_offsets);
+ g_slist_free (cursor_segs);
+}
+
+/* Same as _gtk_text_btree_get_tags(), except it returns GPtrArray,
+ * to be used in gtk_text_layout_get_line_display(). */
+static GPtrArray *
+get_tags_array_at_iter (GtkTextIter *iter)
+{
+ GtkTextTag **tags;
+ GPtrArray *array = NULL;
+ gint n_tags;
+
+ tags = _gtk_text_btree_get_tags (iter, &n_tags);
+
+ if (n_tags > 0)
+ {
+ array = g_ptr_array_sized_new (n_tags);
+ g_ptr_array_set_size (array, n_tags);
+ memcpy (array->pdata, tags, n_tags * sizeof (GtkTextTag*));
+ }
+
+ g_free (tags);
+ return array;
+}
+
+/* Add the tag to the array if it's not there already, and remove
+ * it otherwise. It keeps the array sorted by tags priority. */
+static GPtrArray *
+tags_array_toggle_tag (GPtrArray *array,
+ GtkTextTag *tag)
+{
+ gint pos;
+ GtkTextTag **tags;
+
+ if (array == NULL)
+ array = g_ptr_array_new ();
+
+ tags = (GtkTextTag**) array->pdata;
+
+ for (pos = 0; pos < array->len && tags[pos]->priv->priority < tag->priv->priority; pos++) ;
+
+ if (pos < array->len && tags[pos] == tag)
+ g_ptr_array_remove_index (array, pos);
+ else
+ {
+ g_ptr_array_set_size (array, array->len + 1);
+ if (pos < array->len - 1)
+ memmove (array->pdata + pos + 1, array->pdata + pos,
+ (array->len - pos - 1) * sizeof (GtkTextTag*));
+ array->pdata[pos] = tag;
+ }
+
+ return array;
+}
+