comparison lisp/select.el @ 410:de805c49cfc1 r21-2-35

Import from CVS: tag r21-2-35
author cvs
date Mon, 13 Aug 2007 11:19:21 +0200
parents 2f8bb876ab1d
children 697ef44129c6
comparison
equal deleted inserted replaced
409:301b9ebbdf3b 410:de805c49cfc1
103 (selection-conversion-error 103 (selection-conversion-error
104 (if (cdr data-type) 104 (if (cdr data-type)
105 (get-selection type (cdr data-type)) 105 (get-selection type (cdr data-type))
106 (signal (car err) (cdr err))))) 106 (signal (car err) (cdr err)))))
107 (get-selection-internal type data-type)))) 107 (get-selection-internal type data-type))))
108 (when (and (consp text) (symbolp (car text)))
109 (setq text (cdr text)))
110 (when (not (stringp text))
111 (error "Selection is not a string: %S" text))
112 text)) 108 text))
113 109
114 ;; FSFmacs calls this `x-set-selection', and reverses the 110 ;; FSFmacs calls this `x-set-selection', and reverses the
115 ;; first two arguments (duh ...). This order is more logical. 111 ;; first two arguments (duh ...). This order is more logical.
116 (defun own-selection (data &optional type append) 112 (defun own-selection (data &optional type how-to-add data-type)
117 "Make a window-system selection of type TYPE and value DATA. 113 "Make a window-system selection of type TYPE and value DATA.
118 The argument TYPE (default `PRIMARY') says which selection, 114 The argument TYPE (default `PRIMARY') says which selection,
119 and DATA specifies the contents. DATA may be a string, 115 and DATA specifies the contents. DATA may be any lisp data type
120 a symbol, an integer (or a cons of two integers or list of two integers). 116 that can be converted using the function corresponding to DATA-TYPE
121 If APPEND is non-nil, append the data to the existing selection data. 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 behaviour 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).
122 129
123 The selection may also be a cons of two markers pointing to the same buffer, 130 The selection may also be a cons of two markers pointing to the same buffer,
124 or an overlay. In these cases, the selection is considered to be the text 131 or an overlay. In these cases, the selection is considered to be the text
125 between the markers *at whatever time the selection is examined*. 132 between the markers *at whatever time the selection is examined* (note
133 that the window system clipboard does not necessarily duplicate this
134 behaviour - it doesn't on mswindows for example).
126 Thus, editing done in the buffer after you specify the selection 135 Thus, editing done in the buffer after you specify the selection
127 can alter the effective value of the selection. 136 can alter the effective value of the selection.
128 137
129 The data may also be a vector of valid non-vector selection values. 138 The data may also be a vector of valid non-vector selection values.
130 139
136 (let ((zmacs-region-stays zmacs-region-stays)) 145 (let ((zmacs-region-stays zmacs-region-stays))
137 ;FSFmacs huh?? It says: 146 ;FSFmacs huh?? It says:
138 ;; "This is for temporary compatibility with pre-release Emacs 19." 147 ;; "This is for temporary compatibility with pre-release Emacs 19."
139 ;(if (stringp type) 148 ;(if (stringp type)
140 ; (setq type (intern type))) 149 ; (setq type (intern type)))
141 (or (valid-simple-selection-p data)
142 (and (vectorp data)
143 (let ((valid t)
144 (i (1- (length data))))
145 (while (>= i 0)
146 (or (valid-simple-selection-p (aref data i))
147 (setq valid nil))
148 (setq i (1- i)))
149 valid))
150 (signal 'error (list "invalid selection" data)))
151 (or type (setq type 'PRIMARY)) 150 (or type (setq type 'PRIMARY))
152 (flet ((own-selection-1 151 (if (null data)
153 (type data append) 152 (disown-selection-internal type)
154 (when append 153 (own-selection-internal type data how-to-add data-type)
155 (unless (stringp data) 154 (when (and (eq type 'PRIMARY)
156 ;; kludge! 155 selection-sets-clipboard)
157 (setq data (select-convert-to-text type 'STRING data)) 156 (own-selection-internal 'CLIPBOARD data how-to-add data-type)))
158 (if (stringp data)
159 (setq data (concat (get-selection type) data)))))
160 (own-selection-internal type data)))
161 (if (null data)
162 (disown-selection-internal type)
163 (own-selection-1 type data append)
164 (when (and (eq type 'PRIMARY)
165 selection-sets-clipboard)
166 (own-selection-internal 'CLIPBOARD data append))))
167 (cond ((eq type 'PRIMARY) 157 (cond ((eq type 'PRIMARY)
168 (setq primary-selection-extent 158 (setq primary-selection-extent
169 (select-make-extent-for-selection 159 (select-make-extent-for-selection
170 data primary-selection-extent))) 160 data primary-selection-extent)))
171 ((eq type 'SECONDARY) 161 ((eq type 'SECONDARY)
174 data secondary-selection-extent))))) 164 data secondary-selection-extent)))))
175 ;; zmacs-region-stays is for commands, not low-level functions. 165 ;; zmacs-region-stays is for commands, not low-level functions.
176 ;; when behaving as the latter, we better not set it, or we will 166 ;; when behaving as the latter, we better not set it, or we will
177 ;; cause unwanted sticky-region behavior in kill-region and friends. 167 ;; cause unwanted sticky-region behavior in kill-region and friends.
178 (if (interactive-p) 168 (if (interactive-p)
179 (setq zmacs-region-stays t)) 169 (setq zmacs-region-stays t))
180 data) 170 data)
181 171
182 (defun dehilight-selection (selection) 172 (defun dehilight-selection (selection)
183 "for use as a value of `lost-selection-hooks'." 173 "for use as a value of `lost-selection-hooks'."
184 (cond ((eq selection 'PRIMARY) 174 (cond ((eq selection 'PRIMARY)
198 (setq secondary-selection-extent nil))))) 188 (setq secondary-selection-extent nil)))))
199 nil) 189 nil)
200 190
201 (setq lost-selection-hooks 'dehilight-selection) 191 (setq lost-selection-hooks 'dehilight-selection)
202 192
203 (defun own-clipboard (string &optional append) 193 (defun own-clipboard (string &optional push)
204 "Paste the given string to the window system Clipboard. 194 "Paste the given string to the window system Clipboard.
205 If APPEND is non-nil, append the string to the existing contents." 195 See `interprogram-cut-function' for more information."
206 (own-selection string 'CLIPBOARD)) 196 (own-selection string 'CLIPBOARD))
207 197
208 (defun disown-selection (&optional secondary-p) 198 (defun disown-selection (&optional secondary-p)
209 "Assuming we own the selection, disown it. With an argument, discard the 199 "Assuming we own the selection, disown it. With an argument, discard the
210 secondary selection instead of the primary selection." 200 secondary selection instead of the primary selection."
304 )) 294 ))
305 previous-extent)))) 295 previous-extent))))
306 296
307 ;; moved from x-select.el 297 ;; moved from x-select.el
308 (defun valid-simple-selection-p (data) 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."
309 (or (stringp data) 302 (or (stringp data)
310 ;FSFmacs huh?? (symbolp data) 303 ;FSFmacs huh?? (symbolp data)
311 (integerp data) 304 (integerp data)
312 (and (consp data) 305 (and (consp data)
313 (integerp (car data)) 306 (integerp (car data))
365 (delete-rectangle s e) 358 (delete-rectangle s e)
366 (delete-region s e)))) 359 (delete-region s e))))
367 (disown-selection nil) 360 (disown-selection nil)
368 ))) 361 )))
369 362
363
370 ;;; Functions to convert the selection into various other selection 364 ;;; Functions to convert the selection into various other selection
371 ;;; types. Every selection type that emacs handles is implemented 365 ;;; types.
372 ;;; this way, except for TIMESTAMP, which is a special case. These are 366
373 ;;; all moved from x-select.el 367 ;; These two functions get called by C code...
374 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 ;; The rest of the functions on this "page" are conversion handlers,
387 ;; append handlers and buffer-kill handlers.
375 (defun select-convert-to-text (selection type value) 388 (defun select-convert-to-text (selection type value)
376 (cond ((stringp value) 389 (cond ((stringp value)
377 value) 390 value)
378 ((extentp value) 391 ((extentp value)
379 (save-excursion 392 (save-excursion
395 (save-restriction 408 (save-restriction
396 (widen) 409 (widen)
397 (buffer-substring (car value) (cdr value))))) 410 (buffer-substring (car value) (cdr value)))))
398 (t nil))) 411 (t nil)))
399 412
413 (defun select-convert-from-text (selection type value)
414 (when (stringp value)
415 value))
416
400 (defun select-convert-to-string (selection type value) 417 (defun select-convert-to-string (selection type value)
401 (let ((outval (select-convert-to-text selection type value))) 418 (let ((outval (select-convert-to-text selection type value)))
402 ;; force the string to be not in Compound Text format. 419 ;; force the string to be not in Compound Text format.
403 (if (stringp outval) 420 (if (stringp outval)
404 (cons 'STRING outval) 421 (cons 'STRING outval)
425 (abs (- (car value) (cdr value))))))) 442 (abs (- (car value) (cdr value)))))))
426 (if value ; force it to be in 32-bit format. 443 (if value ; force it to be in 32-bit format.
427 (cons (ash value -16) (logand value 65535)) 444 (cons (ash value -16) (logand value 65535))
428 nil))) 445 nil)))
429 446
447 (defun select-convert-from-length (selection type value)
448 (select-convert-to-length selection type value))
449
430 (defun select-convert-to-targets (selection type value) 450 (defun select-convert-to-targets (selection type value)
431 ;; return a vector of atoms, but remove duplicates first. 451 ;; return a vector of atoms, but remove duplicates first.
432 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist))) 452 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
433 (rest all)) 453 (rest all))
434 (while rest 454 (while rest
435 (cond ((memq (car rest) (cdr rest)) 455 (cond ((memq (car rest) (cdr rest))
436 (setcdr rest (delq (car rest) (cdr rest)))) 456 (setcdr rest (delq (car rest) (cdr rest))))
437 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
438 (setcdr rest (cdr (cdr rest))))
439 (t 457 (t
440 (setq rest (cdr rest))))) 458 (setq rest (cdr rest)))))
441 (apply 'vector all))) 459 (apply 'vector all)))
442 460
443 (defun select-convert-to-delete (selection type value) 461 (defun select-convert-to-delete (selection type value)
455 (markerp (car value)) 473 (markerp (car value))
456 (markerp (cdr value))) 474 (markerp (cdr value)))
457 (buffer-file-name (or (marker-buffer (car value)) 475 (buffer-file-name (or (marker-buffer (car value))
458 (error "selection is in a killed buffer")))) 476 (error "selection is in a killed buffer"))))
459 (t nil))) 477 (t nil)))
478
479 (defun select-convert-from-filename (selection type value)
480 (when (stringp value)
481 value))
460 482
461 (defun select-convert-to-charpos (selection type value) 483 (defun select-convert-to-charpos (selection type value)
462 (let (a b tmp) 484 (let (a b tmp)
463 (cond ((cond ((extentp value) 485 (cond ((cond ((extentp value)
464 (setq a (extent-start-position value) 486 (setq a (extent-start-position value)
573 595
574 (defun select-convert-to-integer (selection type value) 596 (defun select-convert-to-integer (selection type value)
575 (and (integerp value) 597 (and (integerp value)
576 (cons (ash value -16) (logand value 65535)))) 598 (cons (ash value -16) (logand value 65535))))
577 599
600 ;; Can convert from the following integer representations
601 ;;
602 ;; integer
603 ;; (integer . integer)
604 ;; (integer integer)
605 ;; (list [integer|(integer . integer)]*)
606 ;; (vector [integer|(integer . integer)]*)
607 ;;
608 ;; Cons'd integers get cleaned up a little.
609
610 (defun select-convert-from-integer (selection type value)
611 (cond ((integerp value) ; Integer
612 value)
613
614 ((and (consp value) ; (integer . integer)
615 (integerp (car value))
616 (integerp (cdr value)))
617 (if (eq (car value) 0)
618 (cdr value)
619 (if (and (eq (car value) -1)
620 (< (cdr value) 0))
621 (cdr value)
622 value)))
623
624 ((and (listp value) ; (integer integer)
625 (eq (length value) 2)
626 (integerp (car value))
627 (integerp (cadr value)))
628 (if (eq (car value) 0)
629 (cadr value)
630 (if (and (eq (car value) -1)
631 (< (cdr value) 0))
632 (- (cadr value))
633 (cons (car value) (cadr value)))))
634
635 ((listp value) ; list
636 (if (cdr value)
637 (mapcar '(lambda (x)
638 (select-convert-from-integer selection type x))
639 value)
640 (select-convert-from-integer selection type (car value))))
641
642 ((vectorp value) ; vector
643 (if (eq (length value) 1)
644 (select-convert-from-integer selection type (aref value 0))
645 (mapvector '(lambda (x)
646 (select-convert-from-integer selection type x))
647 value)))
648
649 (t nil)
650 ))
651
578 (defun select-convert-to-atom (selection type value) 652 (defun select-convert-to-atom (selection type value)
579 (and (symbolp value) value)) 653 (and (symbolp value) value))
580 654
581 (defun select-convert-to-identity (selection type value) ; used internally 655 ;;; CF_xxx conversions
582 (vector value)) 656 (defun select-convert-from-cf-text (selection type value)
583 657 (replace-in-string (if (string-match "\0" value)
584 (setq selection-converter-alist 658 (substring value 0 (match-beginning 0))
659 value)
660 "\\(\r\n\\|\n\r\\)" "\n" t))
661
662 (defun select-convert-to-cf-text (selection type value)
663 (let ((text (select-convert-to-text selection type value)))
664 (concat (replace-in-string text "\n" "\r\n" t) "\0")))
665
666 ;;; Appenders
667 (defun select-append-to-text (selection type value1 value2)
668 (let ((text1 (select-convert-to-text selection 'STRING value1))
669 (text2 (select-convert-to-text selection 'STRING value2)))
670 (if (and text1 text2)
671 (concat text1 text2)
672 nil)))
673
674 (defun select-append-to-string (selection type value1 value2)
675 (select-append-to-text selection type value1 value2))
676
677 (defun select-append-to-compound-text (selection type value1 value2)
678 (select-append-to-text selection type value1 value2))
679
680 (defun select-append-to-cf-text (selection type value1 value2)
681 (let ((text1 (select-convert-from-cf-text selection 'CF_TEXT value1))
682 (text2 (select-convert-from-cf-text selection 'CF_TEXT value2)))
683 (if (and text1 text2)
684 (select-convert-to-cf-text selection type (concat text1 text2))
685 nil)))
686
687 (defun select-append-default (selection type value1 value2)
688 ;; This appender gets used if the type is "nil" - i.e. default.
689 ;; It should probably have more cases implemented than it does - e.g.
690 ;; appending numbers to strings, etc...
691 (cond ((and (stringp value1) (stringp value2))
692 (select-append-to-string selection 'STRING value1 value2))
693 (t nil)))
694
695 ;;; Buffer kill handlers
696
697 ;; #### Should this function take the text *out* of the buffer that's
698 ;; being killed? Or should it do what the original code did and just
699 ;; destroy the selection?
700 (defun select-buffer-killed-default (selection type value buffer)
701 ;; This handler gets used if the type is "nil".
702 (cond ((extentp value)
703 (unless (eq (extent-object value) buffer)
704 value))
705 ((markerp value)
706 (unless (eq (marker-buffer value) buffer)
707 value))
708 ((and (consp value)
709 (markerp (car value))
710 (markerp (cdr value)))
711 (unless (or (eq (marker-buffer (car value)) buffer)
712 (eq (marker-buffer (cdr value)) buffer))
713 value))
714 (t value)))
715
716 (defun select-buffer-killed-text (selection type value buffer)
717 (select-buffer-killed-default selection type value buffer))
718
719 ;; Types listed in here can be selections of XEmacs
720 (setq selection-converter-out-alist
585 '((TEXT . select-convert-to-text) 721 '((TEXT . select-convert-to-text)
586 (STRING . select-convert-to-string) 722 (STRING . select-convert-to-string)
587 (COMPOUND_TEXT . select-convert-to-compound-text) 723 (COMPOUND_TEXT . select-convert-to-compound-text)
588 (TARGETS . select-convert-to-targets) 724 (TARGETS . select-convert-to-targets)
589 (LENGTH . select-convert-to-length) 725 (LENGTH . select-convert-to-length)
598 (USER . select-convert-to-user) 734 (USER . select-convert-to-user)
599 (CLASS . select-convert-to-class) 735 (CLASS . select-convert-to-class)
600 (NAME . select-convert-to-name) 736 (NAME . select-convert-to-name)
601 (ATOM . select-convert-to-atom) 737 (ATOM . select-convert-to-atom)
602 (INTEGER . select-convert-to-integer) 738 (INTEGER . select-convert-to-integer)
603 (_EMACS_INTERNAL . select-convert-to-identity) 739 (CF_TEXT . select-convert-to-cf-text)
604 )) 740 ))
605 741
742 ;; Types listed here can be selections foreign to XEmacs
743 (setq selection-converter-in-alist
744 '(; Specific types that get handled by generic converters
745 (COMPOUND_TEXT . select-convert-from-text)
746 (SOURCE_LOC . select-convert-from-text)
747 (OWNER_OS . select-convert-from-text)
748 (HOST_NAME . select-convert-from-text)
749 (USER . select-convert-from-text)
750 (CLASS . select-convert-from-text)
751 (NAME . select-convert-from-text)
752 ; Generic types
753 (INTEGER . select-convert-from-integer)
754 (TEXT . select-convert-from-text)
755 (STRING . select-convert-from-text)
756 (LENGTH . select-convert-from-length)
757 (FILE_NAME . select-convert-from-filename)
758 (CF_TEXT . select-convert-from-cf-text)
759 ))
760
761 ;; Types listed here can be appended by own-selection
762 (setq selection-appender-alist
763 '((nil . select-append-default)
764 (TEXT . select-append-to-text)
765 (STRING . select-append-to-string)
766 (COMPOUND_TEXT . select-append-to-compound-text)
767 (CF_TEXT . select-append-to-cf-text)
768 ))
769
770 ;; Types listed here have buffer-kill handlers
771 (setq selection-buffer-killed-alist
772 '((nil . select-buffer-killed-default)
773 (TEXT . select-buffer-killed-text)
774 (STRING . select-buffer-killed-text)
775 (COMPOUND_TEXT . select-buffer-killed-text)
776 (CF_TEXT . select-buffer-killed-text)))
777
778 ;; Lists of types that are coercible (can be converted to other types)
779 (setq selection-coercible-types '(TEXT STRING COMPOUND_TEXT))
780
606 ;;; select.el ends here 781 ;;; select.el ends here