From 489591702b5bfcccbec6fe59d9a5a4ea5cc7b40c Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 19 Mar 2018 12:12:52 +0100 Subject: [PATCH] Add a function to easily swap strings in a file --- lisp/text-manip.el | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lisp/text-manip.el b/lisp/text-manip.el index af0c13e..66f1fc3 100644 --- a/lisp/text-manip.el +++ b/lisp/text-manip.el @@ -49,6 +49,28 @@ DELIMITED, START, and END are passed down verbatim to `perform-replace'." ,from-string)) t t delimited nil nil start end)) +;; From https://emacs.stackexchange.com/a/27170/507 +(defun query-swap-strings (from-string to-string &optional delimited start end) + "Swap occurrences of FROM-STRING and TO-STRING. + +DELIMITED, START, and END are passed to `replace-eval-replacement' verbatim." + (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)) + (provide 'text-manip) ;;; text-manip.el ends here