From 48b1e7cf2bf6bf70340c49aaac393176bf3d7aaf Mon Sep 17 00:00:00 2001
From: Stefan Monnier <monnier@iro.umontreal.ca>
Date: Thu, 11 Feb 2010 14:35:36 -0500
Subject: [PATCH] * subr.el (copy-overlay): Handle deleted overlays.

---
 lisp/ChangeLog |  2 ++
 lisp/subr.el   | 12 ++++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index e5ad1f14462..559d4f54800 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,5 +1,7 @@
 2010-02-11  Stefan Monnier  <monnier@iro.umontreal.ca>
 
+	* subr.el (copy-overlay): Handle deleted overlays.
+
 	* man.el (Man-completion-table): Don't signal an error if we can't run
 	manual-program (bug#4056).
 
diff --git a/lisp/subr.el b/lisp/subr.el
index b80988ad723..3b5d977c1f8 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -2232,10 +2232,14 @@ If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
 
 (defun copy-overlay (o)
   "Return a copy of overlay O."
-  (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
-			  ;; FIXME: there's no easy way to find the
-			  ;; insertion-type of the two markers.
-			  (overlay-buffer o)))
+  (let ((o1 (if (overlay-buffer o)
+                (make-overlay (overlay-start o) (overlay-end o)
+                              ;; FIXME: there's no easy way to find the
+                              ;; insertion-type of the two markers.
+                              (overlay-buffer o))
+              (let ((o1 (make-overlay (point-min) (point-min))))
+                (delete-overlay o1)
+                o1))))
 	(props (overlay-properties o)))
     (while props
       (overlay-put o1 (pop props) (pop props)))
-- 
2.39.5