=== modified file 'src/buffer.c' --- src/buffer.c 2012-07-04 15:49:46 +0000 +++ src/buffer.c 2012-07-05 05:22:03 +0000 @@ -667,27 +667,32 @@ return buf; } +/* Mark OV as no longer associated with B. */ + +static void +drop_overlay (struct buffer *b, struct Lisp_Overlay *ov) +{ + eassert (b == XBUFFER (Fmarker_buffer (ov->start))); + modify_overlay (b, marker_position (ov->start), marker_position (ov->end)); + Fset_marker (ov->start, Qnil, Qnil); + Fset_marker (ov->end, Qnil, Qnil); + +} + +/* Delete all overlays of B and reset it's overlay lists. */ + void delete_all_overlays (struct buffer *b) { - Lisp_Object overlay; - - /* `reset_buffer' blindly sets the list of overlays to NULL, so we - have to empty the list, otherwise we end up with overlays that - think they belong to this buffer while the buffer doesn't know about - them any more. */ - while (b->overlays_before) - { - XSETMISC (overlay, b->overlays_before); - Fdelete_overlay (overlay); - } - while (b->overlays_after) - { - XSETMISC (overlay, b->overlays_after); - Fdelete_overlay (overlay); - } - eassert (b->overlays_before == NULL); - eassert (b->overlays_after == NULL); + struct Lisp_Overlay *ov, *next; + + for (ov = b->overlays_before; ov; ov = next) + drop_overlay (b, ov), next = ov->next, ov->next = NULL; + + for (ov = b->overlays_after; ov; ov = next) + drop_overlay (b, ov), next = ov->next, ov->next = NULL; + + b->overlays_before = b->overlays_after = NULL; } /* Reinitialize everything about a buffer except its name and contents @@ -3821,11 +3826,7 @@ = unchain_overlay (b->overlays_after, XOVERLAY (overlay)); eassert (XOVERLAY (overlay)->next == NULL); - modify_overlay (b, - marker_position (OVERLAY_START (overlay)), - marker_position (OVERLAY_END (overlay))); - Fset_marker (OVERLAY_START (overlay), Qnil, Qnil); - Fset_marker (OVERLAY_END (overlay), Qnil, Qnil); + drop_overlay (b, XOVERLAY (overlay)); /* When deleting an overlay with before or after strings, turn off display optimizations for the affected buffer, on the basis that === modified file 'src/minibuf.c' --- src/minibuf.c 2012-06-28 12:29:37 +0000 +++ src/minibuf.c 2012-07-05 05:12:04 +0000 @@ -804,10 +804,9 @@ else { ptrdiff_t count = SPECPDL_INDEX (); - /* `reset_buffer' blindly sets the list of overlays to NULL, so we - have to empty the list, otherwise we end up with overlays that - think they belong to this buffer while the buffer doesn't know about - them any more. */ + /* We have to empty both overlay lists. Otherwise we end + up with overlays that think they belong to this buffer + while the buffer doesn't know about them any more. */ delete_all_overlays (XBUFFER (buf)); reset_buffer (XBUFFER (buf)); record_unwind_protect (Fset_buffer, Fcurrent_buffer ());