]> code.delx.au - gnu-emacs-elpa/blob - packages/xclip/xclip.el
Merge commit '0cda39255827f283e7578cd469ae42daad9556a2' from js2-mode
[gnu-emacs-elpa] / packages / xclip / xclip.el
1 ;;; xclip.el --- use xclip to copy&paste -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2007, 2012, 2013 Free Software Foundation, Inc.
4
5 ;; Author: Leo Liu <sdl.web@gmail.com>
6 ;; Keywords: convenience, tools
7 ;; Created: 2007-12-30
8 ;; Version: 1.3
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
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.
28 ;;
29 ;; To use: (xclip-mode 1)
30
31 ;;; Code:
32
33 (defcustom xclip-program "xclip"
34 "Name of the xclip program."
35 :type 'string
36 :group 'killing)
37
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."
41 :type 'boolean
42 :group 'killing)
43
44 (defcustom xclip-use-pbcopy&paste (and xclip-select-enable-clipboard
45 (eq system-type 'darwin)
46 (executable-find "pbcopy")
47 t)
48 "Non-nil means using pbcopy and pbpaste instead of xclip.
49 If non-nil `xclip-program' is ignored."
50 :type 'boolean
51 :group 'killing)
52
53 (defvar xclip-last-selected-text-clipboard nil
54 "The value of the CLIPBOARD X selection from xclip.")
55
56 (defvar xclip-last-selected-text-primary nil
57 "The value of the PRIMARY X selection from xclip.")
58
59 (defun xclip-set-selection (type data)
60 "TYPE is a symbol: primary, secondary and clipboard.
61
62 See also `x-set-selection'."
63 (let* ((process-connection-type nil)
64 (proc (cond
65 (xclip-use-pbcopy&paste
66 (start-file-process "pbcopy" nil "pbcopy"))
67 ((getenv "DISPLAY")
68 (start-file-process "xclip" nil xclip-program
69 "-selection" (symbol-name type))))))
70 (when proc
71 (process-send-string proc data)
72 (process-send-eof proc))
73 data))
74
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)))
82
83 (defun xclip-selection-value ()
84 "See `x-selection-value'."
85 (let ((clip-text (when xclip-select-enable-clipboard
86 (with-output-to-string
87 (cond
88 (xclip-use-pbcopy&paste
89 (process-file "pbpaste" nil standard-output nil))
90 ((getenv "DISPLAY")
91 (process-file xclip-program nil standard-output nil
92 "-o" "-selection" "clipboard")))))))
93 (setq clip-text
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)
98 nil)
99 ((string= clip-text xclip-last-selected-text-clipboard)
100 ;; Record the newer string so subsequent calls can use the
101 ;; `eq' test.
102 (setq xclip-last-selected-text-clipboard clip-text)
103 nil)
104 (t (setq xclip-last-selected-text-clipboard clip-text))))
105 (or clip-text
106 (when (and (not xclip-use-pbcopy&paste) (getenv "DISPLAY"))
107 (let ((primary-text (with-output-to-string
108 (process-file xclip-program nil
109 standard-output nil "-o"))))
110 (setq primary-text
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)
115 nil)
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)
120 nil)
121 (t (setq xclip-last-selected-text-primary primary-text))))
122 primary-text)))))
123
124 (defun turn-on-xclip ()
125 (setq interprogram-cut-function 'xclip-select-text)
126 (setq interprogram-paste-function 'xclip-selection-value))
127
128 ;;;###autoload
129 (define-minor-mode xclip-mode
130 "Minor mode to use the `xclip' program to copy&paste."
131 :global t
132 (if xclip-mode
133 (progn
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)))
141
142 (provide 'xclip)
143 ;;; xclip.el ends here