[gnu.emacs.bug] match data cannot always be restored

juha@tds.kth.se (Juha Sarlin) (10/20/89)

After a string-match the value of (match-beginning 0) can be zero.
The zero is changed into one by:
    (store-match-data (match-data))

The function below demonstrates the error. In emacs 18.55 on a Sun 3/60
running SunOS 4.0.1 it returns (0 1), which means that match-beginning
has changed from 0 to 1.

(defun bug ()
  (string-match "foo" "foobar")
  (let ((before (match-beginning 0)))
    (store-match-data (match-data))
    (list before (match-beginning 0))))

This error makes the debugging of some functions that use string-match
difficult, because the debugger uses match-data and store-match-data.
--
Juha Sarlin	juha@tds.kth.se

kjones@talos.uucp (Kyle Jones) (10/24/89)

Juha Sarlin writes:
 > After a string-match the value of (match-beginning 0) can be zero.
 > The zero is changed into one by:
 >     (store-match-data (match-data))

This applies to clipping regions in general.  If a match-datum falls
outside the clipping region of the current buffer, it will be changed to
point-min or point-max, whichever is closer.  This is because match-data
returns match pairs as a list of markers, instead of a list of integers.
Markers, at the time of their creation, are constrained to be within the
clipping region of the buffer in which they reside.

Since match-beginning and match-end return integers, you can almost get
around this problem by building your own match-data list, i.e.

(defun integer-match-data ()
  (let ((index '(9 8 7 6 5 4 3 2 1 0))
        (list))
    (while index
      (setq list (cons (match-beginning (car index))
		       (cons (match-end (car index)) list))
	    index (cdr index)))
    list ))

Unfortunately store-match-data only accepts a list of MARKERS, so you
can't feed this list back into the regexp system.

Changing the match-data function to return integers would probably break
existing code, therefore I suggest that store-match-data be allowed to
accept integers as well as markers.