all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 64185@debbugs.gnu.org, me@eshelyaron.com, zkanfer@gmail.com
Subject: bug#64185: proposal for new function: copy-line
Date: Thu, 22 Jun 2023 21:17:51 +0300	[thread overview]
Message-ID: <86wmzvwecw.fsf@mail.linkov.net> (raw)
In-Reply-To: <83edm32xy9.fsf@gnu.org> (Eli Zaretskii's message of "Thu, 22 Jun 2023 20:45:02 +0300")

[-- Attachment #1: Type: text/plain, Size: 307 bytes --]

>> +(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:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: duplicate-line-final-position.patch --]
[-- Type: text/x-diff, Size: 3526 bytes --]

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")

  parent reply	other threads:[~2023-06-22 18:17 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-20  5:07 bug#64185: proposal for new function: copy-line Zachary Kanfer
2023-06-20  6:15 ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-06-20 11:44   ` Eli Zaretskii
2023-06-22  3:33     ` Zachary Kanfer
2023-06-22  5:08       ` Eli Zaretskii
2023-06-22  6:57         ` Juri Linkov
2023-06-22 16:25           ` Eli Zaretskii
2023-06-22 17:27             ` Juri Linkov
2023-06-22 17:45               ` Eli Zaretskii
2023-06-22 18:13                 ` Drew Adams
2023-06-22 18:29                   ` Juri Linkov
2023-06-22 18:42                     ` Drew Adams
2023-06-22 18:52                       ` Juri Linkov
2023-06-22 19:05                         ` Drew Adams
2023-06-22 18:17                 ` Juri Linkov [this message]
2023-06-22 18:30                   ` Eli Zaretskii
2023-06-23  5:46                     ` Zachary Kanfer
2023-06-23  5:56                       ` Eli Zaretskii
2023-06-23  7:08                   ` Robert Pluim
2023-06-23  7:19                     ` Eli Zaretskii
2023-06-23  9:01                       ` Robert Pluim
2023-06-23 16:46                       ` Juri Linkov
2023-06-23  9:07 ` Mattias Engdegård
2023-06-23 10:28   ` Eli Zaretskii
2023-06-23 10:50     ` Mattias Engdegård
2023-06-23 11:07       ` Eli Zaretskii
2023-06-23 16:45   ` Juri Linkov
2023-06-24 11:29     ` Mattias Engdegård
2023-06-25 17:24       ` Juri Linkov
2023-06-25 19:46         ` Mattias Engdegård
2023-06-26 17:37           ` Juri Linkov
2023-06-26 17:56             ` Drew Adams
2023-06-26 18:35             ` Eli Zaretskii
2023-06-27 15:35             ` Mattias Engdegård
2023-06-27 18:28               ` Juri Linkov
2023-06-28 13:17                 ` Mattias Engdegård
2023-06-28 17:42                   ` Juri Linkov
2023-06-28 18:37                     ` Eli Zaretskii
2023-06-29  7:13                       ` Juri Linkov
2023-06-30 17:13                         ` Mattias Engdegård
2023-06-30 19:03                           ` Eli Zaretskii
2023-07-01  8:45                             ` Mattias Engdegård
2023-07-01  9:53                               ` Eli Zaretskii
2023-07-01 10:07                                 ` Mattias Engdegård
2023-07-01 10:22                                   ` Eli Zaretskii
2023-07-01 10:33                                     ` Mattias Engdegård
2023-06-25  3:45   ` Zachary Kanfer
2023-06-25 17:19     ` Juri Linkov
     [not found]       ` <CAFXT+RPRwpZgfPKsyz22+-v6vy7RJwyuwaOEkmunc2MAMSoqZA@mail.gmail.com>
     [not found]         ` <86h6qut970.fsf@mail.linkov.net>
2023-06-26 19:18           ` Zachary Kanfer
2023-06-27  2:25             ` 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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86wmzvwecw.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=64185@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=me@eshelyaron.com \
    --cc=zkanfer@gmail.com \
    /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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.