unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: 46621@debbugs.gnu.org, "Simen Heggestøyl" <simenheg@runbox.com>,
	"Juri Linkov" <juri@linkov.net>
Subject: bug#46621: Copy line
Date: Mon, 20 Jun 2022 11:26:18 +0200	[thread overview]
Message-ID: <181B4CC3-B934-45AE-A7E8-9AF2B70C302E@acm.org> (raw)
In-Reply-To: <871qvkvft6.fsf@gnus.org>

[-- Attachment #1: Type: text/plain, Size: 717 bytes --]

19 juni 2022 kl. 17.22 skrev Lars Ingebrigtsen <larsi@gnus.org>:

> No, it's consistent -- it always creates a new line.

Either behaviour can be seen as consistent. I suppose it's a matter of what behaviour we find more convenient.
It's expected to be a rare case and I have no strong opinion about it.

> It's edgy compared to what it's supposed to be doing -- duplicating
> lines.

Well, now it does more. Do you mean that you prefer it wouldn't, or that we change the name?
I have no strong opinion about the name but it would be a shame to make the command less useful.

Here is an updated patch that fixes a bug in the original code (would crash when called without an argument) and adds tests.


[-- Attachment #2: 0001-Make-duplicate-line-work-on-regions-bug-46621.patch --]
[-- Type: application/octet-stream, Size: 5415 bytes --]

From 8f210a9ee0f61c22a2b1996b1b61c7242e2372dc 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.

* lisp/rect.el (rectangle--duplicate-right): New.
* lisp/misc.el (duplicate-line): Work on regions.  Allow calls without
an argument.
* test/lisp/misc-tests.el (misc--duplicate-line): New test.
---
 lisp/misc.el            | 48 +++++++++++++++++++++++++++++++++++------
 lisp/rect.el            | 21 ++++++++++++++++++
 test/lisp/misc-tests.el | 35 ++++++++++++++++++++++++++++++
 3 files changed, 97 insertions(+), 7 deletions(-)

diff --git a/lisp/misc.el b/lisp/misc.el
index 3fb30e5372..906d6c2012 100644
--- a/lisp/misc.el
+++ b/lisp/misc.el
@@ -63,19 +63,53 @@ 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 afterwards.
+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")
-  (let ((line (buffer-substring (line-beginning-position) (line-end-position))))
-    (save-excursion
-      (forward-line 1)
-      (unless (bolp)
-        (insert "\n"))
-      (dotimes (_ n)
-        (insert line "\n")))))
+  (unless n
+    (setq n 1))
+  (cond
+   ;; Duplicate rectangle.
+   ((bound-and-true-p rectangle-mark-mode)
+    (require 'rect)
+    (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 236223ef49..2036488f13 100644
--- a/test/lisp/misc-tests.el
+++ b/test/lisp/misc-tests.el
@@ -80,5 +80,40 @@ misc-test-backward-to-word
     (backward-to-word 3)
     (should (equal (point) 1))))
 
+(ert-deftest misc--duplicate-line ()
+  ;; Duplicate a line (twice).
+  (with-temp-buffer
+    (insert "abc\ndefg\nh\n")
+    (goto-char 7)
+    (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)))
+  ;; Duplicate a region.
+  (with-temp-buffer
+    (insert "abc\ndef\n")
+    (set-mark 2)
+    (goto-char 7)
+    (should (use-region-p))
+    (duplicate-line)
+    (should (equal (buffer-string) "abc\ndebc\ndef\n"))
+    (should (equal (point) 7)))
+  ;; 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))))
+
 (provide 'misc-tests)
 ;;; misc-tests.el ends here
-- 
2.32.0 (Apple Git-132)


  reply	other threads:[~2022-06-20  9:26 UTC|newest]

Thread overview: 117+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-18 19:07 bug#46621: Copy line Juri Linkov
2021-02-18 19:30 ` bug#46621: [External] : " Drew Adams
2021-02-20  6:58   ` Richard Stallman
2021-02-19 13:09 ` Lars Ingebrigtsen
2021-02-19 20:27   ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-02-20  6:54     ` Eli Zaretskii
2021-02-20 13:05       ` Lars Ingebrigtsen
2021-02-20 13:12         ` Eli Zaretskii
2021-02-20 13:18           ` Lars Ingebrigtsen
2021-02-20 14:15             ` Eli Zaretskii
2021-02-20 14:35               ` Dmitry Gutov
2021-02-20 13:03     ` Lars Ingebrigtsen
2021-02-20 18:28       ` Juri Linkov
2021-02-21  6:16         ` Richard Stallman
2021-02-21  6:21         ` Richard Stallman
2021-02-21  8:54           ` Juri Linkov
2021-02-21 10:41             ` Eli Zaretskii
2021-02-21 13:12               ` Lars Ingebrigtsen
2021-02-21 13:19                 ` Eli Zaretskii
2021-02-21 15:51                   ` Lars Ingebrigtsen
2021-02-21 17:06                     ` Eli Zaretskii
2021-02-21 17:39                       ` Lars Ingebrigtsen
2021-02-21 18:00                     ` bug#46621: [External] : " Drew Adams
2021-02-21 17:45                 ` Drew Adams
2021-02-22  6:22                 ` Richard Stallman
2021-02-21 17:41               ` bug#46621: [External] : " Drew Adams
2021-02-21 20:37               ` Juri Linkov
2021-02-21 22:06                 ` bug#46621: [External] : " Drew Adams
2021-02-22 15:45                 ` Eli Zaretskii
2021-02-21 23:04             ` Howard Melman
2021-02-22  6:23             ` Richard Stallman
2021-02-22  9:07               ` Juri Linkov
2021-02-22 15:43                 ` Eli Zaretskii
2021-02-22 16:28                   ` Helmut Eller
2021-02-22 16:58                     ` Andreas Schwab
2021-02-22 18:32                       ` Helmut Eller
2021-02-22 19:41                         ` Howard Melman
2021-02-22 19:46                           ` bug#46621: [External] : " Drew Adams
2021-02-23 19:29                             ` Dmitry Gutov
2022-06-28 14:28                               ` Drew Adams
2021-02-22 17:08                     ` Eli Zaretskii
2021-02-22 18:42                       ` Helmut Eller
2021-02-22 17:04                   ` Gregory Heytings
2021-02-22 17:16                     ` Eli Zaretskii
2021-02-22 17:54                       ` Gregory Heytings
2021-02-22 20:51                   ` Stephen Berman
2021-02-21 13:13         ` Lars Ingebrigtsen
2022-06-17 17:34 ` Lars Ingebrigtsen
2022-06-18  9:32   ` Simen Heggestøyl
2022-06-20 18:28   ` Richard Stallman
2022-06-18 18:02 ` Mattias Engdegård
2022-06-18 18:09   ` Eli Zaretskii
2022-06-19 15:02     ` Mattias Engdegård
2022-07-03 17:21     ` Mattias Engdegård
2022-07-04  3:24       ` Pankaj Jangid
2022-07-05 16:02       ` Mattias Engdegård
2022-07-05 16:52         ` Lars Ingebrigtsen
2022-07-05 20:19           ` Mattias Engdegård
2022-06-19 11:43   ` Lars Ingebrigtsen
2022-06-19 15:20     ` Mattias Engdegård
2022-06-19 15:22       ` Lars Ingebrigtsen
2022-06-20  9:26         ` Mattias Engdegård [this message]
2022-06-21 10:35           ` Lars Ingebrigtsen
2022-06-21 11:13             ` Mattias Engdegård
2022-06-22  4:11               ` Lars Ingebrigtsen
2022-06-21 17:41             ` Juri Linkov
2022-06-22  4:07               ` Lars Ingebrigtsen
2022-06-22  7:28                 ` Juri Linkov
2022-06-22  7:54                   ` Lars Ingebrigtsen
2022-06-22 17:21                     ` Pankaj Jangid
2022-06-22 18:24                       ` Juri Linkov
2022-06-22 18:45                         ` Lars Ingebrigtsen
2022-06-23  7:49                       ` Lars Ingebrigtsen
2022-06-23  8:08                         ` Pankaj Jangid
2022-06-23  8:17                           ` Andreas Schwab
2022-06-23  9:05                             ` Lars Ingebrigtsen
2022-07-06 17:34                               ` Juri Linkov
2022-07-07  7:58                                 ` Lars Ingebrigtsen
2022-07-07 16:45                                   ` Juri Linkov
2022-07-07 18:03                                     ` Lars Ingebrigtsen
2022-07-07 18:20                                       ` Juri Linkov
2022-07-07 18:24                                         ` Lars Ingebrigtsen
2022-07-08 17:10                                           ` Juri Linkov
2022-07-10 12:57                                             ` Lars Ingebrigtsen
2022-07-14 17:16                                           ` Juri Linkov
2022-07-14 17:47                                             ` Andreas Schwab
2022-07-14 19:30                                               ` Juri Linkov
2022-06-23  9:22                             ` Robert Pluim
2022-06-23 11:16                               ` Pankaj Jangid
2022-06-23 11:34                                 ` Lars Ingebrigtsen
2022-06-23  9:04                           ` Lars Ingebrigtsen
2022-06-23 11:12                             ` Pankaj Jangid
2022-06-23 15:10                     ` Mattias Engdegård
2022-06-23 15:20                       ` Lars Ingebrigtsen
2022-06-25 16:35                         ` Mattias Engdegård
2022-06-23 17:35                       ` Juri Linkov
2022-06-23 17:49                         ` Drew Adams
2022-06-25 16:51                         ` Mattias Engdegård
2022-06-25 17:48                           ` Drew Adams
2022-06-27 19:40                           ` Juri Linkov
2022-06-28  8:41                             ` Mattias Engdegård
2022-06-28 12:10                               ` Helmut Eller
2022-06-22 14:10                   ` Drew Adams
2022-06-22 17:27                     ` Pankaj Jangid
2022-06-22 20:44   ` Sean Whitton
2022-06-22 20:50     ` Drew Adams
2022-06-23 15:47       ` Helmut Eller
2022-06-23 16:07         ` Eli Zaretskii
2022-06-23 17:46         ` Drew Adams
2022-06-23  5:47     ` Eli Zaretskii
2022-06-23 17:00       ` Sean Whitton
2022-06-23 17:37         ` Sean Whitton
2022-06-23 18:31         ` Eli Zaretskii
2022-06-30 16:31           ` Sean Whitton
2022-07-01  9:27             ` Lars Ingebrigtsen
2022-07-01 16:34               ` Sean Whitton
     [not found] <87a6aal3l5.fsf@simenheg@gmail.com>
2022-06-18 12:56 ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=181B4CC3-B934-45AE-A7E8-9AF2B70C302E@acm.org \
    --to=mattiase@acm.org \
    --cc=46621@debbugs.gnu.org \
    --cc=juri@linkov.net \
    --cc=larsi@gnus.org \
    --cc=simenheg@runbox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).