+ (setq x-last-selected-text-primary text)
+ (when x-select-enable-clipboard
+ (x-set-selection 'CLIPBOARD text)
+ (setq x-last-selected-text-clipboard text))
+ )
+
+(defvar x-select-request-type nil
+ "*Data type request for X selection.
+The value is nil, one of the following data types, or a list of them:
+ `COMPOUND_TEXT', `UTF8_STRING', `STRING', `TEXT'
+
+If the value is nil, try `COMPOUND_TEXT' and `UTF8_STRING', and
+use the more appropriate result. If both fail, try `STRING', and
+then `TEXT'.
+
+If the value is one of the above symbols, try only the specified
+type.
+
+If the value is a list of them, try each of them in the specified
+order until succeed.")
+
+;; Helper function for x-selection-value. Select UTF8 or CTEXT
+;; whichever is more appropriate. Here, we use this heurisitcs.
+;;
+;; (1) If their lengthes are different, select the longer one. This
+;; is because an X client may just cut off unsupported characters.
+;;
+;; (2) Otherwise, if the Nth character of CTEXT is an ASCII
+;; character that is different from the Nth character of UTF8,
+;; select UTF8. This is because an X client may replace unsupported
+;; characters with some ASCII character (typically ` ' or `?') in
+;; CTEXT.
+;;
+;; (3) Otherwise, select CTEXT. This is because legacy charsets are
+;; better for the current Emacs, especially when the selection owner
+;; is also Emacs.
+
+(defun x-select-utf8-or-ctext (utf8 ctext)
+ (let ((len-utf8 (length utf8))
+ (len-ctext (length ctext))
+ (selected ctext)
+ (i 0)
+ char)
+ (if (/= len-utf8 len-ctext)
+ (if (> len-utf8 len-ctext) utf8 ctext)
+ (let ((result (compare-strings utf8 0 len-utf8 ctext 0 len-ctext)))
+ (if (or (eq result t)
+ (>= (aref ctext (1- (abs result))) 128))
+ ctext
+ utf8)))))
+
+;; Get a selection value of type TYPE by calling x-get-selection with
+;; an appropiate DATA-TYPE argument decidd by `x-select-request-type'.
+;; The return value is already decoded. If x-get-selection causes an
+;; error, this function return nil.
+
+(defun x-selection-value (type)
+ (let (text)
+ (cond ((null x-select-request-type)
+ (let (utf8 ctext utf8-coding)
+ ;; We try both UTF8_STRING and COMPOUND_TEXT, and choose
+ ;; the more appropriate one. If both fail, try STRING.
+
+ ;; At first try UTF8_STRING.
+ (setq utf8 (condition-case nil
+ (x-get-selection type 'UTF8_STRING)
+ (error nil))
+ utf8-coding last-coding-system-used)
+ (if utf8
+ ;; If it is a local selection, or it contains only
+ ;; ASCII characers, choose it.
+ (if (or (not (get-text-property 0 'foreign-selection utf8))
+ (= (length utf8) (string-bytes utf8)))
+ (setq text utf8)))
+ ;; If not yet decided, try COMPOUND_TEXT.
+ (if (not text)
+ (if (setq ctext (condition-case nil
+ (x-get-selection type 'COMPOUND_TEXT)
+ (error nil)))
+ ;; If UTF8_STRING was also successful, choose the
+ ;; more appropriate one from UTF8 and CTEXT.
+ (if utf8
+ (setq text (x-select-utf8-or-ctext utf8 ctext))
+ ;; Othewise, choose CTEXT.
+ (setq text ctext))
+ (setq text utf8)))
+ ;; If not yet decided, try STRING.
+ (or text
+ (setq text (condition-case nil
+ (x-get-selection type 'STRING)
+ (error nil))))
+ (if (eq text utf8)
+ (setq last-coding-system-used utf8-coding))))
+
+ ((consp x-select-request-type)
+ (let ((tail x-select-request-type))
+ (while (and tail (not text))
+ (condition-case nil
+ (setq text (x-get-selection type (car tail)))
+ (error nil))
+ (setq tail (cdr tail)))))
+
+ (t
+ (condition-case nil
+ (setq text (x-get-selection type x-select-request-type))
+ (error nil))))
+
+ (if text
+ (remove-text-properties 0 (length text) '(foreign-selection nil) text))
+ text))