The following will swap two regions. It works by setting the mark 3 times, and moving the point to a fourth location. This makes for four buffer positions – the first 2 marks for the first region, and point and mark make the second region.
The benefits of an interactive command are highly suspect, but the routine may be useful in EmacsLisp programming.
(defun swap-regions (beg1 end1 beg2 end2) "Swap region between BEG1 and END1 with region BEG2 and END2. For the first region, mark the first region and set mark at point. The second region only needs to be marked normally. Again, set the mark at the beginning and end of the first region, then mark the second region with mark and point. The order of the two regions in the buffer doesn't matter. Either one can precede the other. However, the regions can not be swapped if they overlap. All arguments can either be a number for a position in the buffer or a marker." (interactive (if (< (length mark-ring) 2) (error "Not enough in mark-ring to swap a region") (let ((region (list (region-beginning) (region-end))) (marks (sort (list (marker-position (car mark-ring)) (marker-position (cadr mark-ring))) '<))) (if (< (car region) (car marks)) (append region marks) (append marks region))))) (if (or (and (< beg2 beg1) (< beg1 end2)) (and (< beg1 beg2) (< beg2 end1))) (error "Unable to swap overlapping regions") (save-excursion (insert (prog1 (delete-and-extract-region beg2 end2) (goto-char beg2) (insert (delete-and-extract-region beg1 end1)) (goto-char beg1))))))