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 `STRING') says
33 how to convert the data.
35 TYPE may be `SECONDARY' or `CLIPBOARD', in addition to `PRIMARY'.
36 DATA-TYPE is usually `STRING', but can also be one of the symbols
37 in `selection-converter-alist', which see."
38 (x-get-selection-internal (or type
'PRIMARY
) (or data-type
'STRING
)))
40 (defun x-get-clipboard ()
41 "Return text pasted to the clipboard."
42 (x-get-selection-internal 'CLIPBOARD
'STRING
))
44 (defun x-set-selection (type data
)
45 "Make an X Windows selection of type TYPE and value DATA.
46 The argument TYPE (default `PRIMARY') says which selection,
47 and DATA specifies the contents. DATA may be a string,
48 a symbol, an integer (or a cons of two integers or list of two integers).
50 The selection may also be a cons of two markers pointing to the same buffer,
51 or an overlay. In these cases, the selection is considered to be the text
52 between the markers *at whatever time the selection is examined*.
53 Thus, editing done in the buffer after you specify the selection
54 can alter the effective value of the selection.
56 The data may also be a vector of valid non-vector selection values.
58 Interactively, the text of the region is used as the selection value
59 if the prefix arg is set."
60 (interactive (if (not current-prefix-arg
)
61 (list 'PRIMARY
(read-string "Set text for pasting: "))
62 (list 'PRIMARY
(buffer-substring (region-beginning) (region-end)))))
63 ;; This is for temporary compatibility with pre-release Emacs 19.
65 (setq type
(intern type
)))
66 (or (x-valid-simple-selection-p data
)
69 (i (1- (length data
))))
71 (or (x-valid-simple-selection-p (aref data i
))
75 (signal 'error
(list "invalid selection" data
)))
76 (or type
(setq type
'PRIMARY
))
78 (x-own-selection-internal type data
)
79 (x-disown-selection-internal type
))
82 (defun x-valid-simple-selection-p (data)
88 (or (integerp (cdr data
))
89 (and (consp (cdr data
))
90 (integerp (car (cdr data
))))))
95 (marker-buffer (car data
))
96 (marker-buffer (cdr data
))
97 (eq (marker-buffer (car data
))
98 (marker-buffer (cdr data
)))
99 (buffer-name (marker-buffer (car data
)))
100 (buffer-name (marker-buffer (cdr data
))))))
102 ;;; Cut Buffer support
104 (defun x-get-cut-buffer (&optional which-one
)
105 "Returns the value of one of the 8 X server cut-buffers. Optional arg
106 WHICH-ONE should be a number from 0 to 7, defaulting to 0.
107 Cut buffers are considered obsolete; you should use selections instead."
108 (x-get-cut-buffer-internal
110 (aref [CUT_BUFFER0 CUT_BUFFER1 CUT_BUFFER2 CUT_BUFFER3
111 CUT_BUFFER4 CUT_BUFFER5 CUT_BUFFER6 CUT_BUFFER7
]
115 (defun x-set-cut-buffer (string &optional push
)
116 "Store STRING into the X server's primary cut buffer.
117 If PUSH is non-nil, also rotate the cut buffers:
118 this means the previous value of the primary cut buffer moves the second
119 cut buffer, and the second to the third, and so on (there are 8 buffers.)
120 Cut buffers are considered obsolete; you should use selections instead."
121 ;; Check the data type of STRING.
122 (substring string
0 0)
124 (x-rotate-cut-buffers-internal 1))
125 (x-store-cut-buffer-internal 'CUT_BUFFER0 string
))
128 ;;; Functions to convert the selection into various other selection types.
129 ;;; Every selection type that Emacs handles is implemented this way, except
130 ;;; for TIMESTAMP, which is a special case.
132 (defun xselect-convert-to-string (selection type value
)
133 (cond ((stringp value
)
137 (or (buffer-name (overlay-buffer value
))
138 (error "selection is in a killed buffer"))
139 (set-buffer (overlay-buffer value
))
140 (buffer-substring (overlay-start value
)
141 (overlay-end value
))))
143 (markerp (car value
))
144 (markerp (cdr value
)))
145 (or (eq (marker-buffer (car value
)) (marker-buffer (cdr value
)))
147 (list "markers must be in the same buffer"
148 (car value
) (cdr value
))))
150 (set-buffer (or (marker-buffer (car value
))
151 (error "selection is in a killed buffer")))
152 (buffer-substring (car value
) (cdr value
))))
155 (defun xselect-convert-to-length (selection type value
)
157 (cond ((stringp value
)
160 (abs (- (overlay-end value
) (overlay-start value
))))
162 (markerp (car value
))
163 (markerp (cdr value
)))
164 (or (eq (marker-buffer (car value
))
165 (marker-buffer (cdr value
)))
167 (list "markers must be in the same buffer"
168 (car value
) (cdr value
))))
169 (abs (- (car value
) (cdr value
)))))))
170 (if value
; force it to be in 32-bit format.
171 (cons (ash value -
16) (logand value
65535))
174 (defun xselect-convert-to-targets (selection type value
)
175 ;; return a vector of atoms, but remove duplicates first.
176 (let* ((all (cons 'TIMESTAMP
(mapcar 'car selection-converter-alist
)))
179 (cond ((memq (car rest
) (cdr rest
))
180 (setcdr rest
(delq (car rest
) (cdr rest
))))
181 ((eq (car (cdr rest
)) '_EMACS_INTERNAL
) ; shh, it's a secret
182 (setcdr rest
(cdr (cdr rest
))))
184 (setq rest
(cdr rest
)))))
185 (apply 'vector all
)))
187 (defun xselect-convert-to-delete (selection type value
)
188 (x-disown-selection-internal selection
)
189 ;; A return value of nil means that we do not know how to do this conversion,
190 ;; and replies with an "error". A return value of NULL means that we have
191 ;; done the conversion (and any side-effects) but have no value to return.
194 (defun xselect-convert-to-filename (selection type value
)
195 (cond ((overlayp value
)
196 (buffer-file-name (or (overlay-buffer value
)
197 (error "selection is in a killed buffer"))))
199 (markerp (car value
))
200 (markerp (cdr value
)))
201 (buffer-file-name (or (marker-buffer (car value
))
202 (error "selection is in a killed buffer"))))
205 (defun xselect-convert-to-charpos (selection type value
)
207 (cond ((cond ((overlayp value
)
208 (setq a
(overlay-start value
)
209 b
(overlay-end value
)))
211 (markerp (car value
))
212 (markerp (cdr value
)))
215 (setq a
(1- a
) b
(1- b
)) ; zero-based
216 (if (< b a
) (setq tmp a a b b tmp
))
218 (vector (cons (ash a -
16) (logand a
65535))
219 (cons (ash b -
16) (logand b
65535))))))))
221 (defun xselect-convert-to-lineno (selection type value
)
223 (cond ((cond ((and (consp value
)
224 (markerp (car value
))
225 (markerp (cdr value
)))
226 (setq a
(marker-position (car value
))
227 b
(marker-position (cdr value
))
228 buf
(marker-buffer (car value
))))
230 (setq buf
(overlay-buffer value
)
231 a
(overlay-start value
)
232 b
(overlay-end value
)))
236 (setq a
(count-lines 1 a
)
237 b
(count-lines 1 b
)))
238 (if (< b a
) (setq tmp a a b b tmp
))
240 (vector (cons (ash a -
16) (logand a
65535))
241 (cons (ash b -
16) (logand b
65535))))))))
243 (defun xselect-convert-to-colno (selection type value
)
245 (cond ((cond ((and (consp value
)
246 (markerp (car value
))
247 (markerp (cdr value
)))
250 buf
(marker-buffer a
)))
252 (setq buf
(overlay-buffer value
)
253 a
(overlay-start value
)
254 b
(overlay-end value
)))
259 (setq a
(current-column))
261 (setq b
(current-column)))
262 (if (< b a
) (setq tmp a a b b tmp
))
264 (vector (cons (ash a -
16) (logand a
65535))
265 (cons (ash b -
16) (logand b
65535))))))))
267 (defun xselect-convert-to-os (selection type size
)
268 (symbol-name system-type
))
270 (defun xselect-convert-to-host (selection type size
)
273 (defun xselect-convert-to-user (selection type size
)
276 (defun xselect-convert-to-class (selection type size
)
279 ;; We do not try to determine the name Emacs was invoked with,
280 ;; because it is not clean for a program's behavior to depend on that.
281 (defun xselect-convert-to-name (selection type size
)
284 (defun xselect-convert-to-integer (selection type value
)
285 (and (integerp value
)
286 (cons (ash value -
16) (logand value
65535))))
288 (defun xselect-convert-to-atom (selection type value
)
289 (and (symbolp value
) value
))
291 (defun xselect-convert-to-identity (selection type value
) ; used internally
294 (setq selection-converter-alist
295 '((TEXT . xselect-convert-to-string
)
296 (COMPOUND_TEXT . xselect-convert-to-string
)
297 (STRING . xselect-convert-to-string
)
298 (TARGETS . xselect-convert-to-targets
)
299 (LENGTH . xselect-convert-to-length
)
300 (DELETE . xselect-convert-to-delete
)
301 (FILE_NAME . xselect-convert-to-filename
)
302 (CHARACTER_POSITION . xselect-convert-to-charpos
)
303 (LINE_NUMBER . xselect-convert-to-lineno
)
304 (COLUMN_NUMBER . xselect-convert-to-colno
)
305 (OWNER_OS . xselect-convert-to-os
)
306 (HOST_NAME . xselect-convert-to-host
)
307 (USER . xselect-convert-to-user
)
308 (CLASS . xselect-convert-to-class
)
309 (NAME . xselect-convert-to-name
)
310 (ATOM . xselect-convert-to-atom
)
311 (INTEGER . xselect-convert-to-integer
)
312 (_EMACS_INTERNAL . xselect-convert-to-identity
)
317 ;;; select.el ends here.