config updates
[emacs-init.git] / setup / copy-paste.el
1 ;; https://hugoheden.wordpress.com/2009/03/08/copypaste-with-emacs-in-terminal/
2 ;; I prefer using the "clipboard" selection (the one the
3 ;; typically is used by c-c/c-v) before the primary selection
4 ;; (that uses mouse-select/middle-button-click)
5 (setq x-select-enable-clipboard t)
6
7 ;; If emacs is run in a terminal, the clipboard- functions have no
8 ;; effect. Instead, we use of xsel, see
9 ;; http://www.vergenet.net/~conrad/software/xsel/ -- "a command-line
10 ;; program for getting and setting the contents of the X selection"
11 (unless window-system
12  (when (getenv "DISPLAY")
13   ;; Callback for when user cuts
14   (defun xsel-cut-function (text &optional push)
15     ;; Insert text to temp-buffer, and "send" content to xsel stdin
16     (with-temp-buffer
17       (insert text)
18       ;; I prefer using the "clipboard" selection (the one the
19       ;; typically is used by c-c/c-v) before the primary selection
20       ;; (that uses mouse-select/middle-button-click)
21       (call-process-region (point-min) (point-max) "xsel" nil 0 nil "--clipboard" "--input")))
22   ;; Call back for when user pastes
23   (defun xsel-paste-function()
24     ;; Find out what is current selection by xsel. If it is different
25     ;; from the top of the kill-ring (car kill-ring), then return
26     ;; it. Else, nil is returned, so whatever is in the top of the
27     ;; kill-ring will be used.
28     (let ((xsel-output (shell-command-to-string "xsel --clipboard --output")))
29       (unless (string= (car kill-ring) xsel-output)
30         xsel-output )))
31   (defun xsel-noop-paste-function()
32     ;; this version will not paste from xsel. This is often not required, since C-S-v or
33     ;; middle-mouse-click will do the same.
34     nil)
35   ;; Attach callbacks to hooks
36   (setq interprogram-cut-function 'xsel-cut-function)
37   (setq interprogram-paste-function 'xsel-noop-paste-function)
38   ;; Idea from
39   ;; http://shreevatsa.wordpress.com/2006/10/22/emacs-copypaste-and-x/
40   ;; http://www.mail-archive.com/help-gnu-emacs@gnu.org/msg03577.html
41  ))