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