summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Zaretskii <eliz@gnu.org>2020-02-15 10:47:08 +0200
committerEli Zaretskii <eliz@gnu.org>2020-02-15 10:47:08 +0200
commitb392c9f365d081d98d9fbad5d54cadb7b9be15af (patch)
treef70afa2a851c2215b71accfa15f0a107f487ce8c
parent7448834f738b243d67f0bb2c9e8b531fc64d9064 (diff)
downloademacs-b392c9f365d081d98d9fbad5d54cadb7b9be15af.tar.gz
Fix 'reverse-region' when less than one line is in region
* lisp/sort.el (reverse-region): Signal a user-error if the region includes less than one full line, thus avoiding an inadvertent deletion of text following the current line. Fix the doc string. Fix comments to start with a capital letter. (Bug#39376)
-rw-r--r--lisp/sort.el17
1 files changed, 12 insertions, 5 deletions
diff --git a/lisp/sort.el b/lisp/sort.el
index 40347e6a8b2..e4ff2afb3d7 100644
--- a/lisp/sort.el
+++ b/lisp/sort.el
@@ -544,23 +544,30 @@ Use \\[untabify] to convert tabs to spaces before sorting."
;;;###autoload
(defun reverse-region (beg end)
"Reverse the order of lines in a region.
-From a program takes two point or marker arguments, BEG and END."
+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
+ (when (or (< (line-beginning-position) beg)
+ (< end (line-end-position)))
+ (user-error "There are no full lines in the region"))
+ ;; 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;
+ ;; 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))
- ;; the real work. this thing cranks through memory on large regions.
+ ;; The real work. This thing cranks through memory on large regions.
(let (ll (do t))
(while do
(goto-char beg)