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
))))))
80 ;;; (and (fboundp 'extentp)
85 (marker-buffer (car data
))
86 (marker-buffer (cdr data
))
87 (eq (marker-buffer (car data
))
88 (marker-buffer (cdr data
)))
89 (buffer-name (marker-buffer (car data
)))
90 (buffer-name (marker-buffer (cdr data
))))))
92 ;;; Cut Buffer support
94 (defun x-get-cut-buffer (&optional which-one
)
95 "Returns the value of one of the 8 X server cut-buffers. Optional arg
96 WHICH-ONE should be a number from 0 to 7, defaulting to 0.
97 Cut buffers are considered obsolete; you should use selections instead."
98 (x-get-cut-buffer-internal
100 (aref [CUT_BUFFER0 CUT_BUFFER1 CUT_BUFFER2 CUT_BUFFER3
101 CUT_BUFFER4 CUT_BUFFER5 CUT_BUFFER6 CUT_BUFFER7
]
105 (defun x-set-cut-buffer (string)
106 "Store STRING into the X server's primary cut buffer.
107 The previous value of the primary cut buffer is rotated to the secondary
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)
112 (x-rotate-cut-buffers-internal 1)
113 (x-store-cut-buffer-internal 'CUT_BUFFER0 string
))
116 ;;; Functions to convert the selection into various other selection types.
117 ;;; Every selection type that Emacs handles is implemented this way, except
118 ;;; for TIMESTAMP, which is a special case.
120 (defun xselect-convert-to-string (selection type value
)
121 (cond ((stringp value
)
125 ;;; (set-buffer (extent-buffer value))
126 ;;; (buffer-substring (extent-start-position value)
127 ;;; (extent-end-position value))))
129 (markerp (car value
))
130 (markerp (cdr value
)))
131 (or (eq (marker-buffer (car value
)) (marker-buffer (cdr value
)))
133 (list "markers must be in the same buffer"
134 (car value
) (cdr value
))))
136 (set-buffer (or (marker-buffer (car value
))
137 (error "selection is in a killed buffer")))
138 (buffer-substring (car value
) (cdr value
))))
141 (defun xselect-convert-to-length (selection type value
)
143 (cond ((stringp value
)
146 ;;; (extent-length value))
148 (markerp (car value
))
149 (markerp (cdr value
)))
150 (or (eq (marker-buffer (car value
))
151 (marker-buffer (cdr value
)))
153 (list "markers must be in the same buffer"
154 (car value
) (cdr value
))))
155 (abs (- (car value
) (cdr value
)))))))
156 (if value
; force it to be in 32-bit format.
157 (cons (ash value -
16) (logand value
65535))
160 (defun xselect-convert-to-targets (selection type value
)
161 ;; return a vector of atoms, but remove duplicates first.
162 (let* ((all (cons 'TIMESTAMP
(mapcar 'car selection-converter-alist
)))
165 (cond ((memq (car rest
) (cdr rest
))
166 (setcdr rest
(delq (car rest
) (cdr rest
))))
167 ((eq (car (cdr rest
)) '_EMACS_INTERNAL
) ; shh, it's a secret
168 (setcdr rest
(cdr (cdr rest
))))
170 (setq rest
(cdr rest
)))))
171 (apply 'vector all
)))
173 (defun xselect-convert-to-delete (selection type value
)
174 (x-disown-selection-internal selection
)
175 ;; A return value of nil means that we do not know how to do this conversion,
176 ;; and replies with an "error". A return value of NULL means that we have
177 ;; done the conversion (and any side-effects) but have no value to return.
180 (defun xselect-convert-to-filename (selection type value
)
183 ;;; (buffer-file-name (or (extent-buffer value)
184 ;;; (error "selection is in a killed buffer"))))
186 (markerp (car value
))
187 (markerp (cdr value
)))
188 (buffer-file-name (or (marker-buffer (car value
))
189 (error "selection is in a killed buffer"))))
192 (defun xselect-convert-to-charpos (selection type value
)
196 ;;; (setq a (extent-start-position value)
197 ;;; b (extent-end-position value)))
199 (markerp (car value
))
200 (markerp (cdr value
)))
203 (setq a
(1- a
) b
(1- b
)) ; zero-based
204 (if (< b a
) (setq tmp a a b b tmp
))
206 (vector (cons (ash a -
16) (logand a
65535))
207 (cons (ash b -
16) (logand b
65535))))))))
209 (defun xselect-convert-to-lineno (selection type value
)
211 (cond ((cond ((and (consp value
)
212 (markerp (car value
))
213 (markerp (cdr value
)))
214 (setq a
(marker-position (car value
))
215 b
(marker-position (cdr value
))
216 buf
(marker-buffer (car value
))))
218 ;;; (setq buf (extent-buffer value)
219 ;;; a (extent-start-position value)
220 ;;; b (extent-end-position value)))
224 (setq a
(count-lines 1 a
)
225 b
(count-lines 1 b
)))
226 (if (< b a
) (setq tmp a a b b tmp
))
228 (vector (cons (ash a -
16) (logand a
65535))
229 (cons (ash b -
16) (logand b
65535))))))))
231 (defun xselect-convert-to-colno (selection type value
)
233 (cond ((cond ((and (consp value
)
234 (markerp (car value
))
235 (markerp (cdr value
)))
238 buf
(marker-buffer a
)))
240 ;;; (setq buf (extent-buffer value)
241 ;;; a (extent-start-position value)
242 ;;; b (extent-end-position value)))
247 (setq a
(current-column))
249 (setq b
(current-column)))
250 (if (< b a
) (setq tmp a a b b tmp
))
252 (vector (cons (ash a -
16) (logand a
65535))
253 (cons (ash b -
16) (logand b
65535))))))))
255 (defun xselect-convert-to-os (selection type size
)
256 (symbol-name system-type
))
258 (defun xselect-convert-to-host (selection type size
)
261 (defun xselect-convert-to-user (selection type size
)
264 (defun xselect-convert-to-class (selection type size
)
267 ;; We do not try to determine the name Emacs was invoked with,
268 ;; because it is not clean for a program's behavior to depend on that.
269 (defun xselect-convert-to-name (selection type size
)
272 (defun xselect-convert-to-integer (selection type value
)
273 (and (integerp value
)
274 (cons (ash value -
16) (logand value
65535))))
276 (defun xselect-convert-to-atom (selection type value
)
277 (and (symbolp value
) value
))
279 (defun xselect-convert-to-identity (selection type value
) ; used internally
282 (setq selection-converter-alist
283 '((TEXT . xselect-convert-to-string
)
284 (STRING . xselect-convert-to-string
)
285 (TARGETS . xselect-convert-to-targets
)
286 (LENGTH . xselect-convert-to-length
)
287 (DELETE . xselect-convert-to-delete
)
288 (FILE_NAME . xselect-convert-to-filename
)
289 (CHARACTER_POSITION . xselect-convert-to-charpos
)
290 (LINE_NUMBER . xselect-convert-to-lineno
)
291 (COLUMN_NUMBER . xselect-convert-to-colno
)
292 (OWNER_OS . xselect-convert-to-os
)
293 (HOST_NAME . xselect-convert-to-host
)
294 (USER . xselect-convert-to-user
)
295 (CLASS . xselect-convert-to-class
)
296 (NAME . xselect-convert-to-name
)
297 (ATOM . xselect-convert-to-atom
)
298 (INTEGER . xselect-convert-to-integer
)
299 (_EMACS_INTERNAL . xselect-convert-to-identity
)
304 ;;; select.el ends here.