From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#67124: 26.3; query-replace Arg out of range with comma option (at end-buffer) Date: Sun, 12 Nov 2023 11:48:17 +0200 Message-ID: <83cywfuwta.fsf@gnu.org> References: <020a72b2-b896-4ecf-abab-111a6c1c9eac@medialab.sissa.it> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38976"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 67124@debbugs.gnu.org To: Gabriele Nicolardi , Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Nov 12 10:49:41 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 1r2765-0009vj-6a for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 12 Nov 2023 10:49:41 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r275n-0005aY-Jz; Sun, 12 Nov 2023 04:49:23 -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 1r275m-0005aC-Fj for bug-gnu-emacs@gnu.org; Sun, 12 Nov 2023 04:49:22 -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 1r275m-0007k5-7Y for bug-gnu-emacs@gnu.org; Sun, 12 Nov 2023 04:49:22 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1r276Q-0005OC-IT for bug-gnu-emacs@gnu.org; Sun, 12 Nov 2023 04:50:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 12 Nov 2023 09:50:02 +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.169978256720663 (code B ref 67124); Sun, 12 Nov 2023 09:50:02 +0000 Original-Received: (at 67124) by debbugs.gnu.org; 12 Nov 2023 09:49:27 +0000 Original-Received: from localhost ([127.0.0.1]:55124 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r275q-0005NB-TX for submit@debbugs.gnu.org; Sun, 12 Nov 2023 04:49:27 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:56518) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r275n-0005MQ-Hz for 67124@debbugs.gnu.org; Sun, 12 Nov 2023 04:49:24 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r2751-0007ZU-LA; Sun, 12 Nov 2023 04:48:35 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=PPsNWj3mQsUsdWugY8prIuZTH0GxLcA/30nTQmdOCxM=; b=TO+w/sgCr16v mlu2mo4tv8hTb3I5GsGELvaApDt3NdS/VhqRKcr5GH5T0vIsC66rwtAxvuuXsYNBb+0DMUEk2kdRy p5U1L+0T9ACSbNDstbqEoc0zKhf16QYU+iUp5vlNdXNVFOVoJJmoZITS4agMKBS/KsK+aQmhOApsE Z6XqAtfGx+0zKkgNkQPyHHU5GrfudE0FlBSR+k36Ffgo5OzJCKn19AJ0CHAh3FdnoWV0pbL2vaPjg t+6A+CeuIfR96dWXLO7JD8sTfXahX0rg+73m52sPG8ieEQOi0a/EO/cnSJPjpDjPxbCyzSt64gTbw 0fwVIoFaJLkkS0FZVqEvpw==; In-Reply-To: <020a72b2-b896-4ecf-abab-111a6c1c9eac@medialab.sissa.it> (message from Gabriele Nicolardi on Sat, 11 Nov 2023 20:40:27 +0100) 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:274191 Archived-At: > Date: Sat, 11 Nov 2023 20:40:27 +0100 > From: Gabriele Nicolardi > > Hi, I'm resending the bug report trying to do better than the previous one. Thanks. (In the future, please respond to the original bug number, instead of creating a new bug report; I've merged them now.) > I often type `,` (`comma`) to check the replacement. It happens that, > some times, I get the error (e.g.): > > match-substitute-replacement: Args out of range: #, > 1667, 1679 > > The error doesn't happen if I type `y` (or `n`) > > Try this: > > (query-replace ",.\\footnote{" ".\\footnote{" nil) > > With the string ",.\footnote{" at the end of the buffer. To see the > error "{" must be the last char in the buffer. > > (I see often this bug because I use "narrowing" a lot) > > Is it a known bug? The kludgey solution in the patch below should fix this. Please see if it indeed fixes your real-life use cases. Stefan, any ideas for a better fix, or other comments? AFAIU, the root cause of the bug was that we are using integers instead of markers there, and that is because bug#31492 needs to preserve the original match-start position when the search string is a regexp that matches an empty string (in which case the match-start marker moves together with the match-end marker instead of staying put). diff --git a/lisp/replace.el b/lisp/replace.el index eeac734..6a3f223 100644 --- a/lisp/replace.el +++ b/lisp/replace.el @@ -2642,8 +2642,11 @@ replace-match-maybe-edit (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 match data (Bug#31492). - (set-match-data match-data) + ;; 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)))