help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: query-replace-regexp-swap ?


From: Bastien
Subject: Re: query-replace-regexp-swap ?
Date: Mon, 28 Nov 2005 13:54:33 +0000
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/22.0.50 (gnu/linux)

David Kastrup <dak@gnu.org> writes:

>> (I guess the "#" above was just a typo.)
>
> Uh, no.  You asked for replacing #000 and #fff, so the # needs to
> get matched.  The error in the above is not that the # is in the
> match string, but that it is missing in the replacement.

Right, no problem.

Now i'm using this:

,----
| (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)))))
`----

It prompts for two regexps, find the first occurrence of each one and
query-replace all occurrences.  This is what i need in most cases.

The limitation concerns match-a and match-b, which are defined once
and for all.  But it seems to me that other behaviors would be quite
non-intuitive and tricky.

-- 
Bastien


reply via email to

[Prev in Thread] Current Thread [Next in Thread]