From: "Mattias Engdegård" <mattiase@acm.org>
To: 56418@debbugs.gnu.org
Subject: bug#56418: Add duplicate-dwim [PATCH]
Date: Wed, 6 Jul 2022 10:29:29 +0200 [thread overview]
Message-ID: <4D430D59-39C0-40DE-862F-FAE7103D1705@acm.org> (raw)
[-- Attachment #1: Type: text/plain, Size: 451 bytes --]
This patch adds duplicate-swim, a command that works like duplicate-line but works on the region if active. It corresponds exactly to upcase-dwim, downcase-dwim and capitalize-dwim which have become quite popular.
Rectangular regions are treated specially by duplicating on the right-hand side. This behaviour turns out to be the most convenient one, and is also confluent with that of ordinary regions when the rectangle is only one line tall.
[-- Attachment #2: 0001-Add-duplicate-dwim.patch --]
[-- Type: application/octet-stream, Size: 4904 bytes --]
From abff5e9e5edcf43c06ca9df2c8c51248f6904b3b 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] Add duplicate-dwim
Like duplicate-line but duplicates the region instead if active.
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):
* lisp/misc.el (duplicate-dwim): New.
* test/lisp/misc-tests.el (misc--duplicate-dwim): New test.
---
lisp/misc.el | 37 +++++++++++++++++++++++++++++++++++++
lisp/rect.el | 21 +++++++++++++++++++++
test/lisp/misc-tests.el | 38 ++++++++++++++++++++++++++++++++++++++
3 files changed, 96 insertions(+)
diff --git a/lisp/misc.el b/lisp/misc.el
index 28c5d6e07f..a53571f463 100644
--- a/lisp/misc.el
+++ b/lisp/misc.el
@@ -79,6 +79,43 @@ duplicate-line
(dotimes (_ n)
(insert line "\n")))))
+(declare-function rectangle--duplicate-right "rect" (n))
+
+;; `duplicate-dwim' preserves an active region and changes the buffer
+;; outside of it: disregard the region when immediately undoing the
+;; actions of this command.
+(put 'duplicate-dwim 'undo-inhibit-region t)
+
+;;;###autoload
+(defun duplicate-dwim (&optional n)
+ "Duplicate the current line or region N times.
+If the region is inactive, duplicate the current line (like `duplicate-line').
+Otherwise, duplicate the region, which 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."
+ (interactive "p")
+ (unless n
+ (setq n 1))
+ (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 (duplicate-line n))))
+
;; Variation of `zap-to-char'.
;;;###autoload
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..f84827ab02 100644
--- a/test/lisp/misc-tests.el
+++ b/test/lisp/misc-tests.el
@@ -96,5 +96,43 @@ misc--duplicate-line
(should (equal (buffer-string) "abc\nabc\n"))
(should (equal (point) 2))))
+(require 'rect)
+
+(ert-deftest misc--duplicate-dwim ()
+ ;; Duplicate a line.
+ (with-temp-buffer
+ (insert "abc\ndefg\nh\n")
+ (goto-char 7)
+ (duplicate-dwim 2)
+ (should (equal (buffer-string) "abc\ndefg\ndefg\ndefg\nh\n"))
+ (should (equal (point) 7)))
+
+ ;; Duplicate a region.
+ (with-temp-buffer
+ (insert "abc\ndef\n")
+ (set-mark 2)
+ (goto-char 7)
+ (transient-mark-mode)
+ (should (use-region-p))
+ (duplicate-dwim)
+ (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-dwim)
+ (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)
next reply other threads:[~2022-07-06 8:29 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-06 8:29 Mattias Engdegård [this message]
2022-07-08 10:05 ` bug#56418: Add duplicate-dwim [PATCH] Mattias Engdegård
2022-07-26 12:26 ` Mattias Engdegård
2022-07-26 12:46 ` Stefan Kangas
2022-07-26 12:56 ` Robert Pluim
2022-07-27 9:01 ` Mattias Engdegård
2022-07-29 3:54 ` Richard Stallman
2022-07-29 6:11 ` Eli Zaretskii
2022-08-04 4:01 ` Richard Stallman
2022-08-04 18:43 ` Mattias Engdegård
2022-08-16 11:08 ` Robert Pluim
2022-07-31 8:36 ` Mattias Engdegård
2022-08-01 3:33 ` Richard Stallman
2022-08-01 11:29 ` Eli Zaretskii
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=4D430D59-39C0-40DE-862F-FAE7103D1705@acm.org \
--to=mattiase@acm.org \
--cc=56418@debbugs.gnu.org \
/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).