From: Zachary Kanfer <zkanfer@gmail.com>
To: 64185@debbugs.gnu.org
Subject: bug#64185: proposal for new function: copy-line
Date: Tue, 20 Jun 2023 01:07:54 -0400 [thread overview]
Message-ID: <CAFXT+RPvQ9z=GEuA58O0Lo3wZPbNsJuoh7QuZ9vzFN-Akfd44A@mail.gmail.com> (raw)
[-- Attachment #1.1: Type: text/plain, Size: 201 bytes --]
This proposal is a function that copies the line point is on, duplicating
it in the buffer.
I find it useful for both text and code. It makes writing certain types of
repetitive things a lot simpler.
[-- Attachment #1.2: Type: text/html, Size: 243 bytes --]
[-- Attachment #2: 0001-Add-copy-line-function.patch --]
[-- Type: text/x-patch, Size: 3883 bytes --]
From 883f4dac31f4c78cdc5dd4622a0b110045840bd9 Mon Sep 17 00:00:00 2001
From: Zachary Kanfer <zkanfer@gmail.com>
Date: Tue, 20 Jun 2023 01:04:47 -0400
Subject: [PATCH] Add copy-line function
* lisp/simple.el (copy-line): Add the copy-line function itself.
* test/lisp/simple-tests.el (simple-tests-zap-to-char): Tests for this
new function.
* etc/NEWS: Update
---
etc/NEWS | 4 ++++
lisp/simple.el | 20 ++++++++++++++++++
test/lisp/simple-tests.el | 44 +++++++++++++++++++++++++++++++++++++++
3 files changed, 68 insertions(+)
diff --git a/etc/NEWS b/etc/NEWS
index 61e6e161665..fe3d68b1b9e 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -665,6 +665,10 @@ Their 'noerror' arguments have no effect and are therefore obsolete.
This function evaluates a command's interactive form and returns the
resultant list.
+** New function 'copy-line' in the simple library.
+This function copies the current line, inserting it before (or, with
+prefix argument, after) point.
+
\f
* Changes in Emacs 30.1 on Non-Free Operating Systems
diff --git a/lisp/simple.el b/lisp/simple.el
index e08bf4fdd64..0c2cd2ba93a 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -10977,6 +10977,26 @@ lax-plist-put
(plist-put plist prop val #'equal))
\f
+(defun copy-line (&optional copy-above)
+ "Copy the line point is on, placing the copy above the current line.
+
+With prefix argument COPY-ABOVE, put the copy below the current line."
+ (interactive "*P")
+ (let ((line (buffer-substring-no-properties (line-beginning-position) (line-end-position)))
+ (where-in-line (- (point) (line-beginning-position))))
+ (if copy-above
+ (progn (beginning-of-line)
+ (open-line 1))
+ (progn (end-of-line)
+ (insert "\n")))
+ (insert line)
+ (goto-char (line-beginning-position))
+ (forward-char where-in-line))
+ (set-transient-map
+ (define-keymap "c" (lambda () (interactive) (copy-line copy-above))))
+ (message "press c to copy again?"))
+\f
+
(provide 'simple)
;;; simple.el ends here
diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el
index 7dabb735522..d419e3493ab 100644
--- a/test/lisp/simple-tests.el
+++ b/test/lisp/simple-tests.el
@@ -1046,5 +1046,49 @@ simple-tests-zap-to-char
(with-zap-to-char-test "abcdeCXYZ" "XYZ"
(zap-to-char 1 ?C 'interactive))))
+;;; Tests for `copy-line'
+
+(ert-deftest copy-test--copy-down--check-text ()
+ (should (equal (with-temp-buffer
+ (insert "a\nb\nc")
+ (previous-line)
+ (copy-line)
+ (buffer-string))
+ "a\nb\nb\nc")))
+
+(ert-deftest copy-test--copy-down--check-point ()
+ (should (equal (with-temp-buffer
+ (insert "a\nb\nc")
+ (previous-line)
+ (copy-line)
+ (point))
+ 6)))
+
+(ert-deftest copy-test--copy-up--check-point ()
+ (should (equal (with-temp-buffer
+ (insert "a\nb\nc")
+ (previous-line)
+ (copy-line t)
+ (point))
+ 4)))
+
+(ert-deftest copy-test--copy-down--middle-of-line--check-text ()
+ (should (equal (with-temp-buffer
+ (insert "a\nb c d e\nf")
+ (beginning-of-line -1)
+ (forward-char 4)
+ (copy-line)
+ (buffer-string))
+ "a\nb c d e\nb c d e\nf")))
+
+(ert-deftest copy-test--copy-down--middle-of-line--check-point ()
+ (should (equal (with-temp-buffer
+ (insert "a\nb c d e\nf")
+ (beginning-of-line 0)
+ (forward-char 4)
+ (copy-line)
+ (point))
+ 15)))
+
(provide 'simple-test)
;;; simple-tests.el ends here
--
2.40.1
next reply other threads:[~2023-06-20 5:07 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-20 5:07 Zachary Kanfer [this message]
2023-06-20 6:15 ` bug#64185: proposal for new function: copy-line 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
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAFXT+RPvQ9z=GEuA58O0Lo3wZPbNsJuoh7QuZ9vzFN-Akfd44A@mail.gmail.com' \
--to=zkanfer@gmail.com \
--cc=64185@debbugs.gnu.org \
/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 public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).