Subject: bug#64185: proposal for new function: copy-line

From: Eshel Yaron <[email-masked]>
Date: Tue, 20 Jun 2023 09:15:51 +0300

Just a couple of thoughts:

Zachary Kanfer <[email-masked]> writes:

> 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.
>

How does this relate to the existing `duplicate-line`?

> +(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?"))

I think you can get the same behavior by using `duplicate-line` as follows:

(defun esy/copy-line (&optional copy-above)
  (interactive "*P")
  (duplicate-line)
  (unless copy-above (line-move 1)))