2234
|
1 ;;; select.el --- lisp portion of standard selection support.
|
|
2
|
|
3 ;; Keywords: internal
|
|
4
|
7300
|
5 ;; Copyright (c) 1993, 1994 Free Software Foundation, Inc.
|
2234
|
6 ;; Based partially on earlier release by Lucid.
|
|
7
|
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
|
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)
|
|
13 ;; any later version.
|
|
14
|
|
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.
|
|
19
|
|
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.
|
|
23
|
|
24 ;;; Code:
|
|
25
|
|
26 ;; This is for temporary compatibility with pre-release Emacs 19.
|
2571
|
27 (defalias 'x-selection 'x-get-selection)
|
2234
|
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)))
|
|
33
|
|
34 (defun x-get-clipboard ()
|
|
35 "Return text pasted to the clipboard."
|
|
36 (x-get-selection-internal 'CLIPBOARD 'STRING))
|
|
37
|
|
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,
|
11406
|
42 a symbol, an integer (or a cons of two integers or list of two integers).
|
|
43
|
|
44 The selection may also be a cons of two markers pointing to the same buffer,
|
|
45 or an overlay. In these cases, the selection is considered to be the text
|
|
46 between the markers *at whatever time the selection is examined*.
|
|
47 Thus, editing done in the buffer after you specify the selection
|
|
48 can alter the effective value of the selection.
|
|
49
|
|
50 The data may also be a vector of valid non-vector selection values.
|
|
51
|
|
52 Interactively, the text of the region is used as the selection value."
|
2234
|
53 (interactive (if (not current-prefix-arg)
|
11406
|
54 (list 'PRIMARY (read-string "Set text for pasting: "))
|
|
55 (list 'PRIMARY (substring (region-beginning) (region-end)))))
|
2234
|
56 ;; This is for temporary compatibility with pre-release Emacs 19.
|
|
57 (if (stringp type)
|
|
58 (setq type (intern type)))
|
|
59 (or (x-valid-simple-selection-p data)
|
|
60 (and (vectorp data)
|
|
61 (let ((valid t)
|
|
62 (i (1- (length data))))
|
|
63 (while (>= i 0)
|
|
64 (or (x-valid-simple-selection-p (aref data i))
|
|
65 (setq valid nil))
|
|
66 (setq i (1- i)))
|
|
67 valid))
|
|
68 (signal 'error (list "invalid selection" data)))
|
|
69 (or type (setq type 'PRIMARY))
|
|
70 (if data
|
|
71 (x-own-selection-internal type data)
|
|
72 (x-disown-selection-internal type))
|
|
73 data)
|
|
74
|
|
75 (defun x-valid-simple-selection-p (data)
|
|
76 (or (stringp data)
|
|
77 (symbolp data)
|
|
78 (integerp data)
|
|
79 (and (consp data)
|
|
80 (integerp (car data))
|
|
81 (or (integerp (cdr data))
|
|
82 (and (consp (cdr data))
|
|
83 (integerp (car (cdr data))))))
|
6442
|
84 (overlayp data)
|
2234
|
85 (and (consp data)
|
|
86 (markerp (car data))
|
|
87 (markerp (cdr data))
|
|
88 (marker-buffer (car data))
|
|
89 (marker-buffer (cdr data))
|
|
90 (eq (marker-buffer (car data))
|
|
91 (marker-buffer (cdr data)))
|
|
92 (buffer-name (marker-buffer (car data)))
|
|
93 (buffer-name (marker-buffer (cdr data))))))
|
|
94
|
|
95 ;;; Cut Buffer support
|
|
96
|
|
97 (defun x-get-cut-buffer (&optional which-one)
|
|
98 "Returns the value of one of the 8 X server cut-buffers. Optional arg
|
|
99 WHICH-ONE should be a number from 0 to 7, defaulting to 0.
|
|
100 Cut buffers are considered obsolete; you should use selections instead."
|
|
101 (x-get-cut-buffer-internal
|
|
102 (if which-one
|
|
103 (aref [CUT_BUFFER0 CUT_BUFFER1 CUT_BUFFER2 CUT_BUFFER3
|
|
104 CUT_BUFFER4 CUT_BUFFER5 CUT_BUFFER6 CUT_BUFFER7]
|
|
105 which-one)
|
|
106 'CUT_BUFFER0)))
|
|
107
|
3035
|
108 (defun x-set-cut-buffer (string &optional push)
|
2234
|
109 "Store STRING into the X server's primary cut buffer.
|
3035
|
110 If PUSH is non-nil, also rotate the cut buffers:
|
|
111 this means the previous value of the primary cut buffer moves the second
|
2234
|
112 cut buffer, and the second to the third, and so on (there are 8 buffers.)
|
|
113 Cut buffers are considered obsolete; you should use selections instead."
|
|
114 ;; Check the data type of STRING.
|
|
115 (substring string 0 0)
|
3035
|
116 (if push
|
|
117 (x-rotate-cut-buffers-internal 1))
|
2234
|
118 (x-store-cut-buffer-internal 'CUT_BUFFER0 string))
|
|
119
|
|
120
|
|
121 ;;; Functions to convert the selection into various other selection types.
|
|
122 ;;; Every selection type that Emacs handles is implemented this way, except
|
|
123 ;;; for TIMESTAMP, which is a special case.
|
|
124
|
|
125 (defun xselect-convert-to-string (selection type value)
|
|
126 (cond ((stringp value)
|
|
127 value)
|
6442
|
128 ((overlayp value)
|
|
129 (save-excursion
|
|
130 (or (buffer-name (overlay-buffer value))
|
|
131 (error "selection is in a killed buffer"))
|
|
132 (set-buffer (overlay-buffer value))
|
|
133 (buffer-substring (overlay-start value)
|
|
134 (overlay-end value))))
|
2234
|
135 ((and (consp value)
|
|
136 (markerp (car value))
|
|
137 (markerp (cdr value)))
|
|
138 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
|
|
139 (signal 'error
|
|
140 (list "markers must be in the same buffer"
|
|
141 (car value) (cdr value))))
|
|
142 (save-excursion
|
|
143 (set-buffer (or (marker-buffer (car value))
|
|
144 (error "selection is in a killed buffer")))
|
|
145 (buffer-substring (car value) (cdr value))))
|
|
146 (t nil)))
|
|
147
|
|
148 (defun xselect-convert-to-length (selection type value)
|
|
149 (let ((value
|
|
150 (cond ((stringp value)
|
|
151 (length value))
|
6442
|
152 ((overlayp value)
|
|
153 (abs (- (overlay-end value) (overlay-start value))))
|
2234
|
154 ((and (consp value)
|
|
155 (markerp (car value))
|
|
156 (markerp (cdr value)))
|
|
157 (or (eq (marker-buffer (car value))
|
|
158 (marker-buffer (cdr value)))
|
|
159 (signal 'error
|
|
160 (list "markers must be in the same buffer"
|
|
161 (car value) (cdr value))))
|
|
162 (abs (- (car value) (cdr value)))))))
|
|
163 (if value ; force it to be in 32-bit format.
|
|
164 (cons (ash value -16) (logand value 65535))
|
|
165 nil)))
|
|
166
|
|
167 (defun xselect-convert-to-targets (selection type value)
|
|
168 ;; return a vector of atoms, but remove duplicates first.
|
|
169 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
|
|
170 (rest all))
|
|
171 (while rest
|
|
172 (cond ((memq (car rest) (cdr rest))
|
|
173 (setcdr rest (delq (car rest) (cdr rest))))
|
|
174 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
|
|
175 (setcdr rest (cdr (cdr rest))))
|
|
176 (t
|
|
177 (setq rest (cdr rest)))))
|
|
178 (apply 'vector all)))
|
|
179
|
|
180 (defun xselect-convert-to-delete (selection type value)
|
|
181 (x-disown-selection-internal selection)
|
|
182 ;; A return value of nil means that we do not know how to do this conversion,
|
|
183 ;; and replies with an "error". A return value of NULL means that we have
|
|
184 ;; done the conversion (and any side-effects) but have no value to return.
|
|
185 'NULL)
|
|
186
|
|
187 (defun xselect-convert-to-filename (selection type value)
|
6442
|
188 (cond ((overlayp value)
|
|
189 (buffer-file-name (or (overlay-buffer value)
|
|
190 (error "selection is in a killed buffer"))))
|
2234
|
191 ((and (consp value)
|
|
192 (markerp (car value))
|
|
193 (markerp (cdr value)))
|
|
194 (buffer-file-name (or (marker-buffer (car value))
|
|
195 (error "selection is in a killed buffer"))))
|
|
196 (t nil)))
|
|
197
|
|
198 (defun xselect-convert-to-charpos (selection type value)
|
|
199 (let (a b tmp)
|
6442
|
200 (cond ((cond ((overlayp value)
|
|
201 (setq a (overlay-start value)
|
|
202 b (overlay-end value)))
|
2234
|
203 ((and (consp value)
|
|
204 (markerp (car value))
|
|
205 (markerp (cdr value)))
|
|
206 (setq a (car value)
|
|
207 b (cdr value))))
|
|
208 (setq a (1- a) b (1- b)) ; zero-based
|
|
209 (if (< b a) (setq tmp a a b b tmp))
|
|
210 (cons 'SPAN
|
|
211 (vector (cons (ash a -16) (logand a 65535))
|
|
212 (cons (ash b -16) (logand b 65535))))))))
|
|
213
|
|
214 (defun xselect-convert-to-lineno (selection type value)
|
|
215 (let (a b buf tmp)
|
|
216 (cond ((cond ((and (consp value)
|
|
217 (markerp (car value))
|
|
218 (markerp (cdr value)))
|
|
219 (setq a (marker-position (car value))
|
|
220 b (marker-position (cdr value))
|
|
221 buf (marker-buffer (car value))))
|
6442
|
222 ((overlayp value)
|
|
223 (setq buf (overlay-buffer value)
|
|
224 a (overlay-start value)
|
|
225 b (overlay-end value)))
|
2234
|
226 )
|
|
227 (save-excursion
|
|
228 (set-buffer buf)
|
|
229 (setq a (count-lines 1 a)
|
|
230 b (count-lines 1 b)))
|
|
231 (if (< b a) (setq tmp a a b b tmp))
|
|
232 (cons 'SPAN
|
|
233 (vector (cons (ash a -16) (logand a 65535))
|
|
234 (cons (ash b -16) (logand b 65535))))))))
|
|
235
|
|
236 (defun xselect-convert-to-colno (selection type value)
|
|
237 (let (a b buf tmp)
|
|
238 (cond ((cond ((and (consp value)
|
|
239 (markerp (car value))
|
|
240 (markerp (cdr value)))
|
|
241 (setq a (car value)
|
|
242 b (cdr value)
|
|
243 buf (marker-buffer a)))
|
6442
|
244 ((overlayp value)
|
|
245 (setq buf (overlay-buffer value)
|
|
246 a (overlay-start value)
|
|
247 b (overlay-end value)))
|
2234
|
248 )
|
|
249 (save-excursion
|
|
250 (set-buffer buf)
|
|
251 (goto-char a)
|
|
252 (setq a (current-column))
|
|
253 (goto-char b)
|
|
254 (setq b (current-column)))
|
|
255 (if (< b a) (setq tmp a a b b tmp))
|
|
256 (cons 'SPAN
|
|
257 (vector (cons (ash a -16) (logand a 65535))
|
|
258 (cons (ash b -16) (logand b 65535))))))))
|
|
259
|
|
260 (defun xselect-convert-to-os (selection type size)
|
|
261 (symbol-name system-type))
|
|
262
|
|
263 (defun xselect-convert-to-host (selection type size)
|
|
264 (system-name))
|
|
265
|
|
266 (defun xselect-convert-to-user (selection type size)
|
|
267 (user-full-name))
|
|
268
|
|
269 (defun xselect-convert-to-class (selection type size)
|
2879
|
270 "Emacs")
|
2234
|
271
|
|
272 ;; We do not try to determine the name Emacs was invoked with,
|
|
273 ;; because it is not clean for a program's behavior to depend on that.
|
|
274 (defun xselect-convert-to-name (selection type size)
|
|
275 "emacs")
|
|
276
|
|
277 (defun xselect-convert-to-integer (selection type value)
|
|
278 (and (integerp value)
|
|
279 (cons (ash value -16) (logand value 65535))))
|
|
280
|
|
281 (defun xselect-convert-to-atom (selection type value)
|
|
282 (and (symbolp value) value))
|
|
283
|
|
284 (defun xselect-convert-to-identity (selection type value) ; used internally
|
|
285 (vector value))
|
|
286
|
|
287 (setq selection-converter-alist
|
|
288 '((TEXT . xselect-convert-to-string)
|
|
289 (STRING . xselect-convert-to-string)
|
|
290 (TARGETS . xselect-convert-to-targets)
|
|
291 (LENGTH . xselect-convert-to-length)
|
|
292 (DELETE . xselect-convert-to-delete)
|
|
293 (FILE_NAME . xselect-convert-to-filename)
|
|
294 (CHARACTER_POSITION . xselect-convert-to-charpos)
|
|
295 (LINE_NUMBER . xselect-convert-to-lineno)
|
|
296 (COLUMN_NUMBER . xselect-convert-to-colno)
|
|
297 (OWNER_OS . xselect-convert-to-os)
|
|
298 (HOST_NAME . xselect-convert-to-host)
|
|
299 (USER . xselect-convert-to-user)
|
|
300 (CLASS . xselect-convert-to-class)
|
|
301 (NAME . xselect-convert-to-name)
|
|
302 (ATOM . xselect-convert-to-atom)
|
|
303 (INTEGER . xselect-convert-to-integer)
|
|
304 (_EMACS_INTERNAL . xselect-convert-to-identity)
|
|
305 ))
|
|
306
|
|
307 (provide 'select)
|
|
308
|
|
309 ;;; select.el ends here.
|