Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbie question on replace-eval-replacement , replace-quote

10 views
Skip to first unread message

bolega

unread,
Aug 26, 2011, 12:33:47 AM8/26/11
to
While searching in groups, I came across the following discussion.

http://groups.google.com/group/gnu.emacs.help/browse_thread/thread/c723bde021ea8183/a2afe24ce98ed739?lnk=gst&q=%22\%2C%22+swap#a2afe24ce98ed739

which presents the following code.

,----
| (defun query-swap-regexp (regexp-a regexp-b)
| "Swap A and B regexp matches in current buffer or region."
| (interactive "sRegexp A: \nsRegexp B: ")
| (let ((match-a (save-excursion
| (re-search-forward regexp-a nil t)
| (match-string 0)))
| (match-b (save-excursion
| (re-search-forward regexp-b nil t)
| (match-string 0))))
| (query-replace-regexp
| (concat "\\(\\(" regexp-a "\\)\\|" regexp-b "\\)")
| `(replace-eval-replacement
| replace-quote
| (if (match-string 2) ,match-b ,match-a))
| nil
| (if (and transient-mark-mode mark-active) (region-beginning))
| (if (and transient-mark-mode mark-active) (region-end)))))
`----

I am find it hard to understand due to lack of any example or
documentation explaining the operations of

replace-eval-replacement
replace-quote

For the latter, emacs says

No apropos matches for 'replace-quote'

and for the former,

replace-eval-replacement is a compiled Lisp function in `replace'.
(replace-eval-replacement EXPRESSION REPLACE-COUNT)
not documented

I cant run the above and play with it.

Thank for any help.
Bolega

Swami Tota Ram Shankar

unread,
Aug 26, 2011, 6:25:00 AM8/26/11
to
On Aug 25, 9:33 pm, bolega <gnuist...@gmail.com> wrote:
> While searching in groups, I came across the following discussion.
>
> http://groups.google.com/group/gnu.emacs.help/browse_thread/thread/c7...%2C%22+swap#a2afe24ce98ed739

Are you sure its not an xemacs or pico thing ?

Stephen Powell

unread,
Aug 26, 2011, 7:53:08 AM8/26/11
to
Swami Tota Ram Shankar <tota...@india.com> writes:

> No apropos matches for 'replace-quote'


replace-quote has been in lisp/replace.el since 17 June 2004:

,----[ ChangeLog.11 ]
| 2004-06-17 David Kastrup <d...@gnu.org>
|
| * replace.el (query-replace-read-args): Only warn about use of \n
| and \t when we are doing a regexp replacement and the actual
| escaped character is n or t.
| (query-replace-regexp): Add \, and \# interpretation to
| interactive call and document it.
| (query-replace-regexp-eval, replace-match-string-symbols): Add \#
| as shortkey for replace-count.
| (replace-quote): New function for doubling backslashes.
`----

steve

unread,
Jul 9, 2021, 9:11:02 PM7/9/21
to
bolega <gnui...@gmail.com> writes:

> which presents the following code.
>
> ,----
> | (defun query-swap-regexp (regexp-a regexp-b)
> | "Swap A and B regexp matches in current buffer or region."
> | (interactive "sRegexp A: \nsRegexp B: ")
> | (let ((match-a (save-excursion
> | (re-search-forward regexp-a nil t)
> | (match-string 0)))
> | (match-b (save-excursion
> | (re-search-forward regexp-b nil t)
> | (match-string 0))))
> | (query-replace-regexp
> | (concat "\\(\\(" regexp-a "\\)\\|" regexp-b "\\)")
> | `(replace-eval-replacement
> | replace-quote
> | (if (match-string 2) ,match-b ,match-a))
> | nil
> | (if (and transient-mark-mode mark-active) (region-beginning))
> | (if (and transient-mark-mode mark-active) (region-end)))))
> `----
>
> I am find it hard to understand due to lack of any example or
> documentation explaining the operations of

Simple answer is: M-x replace-regexp RET.

> replace-eval-replacement

I am going to assume you know how to program a tad. The author is using a
macro to make the function ... The author is using a macro like in C.

#define CONCAT(x,y) x ## y
#define ERR_START fprintf(stderr
#define ERR_STR "Error number %d"
#define ERR_END(x) x

That code probably won't work well, especially with out the commas. it should give you an
idea.

In emacs lisp there is no lexicall scope. That means all variables are
global within their extent.^1

My guess is that the function returns a macro with an enclosed symbol to
be used in a later expansion. This is usually considered evil in lisp.
It is very common in TeX though - sometimes refereed to the ``stomach'' I
believe. In common lisp and in emacs lisp you can use `defvar' to define
a variable. Please do that.

Basically, it's like using the ## in CPP (c preprocessor).

> replace-quote

You don't want my answer on this one. It's called quasi-quote. It's in a
dialect of lisp called scheme.

In lisp there are ``reader macros''

> I cant run the above and play with it.

no fun without some code eya.

usually in emacs lisp you will use re-search-forward. That probably
means ``regular express search forward''. there is also the reverse.
I usully use it in a loop like the following. Just place your cursor at
the end of the sexp (the last paren) and type C-x C-e.

(while (re-search-forward "\\(CALL_[0-9]\\)" nil t nil)
(replace-match "_SYS_\\1"))

CALL_4
CALL_3
CALL5
CALL_8

The use of the \\( and \\) means store this match into match memory. In
replace-match the \\1 refers to the first match. All this is documented
in the emacs lisp manual. C-h f will pull up help for functions.

> Thank for any help.
> Bolega

I hope this helps, more questions please ask.


-------------------------
^1 I'be been reading LISP ins small pieces again...
0 new messages