From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Newsgroups: gmane.emacs.bugs Subject: bug#46621: Copy line Date: Sun, 19 Jun 2022 17:02:51 +0200 Message-ID: <8C73661C-566D-42DB-A6E4-F6036C5E4175@acm.org> References: <87tuq99ova.fsf@mail.linkov.net> <07496A25-CD5D-46E6-8F1C-7BD29143F3C8@acm.org> <8335g1onbr.fsf@gnu.org> Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_97637E62-26D4-4013-A286-B8C1E18D0D7A" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29315"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 46621@debbugs.gnu.org, larsi@gnus.org, juri@linkov.net To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Jun 19 17:03:10 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o2wSE-0007VW-5H for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 19 Jun 2022 17:03:10 +0200 Original-Received: from localhost ([::1]:50882 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o2wSD-0002iF-3Y for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 19 Jun 2022 11:03:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:43824) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o2wS6-0002i5-Pv for bug-gnu-emacs@gnu.org; Sun, 19 Jun 2022 11:03:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:58953) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o2wS6-00077K-Hm for bug-gnu-emacs@gnu.org; Sun, 19 Jun 2022 11:03:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1o2wS6-0000V5-Af for bug-gnu-emacs@gnu.org; Sun, 19 Jun 2022 11:03:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 19 Jun 2022 15:03:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 46621 X-GNU-PR-Package: emacs Original-Received: via spool by 46621-submit@debbugs.gnu.org id=B46621.16556509781913 (code B ref 46621); Sun, 19 Jun 2022 15:03:02 +0000 Original-Received: (at 46621) by debbugs.gnu.org; 19 Jun 2022 15:02:58 +0000 Original-Received: from localhost ([127.0.0.1]:52850 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1o2wS2-0000Un-Bb for submit@debbugs.gnu.org; Sun, 19 Jun 2022 11:02:58 -0400 Original-Received: from mail18c50.megamailservers.eu ([91.136.10.28]:36740) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1o2wS0-0000Ud-8t for 46621@debbugs.gnu.org; Sun, 19 Jun 2022 11:02:57 -0400 X-Authenticated-User: mattiase@bredband.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=megamailservers.eu; s=maildub; t=1655650974; bh=ZqTfal7DUV4PTGXp5ZKHby1/4wsDhwiyRjkox/RoWIE=; h=From:Subject:Date:In-Reply-To:Cc:To:References:From; b=A65hCLKkY1HHMOoAqN+Ouz6ZFbmFsO4tNujMaEZeiWuaFeagWFybvY9iThKPjfhw4 kVWxc2PJEbPvOCsSmg29rt2g/nEKMDDUvUzoJyWcNT0ISIf97kneofSP4BOBwXXrWB 16UqDx5zu1di+Qv+OGRSBZFrludwnCvXFweLPJlM= Feedback-ID: mattiase@acm.or Original-Received: from smtpclient.apple (c188-150-171-71.bredband.tele2.se [188.150.171.71]) (authenticated bits=0) by mail18c50.megamailservers.eu (8.14.9/8.13.1) with ESMTP id 25JF2qpA051334; Sun, 19 Jun 2022 15:02:53 +0000 In-Reply-To: <8335g1onbr.fsf@gnu.org> X-Mailer: Apple Mail (2.3654.120.0.1.13) X-CTCH-RefID: str=0001.0A782F20.62AF3A9E.0053, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0 X-CTCH-VOD: Unknown X-CTCH-Spam: Unknown X-CTCH-Score: 0.000 X-CTCH-Flags: 0 X-CTCH-ScoreCust: 0.000 X-Origin-Country: SE X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:234799 Archived-At: --Apple-Mail=_97637E62-26D4-4013-A286-B8C1E18D0D7A Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii 18 juni 2022 kl. 20.09 skrev Eli Zaretskii : > There's no need to include "or region" in the command's name and doc > string's first line. Changed. > Passive tense alert! > Another one! Both changed. Thanks for the comments! The attached patch now handles rectangle duplication better. = (Rectangular regions are nice to use but can be surprisingly unpleasant = to program with.) That code was moved to rect.el where it probably belongs. There will be tests as well. --Apple-Mail=_97637E62-26D4-4013-A286-B8C1E18D0D7A Content-Disposition: attachment; filename=duplicate-line.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="duplicate-line.diff" Content-Transfer-Encoding: 7bit diff --git a/lisp/misc.el b/lisp/misc.el index 3fb30e5372..8a1dd76847 100644 --- a/lisp/misc.el +++ b/lisp/misc.el @@ -63,19 +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 undoing the actions of +;; this command or the undo wouldn't work properly. +(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"))))) + (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..3e29868b24 100644 --- a/lisp/rect.el +++ b/lisp/rect.el @@ -931,6 +931,30 @@ 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 since the crutches might be outdated. + (let ((p (point)) + (m (mark))) + (rectangle--reset-crutches) + (goto-char m) + (move-to-column (cdr cols) t) + ;(rectangle--col-pos (cdr cols) 'mark) + (set-mark (point)) + (goto-char p) + (move-to-column (car cols) t) + ;(rectangle--col-pos (car cols) 'point) + ))) + (provide 'rect) ;;; rect.el ends here --Apple-Mail=_97637E62-26D4-4013-A286-B8C1E18D0D7A Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii --Apple-Mail=_97637E62-26D4-4013-A286-B8C1E18D0D7A--