Function: gui-get-selection

gui-get-selection is a byte-compiled function defined in select.el.gz.

Signature

(gui-get-selection &optional TYPE DATA-TYPE)

Documentation

Return the value of an X Windows selection.

The argument TYPE (default PRIMARY) says which selection, and the argument DATA-TYPE (default STRING) says how to convert the data.

TYPE may be any symbol (but nil stands for PRIMARY). However, only a few symbols are commonly used. They conventionally have all upper-case names. The most often used ones, in addition to PRIMARY, are SECONDARY and CLIPBOARD.

DATA-TYPE is usually STRING, but can also be one of the symbols in selection-converter-alist, which see. Window systems other than X usually support only a small subset of these symbols, in addition to STRING; MS-Windows supports TARGETS, which reports the formats available in the clipboard if TYPE is CLIPBOARD.

Probably introduced at or before Emacs version 25.1.

Aliases

evil-get-selection x-selection (obsolete since at least 19.34) x-get-selection (obsolete since 25.1)

Source Code

;; Defined in /usr/src/emacs/lisp/select.el.gz
(defun gui-get-selection (&optional type data-type)
  "Return the value of an X Windows selection.
The argument TYPE (default `PRIMARY') says which selection,
and the argument DATA-TYPE (default `STRING') says
how to convert the data.

TYPE may be any symbol \(but nil stands for `PRIMARY').  However,
only a few symbols are commonly used.  They conventionally have
all upper-case names.  The most often used ones, in addition to
`PRIMARY', are `SECONDARY' and `CLIPBOARD'.

DATA-TYPE is usually `STRING', but can also be one of the symbols
in `selection-converter-alist', which see.  Window systems other
than X usually support only a small subset of these symbols, in
addition to `STRING'; MS-Windows supports `TARGETS', which reports
the formats available in the clipboard if TYPE is `CLIPBOARD'."
  (let ((data (gui-backend-get-selection (or type 'PRIMARY)
                                         (or data-type 'STRING))))
    (when (and (stringp data)
	       (setq data-type (get-text-property 0 'foreign-selection data)))
      (let ((coding (or next-selection-coding-system
                        selection-coding-system
                        (pcase data-type
                          ('UTF8_STRING 'utf-8)
                          ('COMPOUND_TEXT 'compound-text-with-extensions)
                          ('C_STRING nil)
                          ('STRING 'iso-8859-1)
                          (_ (error "Unknown selection data type: %S"
                                    type))))))
        (setq data (if coding (decode-coding-string data coding)
                     ;; This is for C_STRING case.
                     ;; We want to convert each non-ASCII byte to the
                     ;; corresponding eight-bit character, which has
                     ;; a codepoint >= #x3FFF00.
                     (string-to-multibyte data))))
      (setq next-selection-coding-system nil)
      (put-text-property 0 (length data) 'foreign-selection data-type data))
    data))