]> code.delx.au - gnu-emacs-elpa/blob - packages/xclip/xclip.el
d08dcd255bce5930220e26f5c9d26338caeb9a13
[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.1
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 (defvar xclip-last-selected-text-clipboard nil
45 "The value of the CLIPBOARD X selection from xclip.")
46
47 (defvar xclip-last-selected-text-primary nil
48 "The value of the PRIMARY X selection from xclip.")
49
50 (defun xclip-set-selection (type data)
51 "TYPE is a symbol: primary, secondary and clipboard.
52
53 See also `x-set-selection'."
54 (when (getenv "DISPLAY")
55 (let* ((process-connection-type nil)
56 (proc (start-process "xclip" nil xclip-program
57 "-selection" (symbol-name type))))
58 (process-send-string proc data)
59 (process-send-eof proc))))
60
61 (defun xclip-select-text (text)
62 "See `x-select-text'."
63 (xclip-set-selection 'primary text)
64 (setq xclip-last-selected-text-primary text)
65 (when xclip-select-enable-clipboard
66 (xclip-set-selection 'clipboard text)
67 (setq xclip-last-selected-text-clipboard text)))
68
69 (defun xclip-selection-value ()
70 "See `x-selection-value'."
71 (when (getenv "DISPLAY")
72 (let ((clip-text (when xclip-select-enable-clipboard
73 (with-output-to-string
74 (process-file xclip-program nil standard-output nil
75 "-o" "-selection" "clipboard")))))
76 (setq clip-text
77 (cond ; Check clipboard selection.
78 ((or (not clip-text) (string= clip-text ""))
79 (setq xclip-last-selected-text-clipboard nil))
80 ((eq clip-text xclip-last-selected-text-clipboard)
81 nil)
82 ((string= clip-text xclip-last-selected-text-clipboard)
83 ;; Record the newer string so subsequent calls can use
84 ;; the `eq' test.
85 (setq xclip-last-selected-text-clipboard clip-text)
86 nil)
87 (t (setq xclip-last-selected-text-clipboard clip-text))))
88 (or clip-text
89 (let ((primary-text (with-output-to-string
90 (process-file xclip-program nil
91 standard-output nil "-o"))))
92 (setq primary-text
93 (cond ; Check primary selection.
94 ((or (not primary-text) (string= primary-text ""))
95 (setq xclip-last-selected-text-primary nil))
96 ((eq primary-text xclip-last-selected-text-primary)
97 nil)
98 ((string= primary-text xclip-last-selected-text-primary)
99 ;; Record the newer string so subsequent calls can
100 ;; use the `eq' test.
101 (setq xclip-last-selected-text-primary primary-text)
102 nil)
103 (t (setq xclip-last-selected-text-primary primary-text))))
104 primary-text)))))
105
106 (defun turn-on-xclip ()
107 (setq interprogram-cut-function 'xclip-select-text)
108 (setq interprogram-paste-function 'xclip-selection-value))
109
110 ;;;###autoload
111 (define-minor-mode xclip-mode
112 "Minor mode to use the `xclip' program to copy&paste."
113 :global t
114 (if xclip-mode
115 (progn
116 (or (executable-find xclip-program)
117 (signal 'file-error (list "Searching for program"
118 xclip-program "no such file")))
119 (add-hook 'terminal-init-xterm-hook 'turn-on-xclip))
120 (remove-hook 'terminal-init-xterm-hook 'turn-on-xclip)))
121
122 (provide 'xclip)
123 ;;; xclip.el ends here