diff options
author | Mattias EngdegÄrd <mattiase@acm.org> | 2022-06-20 11:16:26 +0200 |
---|---|---|
committer | Mattias EngdegÄrd <mattiase@acm.org> | 2022-07-26 14:23:30 +0200 |
commit | 592ae7ffe20aa9b5508fa0ac51dcf0ba33881b3c (patch) | |
tree | bb352983d724597ba5f418060ccc53831f261380 /lisp | |
parent | fc1b7b720b5771a330f36e9a52688d73b790e478 (diff) | |
download | emacs-592ae7ffe20aa9b5508fa0ac51dcf0ba33881b3c.tar.gz emacs-592ae7ffe20aa9b5508fa0ac51dcf0ba33881b3c.tar.bz2 emacs-592ae7ffe20aa9b5508fa0ac51dcf0ba33881b3c.zip |
Add duplicate-dwim (bug#56418)
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.
* etc/NEWS: Announce.
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/misc.el | 37 | ||||
-rw-r--r-- | lisp/rect.el | 21 |
2 files changed, 58 insertions, 0 deletions
diff --git a/lisp/misc.el b/lisp/misc.el index 28c5d6e07f5..a53571f4639 100644 --- a/lisp/misc.el +++ b/lisp/misc.el @@ -79,6 +79,43 @@ Also see the `copy-from-above-command' command." (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 47df95b04e4..eebbf999d40 100644 --- a/lisp/rect.el +++ b/lisp/rect.el @@ -930,6 +930,27 @@ Ignores `line-move-visual'." (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 |