From d5421a1fdb5671414d3e2855f50cfc4b7c8d9e89 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 2 Dec 2016 08:34:50 +0100 Subject: [PATCH] Add function so/query-swap-strings --- lisp/text-manip.el | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lisp/text-manip.el b/lisp/text-manip.el index 7befdd8..a4c25fe 100644 --- a/lisp/text-manip.el +++ b/lisp/text-manip.el @@ -15,3 +15,28 @@ (list (if current-prefix-arg 'unfill) t))) (let ((fill-column (if unfill (point-max) fill-column))) (fill-paragraph nil region))) + +;; Copied from http://emacs.stackexchange.com/a/27170/507 +(defun so/query-swap-strings (from-string + to-string + &optional delimited start end) + "Swap occurrences of FROM-STRING and TO-STRING." + (interactive + (let ((common + (query-replace-read-args + (concat "Query swap" + (if current-prefix-arg + (if (eq current-prefix-arg '-) " backward" " word") + "") + (if (use-region-p) " in region" "")) + nil))) + (list (nth 0 common) (nth 1 common) (nth 2 common) + (if (use-region-p) (region-beginning)) + (if (use-region-p) (region-end))))) + (perform-replace + (concat "\\(" (regexp-quote from-string) "\\)\\|" (regexp-quote to-string)) + `(replace-eval-replacement replace-quote + (if (match-string 1) + ,to-string + ,from-string)) + t t delimited nil nil start end))