Index: gtk/gtktextbtree.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtktextbtree.c,v
retrieving revision 1.77
diff -u -p -r1.77 gtktextbtree.c
--- gtk/gtktextbtree.c	13 Sep 2005 19:57:42 -0000	1.77
+++ gtk/gtktextbtree.c	9 Oct 2005 05:28:06 -0000
@@ -732,7 +732,7 @@ _gtk_text_btree_delete (GtkTextIter *sta
                                              * of the deletion range. */
   GtkTextLineSegment *last_seg;             /* The segment just after the end
                                              * of the deletion range. */
-  GtkTextLineSegment *seg, *next;
+  GtkTextLineSegment *seg, *next, *next2;
   GtkTextLine *curline;
   GtkTextBTreeNode *curnode, *node;
   GtkTextBTree *tree;
@@ -885,12 +885,24 @@ _gtk_text_btree_delete (GtkTextIter *sta
               seg->next = start_line->segments;
               start_line->segments = seg;
             }
-          else
-            {
+          else if (prev_seg->next &&
+		   seg->type == &gtk_text_toggle_off_type &&
+		   prev_seg->next->type == &gtk_text_toggle_on_type &&
+		   seg->body.toggle.info == prev_seg->next->body.toggle.info)
+	    {
+	      next2 = prev_seg->next->next;
+	      g_free ((char *)prev_seg->next);
+	      prev_seg->next = next2;
+	      g_free ((char *)seg);
+	      seg = NULL;
+	    }
+	  else
+	    {
               seg->next = prev_seg->next;
               prev_seg->next = seg;
             }
-          if (seg->type->leftGravity)
+
+          if (seg && seg->type->leftGravity)
             {
               prev_seg = seg;
             }
@@ -4718,16 +4730,20 @@ cleanup_line (GtkTextLine *line)
   while (changed)
     {
       changed = FALSE;
-      for (prev_p = &line->segments, seg = *prev_p;
-           seg != NULL;
-           prev_p = &(*prev_p)->next, seg = *prev_p)
+      prev_p = &line->segments;
+      for (seg = *prev_p; seg != NULL; seg = *prev_p)
         {
           if (seg->type->cleanupFunc != NULL)
             {
               *prev_p = (*seg->type->cleanupFunc)(seg, line);
               if (seg != *prev_p)
-                changed = TRUE;
+		{
+		  changed = TRUE;
+		  continue;
+		}
             }
+
+	  prev_p = &(*prev_p)->next;
         }
     }
 }
