Announcements and discussions for Gnus, the GNU Emacs Usenet newsreader
 help / color / mirror / Atom feed
* Using lisp code in query-replace-regexp
@ 2014-11-04 14:36 Julien Cubizolles
  0 siblings, 0 replies; 4+ messages in thread
From: Julien Cubizolles @ 2014-11-04 14:36 UTC (permalink / raw)
  To: info-gnus-english

I can use the following replace string (for lack of a better term) with
query-replace-regexp

--8<---------------cut here---------------start------------->8---
\,(if (equal \2 nil) "+" (concat "+ " \2 " ::"))
--8<---------------cut here---------------end--------------->8---

How can I input it in a lisp function like:

--8<---------------cut here---------------start------------->8---
(defun jc-item-to-plus (Begin End)
  "Replace \\item by +"
  (interactive "r")
  (query-replace-regexp Begin End "some_standard_regexp" (if (equal \2 nil) "+" (concat "+ " \2 " ::")))
  )
--8<---------------cut here---------------end--------------->8---

Julien.





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Using lisp code in query-replace-regexp
       [not found]   ` <mailman.12880.1415191645.1147.info-gnus-english@gnu.org>
@ 2014-11-05 23:41     ` Emanuel Berg
  0 siblings, 0 replies; 4+ messages in thread
From: Emanuel Berg @ 2014-11-05 23:41 UTC (permalink / raw)
  To: info-gnus-english

Julien Cubizolles <j.cubizolles@free.fr> writes:

> 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.

How do you mean messed up? You are using
`save-excursion' outside of `save-restriction' just as
you should.

-- 
underground experts united

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Using lisp code in query-replace-regexp
  2014-11-04 19:57 ` Emanuel Berg
@ 2014-11-05 12:46   ` Julien Cubizolles
       [not found]   ` <mailman.12880.1415191645.1147.info-gnus-english@gnu.org>
  1 sibling, 0 replies; 4+ messages in thread
From: Julien Cubizolles @ 2014-11-05 12:46 UTC (permalink / raw)
  To: info-gnus-english

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.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Using lisp code in query-replace-regexp
       [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
       [not found]   ` <mailman.12880.1415191645.1147.info-gnus-english@gnu.org>
  0 siblings, 2 replies; 4+ messages in thread
From: Emanuel Berg @ 2014-11-04 19:57 UTC (permalink / raw)
  To: info-gnus-english

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).

Good luck! When you get it to work, post it here :)

-- 
underground experts united

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-11-05 23:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-04 14:36 Using lisp code in query-replace-regexp Julien Cubizolles
     [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
     [not found]   ` <mailman.12880.1415191645.1147.info-gnus-english@gnu.org>
2014-11-05 23:41     ` Emanuel Berg

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).