all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Using lisp code in query-replace-regexp
@ 2014-11-04 14:49 Julien Cubizolles
  2014-11-04 18:38 ` Andreas Röhler
  0 siblings, 1 reply; 3+ messages in thread
From: Julien Cubizolles @ 2014-11-04 14:49 UTC (permalink / raw)
  To: help-gnu-emacs

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] 3+ messages in thread

* Re: Using lisp code in query-replace-regexp
  2014-11-04 14:49 Using lisp code in query-replace-regexp Julien Cubizolles
@ 2014-11-04 18:38 ` Andreas Röhler
  2014-11-04 20:58   ` Julien Cubizolles
  0 siblings, 1 reply; 3+ messages in thread
From: Andreas Röhler @ 2014-11-04 18:38 UTC (permalink / raw)
  To: help-gnu-emacs

On 04.11.2014 15:49, Julien Cubizolles wrote:
> 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.
>
>
>

When writing a program, for example start with

(narrow-to-region beg end)
(goto-char (point-min))
(while (re-search-forward RE end)
(replace-match ...

etc.



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

* Re: Using lisp code in query-replace-regexp
  2014-11-04 18:38 ` Andreas Röhler
@ 2014-11-04 20:58   ` Julien Cubizolles
  0 siblings, 0 replies; 3+ messages in thread
From: Julien Cubizolles @ 2014-11-04 20:58 UTC (permalink / raw)
  To: help-gnu-emacs

Andreas Röhler <andreas.roehler@easy-emacs.de> writes:

> When writing a program, for example start with
>
> (narrow-to-region beg end)
> (goto-char (point-min))
> (while (re-search-forward RE end)
> (replace-match ...

Thanks for this example, it allows me to add the test I needed in the
mix. However, I'd rather be prompted for each replace. I tried with
replace-match-maybe-edit:

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

but it messes up the excursion. If I use replace-match, the excursion is
preserved however.

Julien.




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

end of thread, other threads:[~2014-11-04 20:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-04 14:49 Using lisp code in query-replace-regexp Julien Cubizolles
2014-11-04 18:38 ` Andreas Röhler
2014-11-04 20:58   ` Julien Cubizolles

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.