Reputation: 2758
As part of learning ELisp, I am trying to make a function that makes a copy of the current line below the current line (duplicates the line). What I have so far works pretty well, except on the last line of the buffer. If on the last line, then the line just gets pasted at the end of the line rather than below it.
Here is my code:
(defun duplicate-line ()
"duplicate the current line"
(interactive)
(save-excursion
(kill-ring-save (line-beginning-position) (line-beginning-position 2))
(goto-char (line-beginning-position 2)) ; goto the start of the next line
(yank)
)
(next-line)
)
Is there a better way of doing this? I would also appreciate any other advice concerning writing elisp.
Upvotes: 1
Views: 102
Reputation: 20298
I guess this happens only when the last line doesn't end with a newline character.
The following function inserts a newline if necessary, and avoids using the kill-ring.
(defun duplicate-line ()
(interactive)
(let* ((pos-end (line-beginning-position 2))
(line (buffer-substring (line-beginning-position) pos-end)))
(goto-char pos-end)
(unless (bolp) (newline))
(save-excursion ;; leave point before the duplicate line
(insert line))))
Upvotes: 2
Reputation:
You could do instead:
(kill-whole-line)
(yank)
(yank)
to the same effect. But, perhaps, if I was to write such a function, I'd rather make it so the line isn't copied to the kill-ring. Usually, if I need to duplicate line, I don't want it to be there.
Upvotes: 2