unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Bastien <bastien@xxx.fr>
Subject: query-replace-regexp-swap ?
Date: Thu, 24 Nov 2005 02:10:40 +0000	[thread overview]
Message-ID: <873blmhk2n.fsf@bzg.ath.cx> (raw)

I often come across this problem: how to swap two strings in a buffer?
For instance, say that you want to exchange #000 and #FFF in an HTML
page: all #000 should become #FFF and all #FFF should become #000.

How to do this with one *single* function ?

For now i use this:

==%<==================================================================
(defun query-replace-regexp-swap (regexp-a regexp-b beg end)
  "Swap A and B strings in the selected region."
  (interactive
   (let ((regexp-a (read-string "Regexp A: "))
	 (regexp-b (read-string "Regexp B: ")))
     (list regexp-a regexp-b (region-beginning) (region-end))))
  (save-match-data
    (save-excursion
      (goto-char beg)
      (let* ((regexp (concat "\\(" regexp-a "\\)\\|"
			     "\\(" regexp-b "\\)"))
	     (match-a (save-excursion
			(re-search-forward regexp-a end t)
			(match-string 0)))
	     (match-b (save-excursion
			(re-search-forward regexp-b end t)
			(match-string 0))))
	(while (re-search-forward regexp nil t)
	  (cond ((match-string 1) 
		 (when (y-or-n-p (format "Replace with %s? " match-b))
				 (replace-match match-b t t)))
		((match-string 2) 
		 (when (y-or-n-p (format "Replace with %s? " match-a))
				 (replace-match match-a t t)))))))))
======================================================================

... which is quite UGLY.

Any idea on how to use `perform-replace' to achieve this in a more
efficient way?

-- 
Bastien

             reply	other threads:[~2005-11-24  2:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-24  2:10 Bastien [this message]
2005-11-26  2:03 ` query-replace-regexp-swap ? rgb
2005-11-26  3:54   ` Bastien
2005-11-26  6:22 ` David Kastrup
2005-11-28 12:59   ` Bastien
2005-11-28 13:35     ` David Kastrup
2005-11-28 13:54       ` Bastien

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=873blmhk2n.fsf@bzg.ath.cx \
    --to=bastien@xxx.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).