]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix 'reverse-region' when less than one line is in region
authorEli Zaretskii <eliz@gnu.org>
Sat, 15 Feb 2020 08:47:08 +0000 (10:47 +0200)
committerEli Zaretskii <eliz@gnu.org>
Sat, 15 Feb 2020 08:47:08 +0000 (10:47 +0200)
* 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)

lisp/sort.el

index 40347e6a8b20c49f8dcb1a57c90e294d01249fac..e4ff2afb3d711589275d8955e85640c8a1b23a58 100644 (file)
@@ -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)