[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Using lisp code in query-replace-regexp
From: |
Julien Cubizolles |
Subject: |
Re: Using lisp code in query-replace-regexp |
Date: |
Wed, 05 Nov 2014 13:46:58 +0100 |
User-agent: |
Gnus/5.130012 (Ma Gnus v0.12) Emacs/25.0.50 (gnu/linux) |
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.