]> code.delx.au - gnu-emacs-elpa/blob - packages/xclip/xclip.el
Get "make -k" to go through
[gnu-emacs-elpa] / packages / xclip / xclip.el
1 ;;; xclip.el --- Emacs Interface to XClip
2
3 ;; Copyright (C) 2007, 2012 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.0
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 code provides an Emacs interface to the tool with the same
26 ;; name on http://people.debian.org/~kims/xclip/.
27 ;; Just add (xclip-mode 1) to your ~/.emacs.
28
29 ;;; Code:
30 (defvar xclip-program "xclip"
31 "Name of XClip program tool.")
32
33 (defvar xclip-select-enable-clipboard t
34 "Non-nil means cutting and pasting uses the clipboard.
35 This is in addition to, but in preference to, the primary selection.")
36
37 (defvar xclip-last-selected-text-clipboard nil
38 "The value of the CLIPBOARD X selection from xclip.")
39
40 (defvar xclip-last-selected-text-primary nil
41 "The value of the PRIMARY X selection from xclip.")
42
43 (defun xclip-set-selection (type data)
44 "TYPE is a symbol: primary, secondary and clipboard.
45
46 See `x-set-selection'."
47 (when (and (executable-find xclip-program) (getenv "DISPLAY"))
48 (let* ((process-connection-type nil)
49 (proc (start-process "xclip" nil xclip-program
50 "-selection" (symbol-name type))))
51 (process-send-string proc data)
52 (process-send-eof proc))))
53
54 (defun xclip-select-text (text &optional push)
55 "See `x-select-text'."
56 (xclip-set-selection 'primary text)
57 (setq xclip-last-selected-text-primary text)
58 (when xclip-select-enable-clipboard
59 (xclip-set-selection 'clipboard text)
60 (setq xclip-last-selected-text-clipboard text)))
61
62 (defun xclip-selection-value ()
63 "See `x-cut-buffer-or-selection-value'."
64 (when (and (executable-find xclip-program) (getenv "DISPLAY"))
65 (let (clip-text primary-text)
66 (when xclip-select-enable-clipboard
67 (setq clip-text (shell-command-to-string
68 (concat (shell-quote-argument xclip-program)
69 " -o -selection clipboard")))
70 (setq clip-text
71 (cond ;; check clipboard selection
72 ((or (not clip-text) (string= clip-text ""))
73 (setq xclip-last-selected-text-primary nil))
74 ((eq clip-text xclip-last-selected-text-clipboard) nil)
75 ((string= clip-text xclip-last-selected-text-clipboard)
76 ;; Record the newer string,
77 ;; so subsequent calls can use the `eq' test.
78 (setq xclip-last-selected-text-clipboard clip-text)
79 nil)
80 (t (setq xclip-last-selected-text-clipboard clip-text)))))
81 (setq primary-text (shell-command-to-string
82 (concat (shell-quote-argument xclip-program) " -o")))
83 (setq primary-text
84 (cond ;; check primary selection
85 ((or (not primary-text) (string= primary-text ""))
86 (setq xclip-last-selected-text-primary nil))
87 ((eq primary-text xclip-last-selected-text-primary) nil)
88 ((string= primary-text xclip-last-selected-text-primary)
89 ;; Record the newer string,
90 ;; so subsequent calls can use the `eq' test.
91 (setq xclip-last-selected-text-primary primary-text)
92 nil)
93 (t (setq xclip-last-selected-text-primary primary-text))))
94 (or clip-text primary-text))))
95
96 (defun turn-on-xclip ()
97 (setq interprogram-cut-function 'xclip-select-text)
98 (setq interprogram-paste-function 'xclip-selection-value))
99
100 (defun turn-off-xclip ()
101 (setq interprogram-cut-function nil)
102 (setq interprogram-paste-function nil))
103
104 ;;;###autoload
105 (define-minor-mode xclip-mode
106 "Minor mode to use the `xclip' program to copy&paste."
107 :global t
108 (if xclip-mode
109 (add-hook 'terminal-init-xterm-hook 'turn-on-xclip)
110 (remove-hook 'terminal-init-xterm-hook 'turn-on-xclip)))
111
112 (provide 'xclip)
113 ;;; xclip.el ends here