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/26 15:32:08
@@ -69,6 +69,25 @@
   (and (extentp object)
        (extent-property object 'overlay)))
 
+(defsubst overlay-validate-begin-end-buffer ()
+  "validte the value of BEG, END and BUFFER
+
+If BUFFER is nil, set to current buffer.  If BEG is
+greater than END, exchange their value.  If either
+BEG or END is out of buffer boundary, set their
+value to corresponding boundary-value."
+
+  (if (null buffer)
+      (setq buffer (current-buffer))
+    (check-argument-type 'bufferp buffer))
+
+  (when (> beg end)
+    (setq beg (prog1 end (setq end beg))))
+
+  (setq beg (max beg 1)
+	end (min end (1+ (buffer-size))))
+  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,13 +95,15 @@
 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's value will be exchanged if END is less
+than BEG.  And if BEG or END is out of buffer
+boundary the corresponding boundary-value will
+be used."
+
+  (overlay-validate-begin-end-buffer)
+
   (let ((overlay (make-extent beg end buffer)))
     (set-extent-property overlay 'overlay t)
     (if front-advance
@@ -98,12 +119,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's value will be exchanged if END is less
+than BEG.  And if BEG or END is out of buffer
+boundary the corresponding boundary-value will
+be used."
+
   (check-argument-type 'overlayp overlay)
   (if (null buffer)
       (setq buffer (extent-object overlay)))
-  (if (null buffer)
-      (setq buffer (current-buffer)))
+
+  (overlay-validate-begin-end-buffer)
+
   (check-argument-type 'bufferp buffer)
   (and (= beg end)
        (extent-property overlay 'evaporate)
@@ -150,15 +178,32 @@
 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.
+
+If BEG or END is out of buffer boundary the
+corresponding boundary-value will be used."
+
+  (setq beg (max beg 1)
+	end (min end (1+ (buffer-size))))
+
   (mapcar-extents #'identity nil nil beg end
 		  'all-extents-closed-open 'overlay))
 
+(defsubst overlay-validate-pos ()
+  "validate the pos parameter, set it to the corresponding
+boundary-value if it was out of buffer boundary"
+  (setq pos (max pos 1)
+	pos (min pos (1+ (buffer-size)))))
+
 (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 checked, its value will be set to corresponding
+boundary-value if it was out of buffer boundary."
   (let ((next (point-max))
 	tmp)
+    (overlay-validate-pos)
     (map-extents
      (lambda (overlay ignore)
 	    (when (or (and (< (setq tmp (extent-start-position overlay)) next)
@@ -172,9 +217,13 @@
 
 (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 checked, its value will be set to corresponding
+boundary-value if it was out of buffer boundary."
   (let ((prev (point-min))
 	tmp)
+    (overlay-validate-pos)
     (map-extents
      (lambda (overlay ignore)
        (when (or (and (> (setq tmp (extent-end-position overlay)) prev)
@@ -206,7 +255,11 @@
     (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 checked, its value will be set to corresponding
+boundary-value if it was out of buffer boundary."
+  (overlay-validate-pos)
   (set (make-local-variable 'xemacs-internal-overlay-center-pos) pos))
 
 (defun overlay-get (overlay prop)
