Announcements and discussions for Gnus, the GNU Emacs Usenet newsreader
 help / color / mirror / Atom feed
From: Julien Cubizolles <j.cubizolles@free.fr>
To: info-gnus-english@gnu.org
Subject: Re: Using lisp code in query-replace-regexp
Date: Wed, 05 Nov 2014 13:46:58 +0100	[thread overview]
Message-ID: <878ujp7r25.fsf@free.fr> (raw)
In-Reply-To: <87d292pwm0.fsf@debian.uxu>

Emanuel Berg <embe8573@student.uu.se> writes:

> Julien Cubizolles <j.cubizolles@free.fr> writes:
>
>> I can use the following replace string (for lack of
>> a better term) with query-replace-regexp
>
> Yes, you can do that.
>
> Check out this:
>
> (while (re-search-forward REGEXP nil t)
>   (replace-match TO-STRING nil nil))
>
> from the help of `replace-regexp'.
>
> So instead of the `replace-match' stuff above, you
> write a function that examines `match-beginning',
> `match-end', and `match-string', and then use that as
> input to your Elisp, to produce the on-the-fly
> TO-STRING (in the phrasing of the above Elisp).

I came up with

--8<---------------cut here---------------start------------->8---

(defun test_gen (begin end from to)
  (save-excursion
    (save-restriction
      (let (deactivate-mark)
	(narrow-to-region begin end)
	(goto-char (point-min))
	(while  (re-search-forward from end t)
	  (replace-match-maybe-edit to t nil nil (match-data) nil)))
      )))
--8<---------------cut here---------------end--------------->8---

I used replace-match-maybe-edit because I wanted to be prompted for each
replacement, and replace-match wouldn't allow it. However with
replace-match-maybe-edit, the excursion is messed up at the end. It
wasn't the case with replace-match.

Julien.



  reply	other threads:[~2014-11-05 12:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.12755.1415111824.1147.info-gnus-english@gnu.org>
2014-11-04 19:57 ` Emanuel Berg
2014-11-05 12:46   ` Julien Cubizolles [this message]
     [not found]   ` <mailman.12880.1415191645.1147.info-gnus-english@gnu.org>
2014-11-05 23:41     ` Emanuel Berg
2014-11-04 14:36 Julien Cubizolles

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

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

  git send-email \
    --in-reply-to=878ujp7r25.fsf@free.fr \
    --to=j.cubizolles@free.fr \
    --cc=info-gnus-english@gnu.org \
    /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 NNTP newsgroup(s).