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 ;; Suppress producing escape sequences for compositions.
180 (remove-text-properties 0 (length str) '(composition nil) str)
183 (if (not (multibyte-string-p str))
184 ;; Don't have to encode unibyte string.
186 ;; If STR contains only ASCII, Latin-1, and raw bytes,
187 ;; encode STR by iso-latin-1, and return it as type
188 ;; `STRING'. Otherwise, encode STR by CODING. In that
189 ;; case, the returing type depends on CODING.
190 (let ((charsets (find-charset-string str)))
193 (delq 'latin-iso8859-1
194 (delq 'eight-bit-control
195 (delq 'eight-bit-graphic charsets)))))
197 (setq str (encode-coding-string str coding)
198 type (if (memq coding '(compound-text
199 compound-text-with-extensions))
203 str (encode-coding-string str 'iso-latin-1))))))
205 ((eq type 'COMPOUND_TEXT)
206 (setq str (encode-coding-string str coding)))
209 (if (memq coding '(compound-text
210 compound-text-with-extensions))
211 (setq str (string-make-unibyte str))
212 (setq str (encode-coding-string str coding))))
214 ((eq type 'UTF8_STRING)
215 (setq str (encode-coding-string str 'utf-8)))
218 (error "Unknow selection type: %S" type))
221 (setq next-selection-coding-system nil)
225 (defun xselect-convert-to-length (selection type value)
227 (cond ((stringp value)
230 (abs (- (overlay-end value) (overlay-start value))))
232 (markerp (car value))
233 (markerp (cdr value)))
234 (or (eq (marker-buffer (car value))
235 (marker-buffer (cdr value)))
237 (list "markers must be in the same buffer"
238 (car value) (cdr value))))
239 (abs (- (car value) (cdr value)))))))
240 (if value ; force it to be in 32-bit format.
241 (cons (ash value -16) (logand value 65535))
244 (defun xselect-convert-to-targets (selection type value)
245 ;; return a vector of atoms, but remove duplicates first.
246 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
249 (cond ((memq (car rest) (cdr rest))
250 (setcdr rest (delq (car rest) (cdr rest))))
251 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
252 (setcdr rest (cdr (cdr rest))))
254 (setq rest (cdr rest)))))
255 (apply 'vector all)))
257 (defun xselect-convert-to-delete (selection type value)
258 (x-disown-selection-internal selection)
259 ;; A return value of nil means that we do not know how to do this conversion,
260 ;; and replies with an "error". A return value of NULL means that we have
261 ;; done the conversion (and any side-effects) but have no value to return.
264 (defun xselect-convert-to-filename (selection type value)
265 (cond ((overlayp value)
266 (buffer-file-name (or (overlay-buffer value)
267 (error "selection is in a killed buffer"))))
269 (markerp (car value))
270 (markerp (cdr value)))
271 (buffer-file-name (or (marker-buffer (car value))
272 (error "selection is in a killed buffer"))))
275 (defun xselect-convert-to-charpos (selection type value)
277 (cond ((cond ((overlayp value)
278 (setq a (overlay-start value)
279 b (overlay-end value)))
281 (markerp (car value))
282 (markerp (cdr value)))
285 (setq a (1- a) b (1- b)) ; zero-based
286 (if (< b a) (setq tmp a a b b tmp))
288 (vector (cons (ash a -16) (logand a 65535))
289 (cons (ash b -16) (logand b 65535))))))))
291 (defun xselect-convert-to-lineno (selection type value)
293 (cond ((cond ((and (consp value)
294 (markerp (car value))
295 (markerp (cdr value)))
296 (setq a (marker-position (car value))
297 b (marker-position (cdr value))
298 buf (marker-buffer (car value))))
300 (setq buf (overlay-buffer value)
301 a (overlay-start value)
302 b (overlay-end value)))
306 (setq a (count-lines 1 a)
307 b (count-lines 1 b)))
308 (if (< b a) (setq tmp a a b b tmp))
310 (vector (cons (ash a -16) (logand a 65535))
311 (cons (ash b -16) (logand b 65535))))))))
313 (defun xselect-convert-to-colno (selection type value)
315 (cond ((cond ((and (consp value)
316 (markerp (car value))
317 (markerp (cdr value)))
320 buf (marker-buffer a)))
322 (setq buf (overlay-buffer value)
323 a (overlay-start value)
324 b (overlay-end value)))
329 (setq a (current-column))
331 (setq b (current-column)))
332 (if (< b a) (setq tmp a a b b tmp))
334 (vector (cons (ash a -16) (logand a 65535))
335 (cons (ash b -16) (logand b 65535))))))))
337 (defun xselect-convert-to-os (selection type size)
338 (symbol-name system-type))
340 (defun xselect-convert-to-host (selection type size)
343 (defun xselect-convert-to-user (selection type size)
346 (defun xselect-convert-to-class (selection type size)
347 "Convert selection to class.
348 This function returns the string \"Emacs\"."
351 ;; We do not try to determine the name Emacs was invoked with,
352 ;; because it is not clean for a program's behavior to depend on that.
353 (defun xselect-convert-to-name (selection type size)
354 "Convert selection to name.
355 This function returns the string \"emacs\"."
358 (defun xselect-convert-to-integer (selection type value)
359 (and (integerp value)
360 (cons (ash value -16) (logand value 65535))))
362 (defun xselect-convert-to-atom (selection type value)
363 (and (symbolp value) value))
365 (defun xselect-convert-to-identity (selection type value) ; used internally
368 (setq selection-converter-alist
369 '((TEXT . xselect-convert-to-string)
370 (COMPOUND_TEXT . xselect-convert-to-string)
371 (STRING . xselect-convert-to-string)
372 (UTF8_STRING . xselect-convert-to-string)
373 (TARGETS . xselect-convert-to-targets)
374 (LENGTH . xselect-convert-to-length)
375 (DELETE . xselect-convert-to-delete)
376 (FILE_NAME . xselect-convert-to-filename)
377 (CHARACTER_POSITION . xselect-convert-to-charpos)
378 (LINE_NUMBER . xselect-convert-to-lineno)
379 (COLUMN_NUMBER . xselect-convert-to-colno)
380 (OWNER_OS . xselect-convert-to-os)
381 (HOST_NAME . xselect-convert-to-host)
382 (USER . xselect-convert-to-user)
383 (CLASS . xselect-convert-to-class)
384 (NAME . xselect-convert-to-name)
385 (ATOM . xselect-convert-to-atom)
386 (INTEGER . xselect-convert-to-integer)
387 (_EMACS_INTERNAL . xselect-convert-to-identity)
392 ;;; arch-tag: bb634f97-8a3b-4b0a-b940-f6e09982328c
393 ;;; select.el ends here