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