Index: overlay.el
===================================================================
RCS file: /pack/xemacscvs/XEmacs/packages/xemacs-packages/fsf-compat/overlay.el,v
retrieving revision 1.2
diff -u -r1.2 overlay.el
--- overlay.el	1999/11/27 20:49:51	1.2
+++ overlay.el	2008/02/28 14:26:11
@@ -69,6 +69,40 @@
   (and (extentp object)
        (extent-property object 'overlay)))
 
+
+(defsubst overlay-normalize-pos (pos)
+  "return the normalized POS, so 1 <= POS <= \(1+ \(length BUFFER))."
+  (let ((buffer-min 1)
+	(buffer-max (1+ (if (boundp 'buffer)
+			    (buffer-size buffer)
+			  ;; I assume the current buffer, if there's
+			  ;; no buffer symbol.
+			  (buffer-size)))))
+    (cond ((< pos buffer-min)
+	   buffer-min)
+	  ((> pos buffer-max)
+	   buffer-max)
+	  (t
+	   pos))))
+
+(defsubst overlay-normalize-begin-end-buffer ()
+  "BEG and END will be normalized so 1 <= BEG <= END <= \(1+ \(length BUFFER)).
+
+If BUFFER is nil, the current buffer is assumed.  If BEG is
+greater than END, exchange their value."
+
+  (if (null buffer)
+      (setq buffer (current-buffer))
+    (check-argument-type 'bufferp buffer))
+
+  (setq beg (overlay-normalize-pos beg)
+	end (overlay-normalize-pos end))
+
+  (when (> beg end)
+    (setq beg (prog1 end (setq end beg))))
+
+  t)
+
 (defun make-overlay (beg end &optional buffer front-advance rear-advance)
   "Create a new overlay with range BEG to END in BUFFER.
 If omitted, BUFFER defaults to the current buffer.
@@ -76,12 +110,13 @@
 The fourth arg FRONT-ADVANCE, if non-nil, makes the
 front delimiter advance when text is inserted there.
 The fifth arg REAR-ADVANCE, if non-nil, makes the
-rear delimiter advance when text is inserted there."
-  (if (null buffer)
-      (setq buffer (current-buffer))
-    (check-argument-type 'bufferp buffer))
-  (when (> beg end)
-    (setq beg (prog1 end (setq end beg))))
+rear delimiter advance when text is inserted there.
+
+BEG and END will be normalized so 1 <= BEG <= END <= \(1+ \(length BUFFER)).
+If BUFFER is nil, the current buffer is assumed.  If BEG is
+greater than END, exchange their value."
+
+  (overlay-normalize-begin-end-buffer)
 
   (let ((overlay (make-extent beg end buffer)))
     (set-extent-property overlay 'overlay t)
@@ -98,12 +133,19 @@
   "Set the endpoints of OVERLAY to BEG and END in BUFFER.
 If BUFFER is omitted, leave OVERLAY in the same buffer it inhabits now.
 If BUFFER is omitted, and OVERLAY is in no buffer, put it in the current
-buffer."
+buffer.
+
+
+BEG and END will be normalized so 1 <= BEG <= END <= \(1+ \(length BUFFER)).
+If BUFFER is nil, the current buffer is assumed.  If BEG is
+greater than END, exchange their value."
+
   (check-argument-type 'overlayp overlay)
   (if (null buffer)
       (setq buffer (extent-object overlay)))
-  (if (null buffer)
-      (setq buffer (current-buffer)))
+
+  (overlay-normalize-begin-end-buffer)
+
   (check-argument-type 'bufferp buffer)
   (and (= beg end)
        (extent-property overlay 'evaporate)
@@ -150,15 +192,24 @@
 Overlap means that at least one character is contained within the overlay
 and also contained within the specified region.
 Empty overlays are included in the result if they are located at BEG
-or between BEG and END."
+or between BEG and END.
+
+BEG and END will be normalized so 1 <= BEG <= END <= \(1+ \(length CURRENT-BUFFER))."
+
+  (setq beg (overlay-normalize-pos beg)
+	end (overlay-normalize-pos end))
+
   (mapcar-extents #'identity nil nil beg end
 		  'all-extents-closed-open 'overlay))
 
 (defun next-overlay-change (pos)
   "Return the next position after POS where an overlay starts or ends.
-If there are no more overlay boundaries after POS, return (point-max)."
+If there are no more overlay boundaries after POS, return (point-max).
+
+POS will be normalized  so 1 <= POS <= \(1+ \(length CURRENT-BUFFER))."
   (let ((next (point-max))
 	tmp)
+    (setq pos (overlay-normalize-pos pos))
     (map-extents
      (lambda (overlay ignore)
 	    (when (or (and (< (setq tmp (extent-start-position overlay)) next)
@@ -172,9 +223,12 @@
 
 (defun previous-overlay-change (pos)
   "Return the previous position before POS where an overlay starts or ends.
-If there are no more overlay boundaries before POS, return (point-min)."
+If there are no more overlay boundaries before POS, return (point-min).
+
+POS will be normalized  so 1 <= POS <= \(1+ \(length CURRENT-BUFFER))."
   (let ((prev (point-min))
 	tmp)
+    (setq pos (overlay-normalize-pos pos))
     (map-extents
      (lambda (overlay ignore)
        (when (or (and (> (setq tmp (extent-end-position overlay)) prev)
@@ -206,7 +260,10 @@
     (cons (nreverse before) (nreverse after))))
 
 (defun overlay-recenter (pos)
-  "Recenter the overlays of the current buffer around position POS."
+  "Recenter the overlays of the current buffer around position POS.
+
+POS will be normalized  so 1 <= POS <= \(1+ \(length CURRENT-BUFFER))."
+  (setq pos (overlay-normalize-pos pos))
   (set (make-local-variable 'xemacs-internal-overlay-center-pos) pos))
 
 (defun overlay-get (overlay prop)

