From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#67124: 26.3; query-replace Arg out of range with comma option (at end-buffer) Date: Thu, 16 Nov 2023 09:29:22 -0500 Message-ID: References: <020a72b2-b896-4ecf-abab-111a6c1c9eac@medialab.sissa.it> <83cywfuwta.fsf@gnu.org> <83pm0du49e.fsf@gnu.org> Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="15802"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: gabriele@medialab.sissa.it, 67124@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Nov 16 15:30:44 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1r3dOG-0003rT-Ea for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 16 Nov 2023 15:30:44 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r3dO6-0003vr-QF; Thu, 16 Nov 2023 09:30:34 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r3dNl-0003q6-6S for bug-gnu-emacs@gnu.org; Thu, 16 Nov 2023 09:30:14 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r3dNb-0000z1-VM for bug-gnu-emacs@gnu.org; Thu, 16 Nov 2023 09:30:04 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1r3dNb-00059A-4V for bug-gnu-emacs@gnu.org; Thu, 16 Nov 2023 09:30:03 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 16 Nov 2023 14:30:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 67124 X-GNU-PR-Package: emacs Original-Received: via spool by 67124-submit@debbugs.gnu.org id=B67124.170014497319698 (code B ref 67124); Thu, 16 Nov 2023 14:30:03 +0000 Original-Received: (at 67124) by debbugs.gnu.org; 16 Nov 2023 14:29:33 +0000 Original-Received: from localhost ([127.0.0.1]:54840 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r3dN6-00057d-KB for submit@debbugs.gnu.org; Thu, 16 Nov 2023 09:29:33 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:3203) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r3dN3-00057M-UR for 67124@debbugs.gnu.org; Thu, 16 Nov 2023 09:29:31 -0500 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id C6E224422E5; Thu, 16 Nov 2023 09:29:24 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1700144963; bh=C9Og4+niOVp0KzjskbiIZC4Ugns5j5XGzXBLq2SQd9s=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=UXNhdOhyKu2USLC3NhFQRKEwmzb7+xLs7F0cxKLw2zZX406ag0+RHNatQ1qaEwMVW nrOBA7X64NkdjOCHQW22l6W4+NxaVZgRhwZD1CsYFz80SoKdkfiSSSvHC+6btUZfqF k+FToNrcPwDvW3u+9OtIOv1kAEAE0QKCC/UIYGjghAZNYwJSxdOp7FxGcWnrPMQUtS eH4NsfBfObanxLMvF9Pfz7bypr4NFIOYeulYyEollQG1k2Zp7Eqpk1aPczghbg1yyw b4JA5mWcMrqzqFR3LofILhtQsLqAbc68Rji0csWJlkMcKNtTiK+o7Ow75v+4Io0gg3 5nt3KsdplSJiA== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 71FE6442319; Thu, 16 Nov 2023 09:29:23 -0500 (EST) Original-Received: from pastel (unknown [45.72.227.120]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 45CF4120199; Thu, 16 Nov 2023 09:29:23 -0500 (EST) In-Reply-To: <83pm0du49e.fsf@gnu.org> (Eli Zaretskii's message of "Mon, 13 Nov 2023 16:17:17 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:274457 Archived-At: [ Sorry for my late reply, somehow I missed this reply of yours when you sent it :-( ] >> (nth 1 (match-data)) == (match-end 0), no? > No, because the former normally returns a marker, whereas the latter > returns a number. And here the difference is crucial. It returns a different representation of the same number, agreed. But we pass that to `set-match-data` which won't keep that difference, will it? >> It's probably OK, indeed, but I think the comment should clarify >> that (and should clarify that we (well, presumably the undo feature) >> need the match end in addition to the match beginning). > The comment before the patched part (you can see its end in the patch) > says so, no? It says that we need to adjust (match-beginning 0), while keeping (match-end 0) but it doesn't say that it's OK that we throw away the rest. Probably not important, tho, you're right. >> Also here it's not obvious which match-data is returned by (match-data). >> IIUC it's the match data as adjusted by `replace-match`. > Yes, and that's the root cause here: replace-match updates match-data, > but the original code then clobbered it by overwriting it with the > match-data _before_ the replace-match call. I think the comment should mention it. >> Which makes me wonder why we don't change `replace-match` so it's also >> careful to preserve the match beginning just like it preserves the match >> end. > AFAIU, it's a general issue with markers: But the match data adjusted by `replace-match` is not made of markers. It's plain integers which are manually adjusted (by the code which I changed in my patch). > when you have both match-beginning and match-end at the same buffer > position (because the matched text is an empty string, as when the > search regexp is \b or similar, then replace-match moves them both to > the end of the match, instead of leaving one of them at the beginning > of the match. I agree if they were markers that's what would happen. But instead the behavior we see is because the code of `update_search_regs` mimics the behavior of "insert-before" markers both for start and end boundaries of subgroups. My patch tries to make it more careful. >> > ;; `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))) >> >> and (nth 0 match-data) == (match-beginning 0), no? > See above: not exactly. I believe the numerical value of (nth 0 match-data) will be the same at this point as that of (nth 0 (match-data)) because we just passed that very value to `set-match-data`, and that is always equal (numerically) to (match-beginning 0). Since the only thing we do with that value is pass it immediately to `goto-char`, it makes no difference if it's a marker or an integer, no? What am I missing? >> So, I tried the patch below, which makes sense to my superficial >> understanding of the problem, but it apparently doesn't fix the problem >> in the OP's recipe, so I'm clearly missing something. > I don't understand the fix, so cannot help you here ;-) :-( Stefan