1 ;;; select.el --- lisp portion of standard selection support.
5 ;; Copyright (c) 1993, 1994 Free Software Foundation, Inc.
6 ;; Based partially on earlier release by Lucid.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs 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 2, or (at your option)
15 ;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This is for temporary compatibility with pre-release Emacs 19.
28 (defalias 'x-selection 'x-get-selection)
29 (defun x-get-selection (&optional type data-type)
30 "Return the value of an X Windows selection.
31 The argument TYPE (default `PRIMARY') says which selection,
32 and the argument DATA-TYPE (default `COMPOUND_TEXT') says
33 how to convert the data."
34 (x-get-selection-internal (or type 'PRIMARY) (or data-type 'COMPOUND_TEXT)))
36 (defun x-get-clipboard ()
37 "Return text pasted to the clipboard."
38 (x-get-selection-internal 'CLIPBOARD 'STRING))
40 (defun x-set-selection (type data)
41 "Make an X Windows selection of type TYPE and value DATA.
42 The argument TYPE (default `PRIMARY') says which selection,
43 and DATA specifies the contents. DATA may be a string,
44 a symbol, an integer (or a cons of two integers or list of two integers).
46 The selection may also be a cons of two markers pointing to the same buffer,
47 or an overlay. In these cases, the selection is considered to be the text
48 between the markers *at whatever time the selection is examined*.
49 Thus, editing done in the buffer after you specify the selection
50 can alter the effective value of the selection.
52 The data may also be a vector of valid non-vector selection values.
54 Interactively, the text of the region is used as the selection value."
55 (interactive (if (not current-prefix-arg)
56 (list 'PRIMARY (read-string "Set text for pasting: "))
57 (list 'PRIMARY (substring (region-beginning) (region-end)))))
58 ;; This is for temporary compatibility with pre-release Emacs 19.
60 (setq type (intern type)))
61 (or (x-valid-simple-selection-p data)
64 (i (1- (length data))))
66 (or (x-valid-simple-selection-p (aref data i))
70 (signal 'error (list "invalid selection" data)))
71 (or type (setq type 'PRIMARY))
73 (x-own-selection-internal type data)
74 (x-disown-selection-internal type))
77 (defun x-valid-simple-selection-p (data)
83 (or (integerp (cdr data))
84 (and (consp (cdr data))
85 (integerp (car (cdr data))))))
90 (marker-buffer (car data))
91 (marker-buffer (cdr data))
92 (eq (marker-buffer (car data))
93 (marker-buffer (cdr data)))
94 (buffer-name (marker-buffer (car data)))
95 (buffer-name (marker-buffer (cdr data))))))
97 ;;; Cut Buffer support
99 (defun x-get-cut-buffer (&optional which-one)
100 "Returns the value of one of the 8 X server cut-buffers. Optional arg
101 WHICH-ONE should be a number from 0 to 7, defaulting to 0.
102 Cut buffers are considered obsolete; you should use selections instead."
103 (x-get-cut-buffer-internal
105 (aref [CUT_BUFFER0 CUT_BUFFER1 CUT_BUFFER2 CUT_BUFFER3
106 CUT_BUFFER4 CUT_BUFFER5 CUT_BUFFER6 CUT_BUFFER7]
110 (defun x-set-cut-buffer (string &optional push)
111 "Store STRING into the X server's primary cut buffer.
112 If PUSH is non-nil, also rotate the cut buffers:
113 this means the previous value of the primary cut buffer moves the second
114 cut buffer, and the second to the third, and so on (there are 8 buffers.)
115 Cut buffers are considered obsolete; you should use selections instead."
116 ;; Check the data type of STRING.
117 (substring string 0 0)
119 (x-rotate-cut-buffers-internal 1))
120 (x-store-cut-buffer-internal 'CUT_BUFFER0 string))
123 ;;; Functions to convert the selection into various other selection types.
124 ;;; Every selection type that Emacs handles is implemented this way, except
125 ;;; for TIMESTAMP, which is a special case.
127 (defun xselect-convert-to-string (selection type value)
128 (cond ((stringp value)
132 (or (buffer-name (overlay-buffer value))
133 (error "selection is in a killed buffer"))
134 (set-buffer (overlay-buffer value))
135 (buffer-substring (overlay-start value)
136 (overlay-end value))))
138 (markerp (car value))
139 (markerp (cdr value)))
140 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
142 (list "markers must be in the same buffer"
143 (car value) (cdr value))))
145 (set-buffer (or (marker-buffer (car value))
146 (error "selection is in a killed buffer")))
147 (buffer-substring (car value) (cdr value))))
150 (defun xselect-convert-to-length (selection type value)
152 (cond ((stringp value)
155 (abs (- (overlay-end value) (overlay-start value))))
157 (markerp (car value))
158 (markerp (cdr value)))
159 (or (eq (marker-buffer (car value))
160 (marker-buffer (cdr value)))
162 (list "markers must be in the same buffer"
163 (car value) (cdr value))))
164 (abs (- (car value) (cdr value)))))))
165 (if value ; force it to be in 32-bit format.
166 (cons (ash value -16) (logand value 65535))
169 (defun xselect-convert-to-targets (selection type value)
170 ;; return a vector of atoms, but remove duplicates first.
171 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
174 (cond ((memq (car rest) (cdr rest))
175 (setcdr rest (delq (car rest) (cdr rest))))
176 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
177 (setcdr rest (cdr (cdr rest))))
179 (setq rest (cdr rest)))))
180 (apply 'vector all)))
182 (defun xselect-convert-to-delete (selection type value)
183 (x-disown-selection-internal selection)
184 ;; A return value of nil means that we do not know how to do this conversion,
185 ;; and replies with an "error". A return value of NULL means that we have
186 ;; done the conversion (and any side-effects) but have no value to return.
189 (defun xselect-convert-to-filename (selection type value)
190 (cond ((overlayp value)
191 (buffer-file-name (or (overlay-buffer value)
192 (error "selection is in a killed buffer"))))
194 (markerp (car value))
195 (markerp (cdr value)))
196 (buffer-file-name (or (marker-buffer (car value))
197 (error "selection is in a killed buffer"))))
200 (defun xselect-convert-to-charpos (selection type value)
202 (cond ((cond ((overlayp value)
203 (setq a (overlay-start value)
204 b (overlay-end value)))
206 (markerp (car value))
207 (markerp (cdr value)))
210 (setq a (1- a) b (1- b)) ; zero-based
211 (if (< b a) (setq tmp a a b b tmp))
213 (vector (cons (ash a -16) (logand a 65535))
214 (cons (ash b -16) (logand b 65535))))))))
216 (defun xselect-convert-to-lineno (selection type value)
218 (cond ((cond ((and (consp value)
219 (markerp (car value))
220 (markerp (cdr value)))
221 (setq a (marker-position (car value))
222 b (marker-position (cdr value))
223 buf (marker-buffer (car value))))
225 (setq buf (overlay-buffer value)
226 a (overlay-start value)
227 b (overlay-end value)))
231 (setq a (count-lines 1 a)
232 b (count-lines 1 b)))
233 (if (< b a) (setq tmp a a b b tmp))
235 (vector (cons (ash a -16) (logand a 65535))
236 (cons (ash b -16) (logand b 65535))))))))
238 (defun xselect-convert-to-colno (selection type value)
240 (cond ((cond ((and (consp value)
241 (markerp (car value))
242 (markerp (cdr value)))
245 buf (marker-buffer a)))
247 (setq buf (overlay-buffer value)
248 a (overlay-start value)
249 b (overlay-end value)))
254 (setq a (current-column))
256 (setq b (current-column)))
257 (if (< b a) (setq tmp a a b b tmp))
259 (vector (cons (ash a -16) (logand a 65535))
260 (cons (ash b -16) (logand b 65535))))))))
262 (defun xselect-convert-to-os (selection type size)
263 (symbol-name system-type))
265 (defun xselect-convert-to-host (selection type size)
268 (defun xselect-convert-to-user (selection type size)
271 (defun xselect-convert-to-class (selection type size)
274 ;; We do not try to determine the name Emacs was invoked with,
275 ;; because it is not clean for a program's behavior to depend on that.
276 (defun xselect-convert-to-name (selection type size)
279 (defun xselect-convert-to-integer (selection type value)
280 (and (integerp value)
281 (cons (ash value -16) (logand value 65535))))
283 (defun xselect-convert-to-atom (selection type value)
284 (and (symbolp value) value))
286 (defun xselect-convert-to-identity (selection type value) ; used internally
289 (setq selection-converter-alist
290 '((TEXT . xselect-convert-to-string)
291 (COMPOUND_TEXT . xselect-convert-to-string)
292 (STRING . xselect-convert-to-string)
293 (TARGETS . xselect-convert-to-targets)
294 (LENGTH . xselect-convert-to-length)
295 (DELETE . xselect-convert-to-delete)
296 (FILE_NAME . xselect-convert-to-filename)
297 (CHARACTER_POSITION . xselect-convert-to-charpos)
298 (LINE_NUMBER . xselect-convert-to-lineno)
299 (COLUMN_NUMBER . xselect-convert-to-colno)
300 (OWNER_OS . xselect-convert-to-os)
301 (HOST_NAME . xselect-convert-to-host)
302 (USER . xselect-convert-to-user)
303 (CLASS . xselect-convert-to-class)
304 (NAME . xselect-convert-to-name)
305 (ATOM . xselect-convert-to-atom)
306 (INTEGER . xselect-convert-to-integer)
307 (_EMACS_INTERNAL . xselect-convert-to-identity)
312 ;;; select.el ends here.