Emacs: Xah Replace Pairs, xah-replace-pairs.el
xah-replace-pairs.el
is a emacs lisp package for doing multi-pair find replace.
(the package was named “xfrp_find_replace_pairs.el”)
Here's a sample use:
(require 'xah-replace-pairs) (xah-replace-pairs-in-string "abcdef" [["a" "1"] ["b" "2"] ["c" "3"]]) ;; returns "123def"
Get it by Donation
Goto paypal.com, send $32 to Xah@XahLee.org (be sure to include your correct email address. I need that to send it to you.)
Write in message area: xah-replace-pairs.el
Why Multi-Pair Find Replace?
You have a given region in a buffer. You want to do more than one pair of find replace strings. For example:
HTML entities:
&
→&
<
→<
>
→>
The normal way to do find replace in a region is like this:
(defun replace-html-chars-region (begin end) "Replace “<” to “<” etc in region." (interactive "r") (save-restriction (narrow-to-region begin end) (goto-char (point-min)) (while (search-forward "&" nil t) (replace-match "&" nil t)) (goto-char (point-min)) (while (search-forward "<" nil t) (replace-match "<" nil t)) (goto-char (point-min)) (while (search-forward ">" nil t) (replace-match ">" nil t))))
now you can write it like this:
(require 'xah-replace-pairs) (defun replace-html-chars-region (begin end) (interactive "r") (xah-replace-pairs-region begin end '( ["&" "&"] ["<" "<"] [">" ">"] )))
More Examples
Here's sample use.
(defun replace-greek-to-unicode (@begin @end) "Replace alpha to α, beta to β etc in current line or selection." (interactive (if (use-region-p) (list (region-beginning) (region-end)) (list (line-beginning-position) (line-end-position)))) (let ((case-fold-search nil)) (xah-replace-pairs-region @begin @end '( ["alpha" "α"] ["beta" "β"] ["gamma" "γ"] ["pi" "π"] ) 'REPORT 'HILIGHT )))

Functions
The package has these functions:
xah-replace-pairs-region
xah-replace-pairs-in-string
xah-replace-regexp-pairs-region
xah-replace-regexp-pairs-in-string
xah-replace-pairs-region-recursive
xah-replace-pairs-in-string-recursive
For each function, there's a plain text version and a regex version. They are separate functions so it's simpler for user.
Each function also has a string and region version. The string version works on a given string, the region version works on a region in buffer.
Find Replace Feedback Loop Problem
xah-replace-pairs-in-string
and xah-replace-pairs-region
do not have
Find Replace Feedback Loop Problem
It guarantees that a replacement is done IF AND ONLY IF the original input string contains a substring in one of your find string.
Applications
For many examples of using multi-pair find/replace, See: Emacs Lisp Find Replace String-Pairs Commands.