import xemacs-21.2.37
[chise/xemacs-chise.git.1] / lisp / select.el
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
23 ;; along with XEmacs; see the file COPYING.  If not, write to the
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
31 ;; This file is dumped with XEmacs
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
43 (defvar selection-sets-clipboard nil
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)
86   "Return the value of a window-system selection.
87 The argument TYPE (default `PRIMARY') says which selection,
88 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule)
89 says how to convert the data. Returns NIL if there is no selection."
90   (condition-case nil (get-selection type data-type) (t nil)))
91
92 (defun get-selection (&optional type data-type)
93   "Return the value of a window-system selection.
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
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.
114 The argument TYPE (default `PRIMARY') says which selection,
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).
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
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).
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)))))
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))
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
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."
196   (own-selection string 'CLIPBOARD))
197
198 (defun disown-selection (&optional secondary-p)
199   "Assuming we own the selection, disown it.  With an argument, discard the
200 secondary selection instead of the primary selection."
201   (disown-selection-internal (if secondary-p 'SECONDARY 'PRIMARY))
202   (when (and selection-sets-clipboard
203              (or (not secondary-p)
204                  (eq secondary-p 'PRIMARY)
205                  (eq secondary-p 'CLIPBOARD)))
206     (disown-selection-internal 'CLIPBOARD)))
207
208 ;; from x-init.el
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 ()
223   (if (marker-buffer (mark-marker t))
224       (own-selection (cons (point-marker t) (mark-marker t)))))
225
226 ; moved from x-select.el
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 ;; moved from x-select.el
298 (defun valid-simple-selection-p (data)
299   "An obsolete function that tests whether something was a valid simple
300 selection using the old XEmacs selection support. You shouldn't use this
301 any more, because just about anything could be a valid selection now."
302   (or (stringp data)
303       ;FSFmacs huh?? (symbolp data)
304       (integerp data)
305       (and (consp data)
306            (integerp (car data))
307            (or (integerp (cdr data))
308                (and (consp (cdr data))
309                     (integerp (car (cdr data))))))
310       (extentp data)
311       (and (consp data)
312            (markerp (car data))
313            (markerp (cdr data))
314            (marker-buffer (car data))
315            (marker-buffer (cdr data))
316            (eq (marker-buffer (car data))
317                (marker-buffer (cdr data)))
318            (buffer-live-p (marker-buffer (car data)))
319            (buffer-live-p (marker-buffer (cdr data))))))
320
321 (defun cut-copy-clear-internal (mode)
322   (or (memq mode '(cut copy clear)) (error "unkown mode %S" mode))
323   (or (selection-owner-p)
324       (error "XEmacs does not own the primary selection"))
325   (setq last-command nil)
326   (or primary-selection-extent
327       (error "the primary selection is not an extent?"))
328   (save-excursion
329     (let (rect-p b s e)
330       (cond
331        ((consp primary-selection-extent)
332         (setq rect-p t
333               b (extent-object (car primary-selection-extent))
334               s (extent-start-position (car primary-selection-extent))
335               e (extent-end-position (car (reverse primary-selection-extent)))))
336        (t
337         (setq rect-p nil
338               b (extent-object primary-selection-extent)
339               s (extent-start-position primary-selection-extent)
340               e (extent-end-position primary-selection-extent))))
341       (set-buffer b)
342       (cond ((memq mode '(cut copy))
343              (if rect-p
344                  (progn
345                    ;; why is killed-rectangle free?  Is it used somewhere?
346                    ;; should it be defvarred?
347                    (setq killed-rectangle (extract-rectangle s e))
348                    (kill-new (mapconcat #'identity killed-rectangle "\n")))
349                (copy-region-as-kill s e))
350              ;; Maybe killing doesn't own clipboard.  Make sure it happens.
351              ;; This memq is kind of grody, because they might have done it
352              ;; some other way, but owning the clipboard twice in that case
353              ;; wouldn't actually hurt anything.
354              (or (and (consp kill-hooks) (memq 'own-clipboard kill-hooks))
355                  (own-clipboard (car kill-ring)))))
356       (cond ((memq mode '(cut clear))
357              (if rect-p
358                  (delete-rectangle s e)
359                (delete-region s e))))
360       (disown-selection nil)
361       )))
362
363 \f
364 ;;; Functions to convert the selection into various other selection
365 ;;; types.
366
367 ;; These next three functions get called by C code...
368 (defun select-convert-in (selection type value)
369   "Attempt to convert the specified external VALUE to the specified DATA-TYPE,
370 for the specified SELECTION. Return nil if this is impossible, or a
371 suitable internal representation otherwise."
372   (when value
373     (let ((handler-fn (cdr (assq type selection-converter-in-alist))))
374       (when handler-fn
375         (apply handler-fn (list selection type value))))))
376
377 (defun select-convert-out (selection type value)
378   "Attempt to convert the specified internal VALUE for the specified DATA-TYPE
379 and SELECTION. Return nil if this is impossible, or a suitable external
380 representation otherwise."
381   (when value
382     (let ((handler-fn (cdr (assq type selection-converter-out-alist))))
383       (when handler-fn
384         (apply handler-fn (list selection type value))))))
385
386 (defun select-coerce (selection type value)
387   "Attempt to convert the specified internal VALUE to a representation
388 suitable for return from `get-selection' in the specified DATA-TYPE. Return
389 nil if this is impossible, or a suitable representation otherwise."
390   (when value
391     (let ((handler-fn (cdr (assq type selection-coercion-alist))))
392       (when handler-fn
393         (apply handler-fn (list selection type value))))))
394
395 ;; The rest of the functions on this "page" are conversion handlers,
396 ;; append handlers and buffer-kill handlers.
397 (defun select-convert-to-text (selection type value)
398   (cond ((stringp value)
399          value)
400         ((extentp value)
401          (save-excursion
402            (set-buffer (extent-object value))
403            (save-restriction
404              (widen)
405              (buffer-substring (extent-start-position value)
406                                (extent-end-position value)))))
407         ((and (consp value)
408               (markerp (car value))
409               (markerp (cdr value)))
410          (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
411              (signal 'error
412                      (list "markers must be in the same buffer"
413                            (car value) (cdr value))))
414          (save-excursion
415            (set-buffer (or (marker-buffer (car value))
416                            (error "selection is in a killed buffer")))
417            (save-restriction
418              (widen)
419              (buffer-substring (car value) (cdr value)))))
420         (t nil)))
421
422 (defun select-coerce-to-text (selection type value)
423   (select-convert-to-text selection type value))
424
425 (defun select-convert-from-text (selection type value)
426   (when (stringp value)
427     value))
428
429 (defun select-convert-to-string (selection type value)
430   (let ((outval (select-convert-to-text selection type value)))
431     ;; force the string to be not in Compound Text format. This grubby
432     ;; hack will go soon, to be replaced by a more general mechanism.
433     (if (stringp outval)
434         (cons 'STRING outval)
435       outval)))
436
437 (defun select-convert-to-compound-text (selection type value)
438   ;; converts to compound text automatically
439   (select-convert-to-text selection type value))
440
441 (defun select-convert-to-length (selection type value)
442   (let ((value
443          (cond ((stringp value)
444                 (length value))
445                ((extentp value)
446                 (extent-length value))
447                ((and (consp value)
448                      (markerp (car value))
449                      (markerp (cdr value)))
450                 (or (eq (marker-buffer (car value))
451                         (marker-buffer (cdr value)))
452                     (signal 'error
453                             (list "markers must be in the same buffer"
454                                   (car value) (cdr value))))
455                 (abs (- (car value) (cdr value)))))))
456     (if value ; force it to be in 32-bit format.
457         (cons (ash value -16) (logand value 65535))
458       nil)))
459
460 (defun select-convert-from-length (selection type value)
461   (select-convert-to-length selection type value))
462
463 (defun select-convert-to-targets (selection type value)
464   ;; return a vector of atoms, but remove duplicates first.
465   (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
466          (rest all))
467     (while rest
468       (cond ((memq (car rest) (cdr rest))
469              (setcdr rest (delq (car rest) (cdr rest))))
470             (t
471              (setq rest (cdr rest)))))
472     (apply 'vector all)))
473
474 (defun select-convert-to-delete (selection type value)
475   (disown-selection-internal selection)
476   ;; A return value of nil means that we do not know how to do this conversion,
477   ;; and replies with an "error".  A return value of NULL means that we have
478   ;; done the conversion (and any side-effects) but have no value to return.
479   'NULL)
480
481 (defun select-convert-to-filename (selection type value)
482   (cond ((extentp value)
483          (buffer-file-name (or (extent-object value)
484                                (error "selection is in a killed buffer"))))
485         ((and (consp value)
486               (markerp (car value))
487               (markerp (cdr value)))
488          (buffer-file-name (or (marker-buffer (car value))
489                                (error "selection is in a killed buffer"))))
490         (t nil)))
491
492 (defun select-convert-from-filename (selection type value)
493   (when (stringp value)
494     value))
495
496 (defun select-convert-to-charpos (selection type value)
497   (let (a b tmp)
498     (cond ((cond ((extentp value)
499                   (setq a (extent-start-position value)
500                         b (extent-end-position value)))
501                  ((and (consp value)
502                        (markerp (car value))
503                        (markerp (cdr value)))
504                   (setq a (car value)
505                         b (cdr value))))
506            (setq a (1- a) b (1- b)) ; zero-based
507            (if (< b a) (setq tmp a a b b tmp))
508            (cons 'SPAN
509                  (vector (cons (ash a -16) (logand a 65535))
510                          (cons (ash b -16) (logand b 65535))))))))
511
512 (defun select-convert-to-lineno (selection type value)
513   (let (a b buf tmp)
514     (cond ((cond ((extentp value)
515                   (setq buf (extent-object value)
516                         a (extent-start-position value)
517                         b (extent-end-position value)))
518                  ((and (consp value)
519                        (markerp (car value))
520                        (markerp (cdr value)))
521                   (setq a (marker-position (car value))
522                         b (marker-position (cdr value))
523                         buf (marker-buffer (car value)))))
524            (save-excursion
525              (set-buffer buf)
526              (save-restriction
527                (widen)
528                (goto-char a)
529                (beginning-of-line)
530                (setq a (1+ (count-lines 1 (point))))
531                (goto-char b)
532                (beginning-of-line)
533                (setq b (1+ (count-lines 1 (point))))))
534            (if (< b a) (setq tmp a a b b tmp))
535            (cons 'SPAN
536                  (vector (cons (ash a -16) (logand a 65535))
537                          (cons (ash b -16) (logand b 65535))))))))
538
539 (defun select-convert-to-colno (selection type value)
540   (let (a b buf tmp)
541     (cond ((cond ((extentp value)
542                   (setq buf (extent-object value)
543                         a (extent-start-position value)
544                         b (extent-end-position value)))
545                  ((and (consp value)
546                        (markerp (car value))
547                        (markerp (cdr value)))
548                   (setq a (car value)
549                         b (cdr value)
550                         buf (marker-buffer a))))
551            (save-excursion
552              (set-buffer buf)
553              (goto-char a)
554              (setq a (current-column))
555              (goto-char b)
556              (setq b (current-column)))
557            (if (< b a) (setq tmp a a b b tmp))
558            (cons 'SPAN
559                  (vector (cons (ash a -16) (logand a 65535))
560                          (cons (ash b -16) (logand b 65535))))))))
561
562 (defun select-convert-to-sourceloc (selection type value)
563   (let (a b buf file-name tmp)
564     (cond ((cond ((extentp value)
565                   (setq buf (or (extent-object value)
566                                 (error "selection is in a killed buffer"))
567                         a (extent-start-position value)
568                         b (extent-end-position value)
569                         file-name (buffer-file-name buf)))
570                  ((and (consp value)
571                        (markerp (car value))
572                        (markerp (cdr value)))
573                   (setq a (marker-position (car value))
574                         b (marker-position (cdr value))
575                         buf (or (marker-buffer (car value))
576                                 (error "selection is in a killed buffer"))
577                         file-name (buffer-file-name buf))))
578            (save-excursion
579              (set-buffer buf)
580              (save-restriction
581                (widen)
582                (goto-char a)
583                (beginning-of-line)
584                (setq a (1+ (count-lines 1 (point))))
585                (goto-char b)
586                (beginning-of-line)
587                (setq b (1+ (count-lines 1 (point))))))
588            (if (< b a) (setq tmp a a b b tmp))
589            (format "%s:%d" file-name a)))))
590
591 (defun select-convert-to-os (selection type size)
592   (symbol-name system-type))
593
594 (defun select-convert-to-host (selection type size)
595   (system-name))
596
597 (defun select-convert-to-user (selection type size)
598   (user-full-name))
599
600 (defun select-convert-to-class (selection type size)
601   (symbol-value 'x-emacs-application-class))
602
603 ;; We do not try to determine the name Emacs was invoked with,
604 ;; because it is not clean for a program's behavior to depend on that.
605 (defun select-convert-to-name (selection type size)
606   ;invocation-name
607   "xemacs")
608
609 (defun select-convert-to-integer (selection type value)
610   (and (integerp value)
611        (cons (ash value -16) (logand value 65535))))
612
613 ;; Can convert from the following integer representations
614 ;;
615 ;;    integer
616 ;;    (integer . integer)
617 ;;    (integer integer)
618 ;;    (list [integer|(integer . integer)]*)
619 ;;    (vector [integer|(integer . integer)]*)
620 ;;
621 ;; Cons'd integers get cleaned up a little.
622
623 (defun select-convert-from-integer (selection type value)
624   (cond ((integerp value)               ; Integer
625          value)
626
627         ((and (consp value)             ; (integer . integer)
628               (integerp (car value))
629               (integerp (cdr value)))
630          (if (eq (car value) 0)
631              (cdr value)
632            (if (and (eq (car value) -1)
633                     (< (cdr value) 0))
634                (cdr value)
635              value)))
636
637         ((and (listp value)             ; (integer integer)
638               (eq (length value) 2)
639               (integerp (car value))
640               (integerp (cadr value)))
641          (if (eq (car value) 0)
642              (cadr value)
643            (if (and (eq (car value) -1)
644                     (< (cdr value) 0))
645                (- (cadr value))
646              (cons (car value) (cadr value)))))
647
648         ((listp value)                  ; list
649          (if (cdr value)
650              (mapcar '(lambda (x)
651                         (select-convert-from-integer selection type x))
652                      value)
653            (select-convert-from-integer selection type (car value))))
654
655         ((vectorp value)                ; vector
656          (if (eq (length value) 1)
657              (select-convert-from-integer selection type (aref value 0))
658            (mapvector '(lambda (x)
659                         (select-convert-from-integer selection type x))
660                      value)))
661
662         (t nil)
663         ))
664
665 (defun select-convert-to-atom (selection type value)
666   (and (symbolp value) value))
667
668 ;;; CF_xxx conversions
669 (defun select-convert-from-cf-text (selection type value)
670   (replace-in-string (if (string-match "\0" value)
671                          (substring value 0 (match-beginning 0))
672                        value)
673                      "\\(\r\n\\|\n\r\\)" "\n" t))
674
675 (defun select-convert-to-cf-text (selection type value)
676   (let ((text (select-convert-to-text selection type value)))
677     (concat (replace-in-string text "\n" "\r\n" t) "\0")))
678
679 ;;; Appenders
680 (defun select-append-to-text (selection type value1 value2)
681   (let ((text1 (select-convert-to-text selection 'STRING value1))
682         (text2 (select-convert-to-text selection 'STRING value2)))
683     (if (and text1 text2)
684         (concat text1 text2)
685       nil)))
686
687 (defun select-append-to-string (selection type value1 value2)
688   (select-append-to-text selection type value1 value2))
689
690 (defun select-append-to-compound-text (selection type value1 value2)
691   (select-append-to-text selection type value1 value2))
692
693 (defun select-append-to-cf-text (selection type value1 value2)
694   (let ((text1 (select-convert-from-cf-text selection 'CF_TEXT value1))
695         (text2 (select-convert-from-cf-text selection 'CF_TEXT value2)))
696     (if (and text1 text2)
697         (select-convert-to-cf-text selection type (concat text1 text2))
698       nil)))
699
700 (defun select-append-default (selection type value1 value2)
701 ;; This appender gets used if the type is "nil" - i.e. default.
702 ;; It should probably have more cases implemented than it does - e.g.
703 ;; appending numbers to strings, etc...
704   (cond ((and (stringp value1) (stringp value2))
705          (select-append-to-string selection 'STRING value1 value2))
706         (t nil)))
707
708 ;;; Buffer kill handlers
709
710 (defun select-buffer-killed-default (selection type value buffer)
711 ;; This handler gets used if the type is "nil".
712   (cond ((extentp value)
713          (if (eq (extent-object value) buffer)
714              ; If this selection is on the clipboard, grab it quick
715              (when (eq selection 'CLIPBOARD)
716                (save-excursion
717                  (set-buffer (extent-object value))
718                  (save-restriction
719                   (widen)
720                   (buffer-substring (extent-start-position value)
721                                     (extent-end-position value)))))
722            value))
723         ((markerp value)
724          (unless (eq (marker-buffer value) buffer)
725            value))
726         ((and (consp value)
727               (markerp (car value))
728               (markerp (cdr value)))
729          (if (or (eq (marker-buffer (car value)) buffer)
730                  (eq (marker-buffer (cdr value)) buffer))
731              ; If this selection is on the clipboard, grab it quick
732              (when (eq selection 'CLIPBOARD)
733                (save-excursion
734                  (set-buffer (marker-buffer (car value)))
735                  (save-restriction
736                    (widen)
737                    (buffer-substring (car value) (cdr value)))))
738              value))
739         (t value)))
740
741 (defun select-buffer-killed-text (selection type value buffer)
742   (select-buffer-killed-default selection type value buffer))
743
744 ;; Types listed in here can be selections of XEmacs
745 (setq selection-converter-out-alist
746       '((TEXT . select-convert-to-text)
747         (STRING . select-convert-to-string)
748         (COMPOUND_TEXT . select-convert-to-compound-text)
749         (TARGETS . select-convert-to-targets)
750         (LENGTH . select-convert-to-length)
751         (DELETE . select-convert-to-delete)
752         (FILE_NAME . select-convert-to-filename)
753         (CHARACTER_POSITION . select-convert-to-charpos)
754         (SOURCE_LOC . select-convert-to-sourceloc)
755         (LINE_NUMBER . select-convert-to-lineno)
756         (COLUMN_NUMBER . select-convert-to-colno)
757         (OWNER_OS . select-convert-to-os)
758         (HOST_NAME . select-convert-to-host)
759         (USER . select-convert-to-user)
760         (CLASS . select-convert-to-class)
761         (NAME . select-convert-to-name)
762         (ATOM . select-convert-to-atom)
763         (INTEGER . select-convert-to-integer)
764         (CF_TEXT . select-convert-to-cf-text)
765         ))
766
767 ;; Types listed here can be selections foreign to XEmacs
768 (setq selection-converter-in-alist
769       '(; Specific types that get handled by generic converters
770         (COMPOUND_TEXT . select-convert-from-text)
771         (SOURCE_LOC . select-convert-from-text)
772         (OWNER_OS . select-convert-from-text)
773         (HOST_NAME . select-convert-from-text)
774         (USER . select-convert-from-text)
775         (CLASS . select-convert-from-text)
776         (NAME . select-convert-from-text)
777         ; Generic types
778         (INTEGER . select-convert-from-integer)
779         (TEXT . select-convert-from-text)
780         (STRING . select-convert-from-text)
781         (LENGTH . select-convert-from-length)
782         (FILE_NAME . select-convert-from-filename)
783         (CF_TEXT . select-convert-from-cf-text)
784         ))
785
786 ;; Types listed here have special coercion functions that can munge
787 ;; other types. This can also be used to add special features - e.g.
788 ;; being able to pass a region or a cons of markers to own-selection,
789 ;; but getting the *current* text in the region back when calling
790 ;; get-selection.
791 ;;
792 ;; Any function listed in here *will be called* whenever a value of
793 ;; its type is retrieved from the internal selection cache, or when
794 ;; no suitable values could be found in which case XEmacs looks for
795 ;; values with types listed in selection-coercible-types.
796 (setq selection-coercion-alist
797       '((TEXT . select-coerce-to-text)
798         (STRING . select-coerce-to-text)
799         (COMPOUND_TEXT . select-coerce-to-text)
800         (CF_TEXT . select-coerce-to-text)))
801
802 ;; Types listed here can be appended by own-selection
803 (setq selection-appender-alist
804       '((nil . select-append-default)
805         (TEXT . select-append-to-text)
806         (STRING . select-append-to-string)
807         (COMPOUND_TEXT . select-append-to-compound-text)
808         (CF_TEXT . select-append-to-cf-text)
809         ))
810
811 ;; Types listed here have buffer-kill handlers
812 (setq selection-buffer-killed-alist
813       '((nil . select-buffer-killed-default)
814         (TEXT . select-buffer-killed-text)
815         (STRING . select-buffer-killed-text)
816         (COMPOUND_TEXT . select-buffer-killed-text)
817         (CF_TEXT . select-buffer-killed-text)))
818
819 ;; Lists of types that are coercible (can be converted to other types)
820 (setq selection-coercible-types '(TEXT STRING COMPOUND_TEXT))
821
822 ;;; select.el ends here