Subject: Re: bug#67124: 26.3; query-replace Arg out of range with comma option (at end-buffer)

From: Eli Zaretskii <[email-masked]>
Date: Thu, 16 Nov 2023 18:56:00 +0200

> From: Stefan Monnier <[email-masked]>
> Cc: [email-masked],  [email-masked]
> Date: Thu, 16 Nov 2023 11:15:56 -0500
> 
> > What are you trying to understand?
> 
> Why you're saying that in
> 
>     ;; `replace-match' leaves point at the end of the replacement text,
>     ;; so move point to the beginning when replacing backward.
>     (when backward (goto-char (nth 0 match-data)))
> 
> it is not true that
> 
>     and (nth 0 match-data) == (match-beginning 0), no?

Because of markers vs positions, as I've tried to explain.  The
difference is minor, of course.

> > What my kludge did is simply use a marker, so the adjusted position is
> > not clobbered.
> 
> I don't see that.  E.g. if you change your code from
> 
>     (set-match-data (list (car match-data) (nth 1 (match-data))))
> to
>     (set-match-data (list (car match-data) (nth 1 (match-data t))))
> 
> it fixes the problem just as well, AFAICT.

Yes, but match-data (the function) returns updated positions, which
behave like markers across the replace-match call.

> My understand is that what you patch does is use the match end as
> adjusted by `replace-match` rather than the match end as provided by the
> `match-end` argument (because that argument contains integers and hence
> ends up pointing to the wrong place after the buffer was modified by
> `replace-match`).

Yes.

> Basically, the purpose of the above `set-match-data` originally was to
> correct the (match-beginning 0) info because `replace-match` adjusted it
> incorrectly (i.e. it works around a bug in `update_search_regs`), but
> while doing that it ended up messing up the (match-end 0), so your patch
> refines that code so as to leave (match-end 0) unchanged while
> correcting (match-beginning 0).

Yes.