1 ;;; x-select.el --- Lisp interface to X Selections.
3 ;; Copyright (C) 1990, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Sun Microsystems.
6 ;; Maintainer: XEmacs Development Team
7 ;; Keywords: extensions, dumped
9 ;; This file is part of XEmacs.
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)
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.
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.
26 ;;; Synched up with: FSF 19.30 (select.el).
30 ;; This file is dumped with XEmacs (when X support is compiled in).
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).
38 (defvar x-selected-text-type
39 (if (featurep 'mule) '(COMPOUND_TEXT STRING) 'STRING)
40 "The type atom used to obtain selections from the X server.
41 Can be either a valid X selection data type, or a list of such types.
42 COMPOUND_TEXT and STRING are the most commonly used data types.
43 If a list is provided, the types are tried in sequence until
44 there is a successful conversion.")
46 (defun x-get-selection (&optional type data-type)
47 "Return the value of an X Windows selection.
48 The argument TYPE (default `PRIMARY') says which selection,
49 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule)
50 says how to convert the data."
51 (or type (setq type 'PRIMARY))
52 (or data-type (setq data-type x-selected-text-type))
56 (x-get-selection-internal type (car data-type))
57 (selection-conversion-error
59 (x-get-selection type (cdr data-type))
60 (signal (car err) (cdr err)))))
61 (x-get-selection-internal type data-type))))
62 (when (and (consp text) (symbolp (car text)))
63 (setq text (cdr text)))
64 (when (not (stringp text))
65 (error "Selection is not a string: %S" text))
68 (defun x-get-secondary-selection ()
69 "Return text selected from some X window."
70 (x-get-selection 'SECONDARY))
72 (defun x-get-clipboard ()
73 "Return text pasted to the clipboard."
74 (x-get-selection 'CLIPBOARD))
76 ;; FSFmacs calls this `x-set-selection', and reverses the
77 ;; arguments (duh ...). This order is more logical.
78 (defun x-own-selection (data &optional type)
79 "Make an X Windows selection of type TYPE and value DATA.
80 The argument TYPE (default `PRIMARY') says which selection,
81 and DATA specifies the contents. DATA may be a string,
82 a symbol, an integer (or a cons of two integers or list of two integers).
84 The selection may also be a cons of two markers pointing to the same buffer,
85 or an overlay. In these cases, the selection is considered to be the text
86 between the markers *at whatever time the selection is examined*.
87 Thus, editing done in the buffer after you specify the selection
88 can alter the effective value of the selection.
90 The data may also be a vector of valid non-vector selection values.
92 Interactively, the text of the region is used as the selection value."
93 (interactive (if (not current-prefix-arg)
94 (list (read-string "Store text for pasting: "))
95 (list (substring (region-beginning) (region-end)))))
96 ;FSFmacs huh?? It says:
97 ;; "This is for temporary compatibility with pre-release Emacs 19."
99 ; (setq type (intern type)))
100 (or (x-valid-simple-selection-p data)
103 (i (1- (length data))))
105 (or (x-valid-simple-selection-p (aref data i))
109 (signal 'error (list "invalid selection" data)))
110 (or type (setq type 'PRIMARY))
112 (x-own-selection-internal type data)
113 (x-disown-selection-internal type))
114 (cond ((eq type 'PRIMARY)
115 (setq primary-selection-extent
116 (select-make-extent-for-selection
117 data primary-selection-extent)))
118 ((eq type 'SECONDARY)
119 (setq secondary-selection-extent
120 (select-make-extent-for-selection
121 data secondary-selection-extent))))
122 (setq zmacs-region-stays t)
125 (defun x-valid-simple-selection-p (data)
126 (valid-simple-selection-p data))
128 (defun x-own-secondary-selection (selection &optional type)
129 "Make a secondary X Selection of the given argument. The argument may be a
130 string or a cons of two markers (in which case the selection is considered to
131 be the text between those markers)."
132 (interactive (if (not current-prefix-arg)
133 (list (read-string "Store text for pasting: "))
134 (list (cons ;; these need not be ordered.
135 (copy-marker (point-marker))
136 (copy-marker (mark-marker))))))
137 (x-own-selection selection 'SECONDARY))
140 (defun x-own-clipboard (string)
141 "Paste the given string to the X Clipboard."
142 (x-own-selection string 'CLIPBOARD))
145 (defun x-disown-selection (&optional secondary-p)
146 "Assuming we own the selection, disown it. With an argument, discard the
147 secondary selection instead of the primary selection."
148 (x-disown-selection-internal (if secondary-p 'SECONDARY 'PRIMARY)))
150 (defun x-dehilight-selection (selection)
151 "for use as a value of `x-lost-selection-hooks'."
152 (cond ((eq selection 'PRIMARY)
153 (if primary-selection-extent
154 (let ((inhibit-quit t))
155 (if (consp primary-selection-extent)
156 (mapcar 'delete-extent primary-selection-extent)
157 (delete-extent primary-selection-extent))
158 (setq primary-selection-extent nil)))
159 (if zmacs-regions (zmacs-deactivate-region)))
160 ((eq selection 'SECONDARY)
161 (if secondary-selection-extent
162 (let ((inhibit-quit t))
163 (if (consp secondary-selection-extent)
164 (mapcar 'delete-extent secondary-selection-extent)
165 (delete-extent secondary-selection-extent))
166 (setq secondary-selection-extent nil)))))
169 (setq x-lost-selection-hooks 'x-dehilight-selection)
171 (defun x-notice-selection-requests (selection type successful)
172 "for possible use as the value of x-sent-selection-hooks."
174 (message "Selection request failed to convert %s to %s"
176 (message "Sent selection %s as %s" selection type)))
178 (defun x-notice-selection-failures (selection type successful)
179 "for possible use as the value of x-sent-selection-hooks."
181 (message "Selection request failed to convert %s to %s"
184 ;(setq x-sent-selection-hooks 'x-notice-selection-requests)
185 ;(setq x-sent-selection-hooks 'x-notice-selection-failures)
188 ;;; Selections in killed buffers
189 ;;; this function is called by kill-buffer as if it were on the
190 ;;; kill-buffer-hook (though it isn't really).
192 (defun xselect-kill-buffer-hook ()
193 ;; Probably the right thing is to write a C function to return a list
194 ;; of the selections which emacs owns, since it could conceivably own
195 ;; a user-defined selection type that we've never heard of.
196 (xselect-kill-buffer-hook-1 'PRIMARY)
197 (xselect-kill-buffer-hook-1 'SECONDARY)
198 (xselect-kill-buffer-hook-1 'CLIPBOARD))
200 (defun xselect-kill-buffer-hook-1 (selection)
202 (if (and (x-selection-owner-p selection)
203 (setq value (x-get-selection-internal selection '_EMACS_INTERNAL))
204 ;; The _EMACS_INTERNAL selection type has a converter registered
205 ;; for it that does no translation. This only works if emacs is
206 ;; requesting the selection from itself. We could have done this
207 ;; by writing a C function to return the raw selection data, and
208 ;; that might be the right way to do this, but this was easy.
209 (or (and (consp value)
210 (markerp (car value))
211 (eq (current-buffer) (marker-buffer (car value))))
212 (and (extent-live-p value)
213 (eq (current-buffer) (extent-object value)))
214 (and (extentp value) (not (extent-live-p value)))))
215 (x-disown-selection-internal selection))))
218 ;;; Cut Buffer support
220 ;;; FSF name x-get-cut-buffer
221 (defun x-get-cutbuffer (&optional which-one)
222 "Return the value of one of the 8 X server cut buffers.
223 Optional arg WHICH-ONE should be a number from 0 to 7, defaulting to 0.
224 Cut buffers are considered obsolete; you should use selections instead.
225 This function does nothing if support for cut buffers was not compiled
227 (and (fboundp 'x-get-cutbuffer-internal)
228 (x-get-cutbuffer-internal
230 (aref [CUT_BUFFER0 CUT_BUFFER1 CUT_BUFFER2 CUT_BUFFER3
231 CUT_BUFFER4 CUT_BUFFER5 CUT_BUFFER6 CUT_BUFFER7]
235 ;;; FSF name x-set-cut-buffer
236 (defun x-store-cutbuffer (string &optional push)
237 "Store STRING into the X server's primary cut buffer.
238 If PUSH is non-nil, also rotate the cut buffers:
239 this means the previous value of the primary cut buffer moves the second
240 cut buffer, and the second to the third, and so on (there are 8 buffers.)
241 Cut buffers are considered obsolete; you should use selections instead.
242 This function does nothing if support for cut buffers was not compiled
244 (and (fboundp 'x-store-cutbuffer-internal)
246 ;; Check the data type of STRING.
247 (substring string 0 0)
249 (x-rotate-cutbuffers-internal 1))
250 (x-store-cutbuffer-internal 'CUT_BUFFER0 string))))
253 ;;; Random utility functions
255 (defun x-yank-clipboard-selection ()
256 "Insert the current Clipboard selection at point."
258 (setq last-command nil)
259 (setq this-command 'yank) ; so that yank-pop works.
260 (let ((clip (x-get-clipboard)))
261 (or clip (error "there is no clipboard selection"))
265 ;;; Functions to convert the selection into various other selection types.
266 ;;; Every selection type that emacs handles is implemented this way, except
267 ;;; for TIMESTAMP, which is a special case.
269 (defun xselect-convert-to-text (selection type value)
270 (cond ((stringp value)
274 (set-buffer (extent-object value))
277 (buffer-substring (extent-start-position value)
278 (extent-end-position value)))))
280 (markerp (car value))
281 (markerp (cdr value)))
282 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
284 (list "markers must be in the same buffer"
285 (car value) (cdr value))))
287 (set-buffer (or (marker-buffer (car value))
288 (error "selection is in a killed buffer")))
291 (buffer-substring (car value) (cdr value)))))
294 (defun xselect-convert-to-string (selection type value)
295 (let ((outval (xselect-convert-to-text selection type value)))
296 ;; force the string to be not in Compound Text format.
298 (cons 'STRING outval)
301 (defun xselect-convert-to-compound-text (selection type value)
302 ;; converts to compound text automatically
303 (xselect-convert-to-text selection type value))
305 (defun xselect-convert-to-length (selection type value)
307 (cond ((stringp value)
310 (extent-length value))
312 (markerp (car value))
313 (markerp (cdr value)))
314 (or (eq (marker-buffer (car value))
315 (marker-buffer (cdr value)))
317 (list "markers must be in the same buffer"
318 (car value) (cdr value))))
319 (abs (- (car value) (cdr value)))))))
320 (if value ; force it to be in 32-bit format.
321 (cons (ash value -16) (logand value 65535))
324 (defun xselect-convert-to-targets (selection type value)
325 ;; return a vector of atoms, but remove duplicates first.
326 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
329 (cond ((memq (car rest) (cdr rest))
330 (setcdr rest (delq (car rest) (cdr rest))))
331 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
332 (setcdr rest (cdr (cdr rest))))
334 (setq rest (cdr rest)))))
335 (apply 'vector all)))
337 (defun xselect-convert-to-delete (selection type value)
338 (x-disown-selection-internal selection)
339 ;; A return value of nil means that we do not know how to do this conversion,
340 ;; and replies with an "error". A return value of NULL means that we have
341 ;; done the conversion (and any side-effects) but have no value to return.
344 (defun xselect-convert-to-filename (selection type value)
345 (cond ((extentp value)
346 (buffer-file-name (or (extent-object value)
347 (error "selection is in a killed buffer"))))
349 (markerp (car value))
350 (markerp (cdr value)))
351 (buffer-file-name (or (marker-buffer (car value))
352 (error "selection is in a killed buffer"))))
355 (defun xselect-convert-to-charpos (selection type value)
357 (cond ((cond ((extentp value)
358 (setq a (extent-start-position value)
359 b (extent-end-position value)))
361 (markerp (car value))
362 (markerp (cdr value)))
365 (setq a (1- a) b (1- b)) ; zero-based
366 (if (< b a) (setq tmp a a b b tmp))
368 (vector (cons (ash a -16) (logand a 65535))
369 (cons (ash b -16) (logand b 65535))))))))
371 (defun xselect-convert-to-lineno (selection type value)
373 (cond ((cond ((extentp value)
374 (setq buf (extent-object value)
375 a (extent-start-position value)
376 b (extent-end-position value)))
378 (markerp (car value))
379 (markerp (cdr value)))
380 (setq a (marker-position (car value))
381 b (marker-position (cdr value))
382 buf (marker-buffer (car value)))))
389 (setq a (1+ (count-lines 1 (point))))
392 (setq b (1+ (count-lines 1 (point))))))
393 (if (< b a) (setq tmp a a b b tmp))
395 (vector (cons (ash a -16) (logand a 65535))
396 (cons (ash b -16) (logand b 65535))))))))
398 (defun xselect-convert-to-colno (selection type value)
400 (cond ((cond ((extentp value)
401 (setq buf (extent-object value)
402 a (extent-start-position value)
403 b (extent-end-position value)))
405 (markerp (car value))
406 (markerp (cdr value)))
409 buf (marker-buffer a))))
413 (setq a (current-column))
415 (setq b (current-column)))
416 (if (< b a) (setq tmp a a b b tmp))
418 (vector (cons (ash a -16) (logand a 65535))
419 (cons (ash b -16) (logand b 65535))))))))
421 (defun xselect-convert-to-sourceloc (selection type value)
422 (let (a b buf file-name tmp)
423 (cond ((cond ((extentp value)
424 (setq buf (or (extent-object value)
425 (error "selection is in a killed buffer"))
426 a (extent-start-position value)
427 b (extent-end-position value)
428 file-name (buffer-file-name buf)))
430 (markerp (car value))
431 (markerp (cdr value)))
432 (setq a (marker-position (car value))
433 b (marker-position (cdr value))
434 buf (or (marker-buffer (car value))
435 (error "selection is in a killed buffer"))
436 file-name (buffer-file-name buf))))
443 (setq a (1+ (count-lines 1 (point))))
446 (setq b (1+ (count-lines 1 (point))))))
447 (if (< b a) (setq tmp a a b b tmp))
448 (format "%s:%d" file-name a)))))
450 (defun xselect-convert-to-os (selection type size)
451 (symbol-name system-type))
453 (defun xselect-convert-to-host (selection type size)
456 (defun xselect-convert-to-user (selection type size)
459 (defun xselect-convert-to-class (selection type size)
460 x-emacs-application-class)
462 ;; We do not try to determine the name Emacs was invoked with,
463 ;; because it is not clean for a program's behavior to depend on that.
464 (defun xselect-convert-to-name (selection type size)
468 (defun xselect-convert-to-integer (selection type value)
469 (and (integerp value)
470 (cons (ash value -16) (logand value 65535))))
472 (defun xselect-convert-to-atom (selection type value)
473 (and (symbolp value) value))
475 (defun xselect-convert-to-identity (selection type value) ; used internally
478 (setq selection-converter-alist
479 '((TEXT . xselect-convert-to-text)
480 (STRING . xselect-convert-to-string)
481 (COMPOUND_TEXT . xselect-convert-to-compound-text)
482 (TARGETS . xselect-convert-to-targets)
483 (LENGTH . xselect-convert-to-length)
484 (DELETE . xselect-convert-to-delete)
485 (FILE_NAME . xselect-convert-to-filename)
486 (CHARACTER_POSITION . xselect-convert-to-charpos)
487 (SOURCE_LOC . xselect-convert-to-sourceloc)
488 (LINE_NUMBER . xselect-convert-to-lineno)
489 (COLUMN_NUMBER . xselect-convert-to-colno)
490 (OWNER_OS . xselect-convert-to-os)
491 (HOST_NAME . xselect-convert-to-host)
492 (USER . xselect-convert-to-user)
493 (CLASS . xselect-convert-to-class)
494 (NAME . xselect-convert-to-name)
495 (ATOM . xselect-convert-to-atom)
496 (INTEGER . xselect-convert-to-integer)
497 (_EMACS_INTERNAL . xselect-convert-to-identity)
500 ;FSFmacs (provide 'select)
502 ;;; x-select.el ends here.