428
|
1 ;;; select.el --- Lisp interface to windows selections.
|
|
2
|
|
3 ;; Copyright (C) 1998 Andy Piper.
|
|
4 ;; Copyright (C) 1990, 1997 Free Software Foundation, Inc.
|
|
5 ;; Copyright (C) 1995 Sun Microsystems.
|
|
6
|
|
7 ;; Maintainer: XEmacs Development Team
|
|
8 ;; Keywords: extensions, dumped
|
|
9
|
|
10 ;; This file is part of XEmacs.
|
|
11
|
|
12 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
13 ;; under the terms of the GNU General Public License as published by
|
|
14 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
15 ;; any later version.
|
|
16
|
|
17 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
20 ;; General Public License for more details.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
444
|
23 ;; along with XEmacs; see the file COPYING. If not, write to the
|
428
|
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
25 ;; Boston, MA 02111-1307, USA.
|
|
26
|
|
27 ;;; Synched up with: Not in FSF
|
|
28
|
|
29 ;;; Commentary:
|
|
30
|
444
|
31 ;; This file is dumped with XEmacs
|
428
|
32
|
|
33 ;;; Code:
|
|
34
|
|
35 (defvar selected-text-type
|
|
36 (if (featurep 'mule) '(COMPOUND_TEXT STRING) 'STRING)
|
|
37 "The type atom used to obtain selections from the X server.
|
|
38 Can be either a valid X selection data type, or a list of such types.
|
|
39 COMPOUND_TEXT and STRING are the most commonly used data types.
|
|
40 If a list is provided, the types are tried in sequence until
|
|
41 there is a successful conversion.")
|
|
42
|
444
|
43 (defvar selection-sets-clipboard nil
|
428
|
44 "Controls the selection's relationship to the clipboard.
|
|
45 When non-nil, any operation that sets the primary selection will also
|
|
46 set the clipboard.")
|
|
47
|
|
48 (defun copy-primary-selection ()
|
|
49 "Copy the selection to the Clipboard and the kill ring."
|
|
50 (interactive)
|
|
51 (and (console-on-window-system-p)
|
|
52 (cut-copy-clear-internal 'copy)))
|
|
53
|
|
54 (defun kill-primary-selection ()
|
|
55 "Copy the selection to the Clipboard and the kill ring, then delete it."
|
|
56 (interactive "*")
|
|
57 (and (console-on-window-system-p)
|
|
58 (cut-copy-clear-internal 'cut)))
|
|
59
|
|
60 (defun delete-primary-selection ()
|
|
61 "Delete the selection without copying it to the Clipboard or the kill ring."
|
|
62 (interactive "*")
|
|
63 (and (console-on-window-system-p)
|
|
64 (cut-copy-clear-internal 'clear)))
|
|
65
|
|
66 (defun yank-clipboard-selection ()
|
|
67 "Insert the current Clipboard selection at point."
|
|
68 (interactive "*")
|
|
69 (when (console-on-window-system-p)
|
|
70 (setq last-command nil)
|
|
71 (setq this-command 'yank) ; so that yank-pop works.
|
|
72 (let ((clip (get-clipboard)))
|
|
73 (or clip (error "there is no clipboard selection"))
|
|
74 (push-mark)
|
|
75 (insert clip))))
|
|
76
|
|
77 (defun get-clipboard ()
|
829
|
78 "Return text pasted to the clipboard.
|
843
|
79 Not suitable for `interprogram-paste-function', use `get-clipboard-foreign'."
|
|
80 (get-selection 'CLIPBOARD))
|
|
81
|
|
82 (defun get-clipboard-foreign ()
|
|
83 "Return text pasted to the clipboard by another program.
|
829
|
84 See `interprogram-paste-function' for more information."
|
843
|
85 (get-selection-foreign 'CLIPBOARD))
|
428
|
86
|
|
87 (define-device-method get-cutbuffer
|
|
88 "Return the value of one of the cut buffers.
|
|
89 This will do nothing under anything other than X.")
|
|
90
|
|
91 (defun get-selection-no-error (&optional type data-type)
|
442
|
92 "Return the value of a window-system selection.
|
428
|
93 The argument TYPE (default `PRIMARY') says which selection,
|
|
94 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule)
|
444
|
95 says how to convert the data. Returns NIL if there is no selection."
|
442
|
96 (condition-case nil (get-selection type data-type) (t nil)))
|
428
|
97
|
|
98 (defun get-selection (&optional type data-type)
|
843
|
99 "Return the value of a window-system selection.
|
428
|
100 The argument TYPE (default `PRIMARY') says which selection,
|
|
101 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule)
|
829
|
102 says how to convert the data. If there is no selection an error is signalled.
|
843
|
103 Not suitable in a `interprogram-paste-function', q.v."
|
428
|
104 (or type (setq type 'PRIMARY))
|
|
105 (or data-type (setq data-type selected-text-type))
|
829
|
106 (if (consp data-type)
|
|
107 (condition-case err
|
|
108 (get-selection-internal type (car data-type))
|
|
109 (selection-conversion-error
|
|
110 (if (cdr data-type)
|
|
111 (get-selection type (cdr data-type))
|
|
112 (signal (car err) (cdr err)))))
|
|
113 (get-selection-internal type data-type)))
|
428
|
114
|
|
115 ;; FSFmacs calls this `x-set-selection', and reverses the
|
442
|
116 ;; first two arguments (duh ...). This order is more logical.
|
|
117 (defun own-selection (data &optional type how-to-add data-type)
|
|
118 "Make a window-system selection of type TYPE and value DATA.
|
428
|
119 The argument TYPE (default `PRIMARY') says which selection,
|
442
|
120 and DATA specifies the contents. DATA may be any lisp data type
|
|
121 that can be converted using the function corresponding to DATA-TYPE
|
|
122 in `select-converter-alist'---strings are the usual choice, but
|
|
123 other types may be permissible depending on the DATA-TYPE parameter
|
|
124 (if DATA-TYPE is not supplied, the default behavior is window
|
|
125 system specific, but strings are always accepted).
|
|
126 HOW-TO-ADD may be any of the following:
|
|
127
|
|
128 'replace-all or nil -- replace all data in the selection.
|
|
129 'replace-existing -- replace data for specified DATA-TYPE only.
|
|
130 'append or t -- append data to existing DATA-TYPE data.
|
|
131
|
|
132 DATA-TYPE is the window-system specific data type identifier
|
|
133 (see `register-selection-data-type' for more information).
|
428
|
134
|
|
135 The selection may also be a cons of two markers pointing to the same buffer,
|
|
136 or an overlay. In these cases, the selection is considered to be the text
|
442
|
137 between the markers *at whatever time the selection is examined* (note
|
|
138 that the window system clipboard does not necessarily duplicate this
|
|
139 behavior - it doesn't on mswindows for example).
|
428
|
140 Thus, editing done in the buffer after you specify the selection
|
|
141 can alter the effective value of the selection.
|
|
142
|
|
143 The data may also be a vector of valid non-vector selection values.
|
|
144
|
|
145 Interactively, the text of the region is used as the selection value."
|
|
146 (interactive (if (not current-prefix-arg)
|
|
147 (list (read-string "Store text for pasting: "))
|
|
148 (list (substring (region-beginning) (region-end)))))
|
442
|
149 ;; calling own-selection-internal will mess this up, so preserve it.
|
|
150 (let ((zmacs-region-stays zmacs-region-stays))
|
|
151 ;FSFmacs huh?? It says:
|
|
152 ;; "This is for temporary compatibility with pre-release Emacs 19."
|
|
153 ;(if (stringp type)
|
|
154 ; (setq type (intern type)))
|
|
155 (or type (setq type 'PRIMARY))
|
|
156 (if (null data)
|
|
157 (disown-selection-internal type)
|
|
158 (own-selection-internal type data how-to-add data-type)
|
|
159 (when (and (eq type 'PRIMARY)
|
|
160 selection-sets-clipboard)
|
|
161 (own-selection-internal 'CLIPBOARD data how-to-add data-type)))
|
|
162 (cond ((eq type 'PRIMARY)
|
|
163 (setq primary-selection-extent
|
|
164 (select-make-extent-for-selection
|
|
165 data primary-selection-extent)))
|
|
166 ((eq type 'SECONDARY)
|
|
167 (setq secondary-selection-extent
|
|
168 (select-make-extent-for-selection
|
|
169 data secondary-selection-extent)))))
|
|
170 ;; zmacs-region-stays is for commands, not low-level functions.
|
|
171 ;; when behaving as the latter, we better not set it, or we will
|
|
172 ;; cause unwanted sticky-region behavior in kill-region and friends.
|
|
173 (if (interactive-p)
|
|
174 (setq zmacs-region-stays t))
|
428
|
175 data)
|
|
176
|
|
177 (defun dehilight-selection (selection)
|
|
178 "for use as a value of `lost-selection-hooks'."
|
|
179 (cond ((eq selection 'PRIMARY)
|
|
180 (if primary-selection-extent
|
|
181 (let ((inhibit-quit t))
|
|
182 (if (consp primary-selection-extent)
|
|
183 (mapcar 'delete-extent primary-selection-extent)
|
|
184 (delete-extent primary-selection-extent))
|
|
185 (setq primary-selection-extent nil)))
|
|
186 (if zmacs-regions (zmacs-deactivate-region)))
|
|
187 ((eq selection 'SECONDARY)
|
|
188 (if secondary-selection-extent
|
|
189 (let ((inhibit-quit t))
|
|
190 (if (consp secondary-selection-extent)
|
|
191 (mapcar 'delete-extent secondary-selection-extent)
|
|
192 (delete-extent secondary-selection-extent))
|
|
193 (setq secondary-selection-extent nil)))))
|
|
194 nil)
|
|
195
|
|
196 (setq lost-selection-hooks 'dehilight-selection)
|
|
197
|
442
|
198 (defun own-clipboard (string &optional push)
|
|
199 "Paste the given string to the window system Clipboard.
|
|
200 See `interprogram-cut-function' for more information."
|
428
|
201 (own-selection string 'CLIPBOARD))
|
|
202
|
|
203 (defun disown-selection (&optional secondary-p)
|
487
|
204 "Assuming we own the selection, disown it.
|
|
205 With an argument, discard the secondary selection instead of the
|
|
206 primary selection."
|
428
|
207 (disown-selection-internal (if secondary-p 'SECONDARY 'PRIMARY))
|
|
208 (when (and selection-sets-clipboard
|
|
209 (or (not secondary-p)
|
|
210 (eq secondary-p 'PRIMARY)
|
|
211 (eq secondary-p 'CLIPBOARD)))
|
|
212 (disown-selection-internal 'CLIPBOARD)))
|
|
213
|
|
214 ;; selections and active regions
|
|
215
|
|
216 ;; If and only if zmacs-regions is true:
|
|
217
|
|
218 ;; When a mark is pushed and the region goes into the "active" state, we
|
|
219 ;; assert it as the Primary selection. This causes it to be hilighted.
|
|
220 ;; When the region goes into the "inactive" state, we disown the Primary
|
|
221 ;; selection, causing the region to be dehilighted.
|
|
222
|
|
223 ;; Note that it is possible for the region to be in the "active" state
|
|
224 ;; and not be hilighted, if it is in the active state and then some other
|
|
225 ;; application asserts the selection. This is probably not a big deal.
|
|
226
|
|
227 (defun activate-region-as-selection ()
|
487
|
228 (cond (mouse-track-rectangle-p (mouse-track-activate-rectangular-selection))
|
|
229 ((marker-buffer (mark-marker t))
|
|
230 (own-selection (cons (point-marker t) (mark-marker t))))))
|
428
|
231
|
|
232 (defvar primary-selection-extent nil
|
|
233 "The extent of the primary selection; don't use this.")
|
|
234
|
|
235 (defvar secondary-selection-extent nil
|
|
236 "The extent of the secondary selection; don't use this.")
|
|
237
|
|
238 (defun select-make-extent-for-selection (selection previous-extent)
|
|
239 ;; Given a selection, this makes an extent in the buffer which holds that
|
|
240 ;; selection, for highlighting purposes. If the selection isn't associated
|
|
241 ;; with a buffer, this does nothing.
|
|
242 (let ((buffer nil)
|
|
243 (valid (and (extentp previous-extent)
|
|
244 (extent-object previous-extent)
|
|
245 (buffer-live-p (extent-object previous-extent))))
|
|
246 start end)
|
|
247 (cond ((stringp selection)
|
|
248 ;; if we're selecting a string, lose the previous extent used
|
|
249 ;; to highlight the selection.
|
|
250 (setq valid nil))
|
|
251 ((consp selection)
|
|
252 (setq start (min (car selection) (cdr selection))
|
|
253 end (max (car selection) (cdr selection))
|
|
254 valid (and valid
|
|
255 (eq (marker-buffer (car selection))
|
|
256 (extent-object previous-extent)))
|
|
257 buffer (marker-buffer (car selection))))
|
|
258 ((extentp selection)
|
|
259 (setq start (extent-start-position selection)
|
|
260 end (extent-end-position selection)
|
|
261 valid (and valid
|
|
262 (eq (extent-object selection)
|
|
263 (extent-object previous-extent)))
|
|
264 buffer (extent-object selection)))
|
|
265 (t
|
|
266 (signal 'error (list "invalid selection" selection))))
|
|
267
|
|
268 (if valid
|
|
269 nil
|
|
270 (condition-case ()
|
|
271 (if (listp previous-extent)
|
|
272 (mapcar 'delete-extent previous-extent)
|
|
273 (delete-extent previous-extent))
|
|
274 (error nil)))
|
|
275
|
|
276 (if (not buffer)
|
|
277 ;; string case
|
|
278 nil
|
|
279 ;; normal case
|
|
280 (if valid
|
|
281 (set-extent-endpoints previous-extent start end)
|
|
282 (setq previous-extent (make-extent start end buffer))
|
|
283
|
|
284 ;; Make the extent be closed on the right, which means that if
|
|
285 ;; characters are inserted exactly at the end of the extent, the
|
|
286 ;; extent will grow to cover them. This is important for shell
|
|
287 ;; buffers - suppose one makes a selection, and one end is at
|
|
288 ;; point-max. If the shell produces output, that marker will remain
|
|
289 ;; at point-max (its position will increase). So it's important that
|
|
290 ;; the extent exhibit the same behavior, lest the region covered by
|
|
291 ;; the extent (the visual indication), and the region between point
|
|
292 ;; and mark (the actual selection value) become different!
|
|
293 (set-extent-property previous-extent 'end-open nil)
|
|
294
|
|
295 (cond
|
|
296 (mouse-track-rectangle-p
|
|
297 (setq previous-extent (list previous-extent))
|
|
298 (default-mouse-track-next-move-rect start end previous-extent)
|
|
299 ))
|
|
300 previous-extent))))
|
|
301
|
|
302 (defun valid-simple-selection-p (data)
|
442
|
303 "An obsolete function that tests whether something was a valid simple
|
|
304 selection using the old XEmacs selection support. You shouldn't use this
|
|
305 any more, because just about anything could be a valid selection now."
|
428
|
306 (or (stringp data)
|
|
307 ;FSFmacs huh?? (symbolp data)
|
|
308 (integerp data)
|
|
309 (and (consp data)
|
|
310 (integerp (car data))
|
|
311 (or (integerp (cdr data))
|
|
312 (and (consp (cdr data))
|
|
313 (integerp (car (cdr data))))))
|
|
314 (extentp data)
|
|
315 (and (consp data)
|
|
316 (markerp (car data))
|
|
317 (markerp (cdr data))
|
|
318 (marker-buffer (car data))
|
|
319 (marker-buffer (cdr data))
|
|
320 (eq (marker-buffer (car data))
|
|
321 (marker-buffer (cdr data)))
|
|
322 (buffer-live-p (marker-buffer (car data)))
|
|
323 (buffer-live-p (marker-buffer (cdr data))))))
|
|
324
|
|
325 (defun cut-copy-clear-internal (mode)
|
|
326 (or (memq mode '(cut copy clear)) (error "unkown mode %S" mode))
|
|
327 (or (selection-owner-p)
|
|
328 (error "XEmacs does not own the primary selection"))
|
|
329 (setq last-command nil)
|
|
330 (or primary-selection-extent
|
|
331 (error "the primary selection is not an extent?"))
|
|
332 (save-excursion
|
|
333 (let (rect-p b s e)
|
|
334 (cond
|
|
335 ((consp primary-selection-extent)
|
|
336 (setq rect-p t
|
|
337 b (extent-object (car primary-selection-extent))
|
|
338 s (extent-start-position (car primary-selection-extent))
|
|
339 e (extent-end-position (car (reverse primary-selection-extent)))))
|
|
340 (t
|
|
341 (setq rect-p nil
|
|
342 b (extent-object primary-selection-extent)
|
|
343 s (extent-start-position primary-selection-extent)
|
|
344 e (extent-end-position primary-selection-extent))))
|
|
345 (set-buffer b)
|
|
346 (cond ((memq mode '(cut copy))
|
|
347 (if rect-p
|
|
348 (progn
|
|
349 ;; why is killed-rectangle free? Is it used somewhere?
|
|
350 ;; should it be defvarred?
|
|
351 (setq killed-rectangle (extract-rectangle s e))
|
|
352 (kill-new (mapconcat #'identity killed-rectangle "\n")))
|
|
353 (copy-region-as-kill s e))
|
|
354 ;; Maybe killing doesn't own clipboard. Make sure it happens.
|
|
355 ;; This memq is kind of grody, because they might have done it
|
|
356 ;; some other way, but owning the clipboard twice in that case
|
|
357 ;; wouldn't actually hurt anything.
|
|
358 (or (and (consp kill-hooks) (memq 'own-clipboard kill-hooks))
|
|
359 (own-clipboard (car kill-ring)))))
|
|
360 (cond ((memq mode '(cut clear))
|
|
361 (if rect-p
|
|
362 (delete-rectangle s e)
|
|
363 (delete-region s e))))
|
|
364 (disown-selection nil)
|
|
365 )))
|
|
366
|
442
|
367
|
428
|
368 ;;; Functions to convert the selection into various other selection
|
442
|
369 ;;; types.
|
|
370
|
|
371 ;; These next three functions get called by C code...
|
|
372 (defun select-convert-in (selection type value)
|
|
373 "Attempt to convert the specified external VALUE to the specified DATA-TYPE,
|
|
374 for the specified SELECTION. Return nil if this is impossible, or a
|
|
375 suitable internal representation otherwise."
|
|
376 (when value
|
|
377 (let ((handler-fn (cdr (assq type selection-converter-in-alist))))
|
|
378 (when handler-fn
|
|
379 (apply handler-fn (list selection type value))))))
|
428
|
380
|
442
|
381 (defun select-convert-out (selection type value)
|
|
382 "Attempt to convert the specified internal VALUE for the specified DATA-TYPE
|
|
383 and SELECTION. Return nil if this is impossible, or a suitable external
|
|
384 representation otherwise."
|
|
385 (when value
|
|
386 (let ((handler-fn (cdr (assq type selection-converter-out-alist))))
|
|
387 (when handler-fn
|
|
388 (apply handler-fn (list selection type value))))))
|
|
389
|
|
390 (defun select-coerce (selection type value)
|
|
391 "Attempt to convert the specified internal VALUE to a representation
|
|
392 suitable for return from `get-selection' in the specified DATA-TYPE. Return
|
|
393 nil if this is impossible, or a suitable representation otherwise."
|
|
394 (when value
|
|
395 (let ((handler-fn (cdr (assq type selection-coercion-alist))))
|
|
396 (when handler-fn
|
|
397 (apply handler-fn (list selection type value))))))
|
|
398
|
|
399 ;; The rest of the functions on this "page" are conversion handlers,
|
|
400 ;; append handlers and buffer-kill handlers.
|
428
|
401 (defun select-convert-to-text (selection type value)
|
|
402 (cond ((stringp value)
|
|
403 value)
|
|
404 ((extentp value)
|
|
405 (save-excursion
|
|
406 (set-buffer (extent-object value))
|
|
407 (save-restriction
|
|
408 (widen)
|
|
409 (buffer-substring (extent-start-position value)
|
|
410 (extent-end-position value)))))
|
|
411 ((and (consp value)
|
|
412 (markerp (car value))
|
|
413 (markerp (cdr value)))
|
|
414 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
|
|
415 (signal 'error
|
|
416 (list "markers must be in the same buffer"
|
|
417 (car value) (cdr value))))
|
|
418 (save-excursion
|
|
419 (set-buffer (or (marker-buffer (car value))
|
|
420 (error "selection is in a killed buffer")))
|
|
421 (save-restriction
|
|
422 (widen)
|
|
423 (buffer-substring (car value) (cdr value)))))
|
|
424 (t nil)))
|
|
425
|
442
|
426 (defun select-coerce-to-text (selection type value)
|
|
427 (select-convert-to-text selection type value))
|
|
428
|
|
429 (defun select-convert-from-text (selection type value)
|
|
430 (when (stringp value)
|
|
431 value))
|
|
432
|
428
|
433 (defun select-convert-to-string (selection type value)
|
|
434 (let ((outval (select-convert-to-text selection type value)))
|
442
|
435 ;; force the string to be not in Compound Text format. This grubby
|
|
436 ;; hack will go soon, to be replaced by a more general mechanism.
|
428
|
437 (if (stringp outval)
|
|
438 (cons 'STRING outval)
|
|
439 outval)))
|
|
440
|
|
441 (defun select-convert-to-compound-text (selection type value)
|
|
442 ;; converts to compound text automatically
|
|
443 (select-convert-to-text selection type value))
|
|
444
|
|
445 (defun select-convert-to-length (selection type value)
|
|
446 (let ((value
|
|
447 (cond ((stringp value)
|
|
448 (length value))
|
|
449 ((extentp value)
|
|
450 (extent-length value))
|
|
451 ((and (consp value)
|
|
452 (markerp (car value))
|
|
453 (markerp (cdr value)))
|
|
454 (or (eq (marker-buffer (car value))
|
|
455 (marker-buffer (cdr value)))
|
|
456 (signal 'error
|
|
457 (list "markers must be in the same buffer"
|
|
458 (car value) (cdr value))))
|
|
459 (abs (- (car value) (cdr value)))))))
|
|
460 (if value ; force it to be in 32-bit format.
|
|
461 (cons (ash value -16) (logand value 65535))
|
|
462 nil)))
|
|
463
|
442
|
464 (defun select-convert-from-length (selection type value)
|
|
465 (select-convert-to-length selection type value))
|
|
466
|
428
|
467 (defun select-convert-to-targets (selection type value)
|
|
468 ;; return a vector of atoms, but remove duplicates first.
|
|
469 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
|
|
470 (rest all))
|
|
471 (while rest
|
|
472 (cond ((memq (car rest) (cdr rest))
|
|
473 (setcdr rest (delq (car rest) (cdr rest))))
|
|
474 (t
|
|
475 (setq rest (cdr rest)))))
|
|
476 (apply 'vector all)))
|
|
477
|
|
478 (defun select-convert-to-delete (selection type value)
|
|
479 (disown-selection-internal selection)
|
|
480 ;; A return value of nil means that we do not know how to do this conversion,
|
|
481 ;; and replies with an "error". A return value of NULL means that we have
|
|
482 ;; done the conversion (and any side-effects) but have no value to return.
|
|
483 'NULL)
|
|
484
|
|
485 (defun select-convert-to-filename (selection type value)
|
|
486 (cond ((extentp value)
|
|
487 (buffer-file-name (or (extent-object value)
|
|
488 (error "selection is in a killed buffer"))))
|
|
489 ((and (consp value)
|
|
490 (markerp (car value))
|
|
491 (markerp (cdr value)))
|
|
492 (buffer-file-name (or (marker-buffer (car value))
|
|
493 (error "selection is in a killed buffer"))))
|
|
494 (t nil)))
|
|
495
|
442
|
496 (defun select-convert-from-filename (selection type value)
|
|
497 (when (stringp value)
|
|
498 value))
|
|
499
|
428
|
500 (defun select-convert-to-charpos (selection type value)
|
|
501 (let (a b tmp)
|
|
502 (cond ((cond ((extentp value)
|
|
503 (setq a (extent-start-position value)
|
|
504 b (extent-end-position value)))
|
|
505 ((and (consp value)
|
|
506 (markerp (car value))
|
|
507 (markerp (cdr value)))
|
|
508 (setq a (car value)
|
|
509 b (cdr value))))
|
|
510 (setq a (1- a) b (1- b)) ; zero-based
|
|
511 (if (< b a) (setq tmp a a b b tmp))
|
|
512 (cons 'SPAN
|
|
513 (vector (cons (ash a -16) (logand a 65535))
|
|
514 (cons (ash b -16) (logand b 65535))))))))
|
|
515
|
|
516 (defun select-convert-to-lineno (selection type value)
|
|
517 (let (a b buf tmp)
|
|
518 (cond ((cond ((extentp value)
|
|
519 (setq buf (extent-object value)
|
|
520 a (extent-start-position value)
|
|
521 b (extent-end-position value)))
|
|
522 ((and (consp value)
|
|
523 (markerp (car value))
|
|
524 (markerp (cdr value)))
|
|
525 (setq a (marker-position (car value))
|
|
526 b (marker-position (cdr value))
|
|
527 buf (marker-buffer (car value)))))
|
|
528 (save-excursion
|
|
529 (set-buffer buf)
|
|
530 (save-restriction
|
|
531 (widen)
|
|
532 (goto-char a)
|
|
533 (beginning-of-line)
|
|
534 (setq a (1+ (count-lines 1 (point))))
|
|
535 (goto-char b)
|
|
536 (beginning-of-line)
|
|
537 (setq b (1+ (count-lines 1 (point))))))
|
|
538 (if (< b a) (setq tmp a a b b tmp))
|
|
539 (cons 'SPAN
|
|
540 (vector (cons (ash a -16) (logand a 65535))
|
|
541 (cons (ash b -16) (logand b 65535))))))))
|
|
542
|
|
543 (defun select-convert-to-colno (selection type value)
|
|
544 (let (a b buf tmp)
|
|
545 (cond ((cond ((extentp value)
|
|
546 (setq buf (extent-object value)
|
|
547 a (extent-start-position value)
|
|
548 b (extent-end-position value)))
|
|
549 ((and (consp value)
|
|
550 (markerp (car value))
|
|
551 (markerp (cdr value)))
|
|
552 (setq a (car value)
|
|
553 b (cdr value)
|
|
554 buf (marker-buffer a))))
|
|
555 (save-excursion
|
|
556 (set-buffer buf)
|
|
557 (goto-char a)
|
|
558 (setq a (current-column))
|
|
559 (goto-char b)
|
|
560 (setq b (current-column)))
|
|
561 (if (< b a) (setq tmp a a b b tmp))
|
|
562 (cons 'SPAN
|
|
563 (vector (cons (ash a -16) (logand a 65535))
|
|
564 (cons (ash b -16) (logand b 65535))))))))
|
|
565
|
|
566 (defun select-convert-to-sourceloc (selection type value)
|
|
567 (let (a b buf file-name tmp)
|
|
568 (cond ((cond ((extentp value)
|
|
569 (setq buf (or (extent-object value)
|
|
570 (error "selection is in a killed buffer"))
|
|
571 a (extent-start-position value)
|
|
572 b (extent-end-position value)
|
|
573 file-name (buffer-file-name buf)))
|
|
574 ((and (consp value)
|
|
575 (markerp (car value))
|
|
576 (markerp (cdr value)))
|
|
577 (setq a (marker-position (car value))
|
|
578 b (marker-position (cdr value))
|
|
579 buf (or (marker-buffer (car value))
|
|
580 (error "selection is in a killed buffer"))
|
|
581 file-name (buffer-file-name buf))))
|
|
582 (save-excursion
|
|
583 (set-buffer buf)
|
|
584 (save-restriction
|
|
585 (widen)
|
|
586 (goto-char a)
|
|
587 (beginning-of-line)
|
|
588 (setq a (1+ (count-lines 1 (point))))
|
|
589 (goto-char b)
|
|
590 (beginning-of-line)
|
|
591 (setq b (1+ (count-lines 1 (point))))))
|
|
592 (if (< b a) (setq tmp a a b b tmp))
|
|
593 (format "%s:%d" file-name a)))))
|
|
594
|
|
595 (defun select-convert-to-os (selection type size)
|
|
596 (symbol-name system-type))
|
|
597
|
|
598 (defun select-convert-to-host (selection type size)
|
|
599 (system-name))
|
|
600
|
|
601 (defun select-convert-to-user (selection type size)
|
|
602 (user-full-name))
|
|
603
|
|
604 (defun select-convert-to-class (selection type size)
|
442
|
605 (symbol-value 'x-emacs-application-class))
|
428
|
606
|
|
607 ;; We do not try to determine the name Emacs was invoked with,
|
|
608 ;; because it is not clean for a program's behavior to depend on that.
|
|
609 (defun select-convert-to-name (selection type size)
|
|
610 ;invocation-name
|
|
611 "xemacs")
|
|
612
|
|
613 (defun select-convert-to-integer (selection type value)
|
|
614 (and (integerp value)
|
|
615 (cons (ash value -16) (logand value 65535))))
|
|
616
|
442
|
617 ;; Can convert from the following integer representations
|
|
618 ;;
|
|
619 ;; integer
|
|
620 ;; (integer . integer)
|
|
621 ;; (integer integer)
|
|
622 ;; (list [integer|(integer . integer)]*)
|
|
623 ;; (vector [integer|(integer . integer)]*)
|
|
624 ;;
|
|
625 ;; Cons'd integers get cleaned up a little.
|
|
626
|
|
627 (defun select-convert-from-integer (selection type value)
|
|
628 (cond ((integerp value) ; Integer
|
|
629 value)
|
444
|
630
|
442
|
631 ((and (consp value) ; (integer . integer)
|
|
632 (integerp (car value))
|
|
633 (integerp (cdr value)))
|
|
634 (if (eq (car value) 0)
|
|
635 (cdr value)
|
|
636 (if (and (eq (car value) -1)
|
|
637 (< (cdr value) 0))
|
|
638 (cdr value)
|
|
639 value)))
|
444
|
640
|
442
|
641 ((and (listp value) ; (integer integer)
|
|
642 (eq (length value) 2)
|
|
643 (integerp (car value))
|
|
644 (integerp (cadr value)))
|
|
645 (if (eq (car value) 0)
|
|
646 (cadr value)
|
|
647 (if (and (eq (car value) -1)
|
|
648 (< (cdr value) 0))
|
|
649 (- (cadr value))
|
|
650 (cons (car value) (cadr value)))))
|
444
|
651
|
442
|
652 ((listp value) ; list
|
|
653 (if (cdr value)
|
|
654 (mapcar '(lambda (x)
|
|
655 (select-convert-from-integer selection type x))
|
|
656 value)
|
|
657 (select-convert-from-integer selection type (car value))))
|
444
|
658
|
442
|
659 ((vectorp value) ; vector
|
|
660 (if (eq (length value) 1)
|
|
661 (select-convert-from-integer selection type (aref value 0))
|
|
662 (mapvector '(lambda (x)
|
|
663 (select-convert-from-integer selection type x))
|
|
664 value)))
|
444
|
665
|
442
|
666 (t nil)
|
|
667 ))
|
|
668
|
428
|
669 (defun select-convert-to-atom (selection type value)
|
|
670 (and (symbolp value) value))
|
|
671
|
442
|
672 ;;; CF_xxx conversions
|
|
673 (defun select-convert-from-cf-text (selection type value)
|
771
|
674 (let ((value (decode-coding-string value 'mswindows-multibyte)))
|
|
675 (replace-in-string (if (string-match "\0" value)
|
|
676 (substring value 0 (match-beginning 0))
|
|
677 value)
|
|
678 "\\(\r\n\\|\n\r\\)" "\n" t)))
|
|
679
|
|
680 (defun select-convert-from-cf-unicodetext (selection type value)
|
|
681 (let ((value (decode-coding-string value 'mswindows-unicode)))
|
|
682 (replace-in-string (if (string-match "\0" value)
|
|
683 (substring value 0 (match-beginning 0))
|
|
684 value)
|
|
685 "\\(\r\n\\|\n\r\\)" "\n" t)))
|
442
|
686
|
|
687 (defun select-convert-to-cf-text (selection type value)
|
|
688 (let ((text (select-convert-to-text selection type value)))
|
771
|
689 (encode-coding-string
|
|
690 (concat (replace-in-string text "\n" "\r\n" t) "\0")
|
|
691 'mswindows-multibyte)))
|
|
692
|
|
693 (defun select-convert-to-cf-unicodetext (selection type value)
|
|
694 (let ((text (select-convert-to-text selection type value)))
|
|
695 (encode-coding-string
|
|
696 (concat (replace-in-string text "\n" "\r\n" t) "\0")
|
|
697 'mswindows-unicode)))
|
442
|
698
|
|
699 ;;; Appenders
|
|
700 (defun select-append-to-text (selection type value1 value2)
|
|
701 (let ((text1 (select-convert-to-text selection 'STRING value1))
|
|
702 (text2 (select-convert-to-text selection 'STRING value2)))
|
|
703 (if (and text1 text2)
|
|
704 (concat text1 text2)
|
|
705 nil)))
|
|
706
|
|
707 (defun select-append-to-string (selection type value1 value2)
|
|
708 (select-append-to-text selection type value1 value2))
|
|
709
|
|
710 (defun select-append-to-compound-text (selection type value1 value2)
|
|
711 (select-append-to-text selection type value1 value2))
|
|
712
|
|
713 (defun select-append-to-cf-text (selection type value1 value2)
|
|
714 (let ((text1 (select-convert-from-cf-text selection 'CF_TEXT value1))
|
|
715 (text2 (select-convert-from-cf-text selection 'CF_TEXT value2)))
|
|
716 (if (and text1 text2)
|
|
717 (select-convert-to-cf-text selection type (concat text1 text2))
|
|
718 nil)))
|
428
|
719
|
771
|
720 (defun select-append-to-cf-unicodetext (selection type value1 value2)
|
|
721 (let ((text1 (select-convert-from-cf-unicodetext selection
|
|
722 'CF_UNICODETEXT value1))
|
|
723 (text2 (select-convert-from-cf-unicodetext selection
|
|
724 'CF_UNICODETEXT value2)))
|
|
725 (if (and text1 text2)
|
|
726 (select-convert-to-cf-unicodetext selection type (concat text1 text2))
|
|
727 nil)))
|
|
728
|
442
|
729 (defun select-append-default (selection type value1 value2)
|
|
730 ;; This appender gets used if the type is "nil" - i.e. default.
|
|
731 ;; It should probably have more cases implemented than it does - e.g.
|
|
732 ;; appending numbers to strings, etc...
|
|
733 (cond ((and (stringp value1) (stringp value2))
|
|
734 (select-append-to-string selection 'STRING value1 value2))
|
|
735 (t nil)))
|
|
736
|
|
737 ;;; Buffer kill handlers
|
|
738
|
|
739 (defun select-buffer-killed-default (selection type value buffer)
|
|
740 ;; This handler gets used if the type is "nil".
|
|
741 (cond ((extentp value)
|
|
742 (if (eq (extent-object value) buffer)
|
|
743 ; If this selection is on the clipboard, grab it quick
|
|
744 (when (eq selection 'CLIPBOARD)
|
|
745 (save-excursion
|
|
746 (set-buffer (extent-object value))
|
|
747 (save-restriction
|
|
748 (widen)
|
|
749 (buffer-substring (extent-start-position value)
|
|
750 (extent-end-position value)))))
|
|
751 value))
|
|
752 ((markerp value)
|
|
753 (unless (eq (marker-buffer value) buffer)
|
|
754 value))
|
|
755 ((and (consp value)
|
|
756 (markerp (car value))
|
|
757 (markerp (cdr value)))
|
|
758 (if (or (eq (marker-buffer (car value)) buffer)
|
|
759 (eq (marker-buffer (cdr value)) buffer))
|
|
760 ; If this selection is on the clipboard, grab it quick
|
|
761 (when (eq selection 'CLIPBOARD)
|
|
762 (save-excursion
|
|
763 (set-buffer (marker-buffer (car value)))
|
|
764 (save-restriction
|
|
765 (widen)
|
|
766 (buffer-substring (car value) (cdr value)))))
|
|
767 value))
|
|
768 (t value)))
|
|
769
|
|
770 (defun select-buffer-killed-text (selection type value buffer)
|
|
771 (select-buffer-killed-default selection type value buffer))
|
444
|
772
|
442
|
773 ;; Types listed in here can be selections of XEmacs
|
|
774 (setq selection-converter-out-alist
|
428
|
775 '((TEXT . select-convert-to-text)
|
|
776 (STRING . select-convert-to-string)
|
|
777 (COMPOUND_TEXT . select-convert-to-compound-text)
|
|
778 (TARGETS . select-convert-to-targets)
|
|
779 (LENGTH . select-convert-to-length)
|
|
780 (DELETE . select-convert-to-delete)
|
|
781 (FILE_NAME . select-convert-to-filename)
|
|
782 (CHARACTER_POSITION . select-convert-to-charpos)
|
|
783 (SOURCE_LOC . select-convert-to-sourceloc)
|
|
784 (LINE_NUMBER . select-convert-to-lineno)
|
|
785 (COLUMN_NUMBER . select-convert-to-colno)
|
|
786 (OWNER_OS . select-convert-to-os)
|
|
787 (HOST_NAME . select-convert-to-host)
|
|
788 (USER . select-convert-to-user)
|
|
789 (CLASS . select-convert-to-class)
|
|
790 (NAME . select-convert-to-name)
|
|
791 (ATOM . select-convert-to-atom)
|
|
792 (INTEGER . select-convert-to-integer)
|
442
|
793 (CF_TEXT . select-convert-to-cf-text)
|
771
|
794 (CF_UNICODETEXT . select-convert-to-cf-unicodetext)
|
442
|
795 ))
|
|
796
|
|
797 ;; Types listed here can be selections foreign to XEmacs
|
|
798 (setq selection-converter-in-alist
|
|
799 '(; Specific types that get handled by generic converters
|
|
800 (COMPOUND_TEXT . select-convert-from-text)
|
|
801 (SOURCE_LOC . select-convert-from-text)
|
|
802 (OWNER_OS . select-convert-from-text)
|
|
803 (HOST_NAME . select-convert-from-text)
|
|
804 (USER . select-convert-from-text)
|
|
805 (CLASS . select-convert-from-text)
|
|
806 (NAME . select-convert-from-text)
|
|
807 ; Generic types
|
|
808 (INTEGER . select-convert-from-integer)
|
|
809 (TEXT . select-convert-from-text)
|
|
810 (STRING . select-convert-from-text)
|
|
811 (LENGTH . select-convert-from-length)
|
|
812 (FILE_NAME . select-convert-from-filename)
|
|
813 (CF_TEXT . select-convert-from-cf-text)
|
771
|
814 (CF_UNICODETEXT . select-convert-from-cf-unicodetext)
|
428
|
815 ))
|
|
816
|
442
|
817 ;; Types listed here have special coercion functions that can munge
|
|
818 ;; other types. This can also be used to add special features - e.g.
|
|
819 ;; being able to pass a region or a cons of markers to own-selection,
|
|
820 ;; but getting the *current* text in the region back when calling
|
|
821 ;; get-selection.
|
|
822 ;;
|
|
823 ;; Any function listed in here *will be called* whenever a value of
|
|
824 ;; its type is retrieved from the internal selection cache, or when
|
|
825 ;; no suitable values could be found in which case XEmacs looks for
|
|
826 ;; values with types listed in selection-coercible-types.
|
|
827 (setq selection-coercion-alist
|
|
828 '((TEXT . select-coerce-to-text)
|
|
829 (STRING . select-coerce-to-text)
|
|
830 (COMPOUND_TEXT . select-coerce-to-text)
|
771
|
831 (CF_TEXT . select-coerce-to-text)
|
|
832 (CF_UNICODETEXT . select-coerce-to-text)
|
|
833 ))
|
442
|
834
|
|
835 ;; Types listed here can be appended by own-selection
|
|
836 (setq selection-appender-alist
|
|
837 '((nil . select-append-default)
|
|
838 (TEXT . select-append-to-text)
|
|
839 (STRING . select-append-to-string)
|
|
840 (COMPOUND_TEXT . select-append-to-compound-text)
|
|
841 (CF_TEXT . select-append-to-cf-text)
|
771
|
842 (CF_UNICODETEXT . select-append-to-cf-unicodetext)
|
442
|
843 ))
|
|
844
|
|
845 ;; Types listed here have buffer-kill handlers
|
|
846 (setq selection-buffer-killed-alist
|
|
847 '((nil . select-buffer-killed-default)
|
|
848 (TEXT . select-buffer-killed-text)
|
|
849 (STRING . select-buffer-killed-text)
|
|
850 (COMPOUND_TEXT . select-buffer-killed-text)
|
771
|
851 (CF_TEXT . select-buffer-killed-text)
|
|
852 (CF_UNICODETEXT . select-buffer-killed-text)
|
|
853 ))
|
442
|
854
|
|
855 ;; Lists of types that are coercible (can be converted to other types)
|
771
|
856 (setq selection-coercible-types '(TEXT STRING COMPOUND_TEXT CF_TEXT CF_UNICODETEXT))
|
442
|
857
|
428
|
858 ;;; select.el ends here
|