From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuri Khan Newsgroups: gmane.emacs.help Subject: Re: replace-regexp Date: Sat, 8 May 2021 12:38:35 +0700 Message-ID: References: <87tunf1mz5.fsf@zoho.eu> <87v97v6mxg.fsf@gnu.org> <87wnsaz9df.fsf@zoho.eu> <877dkaytwu.fsf@zoho.eu> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33909"; mail-complaints-to="usenet@ciao.gmane.io" To: Emanuel Berg , help-gnu-emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sat May 08 07:39:36 2021 Return-path: Envelope-to: geh-help-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 1lfFge-0008i0-93 for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 08 May 2021 07:39:36 +0200 Original-Received: from localhost ([::1]:56242 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lfFgd-0001VH-5l for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 08 May 2021 01:39:35 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47842) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lfFfy-0001Us-Lx for help-gnu-emacs@gnu.org; Sat, 08 May 2021 01:38:54 -0400 Original-Received: from mail-vs1-xe33.google.com ([2607:f8b0:4864:20::e33]:34346) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lfFfs-0000rZ-Kt for help-gnu-emacs@gnu.org; Sat, 08 May 2021 01:38:54 -0400 Original-Received: by mail-vs1-xe33.google.com with SMTP id d25so5795378vsp.1 for ; Fri, 07 May 2021 22:38:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-transfer-encoding; bh=kmlRniU5J/E7SjpqCMfRs6DwwybBQxoVJzyc4KtEco0=; b=ATgAXW6a7w3h0HQCMaBdoZ2goCF1ntxfgfd8ig56jcWwW8Bn2piIuN3jlMJ9P1gEKy sSIjI/QbDe99n4ny0kxGvurQnNf9GnCrZ+5K83J8sbWvpdsSaKr8zckZvnun+TwYfFzn UFJ1tVoQaiz1nMR2ABzxcYmLKClNm76WjLfhYpYdb+/OJhx+2rtJ+kjMQobvhsK6FOo0 AvPYawqAUNvGbxbgUGTJJ82XaW6b0JRcUl/EhrOe9Y+63z4x0JzAP9KNOwYrDg8X5fvG 8PLXUEBDI1YJO9x829UIKeuF//65ZDQXRLckqQOtWyscsIzt9m4ny80vOPCDlflD9YXt jMCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=kmlRniU5J/E7SjpqCMfRs6DwwybBQxoVJzyc4KtEco0=; b=WwzMd83ZnHN1VugHM3TZBz6FbhVLAZHn6JRIIqfKmHiE9TMaNwVS32WArHh59S8wfS oAHtynNICdM0K98nl+jAFQ7lYvjMxVx+8b7wLH85MG/s3T9UbPpduZCF06FfOnfwE5vf hzYT2kNESBGX7tGvnkjDrOusgku/ggqmvhXkIpErMEiR0mtvvUVEP6Lpyip53Km3XxYm uPN6G9cPjn5litWwsyZ2VEzWOg2OvCBE2wEU5k8mIlceYLlYiQGja/Xx8Ggn6oQ2T2gD Y4/NlITnTgDSAKfrOWLg4xEz6WpyfWwL7MzG070kK1vuL3uRR5zlK4xSvSo4DPLi+4KL 2sVQ== X-Gm-Message-State: AOAM530f0hznJxVVfNiem1cyzPtJaESIIVAC7SBmkvPaD40zFtPaWFnV PzoYF5ZVvC9rjkS/GPZzMSY2Zzk2sTO42aZ2V6o= X-Google-Smtp-Source: ABdhPJwjSN7RHhSdvQ2htF1pNBsqeu2lr3e+16nH03DlWtEAh5eNYyxjT33EQUk0z+Q1l/XQg4sa1CHAwspN5YlJvwU= X-Received: by 2002:a05:6102:c4:: with SMTP id u4mr12322961vsp.51.1620452326532; Fri, 07 May 2021 22:38:46 -0700 (PDT) In-Reply-To: <877dkaytwu.fsf@zoho.eu> Received-SPF: pass client-ip=2607:f8b0:4864:20::e33; envelope-from=yurivkhan@gmail.com; helo=mail-vs1-xe33.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:129557 Archived-At: On Sat, 8 May 2021 at 07:02, Emanuel Berg via Users list for the GNU Emacs text editor wrote: > > Maybe it has something to do with the replacement being > > longer than the original string? > > That's it, I think, as this doesn't give me the error: > > (defun md-latex-reduce (beg end) > (interactive "r") > (save-excursion > (goto-char beg) > (while (re-search-forward "_\\(.*\\)_" end t) > (replace-match "\\1") ))) And this is probably one of the reasons why most[citation needed] tutorials, when implementing a function operating on a region, do a =E2=80=98narrow-to-region=E2=80=99 first thing. (defun md-latex-reduce (beg end) (interactive "r") (save-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (while (re-search-forward "_\\(.*\\)_" nil t) (replace-match "\\\\textit{\\1}")))) You might probably solve the same issue by carefully tracking the lengthening of text =E2=80=94 e.g. by incrementing =E2=80=98end=E2=80=99 by= (- (length "\\\\textit{}") (length "__")) on each iteration =E2=80=94 or by putting a marker at end and then passing that marker in re-search-forward. But narrowing is easier.