99a72991868a941f5b9150fee397ca5fc243923e
[chise/xemacs-chise.git.1] / lisp / x-select.el
1 ;;; x-select.el --- Lisp interface to X Selections.
2
3 ;; Copyright (C) 1990, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Sun Microsystems.
5
6 ;; Maintainer: XEmacs Development Team
7 ;; Keywords: extensions, dumped
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Synched up with: FSF 19.30 (select.el).
27
28 ;;; Commentary:
29
30 ;; This file is dumped with XEmacs (when X support is compiled in).
31
32 ;; The selection code requires us to use certain symbols whose names are
33 ;; all upper-case; this may seem tasteless, but it makes there be a 1:1
34 ;; correspondence between these symbols and X Atoms (which are upcased).
35
36 ;;; Code:
37
38 (define-obsolete-function-alias 'x-selection-exists-p 'selection-exists-p)
39 (define-obsolete-function-alias 'x-selection-owner-p 'selection-owner-p)
40 (define-obsolete-variable-alias 'x-selection-converter-alist 'selection-converter-alist)
41 (define-obsolete-variable-alias 'x-lost-selection-hooks 'lost-selection-hooks)
42 (define-obsolete-variable-alias 'x-selected-text-type 'selected-text-type)
43 (define-obsolete-function-alias 'x-valid-simple-selection-p 'valid-simple-selection-p)
44 (define-obsolete-function-alias 'x-own-selection 'own-selection)
45 (define-obsolete-function-alias 'x-disown-selection 'disown-selection)
46 (define-obsolete-function-alias 'x-delete-primary-selection 'delete-primary-selection)
47 (define-obsolete-function-alias 'x-copy-primary-selection 'copy-primary-selection)
48 (define-obsolete-function-alias 'x-kill-primary-selection 'kill-primary-selection)
49 (define-obsolete-function-alias 'x-select-make-extent-for-selection
50   'select-make-extent-for-selection)
51 (define-obsolete-function-alias 'x-cut-copy-clear-internal 'cut-copy-clear-internal)
52 (define-obsolete-function-alias 'x-get-selection 'get-selection)
53
54 (defun x-get-secondary-selection ()
55   "Return text selected from some X window."
56   (get-selection 'SECONDARY))
57
58 (defun x-get-clipboard ()
59   "Return text pasted to the clipboard."
60   (get-selection 'CLIPBOARD))
61
62 (defun x-own-secondary-selection (selection &optional type)
63   "Make a secondary X Selection of the given argument.  The argument may be a
64 string or a cons of two markers (in which case the selection is considered to
65 be the text between those markers)."
66   (interactive (if (not current-prefix-arg)
67                    (list (read-string "Store text for pasting: "))
68                  (list (cons ;; these need not be ordered.
69                         (copy-marker (point-marker))
70                         (copy-marker (mark-marker))))))
71   (x-own-selection selection 'SECONDARY))
72
73 (defun x-notice-selection-requests (selection type successful)
74   "for possible use as the value of x-sent-selection-hooks."
75   (if (not successful)
76       (message "Selection request failed to convert %s to %s"
77                selection type)
78     (message "Sent selection %s as %s" selection type)))
79
80 (defun x-notice-selection-failures (selection type successful)
81   "for possible use as the value of x-sent-selection-hooks."
82   (or successful
83       (message "Selection request failed to convert %s to %s"
84                selection type)))
85
86 ;(setq x-sent-selection-hooks 'x-notice-selection-requests)
87 ;(setq x-sent-selection-hooks 'x-notice-selection-failures)
88
89 \f
90 ;;; Selections in killed buffers
91 ;;; this function is called by kill-buffer as if it were on the
92 ;;; kill-buffer-hook (though it isn't really).
93
94 (defun xselect-kill-buffer-hook ()
95   ;; Probably the right thing is to write a C function to return a list
96   ;; of the selections which emacs owns, since it could conceivably own
97   ;; a user-defined selection type that we've never heard of.
98   (xselect-kill-buffer-hook-1 'PRIMARY)
99   (xselect-kill-buffer-hook-1 'SECONDARY)
100   (xselect-kill-buffer-hook-1 'CLIPBOARD))
101
102 (defun xselect-kill-buffer-hook-1 (selection)
103   (let (value)
104     (if (and (x-selection-owner-p selection)
105              (setq value (get-selection-internal selection '_EMACS_INTERNAL))
106              ;; The _EMACS_INTERNAL selection type has a converter registered
107              ;; for it that does no translation.  This only works if emacs is
108              ;; requesting the selection from itself.  We could have done this
109              ;; by writing a C function to return the raw selection data, and
110              ;; that might be the right way to do this, but this was easy.
111              (or (and (consp value)
112                       (markerp (car value))
113                       (eq (current-buffer) (marker-buffer (car value))))
114                  (and (extent-live-p value)
115                       (eq (current-buffer) (extent-object value)))
116                  (and (extentp value) (not (extent-live-p value)))))
117         (x-disown-selection-internal selection))))
118
119 \f
120 ;;; Cut Buffer support
121
122 ;;; FSF name x-get-cut-buffer
123 (defun x-get-cutbuffer (&optional which-one)
124   "Return the value of one of the 8 X server cut buffers.
125 Optional arg WHICH-ONE should be a number from 0 to 7, defaulting to 0.
126 Cut buffers are considered obsolete; you should use selections instead.
127 This function does nothing if support for cut buffers was not compiled
128 into Emacs."
129   (and (fboundp 'x-get-cutbuffer-internal)
130        (x-get-cutbuffer-internal
131         (if which-one
132             (aref [CUT_BUFFER0 CUT_BUFFER1 CUT_BUFFER2 CUT_BUFFER3
133                                CUT_BUFFER4 CUT_BUFFER5 CUT_BUFFER6 CUT_BUFFER7]
134                   which-one)
135           'CUT_BUFFER0))))
136
137 ;;; FSF name x-set-cut-buffer
138 (defun x-store-cutbuffer (string &optional push)
139   "Store STRING into the X server's primary cut buffer.
140 If PUSH is non-nil, also rotate the cut buffers:
141 this means the previous value of the primary cut buffer moves the second
142 cut buffer, and the second to the third, and so on (there are 8 buffers.)
143 Cut buffers are considered obsolete; you should use selections instead.
144 This function does nothing if support for cut buffers was not compiled
145 into Emacs."
146   (and (fboundp 'x-store-cutbuffer-internal)
147        (progn
148          ;; Check the data type of STRING.
149          (substring string 0 0)
150          (if push
151              (x-rotate-cutbuffers-internal 1))
152          (x-store-cutbuffer-internal 'CUT_BUFFER0 string))))
153
154 \f
155 ;;; Random utility functions
156
157 (defun x-yank-clipboard-selection ()
158   "Insert the current Clipboard selection at point."
159   (interactive "*")
160   (setq last-command nil)
161   (setq this-command 'yank) ; so that yank-pop works.
162   (let ((clip (x-get-clipboard)))
163     (or clip (error "there is no clipboard selection"))
164     (push-mark)
165     (insert clip)))
166 \f
167
168 ;FSFmacs (provide 'select)
169
170 ;;; x-select.el ends here.