1 ;;; xclip.el --- use xclip to copy&paste -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2007, 2012, 2013 Free Software Foundation, Inc.
5 ;; Author: Leo Liu <sdl.web@gmail.com>
6 ;; Keywords: convenience, tools
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;; This package allows emacs to copy to and paste from the X clipboard
26 ;; when running in xterm. It uses the external command-line tool xclip
27 ;; found on http://xclip.sourceforge.net.
29 ;; To use: (xclip-mode 1)
33 (defcustom xclip-program "xclip"
34 "Name of the xclip program."
38 (defcustom xclip-select-enable-clipboard t
39 "Non-nil means cutting and pasting uses the clipboard.
40 This is in addition to, but in preference to, the primary selection."
44 (defcustom xclip-use-pbcopy&paste (and xclip-select-enable-clipboard
45 (eq system-type 'darwin)
46 (executable-find "pbcopy")
48 "Non-nil means using pbcopy and pbpaste instead of xclip.
49 If non-nil `xclip-program' is ignored."
53 (defvar xclip-last-selected-text-clipboard nil
54 "The value of the CLIPBOARD X selection from xclip.")
56 (defvar xclip-last-selected-text-primary nil
57 "The value of the PRIMARY X selection from xclip.")
59 (defun xclip-set-selection (type data)
60 "TYPE is a symbol: primary, secondary and clipboard.
62 See also `x-set-selection'."
63 (let* ((process-connection-type nil)
65 (xclip-use-pbcopy&paste
66 (start-file-process "pbcopy" nil "pbcopy"))
68 (start-file-process "xclip" nil xclip-program
69 "-selection" (symbol-name type))))))
71 (process-send-string proc data)
72 (process-send-eof proc))
75 (defun xclip-select-text (text)
76 "See `x-select-text'."
77 (xclip-set-selection 'primary text)
78 (setq xclip-last-selected-text-primary text)
79 (when xclip-select-enable-clipboard
80 (xclip-set-selection 'clipboard text)
81 (setq xclip-last-selected-text-clipboard text)))
83 (defun xclip-selection-value ()
84 "See `x-selection-value'."
85 (let ((clip-text (when xclip-select-enable-clipboard
86 (with-output-to-string
88 (xclip-use-pbcopy&paste
89 (process-file "pbpaste" nil standard-output nil))
91 (process-file xclip-program nil standard-output nil
92 "-o" "-selection" "clipboard")))))))
94 (cond ; Check clipboard selection.
95 ((or (not clip-text) (string= clip-text ""))
96 (setq xclip-last-selected-text-clipboard nil))
97 ((eq clip-text xclip-last-selected-text-clipboard)
99 ((string= clip-text xclip-last-selected-text-clipboard)
100 ;; Record the newer string so subsequent calls can use the
102 (setq xclip-last-selected-text-clipboard clip-text)
104 (t (setq xclip-last-selected-text-clipboard clip-text))))
106 (when (getenv "DISPLAY")
107 (let ((primary-text (with-output-to-string
108 (process-file xclip-program nil
109 standard-output nil "-o"))))
111 (cond ; Check primary selection.
112 ((or (not primary-text) (string= primary-text ""))
113 (setq xclip-last-selected-text-primary nil))
114 ((eq primary-text xclip-last-selected-text-primary)
116 ((string= primary-text xclip-last-selected-text-primary)
117 ;; Record the newer string so subsequent calls can
118 ;; use the `eq' test.
119 (setq xclip-last-selected-text-primary primary-text)
121 (t (setq xclip-last-selected-text-primary primary-text))))
124 (defun turn-on-xclip ()
125 (setq interprogram-cut-function 'xclip-select-text)
126 (setq interprogram-paste-function 'xclip-selection-value))
129 (define-minor-mode xclip-mode
130 "Minor mode to use the `xclip' program to copy&paste."
134 (or xclip-use-pbcopy&paste
135 (executable-find xclip-program)
136 (signal 'file-error (list "Searching for program"
137 xclip-program "no such file")))
138 ;; NOTE: See `tty-run-terminal-initialization' and term/README
139 (add-hook 'terminal-init-xterm-hook 'turn-on-xclip))
140 (remove-hook 'terminal-init-xterm-hook 'turn-on-xclip)))
143 ;;; xclip.el ends here