1 ;;; select.el --- lisp portion of standard selection support.
5 ;; Copyright (c) 1993 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),
43 or a cons of two markers pointing to the same buffer.
44 In the last case, the selection is considered to be the text
46 The data may also be a vector of valid non-vector selection values."
47 (interactive (if (not current-prefix-arg
)
48 (list (read-string "Store text for pasting: "))
49 (list (cons ;; these need not be ordered.
50 (copy-marker (point-marker))
51 (copy-marker (mark-marker))))))
52 ;; This is for temporary compatibility with pre-release Emacs 19.
54 (setq type
(intern type
)))
55 (or (x-valid-simple-selection-p data
)
58 (i (1- (length data
))))
60 (or (x-valid-simple-selection-p (aref data i
))
64 (signal 'error
(list "invalid selection" data
)))
65 (or type
(setq type
'PRIMARY
))
67 (x-own-selection-internal type data
)
68 (x-disown-selection-internal type
))
71 (defun x-valid-simple-selection-p (data)
77 (or (integerp (cdr data
))
78 (and (consp (cdr data
))
79 (integerp (car (cdr data
))))))
84 (marker-buffer (car data
))
85 (marker-buffer (cdr data
))
86 (eq (marker-buffer (car data
))
87 (marker-buffer (cdr data
)))
88 (buffer-name (marker-buffer (car data
)))
89 (buffer-name (marker-buffer (cdr data
))))))
91 ;;; Cut Buffer support
93 (defun x-get-cut-buffer (&optional which-one
)
94 "Returns the value of one of the 8 X server cut-buffers. Optional arg
95 WHICH-ONE should be a number from 0 to 7, defaulting to 0.
96 Cut buffers are considered obsolete; you should use selections instead."
97 (x-get-cut-buffer-internal
99 (aref [CUT_BUFFER0 CUT_BUFFER1 CUT_BUFFER2 CUT_BUFFER3
100 CUT_BUFFER4 CUT_BUFFER5 CUT_BUFFER6 CUT_BUFFER7
]
104 (defun x-set-cut-buffer (string &optional push
)
105 "Store STRING into the X server's primary cut buffer.
106 If PUSH is non-nil, also rotate the cut buffers:
107 this means the previous value of the primary cut buffer moves the second
108 cut buffer, and the second to the third, and so on (there are 8 buffers.)
109 Cut buffers are considered obsolete; you should use selections instead."
110 ;; Check the data type of STRING.
111 (substring string
0 0)
113 (x-rotate-cut-buffers-internal 1))
114 (x-store-cut-buffer-internal 'CUT_BUFFER0 string
))
117 ;;; Functions to convert the selection into various other selection types.
118 ;;; Every selection type that Emacs handles is implemented this way, except
119 ;;; for TIMESTAMP, which is a special case.
121 (defun xselect-convert-to-string (selection type value
)
122 (cond ((stringp value
)
126 (or (buffer-name (overlay-buffer value
))
127 (error "selection is in a killed buffer"))
128 (set-buffer (overlay-buffer value
))
129 (buffer-substring (overlay-start value
)
130 (overlay-end value
))))
132 (markerp (car value
))
133 (markerp (cdr value
)))
134 (or (eq (marker-buffer (car value
)) (marker-buffer (cdr value
)))
136 (list "markers must be in the same buffer"
137 (car value
) (cdr value
))))
139 (set-buffer (or (marker-buffer (car value
))
140 (error "selection is in a killed buffer")))
141 (buffer-substring (car value
) (cdr value
))))
144 (defun xselect-convert-to-length (selection type value
)
146 (cond ((stringp value
)
149 (abs (- (overlay-end value
) (overlay-start value
))))
151 (markerp (car value
))
152 (markerp (cdr value
)))
153 (or (eq (marker-buffer (car value
))
154 (marker-buffer (cdr value
)))
156 (list "markers must be in the same buffer"
157 (car value
) (cdr value
))))
158 (abs (- (car value
) (cdr value
)))))))
159 (if value
; force it to be in 32-bit format.
160 (cons (ash value -
16) (logand value
65535))
163 (defun xselect-convert-to-targets (selection type value
)
164 ;; return a vector of atoms, but remove duplicates first.
165 (let* ((all (cons 'TIMESTAMP
(mapcar 'car selection-converter-alist
)))
168 (cond ((memq (car rest
) (cdr rest
))
169 (setcdr rest
(delq (car rest
) (cdr rest
))))
170 ((eq (car (cdr rest
)) '_EMACS_INTERNAL
) ; shh, it's a secret
171 (setcdr rest
(cdr (cdr rest
))))
173 (setq rest
(cdr rest
)))))
174 (apply 'vector all
)))
176 (defun xselect-convert-to-delete (selection type value
)
177 (x-disown-selection-internal selection
)
178 ;; A return value of nil means that we do not know how to do this conversion,
179 ;; and replies with an "error". A return value of NULL means that we have
180 ;; done the conversion (and any side-effects) but have no value to return.
183 (defun xselect-convert-to-filename (selection type value
)
184 (cond ((overlayp value
)
185 (buffer-file-name (or (overlay-buffer value
)
186 (error "selection is in a killed buffer"))))
188 (markerp (car value
))
189 (markerp (cdr value
)))
190 (buffer-file-name (or (marker-buffer (car value
))
191 (error "selection is in a killed buffer"))))
194 (defun xselect-convert-to-charpos (selection type value
)
196 (cond ((cond ((overlayp value
)
197 (setq a
(overlay-start value
)
198 b
(overlay-end value
)))
200 (markerp (car value
))
201 (markerp (cdr value
)))
204 (setq a
(1- a
) b
(1- b
)) ; zero-based
205 (if (< b a
) (setq tmp a a b b tmp
))
207 (vector (cons (ash a -
16) (logand a
65535))
208 (cons (ash b -
16) (logand b
65535))))))))
210 (defun xselect-convert-to-lineno (selection type value
)
212 (cond ((cond ((and (consp value
)
213 (markerp (car value
))
214 (markerp (cdr value
)))
215 (setq a
(marker-position (car value
))
216 b
(marker-position (cdr value
))
217 buf
(marker-buffer (car value
))))
219 (setq buf
(overlay-buffer value
)
220 a
(overlay-start value
)
221 b
(overlay-end value
)))
225 (setq a
(count-lines 1 a
)
226 b
(count-lines 1 b
)))
227 (if (< b a
) (setq tmp a a b b tmp
))
229 (vector (cons (ash a -
16) (logand a
65535))
230 (cons (ash b -
16) (logand b
65535))))))))
232 (defun xselect-convert-to-colno (selection type value
)
234 (cond ((cond ((and (consp value
)
235 (markerp (car value
))
236 (markerp (cdr value
)))
239 buf
(marker-buffer a
)))
241 (setq buf
(overlay-buffer value
)
242 a
(overlay-start value
)
243 b
(overlay-end value
)))
248 (setq a
(current-column))
250 (setq b
(current-column)))
251 (if (< b a
) (setq tmp a a b b tmp
))
253 (vector (cons (ash a -
16) (logand a
65535))
254 (cons (ash b -
16) (logand b
65535))))))))
256 (defun xselect-convert-to-os (selection type size
)
257 (symbol-name system-type
))
259 (defun xselect-convert-to-host (selection type size
)
262 (defun xselect-convert-to-user (selection type size
)
265 (defun xselect-convert-to-class (selection type size
)
268 ;; We do not try to determine the name Emacs was invoked with,
269 ;; because it is not clean for a program's behavior to depend on that.
270 (defun xselect-convert-to-name (selection type size
)
273 (defun xselect-convert-to-integer (selection type value
)
274 (and (integerp value
)
275 (cons (ash value -
16) (logand value
65535))))
277 (defun xselect-convert-to-atom (selection type value
)
278 (and (symbolp value
) value
))
280 (defun xselect-convert-to-identity (selection type value
) ; used internally
283 (setq selection-converter-alist
284 '((TEXT . xselect-convert-to-string
)
285 (STRING . xselect-convert-to-string
)
286 (TARGETS . xselect-convert-to-targets
)
287 (LENGTH . xselect-convert-to-length
)
288 (DELETE . xselect-convert-to-delete
)
289 (FILE_NAME . xselect-convert-to-filename
)
290 (CHARACTER_POSITION . xselect-convert-to-charpos
)
291 (LINE_NUMBER . xselect-convert-to-lineno
)
292 (COLUMN_NUMBER . xselect-convert-to-colno
)
293 (OWNER_OS . xselect-convert-to-os
)
294 (HOST_NAME . xselect-convert-to-host
)
295 (USER . xselect-convert-to-user
)
296 (CLASS . xselect-convert-to-class
)
297 (NAME . xselect-convert-to-name
)
298 (ATOM . xselect-convert-to-atom
)
299 (INTEGER . xselect-convert-to-integer
)
300 (_EMACS_INTERNAL . xselect-convert-to-identity
)
305 ;;; select.el ends here.