unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Yuri Khan <yuri.v.khan@gmail.com>
To: Emanuel Berg <moasenwood@zoho.eu>,
	help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: replace-regexp
Date: Sat, 8 May 2021 12:38:35 +0700	[thread overview]
Message-ID: <CAP_d_8VFdat+2OBrLTraaVskGZncbMCSGD9Ck3j31RQkxO_Bqw@mail.gmail.com> (raw)
In-Reply-To: <877dkaytwu.fsf@zoho.eu>

On Sat, 8 May 2021 at 07:02, Emanuel Berg via Users list for the GNU
Emacs text editor <help-gnu-emacs@gnu.org> 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
‘narrow-to-region’ 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 — e.g. by incrementing ‘end’ by (- (length
"\\\\textit{}") (length "__")) on each iteration — or by putting a
marker at end and then passing that marker in re-search-forward. But
narrowing is easier.



  parent reply	other threads:[~2021-05-08  5:38 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-06 23:06 replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-07  6:54 ` replace-regexp Tassilo Horn
2021-05-07 18:28   ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08  0:02     ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08  0:16       ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08  5:38       ` Yuri Khan [this message]
2021-05-08 13:53         ` replace-regexp Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-08 18:41           ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 18:50             ` replace-regexp Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-08 21:59               ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 22:03                 ` replace-regexp Tassilo Horn
2021-05-08 22:25                   ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 22:54                     ` [External] : replace-regexp Drew Adams
2021-05-09  2:48             ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 18:46           ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 21:10             ` replace-regexp Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-08 21:54               ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 23:11                 ` replace-regexp Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-08 23:16                   ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 23:46                     ` replace-regexp Stefan Monnier via Users list for the GNU Emacs text editor
2021-05-08 23:51                       ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09  7:38                       ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-08 22:59           ` avoid narrow-to-region (was: Re: replace-regexp) Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09  5:48             ` Yuri Khan
2021-05-09  6:09               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09  6:34                 ` Yuri Khan
2021-05-09  6:59                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09  7:22                     ` Jean Louis
2021-05-09  7:40                       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09 10:06                     ` Yuri Khan
2021-05-09 10:54                       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09 12:27                         ` Yuri Khan
2021-05-09 12:43                           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09 13:14                           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09 14:04                             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-09 15:13                               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-21 17:35                                 ` same sound random sort everywhere (was: Re: avoid narrow-to-region (was: Re: replace-regexp)) Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-21 20:12                                   ` Jean Louis
2021-05-21 20:47                                     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-07  8:02 ` replace-regexp Jean Louis
2021-05-07 18:29   ` replace-regexp Emanuel Berg via Users list for the GNU Emacs text editor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAP_d_8VFdat+2OBrLTraaVskGZncbMCSGD9Ck3j31RQkxO_Bqw@mail.gmail.com \
    --to=yuri.v.khan@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=moasenwood@zoho.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).