1 ;;; select.el --- lisp portion of standard selection support
6 ;; Copyright (c) 1993, 1994 Free Software Foundation, Inc.
7 ;; Based partially on earlier release by Lucid.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; 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.
30 ;; This is for temporary compatibility with pre-release Emacs 19.
31 (defalias 'x-selection 'x-get-selection)
32 (defun x-get-selection (&optional type data-type)
33 "Return the value of an X Windows selection.
34 The argument TYPE (default `PRIMARY') says which selection,
35 and the argument DATA-TYPE (default `STRING') says
36 how to convert the data.
38 TYPE may be `SECONDARY' or `CLIPBOARD', in addition to `PRIMARY'.
39 DATA-TYPE is usually `STRING', but can also be one of the symbols
40 in `selection-converter-alist', which see."
41 (let ((data (x-get-selection-internal (or type 'PRIMARY)
42 (or data-type 'STRING)))
44 (when (and (stringp data)
45 (setq data-type (get-text-property 0 'foreign-selection data)))
46 (setq coding (if (eq data-type 'UTF8_STRING)
48 (or next-selection-coding-system
49 selection-coding-system))
50 data (decode-coding-string data coding))
51 (put-text-property 0 (length data) 'foreign-selection data-type data))
54 (defun x-get-clipboard ()
55 "Return text pasted to the clipboard."
56 (x-get-selection-internal 'CLIPBOARD 'STRING))
58 (defun x-set-selection (type data)
59 "Make an X Windows selection of type TYPE and value DATA.
60 The argument TYPE (default `PRIMARY') says which selection,
61 and DATA specifies the contents. DATA may be a string,
62 a symbol, an integer (or a cons of two integers or list of two integers).
64 The selection may also be a cons of two markers pointing to the same buffer,
65 or an overlay. In these cases, the selection is considered to be the text
66 between the markers *at whatever time the selection is examined*.
67 Thus, editing done in the buffer after you specify the selection
68 can alter the effective value of the selection.
70 The data may also be a vector of valid non-vector selection values.
72 Interactively, the text of the region is used as the selection value
73 if the prefix arg is set."
74 (interactive (if (not current-prefix-arg)
75 (list 'PRIMARY (read-string "Set text for pasting: "))
76 (list 'PRIMARY (buffer-substring (region-beginning) (region-end)))))
77 ;; This is for temporary compatibility with pre-release Emacs 19.
79 (setq type (intern type)))
80 (or (x-valid-simple-selection-p data)
83 (i (1- (length data))))
85 (or (x-valid-simple-selection-p (aref data i))
89 (signal 'error (list "invalid selection" data)))
90 (or type (setq type 'PRIMARY))
92 (x-own-selection-internal type data)
93 (x-disown-selection-internal type))
96 (defun x-valid-simple-selection-p (data)
101 (integerp (car data))
102 (or (integerp (cdr data))
103 (and (consp (cdr data))
104 (integerp (car (cdr data))))))
109 (marker-buffer (car data))
110 (marker-buffer (cdr data))
111 (eq (marker-buffer (car data))
112 (marker-buffer (cdr data)))
113 (buffer-name (marker-buffer (car data)))
114 (buffer-name (marker-buffer (cdr data))))))
116 ;;; Cut Buffer support
118 (defun x-get-cut-buffer (&optional which-one)
119 "Returns the value of one of the 8 X server cut-buffers.
120 Optional arg WHICH-ONE should be a number from 0 to 7, defaulting to 0.
121 Cut buffers are considered obsolete; you should use selections instead."
122 (x-get-cut-buffer-internal
124 (aref [CUT_BUFFER0 CUT_BUFFER1 CUT_BUFFER2 CUT_BUFFER3
125 CUT_BUFFER4 CUT_BUFFER5 CUT_BUFFER6 CUT_BUFFER7]
129 (defun x-set-cut-buffer (string &optional push)
130 "Store STRING into the X server's primary cut buffer.
131 If PUSH is non-nil, also rotate the cut buffers:
132 this means the previous value of the primary cut buffer moves to the second
133 cut buffer, and the second to the third, and so on (there are 8 buffers.)
134 Cut buffers are considered obsolete; you should use selections instead."
135 (or (stringp string) (signal 'wrong-type-argument (list 'string string)))
137 (x-rotate-cut-buffers-internal 1))
138 (x-store-cut-buffer-internal 'CUT_BUFFER0 string))
141 ;;; Functions to convert the selection into various other selection types.
142 ;;; Every selection type that Emacs handles is implemented this way, except
143 ;;; for TIMESTAMP, which is a special case.
145 (defun xselect-convert-to-string (selection type value)
147 ;; Get the actual string from VALUE.
148 (cond ((stringp value)
153 (or (buffer-name (overlay-buffer value))
154 (error "selection is in a killed buffer"))
155 (set-buffer (overlay-buffer value))
156 (setq str (buffer-substring (overlay-start value)
157 (overlay-end value)))))
159 (markerp (car value))
160 (markerp (cdr value)))
161 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
163 (list "markers must be in the same buffer"
164 (car value) (cdr value))))
166 (set-buffer (or (marker-buffer (car value))
167 (error "selection is in a killed buffer")))
168 (setq str (buffer-substring (car value) (cdr value))))))
171 ;; If TYPE is nil, this is a local request, thus return STR as
172 ;; is. Otherwise, encode STR.
175 (setq coding (or next-selection-coding-system selection-coding-system))
177 (setq coding (coding-system-base coding))
178 (setq coding 'raw-text))
179 (let ((inhibit-read-only t))
180 ;; Suppress producing escape sequences for compositions.
181 (remove-text-properties 0 (length str) '(composition nil) str)
184 (if (not (multibyte-string-p str))
185 ;; Don't have to encode unibyte string.
187 ;; If STR contains only ASCII, Latin-1, and raw bytes,
188 ;; encode STR by iso-latin-1, and return it as type
189 ;; `STRING'. Otherwise, encode STR by CODING. In that
190 ;; case, the returing type depends on CODING.
191 (let ((charsets (find-charset-string str)))
194 (delq 'latin-iso8859-1
195 (delq 'eight-bit-control
196 (delq 'eight-bit-graphic charsets)))))
198 (setq str (encode-coding-string str coding)
199 type (if (memq coding '(compound-text
200 compound-text-with-extensions))
204 str (encode-coding-string str 'iso-latin-1))))))
206 ((eq type 'COMPOUND_TEXT)
207 (setq str (encode-coding-string str coding)))
210 (if (memq coding '(compound-text
211 compound-text-with-extensions))
212 (setq str (string-make-unibyte str))
213 (setq str (encode-coding-string str coding))))
215 ((eq type 'UTF8_STRING)
216 (setq str (encode-coding-string str 'utf-8)))
219 (error "Unknow selection type: %S" type))
222 (setq next-selection-coding-system nil)
226 (defun xselect-convert-to-length (selection type value)
228 (cond ((stringp value)
231 (abs (- (overlay-end value) (overlay-start value))))
233 (markerp (car value))
234 (markerp (cdr value)))
235 (or (eq (marker-buffer (car value))
236 (marker-buffer (cdr value)))
238 (list "markers must be in the same buffer"
239 (car value) (cdr value))))
240 (abs (- (car value) (cdr value)))))))
241 (if value ; force it to be in 32-bit format.
242 (cons (ash value -16) (logand value 65535))
245 (defun xselect-convert-to-targets (selection type value)
246 ;; return a vector of atoms, but remove duplicates first.
247 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
250 (cond ((memq (car rest) (cdr rest))
251 (setcdr rest (delq (car rest) (cdr rest))))
252 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
253 (setcdr rest (cdr (cdr rest))))
255 (setq rest (cdr rest)))))
256 (apply 'vector all)))
258 (defun xselect-convert-to-delete (selection type value)
259 (x-disown-selection-internal selection)
260 ;; A return value of nil means that we do not know how to do this conversion,
261 ;; and replies with an "error". A return value of NULL means that we have
262 ;; done the conversion (and any side-effects) but have no value to return.
265 (defun xselect-convert-to-filename (selection type value)
266 (cond ((overlayp value)
267 (buffer-file-name (or (overlay-buffer value)
268 (error "selection is in a killed buffer"))))
270 (markerp (car value))
271 (markerp (cdr value)))
272 (buffer-file-name (or (marker-buffer (car value))
273 (error "selection is in a killed buffer"))))
276 (defun xselect-convert-to-charpos (selection type value)
278 (cond ((cond ((overlayp value)
279 (setq a (overlay-start value)
280 b (overlay-end value)))
282 (markerp (car value))
283 (markerp (cdr value)))
286 (setq a (1- a) b (1- b)) ; zero-based
287 (if (< b a) (setq tmp a a b b tmp))
289 (vector (cons (ash a -16) (logand a 65535))
290 (cons (ash b -16) (logand b 65535))))))))
292 (defun xselect-convert-to-lineno (selection type value)
294 (cond ((cond ((and (consp value)
295 (markerp (car value))
296 (markerp (cdr value)))
297 (setq a (marker-position (car value))
298 b (marker-position (cdr value))
299 buf (marker-buffer (car value))))
301 (setq buf (overlay-buffer value)
302 a (overlay-start value)
303 b (overlay-end value)))
307 (setq a (count-lines 1 a)
308 b (count-lines 1 b)))
309 (if (< b a) (setq tmp a a b b tmp))
311 (vector (cons (ash a -16) (logand a 65535))
312 (cons (ash b -16) (logand b 65535))))))))
314 (defun xselect-convert-to-colno (selection type value)
316 (cond ((cond ((and (consp value)
317 (markerp (car value))
318 (markerp (cdr value)))
321 buf (marker-buffer a)))
323 (setq buf (overlay-buffer value)
324 a (overlay-start value)
325 b (overlay-end value)))
330 (setq a (current-column))
332 (setq b (current-column)))
333 (if (< b a) (setq tmp a a b b tmp))
335 (vector (cons (ash a -16) (logand a 65535))
336 (cons (ash b -16) (logand b 65535))))))))
338 (defun xselect-convert-to-os (selection type size)
339 (symbol-name system-type))
341 (defun xselect-convert-to-host (selection type size)
344 (defun xselect-convert-to-user (selection type size)
347 (defun xselect-convert-to-class (selection type size)
348 "Convert selection to class.
349 This function returns the string \"Emacs\"."
352 ;; We do not try to determine the name Emacs was invoked with,
353 ;; because it is not clean for a program's behavior to depend on that.
354 (defun xselect-convert-to-name (selection type size)
355 "Convert selection to name.
356 This function returns the string \"emacs\"."
359 (defun xselect-convert-to-integer (selection type value)
360 (and (integerp value)
361 (cons (ash value -16) (logand value 65535))))
363 (defun xselect-convert-to-atom (selection type value)
364 (and (symbolp value) value))
366 (defun xselect-convert-to-identity (selection type value) ; used internally
369 (setq selection-converter-alist
370 '((TEXT . xselect-convert-to-string)
371 (COMPOUND_TEXT . xselect-convert-to-string)
372 (STRING . xselect-convert-to-string)
373 (UTF8_STRING . xselect-convert-to-string)
374 (TARGETS . xselect-convert-to-targets)
375 (LENGTH . xselect-convert-to-length)
376 (DELETE . xselect-convert-to-delete)
377 (FILE_NAME . xselect-convert-to-filename)
378 (CHARACTER_POSITION . xselect-convert-to-charpos)
379 (LINE_NUMBER . xselect-convert-to-lineno)
380 (COLUMN_NUMBER . xselect-convert-to-colno)
381 (OWNER_OS . xselect-convert-to-os)
382 (HOST_NAME . xselect-convert-to-host)
383 (USER . xselect-convert-to-user)
384 (CLASS . xselect-convert-to-class)
385 (NAME . xselect-convert-to-name)
386 (ATOM . xselect-convert-to-atom)
387 (INTEGER . xselect-convert-to-integer)
388 (_EMACS_INTERNAL . xselect-convert-to-identity)
393 ;;; arch-tag: bb634f97-8a3b-4b0a-b940-f6e09982328c
394 ;;; select.el ends here