0

This code copies the current line:

    (defun copy-line ()                                                                                                  
  "Copy the whole line that point is on."                                                                            
  (interactive)                                                                                                      
  (let ((beg (line-beginning-position 1))                                                                            
        (end (line-beginning-position 2)))                                                                           
    (if (eq last-command 'quick-copy-line)                                                                           
        (kill-append (buffer-substring beg end) (< end beg))                                                         
      (kill-new (buffer-substring beg end)))))  

, but this one also uses kill, which marks the buffer as changed.

Can we "copy", so that the buffer remains unchanged?

Dan
  • 32,980
  • 7
  • 102
  • 169
Jason Hunter
  • 709
  • 4
  • 10

1 Answers1

3

Use the function copy-region-as-kill instead. Here are some highlights from the docstring:

(copy-region-as-kill BEG END &optional REGION)

Probably introduced at or before Emacs version 19.29.

Save the region as if killed, but don’t kill it.

...

When called from Lisp, save in the kill ring the stretch of text between BEG and END, unless the optional argument REGION is non-nil, in which case ignore BEG and END, and save the current region instead.

And here's a quick one-liner that does it:

(defun copy-line ()
  (interactive)
  (copy-region-as-kill (line-beginning-position 1)
                       (line-end-position 1)))
Dan
  • 32,980
  • 7
  • 102
  • 169