Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elisp: Copy buffer to clipboard

Tags:

emacs

lisp

elisp

Made an effort with Elisp, but didn't work - says incorrect number of arguments. If you know Elips, probably this could be done elegantly with zero effort. But I include my heavy-handed stuff so you immediately will understand what I'm trying to do.

(defun copy-all ()
    "Copy entire buffer to clipboard"
    (interactive)
    (let ((pos (point)))
        (progn
            (mark-whole-buffer)
            (clipboard-kill-ring-save)
            (keyboard-quit)
            (goto-char pos)
            (message "Copy done."))))
like image 606
Emanuel Berg Avatar asked Apr 18 '12 19:04

Emanuel Berg


2 Answers

Instead of saving the point and restoring it later, use save-excursion. It's more robust and will restore the buffer as well. There's no need for an explicit progn either.

That said, in this case simply pass the ranges to clipboard-kill-ring-save instead of trying to mess around with the region. For example:

(defun copy-all ()
    "Copy entire buffer to clipboard"
    (interactive)
    (clipboard-kill-ring-save (point-min) (point-max)))

Remember, elisp help is always available inside emacs with describe-function (C-h f) if you're unsure about what arguments a function requires.

like image 121
ataylor Avatar answered Oct 08 '22 07:10

ataylor


You're making it tougher than you have to.

(defun copy-whole-buffer ()
  "Copy entire buffer to clipboard"
  (interactive)
  (clipboard-kill-ring-save (point-min) (point-max)))
like image 40
Trey Jackson Avatar answered Oct 08 '22 06:10

Trey Jackson