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: Sat, 18 Jun 2022 20:02:58 +0200 Message-ID: <07496A25-CD5D-46E6-8F1C-7BD29143F3C8@acm.org> References: <87tuq99ova.fsf@mail.linkov.net> Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_137EE32B-E9C8-4213-B3D8-97036C227589" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4501"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 46621@debbugs.gnu.org, Juri Linkov To: Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Jun 18 20:04:11 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 1o2cnq-0000vG-VN for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 18 Jun 2022 20:04:11 +0200 Original-Received: from localhost ([::1]:45100 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o2cnp-0007FO-R7 for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 18 Jun 2022 14:04:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47288) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o2cni-0007EY-RS for bug-gnu-emacs@gnu.org; Sat, 18 Jun 2022 14:04:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:56107) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o2cni-0005Kv-Iq for bug-gnu-emacs@gnu.org; Sat, 18 Jun 2022 14:04:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1o2cni-0007dR-CI for bug-gnu-emacs@gnu.org; Sat, 18 Jun 2022 14:04:02 -0400 X-Loop: help-debbugs@gnu.org In-Reply-To: <87tuq99ova.fsf@mail.linkov.net> Resent-From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 18 Jun 2022 18:04: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.165557539129286 (code B ref 46621); Sat, 18 Jun 2022 18:04:02 +0000 Original-Received: (at 46621) by debbugs.gnu.org; 18 Jun 2022 18:03:11 +0000 Original-Received: from localhost ([127.0.0.1]:50004 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1o2cmt-0007cI-EN for submit@debbugs.gnu.org; Sat, 18 Jun 2022 14:03:11 -0400 Original-Received: from mail1437c50.megamailservers.eu ([91.136.14.37]:44504 helo=mail263c50.megamailservers.eu) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1o2cmq-0007bj-4V for 46621@debbugs.gnu.org; Sat, 18 Jun 2022 14:03:09 -0400 X-Authenticated-User: mattiase@bredband.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=megamailservers.eu; s=maildub; t=1655575381; bh=wJxd0KTB46uFMuT+/G7+7rQqEaLtDzwXSyqHnNMi43Y=; h=From:Subject:Date:Cc:To:From; b=eIh1iyadHRQFi0XVmrr0XNLVfooE3aEXV1W2cO7Z77O67v63q4xfB5acHSWwDObS2 ip1CwVQNJieyin/akdzkeuwdfsY4VMNFjSfyclReE49+9A8E2PH7w1gb0+VmleH/cb a5Ts/xP6jak4jEiBcKYGxO2VIKE8AfCAWM+CrkfY= 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 mail263c50.megamailservers.eu (8.14.9/8.13.1) with ESMTP id 25II2wSR040505; Sat, 18 Jun 2022 18:03:00 +0000 X-Mailer: Apple Mail (2.3654.120.0.1.13) X-CTCH-RefID: str=0001.0A782F26.62AE1355.0006, 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:234760 Archived-At: --Apple-Mail=_137EE32B-E9C8-4213-B3D8-97036C227589 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > I've now added this command to Emacs 29 as `duplicate-line'.=20 Thank you! There was quite some discussion about generalising the = concept last time it came up for discussion, and making it work on the = region when active was found to be very useful. It comes quite naturally = in use, much like upcase-dwim etc. The attached patch does that. For extra convenience, when the active = region is rectangular it is duplicated to the right, since that is what = is usually most convenient. There's a few subtleties here: the code takes care to preserve the = region because the user may want to press the key again for another = copy. (This means that we need to use undo-inhibit-region for undo to = work properly.) Do you feel audacious enough for a standard key binding? --Apple-Mail=_137EE32B-E9C8-4213-B3D8-97036C227589 Content-Disposition: attachment; filename=duplicate-line-or-region.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="duplicate-line-or-region.diff" Content-Transfer-Encoding: 7bit diff --git a/lisp/misc.el b/lisp/misc.el index 3fb30e5372..d5db0a35ae 100644 --- a/lisp/misc.el +++ b/lisp/misc.el @@ -63,19 +63,51 @@ copy-from-above-command (+ n (point))))))) (insert string))) +(declare-function extract-rectangle-line "rect" (startcol endcol lines)) +(declare-function apply-on-rectangle "rect" (function start end &rest args)) + +(put 'duplicate-line-or-region 'undo-inhibit-region t) + ;;;###autoload -(defun duplicate-line (&optional n) - "Duplicate the current line N times. +(defun duplicate-line-or-region (&optional n) + "Duplicate the current line or region N times. +If the region is inactive, the current line is duplicated. +Otherwise, duplicate the region's contents. The region remains +active afterwards. +If the region is rectangular, duplication is made 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 + ((bound-and-true-p rectangle-mark-mode) + (require 'rect) + (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)) + (setq deactivate-mark nil)) + ((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)) + (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'. --Apple-Mail=_137EE32B-E9C8-4213-B3D8-97036C227589--