From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#64185: proposal for new function: copy-line Date: Thu, 22 Jun 2023 21:17:51 +0300 Organization: LINKOV.NET Message-ID: <86wmzvwecw.fsf@mail.linkov.net> References: <837cry73z9.fsf@gnu.org> <838rcc3wzs.fsf@gnu.org> <86ttv0ynyk.fsf@mail.linkov.net> <83mt0r31n4.fsf@gnu.org> <86a5wrxv9z.fsf@mail.linkov.net> <83edm32xy9.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="586"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) Cc: 64185@debbugs.gnu.org, me@eshelyaron.com, zkanfer@gmail.com To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Jun 22 20:21:16 2023 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 1qCOvj-000AVp-JB for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 22 Jun 2023 20:21:15 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qCOvX-0004rY-TU; Thu, 22 Jun 2023 14:21:03 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qCOvW-0004r1-FU for bug-gnu-emacs@gnu.org; Thu, 22 Jun 2023 14:21:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qCOvW-0008M9-6g for bug-gnu-emacs@gnu.org; Thu, 22 Jun 2023 14:21:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qCOvW-0007ZX-1l for bug-gnu-emacs@gnu.org; Thu, 22 Jun 2023 14:21:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 22 Jun 2023 18:21:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 64185 X-GNU-PR-Package: emacs Original-Received: via spool by 64185-submit@debbugs.gnu.org id=B64185.168745800929011 (code B ref 64185); Thu, 22 Jun 2023 18:21:02 +0000 Original-Received: (at 64185) by debbugs.gnu.org; 22 Jun 2023 18:20:09 +0000 Original-Received: from localhost ([127.0.0.1]:36697 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qCOuf-0007Xq-9K for submit@debbugs.gnu.org; Thu, 22 Jun 2023 14:20:09 -0400 Original-Received: from relay5-d.mail.gandi.net ([217.70.183.197]:43667) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qCOud-0007X9-UR for 64185@debbugs.gnu.org; Thu, 22 Jun 2023 14:20:08 -0400 X-GND-Sasl: juri@linkov.net X-GND-Sasl: juri@linkov.net X-GND-Sasl: juri@linkov.net X-GND-Sasl: juri@linkov.net Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id 74A7F1C0005; Thu, 22 Jun 2023 18:20:00 +0000 (UTC) In-Reply-To: <83edm32xy9.fsf@gnu.org> (Eli Zaretskii's message of "Thu, 22 Jun 2023 20:45:02 +0300") 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-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:263908 Archived-At: --=-=-= Content-Type: text/plain >> +(defcustom duplicate-line-pos 0 >> + "Where to put point after copying the line. > > A better name for this would be duplicate-line-final-position or maybe > duplicate-line-point-position. Unless someone has an idea for a shorter name that is still descriptive, here is the final patch for emacs-29: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=duplicate-line-final-position.patch diff --git a/etc/NEWS b/etc/NEWS index ca0d602e9ad..aa3b758a815 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -696,7 +696,9 @@ between these modes while the user is inputting a command by hitting 'duplicate-line' duplicates the current line the specified number of times. 'duplicate-dwim' duplicates the region if it is active. If not, it works like 'duplicate-line'. An active rectangular region is -duplicated on its right-hand side. +duplicated on its right-hand side. The new user option +'duplicate-line-final-position' specifies where to move point +after duplicating the line. --- ** Files with the ".eld" extension are now visited in 'lisp-data-mode'. diff --git a/lisp/misc.el b/lisp/misc.el index ca013d5f72f..67048d4ec34 100644 --- a/lisp/misc.el +++ b/lisp/misc.el @@ -63,21 +63,41 @@ copy-from-above-command (+ n (point))))))) (insert string))) +(defcustom duplicate-line-final-position 0 + "Where to put point after duplicating the line with `duplicate-line'. +When 0, leave point on the original line. +When 1, move point to the first new line. +When -1, move point to the last new line." + :type '(choice (const :tag "Leave point on old line" 0) + (const :tag "Move point to first new line" 1) + (const :tag "Move point to last new line" -1) + (integer)) + :group 'editing + :version "29.1") + ;;;###autoload (defun duplicate-line (&optional n) "Duplicate the current line N times. Interactively, N is the prefix numeric argument, and defaults to 1. +The user option `duplicate-line-final-position' specifies where to +move point after duplicating the line. Also see the `copy-from-above-command' command." (interactive "p") (unless n (setq n 1)) - (let ((line (buffer-substring (line-beginning-position) (line-end-position)))) - (save-excursion - (forward-line 1) - (unless (bolp) - (insert "\n")) - (dotimes (_ n) - (insert line "\n"))))) + (let ((line (buffer-substring (line-beginning-position) (line-end-position))) + (pos (point)) + (col (current-column))) + (forward-line 1) + (unless (bolp) + (insert "\n")) + (dotimes (_ n) + (insert line "\n")) + (unless (< duplicate-line-final-position 0) + (goto-char pos)) + (unless (eq duplicate-line-final-position 0) + (forward-line duplicate-line-final-position) + (move-to-column col)))) (declare-function rectangle--duplicate-right "rect" (n)) diff --git a/test/lisp/misc-tests.el b/test/lisp/misc-tests.el index f1d22e099b9..ea27ea1653b 100644 --- a/test/lisp/misc-tests.el +++ b/test/lisp/misc-tests.el @@ -88,6 +88,20 @@ misc--duplicate-line (duplicate-line 2) (should (equal (buffer-string) "abc\ndefg\ndefg\ndefg\nh\n")) (should (equal (point) 7))) + ;; Duplicate a line (twice) and move point to the first duplicated line. + (with-temp-buffer + (insert "abc\ndefg\nh\n") + (goto-char 7) + (let ((duplicate-line-final-position 1)) (duplicate-line 2)) + (should (equal (buffer-string) "abc\ndefg\ndefg\ndefg\nh\n")) + (should (equal (point) 12))) + ;; Duplicate a line (twice) and move point to the last duplicated line. + (with-temp-buffer + (insert "abc\ndefg\nh\n") + (goto-char 7) + (let ((duplicate-line-final-position -1)) (duplicate-line 2)) + (should (equal (buffer-string) "abc\ndefg\ndefg\ndefg\nh\n")) + (should (equal (point) 17))) ;; Duplicate a non-terminated line. (with-temp-buffer (insert "abc") --=-=-=--