-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmy-copy.el
66 lines (61 loc) · 2.99 KB
/
my-copy.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
;; Basic copy-paste setup. From wiki.
(setq x-select-enable-clipboard t)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
; Brilliant working copy-paste (even in Evil mode!) ripped from:
; http://hugoheden.wordpress.com/2009/03/08/copypaste-with-emacs-in-terminal/
(unless window-system
;; Callback for when user cuts
(defun my-cut-function (text &optional push) ;; Insert text to temp-buffer, and "send" content to xsel stdin
(with-temp-buffer
(insert text)
;; I prefer using the "clipboard" selection (the one the
;; typically is used by c-c/c-v) before the primary selection
;; (that uses mouse-select/middle-button-click)
(if (string-equal (getenv "WAYLAND_DISPLAY") "wayland-0")
(call-process-region (point-min) (point-max) "wl-copy")
(call-process-region (point-min) (point-max) "xsel" nil 0 nil "--clipboard" "--input"))))
;; Call back for when user pastes
(defun my-paste-function()
;; Find out what is current selection by xsel. If it is different
;; from the top of the kill-ring (car kill-ring), then return
;; it. Else, nil is returned, so whatever is in the top of the
;; kill-ring will be used.
(let ((paste-output (if (string-equal (getenv "WAYLAND_DISPLAY") "wayland-0")
(shell-command-to-string "wl-paste")
(shell-command-to-string "xsel --clipboard --output"))))
(unless (string= (car kill-ring) paste-output)
paste-output )))
;; Attach callbacks to hooks
(setq interprogram-cut-function 'my-cut-function)
(setq interprogram-paste-function 'my-paste-function)
;; Idea from
;; http://shreevatsa.wordpress.com/2006/10/22/emacs-copypaste-and-x/
;; http://www.mail-archive.com/[email protected]/msg03577.html
))
(when (getenv "WAYLAND_DISPLAY")
;; Callback for when user cuts
(defun my-cut-function (text &optional push)
;; Insert text to temp-buffer, and "send" content to xsel stdin
(with-temp-buffer
(insert text)
;; I prefer using the "clipboard" selection (the one the
;; typically is used by c-c/c-v) before the primary selection
;; (that uses mouse-select/middle-button-click)
(call-process-region (point-min) (point-max) "wl-copy")))
;; Call back for when user pastes
(defun my-paste-function()
;; Find out what is current selection by xsel. If it is different
;; from the top of the kill-ring (car kill-ring), then return
;; it. Else, nil is returned, so whatever is in the top of the
;; kill-ring will be used.
(let ((paste-output (shell-command-to-string "wl-paste"))
(unless (string= (car kill-ring) paste-output)
paste-output )))
;; Attach callbacks to hooks
(setq interprogram-cut-function 'my-cut-function)
(setq interprogram-paste-function 'my-paste-function)
;; Idea from
;; http://shreevatsa.wordpress.com/2006/10/22/emacs-copypaste-and-x/
;; http://www.mail-archive.com/[email protected]/msg03577.html
)
(provide 'my-copy)