From aa0d3d5b1eadbc7adcf9d12461c5855a6847730a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Mon, 20 Jun 2022 11:16:26 +0200
Subject: [PATCH] Make duplicate-line work on regions (bug#46621)

With an active region, duplicate that region instead of the current
line.  Rectangular regions are duplicated on the right-hand side.
The region remains active afterwards, to facilitate further
duplication or other operations on the same text.

* lisp/rect.el (rectangle--duplicate-right): New.
* lisp/misc.el (duplicate-line): Work on regions.
* test/lisp/misc-tests.el (misc--duplicate-line): Add tests.
---
 etc/NEWS                |  2 ++
 lisp/misc.el            | 44 ++++++++++++++++++++++++++++++++++-------
 lisp/rect.el            | 21 ++++++++++++++++++++
 test/lisp/misc-tests.el | 32 +++++++++++++++++++++++++++++-
 4 files changed, 91 insertions(+), 8 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 3d679fdec6..1b9d690e92 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -372,6 +372,8 @@ between these modes while the user is inputting a command by hitting
 ---
 ** New command 'duplicate-line'.
 This command duplicates the current line the specified number of times.
+When the region is active, the command duplicates that region instead,
+on the right-hand side if the region is rectangular.
 
 ---
 ** Files with the '.eld' extension are now visited in 'lisp-data-mode'.
diff --git a/lisp/misc.el b/lisp/misc.el
index 8a01b51c6d..80ba1517c8 100644
--- a/lisp/misc.el
+++ b/lisp/misc.el
@@ -63,21 +63,51 @@ copy-from-above-command
 				 (+ n (point)))))))
     (insert string)))
 
+(declare-function rectangle--duplicate-right "rect" (n))
+
+;; `duplicate-line' preserves an active region and changes the buffer
+;; outside of it: disregard the region when immediately undoing the
+;; actions of this command.
+(put 'duplicate-line 'undo-inhibit-region t)
+
 ;;;###autoload
 (defun duplicate-line (&optional n)
   "Duplicate the current line N times.
+If the region is inactive, duplicate the current line.
+Otherwise, duplicate the region's contents.  The region remains active.
+If the region is rectangular, duplicate on its right-hand side.
 Interactively, N is the prefix numeric argument, and defaults to 1.
 Also see the `copy-from-above-command' command."
   (interactive "p")
   (unless n
     (setq n 1))
-  (let ((line (buffer-substring (line-beginning-position) (line-end-position))))
-    (save-excursion
-      (forward-line 1)
-      (unless (bolp)
-        (insert "\n"))
-      (dotimes (_ n)
-        (insert line "\n")))))
+  (cond
+   ;; Duplicate rectangle.
+   ((bound-and-true-p rectangle-mark-mode)
+    (rectangle--duplicate-right n)
+    (setq deactivate-mark nil))
+
+   ;; Duplicate (contiguous) region.
+   ((use-region-p)
+    (let* ((beg (region-beginning))
+           (end (region-end))
+           (text (buffer-substring beg end)))
+      (save-excursion
+        (goto-char end)
+        (dotimes (_ n)
+          (insert text))))
+    (setq deactivate-mark nil))
+
+   ;; Duplicate line.
+   (t
+    (let ((line (buffer-substring (line-beginning-position)
+                                  (line-end-position))))
+      (save-excursion
+        (forward-line 1)
+        (unless (bolp)
+          (insert "\n"))
+        (dotimes (_ n)
+          (insert line "\n")))))))
 
 ;; Variation of `zap-to-char'.
 
diff --git a/lisp/rect.el b/lisp/rect.el
index e717d2ac7e..c41a545e85 100644
--- a/lisp/rect.el
+++ b/lisp/rect.el
@@ -931,6 +931,27 @@ rectangle--unhighlight-for-redisplay
     (mapc #'delete-overlay (nthcdr 5 rol))
     (setcar (cdr rol) nil)))
 
+(defun rectangle--duplicate-right (n)
+  "Duplicate the rectangular region N times on the right-hand side."
+  (let ((cols (rectangle--pos-cols (point) (mark))))
+    (apply-on-rectangle
+     (lambda (startcol endcol)
+       (let ((lines (list nil)))
+         (extract-rectangle-line startcol endcol lines)
+         (move-to-column endcol t)
+         (dotimes (_ n)
+           (insert (cadr lines)))))
+     (region-beginning) (region-end))
+    ;; Recompute the rectangle state; no crutches should be needed now.
+    (let ((p (point))
+          (m (mark)))
+      (rectangle--reset-crutches)
+      (goto-char m)
+      (move-to-column (cdr cols) t)
+      (set-mark (point))
+      (goto-char p)
+      (move-to-column (car cols) t))))
+
 (provide 'rect)
 
 ;;; rect.el ends here
diff --git a/test/lisp/misc-tests.el b/test/lisp/misc-tests.el
index a56feaa049..83f82d0554 100644
--- a/test/lisp/misc-tests.el
+++ b/test/lisp/misc-tests.el
@@ -80,6 +80,8 @@ misc-test-backward-to-word
     (backward-to-word 3)
     (should (equal (point) 1))))
 
+(require 'rect)
+
 (ert-deftest misc--duplicate-line ()
   ;; Duplicate a line (twice).
   (with-temp-buffer
@@ -88,13 +90,41 @@ misc--duplicate-line
     (duplicate-line 2)
     (should (equal (buffer-string) "abc\ndefg\ndefg\ndefg\nh\n"))
     (should (equal (point) 7)))
+
   ;; Duplicate a non-terminated line.
   (with-temp-buffer
     (insert "abc")
     (goto-char 2)
     (duplicate-line)
     (should (equal (buffer-string) "abc\nabc\n"))
-    (should (equal (point) 2))))
+    (should (equal (point) 2)))
+
+  ;; Duplicate a region.
+  (with-temp-buffer
+    (insert "abc\ndef\n")
+    (set-mark 2)
+    (goto-char 7)
+    (transient-mark-mode)
+    (should (use-region-p))
+    (duplicate-line)
+    (should (equal (buffer-string) "abc\ndebc\ndef\n"))
+    (should (equal (point) 7))
+    (should (region-active-p))
+    (should (equal (mark) 2)))
+
+  ;; Duplicate a rectangular region.
+  (with-temp-buffer
+    (insert "x\n>a\n>bcde\n>fg\nyz\n")
+    (goto-char 4)
+    (rectangle-mark-mode)
+    (goto-char 15)
+    (rectangle-forward-char 1)
+    (duplicate-line)
+    (should (equal (buffer-string) "x\n>a  a  \n>bcdbcde\n>fg fg \nyz\n"))
+    (should (equal (point) 24))
+    (should (region-active-p))
+    (should rectangle-mark-mode)
+    (should (equal (mark) 4))))
 
 (provide 'misc-tests)
 ;;; misc-tests.el ends here
-- 
2.32.0 (Apple Git-132)

