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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; This is for temporary compatibility with pre-release Emacs 19.
27 (defalias 'x-selection 'x-get-selection)
28 (defun x-get-selection (&optional type data-type)
29 "Return the value of an X Windows selection.
30 The argument TYPE (default `PRIMARY') says which selection,
31 and the argument DATA-TYPE (default `STRING') says how to convert the data."
32 (x-get-selection-internal (or type 'PRIMARY) (or data-type 'STRING)))
34 (defun x-get-clipboard ()
35 "Return text pasted to the clipboard."
36 (x-get-selection-internal 'CLIPBOARD 'STRING))
38 (defun x-set-selection (type data)
39 "Make an X Windows selection of type TYPE and value DATA.
40 The argument TYPE (default `PRIMARY') says which selection,
41 and DATA specifies the contents. DATA may be a string,
42 a symbol, an integer (or a cons of two integers or list of two integers).
44 The selection may also be a cons of two markers pointing to the same buffer,
45 or an overlay. In these cases, the selection is considered to be the text
46 between the markers *at whatever time the selection is examined*.
47 Thus, editing done in the buffer after you specify the selection
48 can alter the effective value of the selection.
50 The data may also be a vector of valid non-vector selection values.
52 Interactively, the text of the region is used as the selection value."
53 (interactive (if (not current-prefix-arg)
54 (list 'PRIMARY (read-string "Set text for pasting: "))
55 (list 'PRIMARY (substring (region-beginning) (region-end)))))
56 ;; This is for temporary compatibility with pre-release Emacs 19.
58 (setq type (intern type)))
59 (or (x-valid-simple-selection-p data)
62 (i (1- (length data))))
64 (or (x-valid-simple-selection-p (aref data i))
68 (signal 'error (list "invalid selection" data)))
69 (or type (setq type 'PRIMARY))
71 (x-own-selection-internal type data)
72 (x-disown-selection-internal type))
75 (defun x-valid-simple-selection-p (data)
81 (or (integerp (cdr data))
82 (and (consp (cdr data))
83 (integerp (car (cdr data))))))
88 (marker-buffer (car data))
89 (marker-buffer (cdr data))
90 (eq (marker-buffer (car data))
91 (marker-buffer (cdr data)))
92 (buffer-name (marker-buffer (car data)))
93 (buffer-name (marker-buffer (cdr data))))))
95 ;;; Cut Buffer support
97 (defun x-get-cut-buffer (&optional which-one)
98 "Returns the value of one of the 8 X server cut-buffers. Optional arg
99 WHICH-ONE should be a number from 0 to 7, defaulting to 0.
100 Cut buffers are considered obsolete; you should use selections instead."
101 (x-get-cut-buffer-internal
103 (aref [CUT_BUFFER0 CUT_BUFFER1 CUT_BUFFER2 CUT_BUFFER3
104 CUT_BUFFER4 CUT_BUFFER5 CUT_BUFFER6 CUT_BUFFER7]
108 (defun x-set-cut-buffer (string &optional push)
109 "Store STRING into the X server's primary cut buffer.
110 If PUSH is non-nil, also rotate the cut buffers:
111 this means the previous value of the primary cut buffer moves the second
112 cut buffer, and the second to the third, and so on (there are 8 buffers.)
113 Cut buffers are considered obsolete; you should use selections instead."
114 ;; Check the data type of STRING.
115 (substring string 0 0)
117 (x-rotate-cut-buffers-internal 1))
118 (x-store-cut-buffer-internal 'CUT_BUFFER0 string))
121 ;;; Functions to convert the selection into various other selection types.
122 ;;; Every selection type that Emacs handles is implemented this way, except
123 ;;; for TIMESTAMP, which is a special case.
125 (defun xselect-convert-to-string (selection type value)
126 (cond ((stringp value)
130 (or (buffer-name (overlay-buffer value))
131 (error "selection is in a killed buffer"))
132 (set-buffer (overlay-buffer value))
133 (buffer-substring (overlay-start value)
134 (overlay-end value))))
136 (markerp (car value))
137 (markerp (cdr value)))
138 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
140 (list "markers must be in the same buffer"
141 (car value) (cdr value))))
143 (set-buffer (or (marker-buffer (car value))
144 (error "selection is in a killed buffer")))
145 (buffer-substring (car value) (cdr value))))
148 (defun xselect-convert-to-length (selection type value)
150 (cond ((stringp value)
153 (abs (- (overlay-end value) (overlay-start value))))
155 (markerp (car value))
156 (markerp (cdr value)))
157 (or (eq (marker-buffer (car value))
158 (marker-buffer (cdr value)))
160 (list "markers must be in the same buffer"
161 (car value) (cdr value))))
162 (abs (- (car value) (cdr value)))))))
163 (if value ; force it to be in 32-bit format.
164 (cons (ash value -16) (logand value 65535))
167 (defun xselect-convert-to-targets (selection type value)
168 ;; return a vector of atoms, but remove duplicates first.
169 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
172 (cond ((memq (car rest) (cdr rest))
173 (setcdr rest (delq (car rest) (cdr rest))))
174 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
175 (setcdr rest (cdr (cdr rest))))
177 (setq rest (cdr rest)))))
178 (apply 'vector all)))
180 (defun xselect-convert-to-delete (selection type value)
181 (x-disown-selection-internal selection)
182 ;; A return value of nil means that we do not know how to do this conversion,
183 ;; and replies with an "error". A return value of NULL means that we have
184 ;; done the conversion (and any side-effects) but have no value to return.
187 (defun xselect-convert-to-filename (selection type value)
188 (cond ((overlayp value)
189 (buffer-file-name (or (overlay-buffer value)
190 (error "selection is in a killed buffer"))))
192 (markerp (car value))
193 (markerp (cdr value)))
194 (buffer-file-name (or (marker-buffer (car value))
195 (error "selection is in a killed buffer"))))
198 (defun xselect-convert-to-charpos (selection type value)
200 (cond ((cond ((overlayp value)
201 (setq a (overlay-start value)
202 b (overlay-end value)))
204 (markerp (car value))
205 (markerp (cdr value)))
208 (setq a (1- a) b (1- b)) ; zero-based
209 (if (< b a) (setq tmp a a b b tmp))
211 (vector (cons (ash a -16) (logand a 65535))
212 (cons (ash b -16) (logand b 65535))))))))
214 (defun xselect-convert-to-lineno (selection type value)
216 (cond ((cond ((and (consp value)
217 (markerp (car value))
218 (markerp (cdr value)))
219 (setq a (marker-position (car value))
220 b (marker-position (cdr value))
221 buf (marker-buffer (car value))))
223 (setq buf (overlay-buffer value)
224 a (overlay-start value)
225 b (overlay-end value)))
229 (setq a (count-lines 1 a)
230 b (count-lines 1 b)))
231 (if (< b a) (setq tmp a a b b tmp))
233 (vector (cons (ash a -16) (logand a 65535))
234 (cons (ash b -16) (logand b 65535))))))))
236 (defun xselect-convert-to-colno (selection type value)
238 (cond ((cond ((and (consp value)
239 (markerp (car value))
240 (markerp (cdr value)))
243 buf (marker-buffer a)))
245 (setq buf (overlay-buffer value)
246 a (overlay-start value)
247 b (overlay-end value)))
252 (setq a (current-column))
254 (setq b (current-column)))
255 (if (< b a) (setq tmp a a b b tmp))
257 (vector (cons (ash a -16) (logand a 65535))
258 (cons (ash b -16) (logand b 65535))))))))
260 (defun xselect-convert-to-os (selection type size)
261 (symbol-name system-type))
263 (defun xselect-convert-to-host (selection type size)
266 (defun xselect-convert-to-user (selection type size)
269 (defun xselect-convert-to-class (selection type size)
272 ;; We do not try to determine the name Emacs was invoked with,
273 ;; because it is not clean for a program's behavior to depend on that.
274 (defun xselect-convert-to-name (selection type size)
277 (defun xselect-convert-to-integer (selection type value)
278 (and (integerp value)
279 (cons (ash value -16) (logand value 65535))))
281 (defun xselect-convert-to-atom (selection type value)
282 (and (symbolp value) value))
284 (defun xselect-convert-to-identity (selection type value) ; used internally
287 (setq selection-converter-alist
288 '((TEXT . xselect-convert-to-string)
289 (STRING . xselect-convert-to-string)
290 (TARGETS . xselect-convert-to-targets)
291 (LENGTH . xselect-convert-to-length)
292 (DELETE . xselect-convert-to-delete)
293 (FILE_NAME . xselect-convert-to-filename)
294 (CHARACTER_POSITION . xselect-convert-to-charpos)
295 (LINE_NUMBER . xselect-convert-to-lineno)
296 (COLUMN_NUMBER . xselect-convert-to-colno)
297 (OWNER_OS . xselect-convert-to-os)
298 (HOST_NAME . xselect-convert-to-host)
299 (USER . xselect-convert-to-user)
300 (CLASS . xselect-convert-to-class)
301 (NAME . xselect-convert-to-name)
302 (ATOM . xselect-convert-to-atom)
303 (INTEGER . xselect-convert-to-integer)
304 (_EMACS_INTERNAL . xselect-convert-to-identity)
309 ;;; select.el ends here.