Function: reverse-region
reverse-region is an autoloaded, interactive and byte-compiled
function defined in sort.el.gz.
Signature
(reverse-region BEG END)
Documentation
Reverse the order of lines in a region.
When called from Lisp, takes two point or marker arguments, BEG and END. If BEG is not at the beginning of a line, the first line of those to be reversed is the line starting after BEG. If END is not at the end of a line, the last line to be reversed is the one that ends before END.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/sort.el.gz
;;;###autoload
(defun reverse-region (beg end)
"Reverse the order of lines in a region.
When called from Lisp, takes two point or marker arguments, BEG and END.
If BEG is not at the beginning of a line, the first line of those
to be reversed is the line starting after BEG.
If END is not at the end of a line, the last line to be reversed
is the one that ends before END."
(interactive "r")
(if (> beg end)
(let (mid) (setq mid end end beg beg mid)))
(save-excursion
;; Put beg at the start of a line and end and the end of one --
;; the largest possible region which fits this criteria.
(goto-char beg)
(or (bolp) (forward-line 1))
(setq beg (point))
(goto-char end)
;; The test for bolp is for those times when end is on an empty line;
;; it is probably not the case that the line should be included in the
;; reversal; it isn't difficult to add it afterward.
(or (and (eolp) (not (bolp))) (progn (forward-line -1) (end-of-line)))
(setq end (point-marker))
(when (<= end beg)
(user-error "There are no full lines in the region"))
;; The real work. This thing cranks through memory on large regions.
(let (ll (do t))
(while do
(goto-char beg)
(setq ll (cons (buffer-substring (point) (progn (end-of-line) (point)))
ll))
(setq do (/= (point) end))
(delete-region beg (if do (1+ (point)) (point))))
(while (cdr ll)
(insert (car ll) "\n")
(setq ll (cdr ll)))
(insert (car ll)))))