comparison lisp/select.el @ 442:abe6d1db359e r21-2-36

Import from CVS: tag r21-2-36
author cvs
date Mon, 13 Aug 2007 11:35:02 +0200
parents 3ecd8885ac67
children 576fb035e263
comparison
equal deleted inserted replaced
441:72a7cfa4a488 442:abe6d1db359e
81 (define-device-method get-cutbuffer 81 (define-device-method get-cutbuffer
82 "Return the value of one of the cut buffers. 82 "Return the value of one of the cut buffers.
83 This will do nothing under anything other than X.") 83 This will do nothing under anything other than X.")
84 84
85 (defun get-selection-no-error (&optional type data-type) 85 (defun get-selection-no-error (&optional type data-type)
86 "Return the value of a Windows selection. 86 "Return the value of a window-system selection.
87 The argument TYPE (default `PRIMARY') says which selection, 87 The argument TYPE (default `PRIMARY') says which selection,
88 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule) 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" 89 says how to convert the data. Returns NIL if there is no selection"
90 (condition-case err (get-selection type data-type) (t nil))) 90 (condition-case nil (get-selection type data-type) (t nil)))
91 91
92 (defun get-selection (&optional type data-type) 92 (defun get-selection (&optional type data-type)
93 "Return the value of a Windows selection. 93 "Return the value of a window-system selection.
94 The argument TYPE (default `PRIMARY') says which selection, 94 The argument TYPE (default `PRIMARY') says which selection,
95 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule) 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." 96 says how to convert the data. If there is no selection an error is signalled."
97 (or type (setq type 'PRIMARY)) 97 (or type (setq type 'PRIMARY))
98 (or data-type (setq data-type selected-text-type)) 98 (or data-type (setq data-type selected-text-type))
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 ;; arguments (duh ...). This order is more logical. 111 ;; first two arguments (duh ...). This order is more logical.
116 (defun own-selection (data &optional type) 112 (defun own-selection (data &optional type how-to-add data-type)
117 "Make an Windows 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
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).
121 129
122 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,
123 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
124 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 behavior - it doesn't on mswindows for example).
125 Thus, editing done in the buffer after you specify the selection 135 Thus, editing done in the buffer after you specify the selection
126 can alter the effective value of the selection. 136 can alter the effective value of the selection.
127 137
128 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.
129 139
130 Interactively, the text of the region is used as the selection value." 140 Interactively, the text of the region is used as the selection value."
131 (interactive (if (not current-prefix-arg) 141 (interactive (if (not current-prefix-arg)
132 (list (read-string "Store text for pasting: ")) 142 (list (read-string "Store text for pasting: "))
133 (list (substring (region-beginning) (region-end))))) 143 (list (substring (region-beginning) (region-end)))))
134 ;FSFmacs huh?? It says: 144 ;; calling own-selection-internal will mess this up, so preserve it.
135 ;; "This is for temporary compatibility with pre-release Emacs 19." 145 (let ((zmacs-region-stays zmacs-region-stays))
136 ;(if (stringp type) 146 ;FSFmacs huh?? It says:
137 ; (setq type (intern type))) 147 ;; "This is for temporary compatibility with pre-release Emacs 19."
138 (or (valid-simple-selection-p data) 148 ;(if (stringp type)
139 (and (vectorp data) 149 ; (setq type (intern type)))
140 (let ((valid t) 150 (or type (setq type 'PRIMARY))
141 (i (1- (length data)))) 151 (if (null data)
142 (while (>= i 0) 152 (disown-selection-internal type)
143 (or (valid-simple-selection-p (aref data i)) 153 (own-selection-internal type data how-to-add data-type)
144 (setq valid nil)) 154 (when (and (eq type 'PRIMARY)
145 (setq i (1- i))) 155 selection-sets-clipboard)
146 valid)) 156 (own-selection-internal 'CLIPBOARD data how-to-add data-type)))
147 (signal 'error (list "invalid selection" data))) 157 (cond ((eq type 'PRIMARY)
148 (or type (setq type 'PRIMARY)) 158 (setq primary-selection-extent
149 (if (null data) 159 (select-make-extent-for-selection
150 (disown-selection-internal type) 160 data primary-selection-extent)))
151 (own-selection-internal type data) 161 ((eq type 'SECONDARY)
152 (when (and (eq type 'PRIMARY) 162 (setq secondary-selection-extent
153 selection-sets-clipboard) 163 (select-make-extent-for-selection
154 (own-selection-internal 'CLIPBOARD data))) 164 data secondary-selection-extent)))))
155 (cond ((eq type 'PRIMARY) 165 ;; zmacs-region-stays is for commands, not low-level functions.
156 (setq primary-selection-extent 166 ;; when behaving as the latter, we better not set it, or we will
157 (select-make-extent-for-selection 167 ;; cause unwanted sticky-region behavior in kill-region and friends.
158 data primary-selection-extent))) 168 (if (interactive-p)
159 ((eq type 'SECONDARY) 169 (setq zmacs-region-stays t))
160 (setq secondary-selection-extent
161 (select-make-extent-for-selection
162 data secondary-selection-extent))))
163 (setq zmacs-region-stays t)
164 data) 170 data)
165 171
166 (defun dehilight-selection (selection) 172 (defun dehilight-selection (selection)
167 "for use as a value of `lost-selection-hooks'." 173 "for use as a value of `lost-selection-hooks'."
168 (cond ((eq selection 'PRIMARY) 174 (cond ((eq selection 'PRIMARY)
182 (setq secondary-selection-extent nil))))) 188 (setq secondary-selection-extent nil)))))
183 nil) 189 nil)
184 190
185 (setq lost-selection-hooks 'dehilight-selection) 191 (setq lost-selection-hooks 'dehilight-selection)
186 192
187 (defun own-clipboard (string) 193 (defun own-clipboard (string &optional push)
188 "Paste the given string to the window system Clipboard." 194 "Paste the given string to the window system Clipboard.
195 See `interprogram-cut-function' for more information."
189 (own-selection string 'CLIPBOARD)) 196 (own-selection string 'CLIPBOARD))
190 197
191 (defun disown-selection (&optional secondary-p) 198 (defun disown-selection (&optional secondary-p)
192 "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
193 secondary selection instead of the primary selection." 200 secondary selection instead of the primary selection."
287 )) 294 ))
288 previous-extent)))) 295 previous-extent))))
289 296
290 ;; moved from x-select.el 297 ;; moved from x-select.el
291 (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."
292 (or (stringp data) 302 (or (stringp data)
293 ;FSFmacs huh?? (symbolp data) 303 ;FSFmacs huh?? (symbolp data)
294 (integerp data) 304 (integerp data)
295 (and (consp data) 305 (and (consp data)
296 (integerp (car data)) 306 (integerp (car data))
348 (delete-rectangle s e) 358 (delete-rectangle s e)
349 (delete-region s e)))) 359 (delete-region s e))))
350 (disown-selection nil) 360 (disown-selection nil)
351 ))) 361 )))
352 362
363
353 ;;; Functions to convert the selection into various other selection 364 ;;; Functions to convert the selection into various other selection
354 ;;; types. Every selection type that emacs handles is implemented 365 ;;; types.
355 ;;; this way, except for TIMESTAMP, which is a special case. These are 366
356 ;;; all moved from x-select.el 367 ;; These next three functions get called by C code...
357 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.
358 (defun select-convert-to-text (selection type value) 397 (defun select-convert-to-text (selection type value)
359 (cond ((stringp value) 398 (cond ((stringp value)
360 value) 399 value)
361 ((extentp value) 400 ((extentp value)
362 (save-excursion 401 (save-excursion
378 (save-restriction 417 (save-restriction
379 (widen) 418 (widen)
380 (buffer-substring (car value) (cdr value))))) 419 (buffer-substring (car value) (cdr value)))))
381 (t nil))) 420 (t nil)))
382 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
383 (defun select-convert-to-string (selection type value) 429 (defun select-convert-to-string (selection type value)
384 (let ((outval (select-convert-to-text selection type value))) 430 (let ((outval (select-convert-to-text selection type value)))
385 ;; force the string to be not in Compound Text format. 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.
386 (if (stringp outval) 433 (if (stringp outval)
387 (cons 'STRING outval) 434 (cons 'STRING outval)
388 outval))) 435 outval)))
389 436
390 (defun select-convert-to-compound-text (selection type value) 437 (defun select-convert-to-compound-text (selection type value)
408 (abs (- (car value) (cdr value))))))) 455 (abs (- (car value) (cdr value)))))))
409 (if value ; force it to be in 32-bit format. 456 (if value ; force it to be in 32-bit format.
410 (cons (ash value -16) (logand value 65535)) 457 (cons (ash value -16) (logand value 65535))
411 nil))) 458 nil)))
412 459
460 (defun select-convert-from-length (selection type value)
461 (select-convert-to-length selection type value))
462
413 (defun select-convert-to-targets (selection type value) 463 (defun select-convert-to-targets (selection type value)
414 ;; return a vector of atoms, but remove duplicates first. 464 ;; return a vector of atoms, but remove duplicates first.
415 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist))) 465 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
416 (rest all)) 466 (rest all))
417 (while rest 467 (while rest
418 (cond ((memq (car rest) (cdr rest)) 468 (cond ((memq (car rest) (cdr rest))
419 (setcdr rest (delq (car rest) (cdr rest)))) 469 (setcdr rest (delq (car rest) (cdr rest))))
420 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
421 (setcdr rest (cdr (cdr rest))))
422 (t 470 (t
423 (setq rest (cdr rest))))) 471 (setq rest (cdr rest)))))
424 (apply 'vector all))) 472 (apply 'vector all)))
425 473
426 (defun select-convert-to-delete (selection type value) 474 (defun select-convert-to-delete (selection type value)
438 (markerp (car value)) 486 (markerp (car value))
439 (markerp (cdr value))) 487 (markerp (cdr value)))
440 (buffer-file-name (or (marker-buffer (car value)) 488 (buffer-file-name (or (marker-buffer (car value))
441 (error "selection is in a killed buffer")))) 489 (error "selection is in a killed buffer"))))
442 (t nil))) 490 (t nil)))
491
492 (defun select-convert-from-filename (selection type value)
493 (when (stringp value)
494 value))
443 495
444 (defun select-convert-to-charpos (selection type value) 496 (defun select-convert-to-charpos (selection type value)
445 (let (a b tmp) 497 (let (a b tmp)
446 (cond ((cond ((extentp value) 498 (cond ((cond ((extentp value)
447 (setq a (extent-start-position value) 499 (setq a (extent-start-position value)
544 596
545 (defun select-convert-to-user (selection type size) 597 (defun select-convert-to-user (selection type size)
546 (user-full-name)) 598 (user-full-name))
547 599
548 (defun select-convert-to-class (selection type size) 600 (defun select-convert-to-class (selection type size)
549 x-emacs-application-class) 601 (symbol-value 'x-emacs-application-class))
550 602
551 ;; We do not try to determine the name Emacs was invoked with, 603 ;; We do not try to determine the name Emacs was invoked with,
552 ;; because it is not clean for a program's behavior to depend on that. 604 ;; because it is not clean for a program's behavior to depend on that.
553 (defun select-convert-to-name (selection type size) 605 (defun select-convert-to-name (selection type size)
554 ;invocation-name 606 ;invocation-name
556 608
557 (defun select-convert-to-integer (selection type value) 609 (defun select-convert-to-integer (selection type value)
558 (and (integerp value) 610 (and (integerp value)
559 (cons (ash value -16) (logand value 65535)))) 611 (cons (ash value -16) (logand value 65535))))
560 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
561 (defun select-convert-to-atom (selection type value) 665 (defun select-convert-to-atom (selection type value)
562 (and (symbolp value) value)) 666 (and (symbolp value) value))
563 667
564 (defun select-convert-to-identity (selection type value) ; used internally 668 ;;; CF_xxx conversions
565 (vector value)) 669 (defun select-convert-from-cf-text (selection type value)
566 670 (replace-in-string (if (string-match "\0" value)
567 (setq selection-converter-alist 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
568 '((TEXT . select-convert-to-text) 746 '((TEXT . select-convert-to-text)
569 (STRING . select-convert-to-string) 747 (STRING . select-convert-to-string)
570 (COMPOUND_TEXT . select-convert-to-compound-text) 748 (COMPOUND_TEXT . select-convert-to-compound-text)
571 (TARGETS . select-convert-to-targets) 749 (TARGETS . select-convert-to-targets)
572 (LENGTH . select-convert-to-length) 750 (LENGTH . select-convert-to-length)
581 (USER . select-convert-to-user) 759 (USER . select-convert-to-user)
582 (CLASS . select-convert-to-class) 760 (CLASS . select-convert-to-class)
583 (NAME . select-convert-to-name) 761 (NAME . select-convert-to-name)
584 (ATOM . select-convert-to-atom) 762 (ATOM . select-convert-to-atom)
585 (INTEGER . select-convert-to-integer) 763 (INTEGER . select-convert-to-integer)
586 (_EMACS_INTERNAL . select-convert-to-identity) 764 (CF_TEXT . select-convert-to-cf-text)
587 )) 765 ))
588 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
589 ;;; select.el ends here 822 ;;; select.el ends here