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 10:35:26 -0500 Message-ID: References: <020a72b2-b896-4ecf-abab-111a6c1c9eac@medialab.sissa.it> <83cywfuwta.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="5518"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Gabriele Nicolardi , 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 16:36:11 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 1r3ePb-0001A6-BX for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 16 Nov 2023 16:36:11 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r3ePT-0006eS-Ow; Thu, 16 Nov 2023 10:36:03 -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 1r3ePS-0006di-Ja for bug-gnu-emacs@gnu.org; Thu, 16 Nov 2023 10:36:02 -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 1r3ePS-00050u-Ay for bug-gnu-emacs@gnu.org; Thu, 16 Nov 2023 10:36:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1r3ePR-0007sk-W4 for bug-gnu-emacs@gnu.org; Thu, 16 Nov 2023 10:36:02 -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 15:36:01 +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.170014893830267 (code B ref 67124); Thu, 16 Nov 2023 15:36:01 +0000 Original-Received: (at 67124) by debbugs.gnu.org; 16 Nov 2023 15:35:38 +0000 Original-Received: from localhost ([127.0.0.1]:57435 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r3eP3-0007s5-UX for submit@debbugs.gnu.org; Thu, 16 Nov 2023 10:35:38 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:47836) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r3eP0-0007rp-2g for 67124@debbugs.gnu.org; Thu, 16 Nov 2023 10:35:36 -0500 Original-Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id EBBD744234C; Thu, 16 Nov 2023 10:35:28 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1700148927; bh=AvCmShF2QCPxCzoNeRmgYm8rSLZ2rbT6OZN34XPFig8=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=dGRxjq2Bo0yEWlf+Z027gkI7/T1Z8uhmP4N1qweNUKHav+3j6qKdi2lnS5bsYf6TS UokjhLH3sR2IHY9QUgeVH2tyJQk8q7Qjxgp82m/aTkLJ8hIUkbELkKi/UYVMpjru5n GUfn+5vq6QZ6xwK0SjUeleMXIR/Za8RuG7JFBLuKExS3ZczPTFGDQ55TL61ONTnkeu 8oSPVT5oZHzHhKQrLkec/E0SczW5p1mHIPuEM3h6BCj0mVNxFjuwA+FuHlbVHu1+G5 TKiOZh5GBGTju6ChM4lq/OPnPSCfvz92GzvwBMkTR15Sk9uKaBAsAsd5QDb8J8h5yO HluNXD2DrYnJg== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id B2EF2442347; Thu, 16 Nov 2023 10:35:27 -0500 (EST) Original-Received: from pastel (unknown [45.72.227.120]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 87492120277; Thu, 16 Nov 2023 10:35:27 -0500 (EST) In-Reply-To: (Stefan Monnier's message of "Sun, 12 Nov 2023 22:56:16 -0500") 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:274466 Archived-At: > 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. [...] > 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. Hmm... not sure what happened, because I just tried it again and it does seem to fix the OP's problem. So I suggest the patch below (adjusted to the new code on `master`). Stefan diff --git a/lisp/replace.el b/lisp/replace.el index 7fec54ecb27..acf6b8a4652 100644 --- a/lisp/replace.el +++ b/lisp/replace.el @@ -2642,16 +2642,9 @@ replace-match-maybe-edit noedit nil))) (set-match-data match-data) (replace-match newtext fixedcase literal) - ;; `query-replace' undo feature needs the beginning of the match position, - ;; but `replace-match' may change it, for instance, with a regexp like "^". - ;; Ensure that this function preserves the beginning of the match position - ;; (bug#31492). But we need to avoid clobbering the end of the match with - ;; the original match-end position, since `replace-match' could have made - ;; that incorrect or even invalid (bug#67124). - (set-match-data (list (car match-data) (nth 1 (match-data)))) ;; `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))) + (when backward (goto-char (match-beginning 0))) noedit) (defvar replace-update-post-hook nil diff --git a/src/search.c b/src/search.c index 692d8488049..177bb720641 100644 --- a/src/search.c +++ b/src/search.c @@ -3142,9 +3142,16 @@ update_search_regs (ptrdiff_t oldstart, ptrdiff_t oldend, ptrdiff_t newend) for (i = 0; i < search_regs.num_regs; i++) { - if (search_regs.start[i] >= oldend) + if (search_regs.start[i] <= oldstart) + /* If the subgroup that `replace-match` is modifying encloses the + subgroup `i`, then its `start` position should stay unchanged. + That's always true for subgroup 0. + For other subgroups it depends on details we don't have, so + we optimistically assume that it also holds for them. */ + ; + else if (search_regs.start[i] >= oldend) search_regs.start[i] += change; - else if (search_regs.start[i] > oldstart) + else search_regs.start[i] = oldstart; if (search_regs.end[i] >= oldend) search_regs.end[i] += change;