Emanuel Berg
Emanuel Berg

Reputation: 499

Elisp: Copy buffer to clipboard

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

Upvotes: 10

Views: 2617

Answers (2)

Trey Jackson
Trey Jackson

Reputation: 74430

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

Upvotes: 6

ataylor
ataylor

Reputation: 66069

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.

Upvotes: 15

Related Questions