1;;; select.el --- lisp portion of standard selection support  -*- lexical-binding:t -*-
2
3;; Copyright (C) 1993-1994, 2001-2021 Free Software Foundation, Inc.
4
5;; Maintainer: emacs-devel@gnu.org
6;; Keywords: internal
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
22
23;;; Commentary:
24
25;; Based partially on earlier release by Lucid.
26
27;; The functionality here is divided in two parts:
28;; - Low-level: gui-get-selection, gui-set-selection, gui-selection-owner-p,
29;;   gui-selection-exists-p are the backend-dependent functions meant to access
30;;   various kinds of selections (CLIPBOARD, PRIMARY, SECONDARY).
31;; - Higher-level: gui-select-text and gui-selection-value go together to
32;;   access the general notion of "GUI selection" for interoperation with other
33;;   applications.  This can use either the clipboard or the primary selection,
34;;   or both or none according to select-enable-clipboard/primary.  These are
35;;   the default values of interprogram-cut/paste-function.
36;;   Additionally, there's gui-get-primary-selection which is used to get the
37;;   PRIMARY selection, specifically for mouse-yank-primary.
38
39;;; Code:
40
41(defcustom selection-coding-system nil
42  "Coding system for communicating with other programs.
43
44For MS-Windows and MS-DOS:
45When sending or receiving text via selection and clipboard, the text
46is encoded or decoded by this coding system.  The default value is
47the current system default encoding on 9x/Me, `utf-16le-dos'
48\(Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS.
49
50For X Windows:
51When sending text via selection and clipboard, if the target
52data-type matches this coding system according to the table
53below, it is used for encoding the text.  Otherwise (including
54the case that this variable is nil), a proper coding system is
55selected as below:
56
57data-type	coding system
58---------	-------------
59UTF8_STRING	utf-8
60COMPOUND_TEXT	compound-text-with-extensions
61STRING		iso-latin-1
62C_STRING	raw-text-unix
63
64When receiving text, if this coding system is non-nil, it is used
65for decoding regardless of the data-type.  If this is nil, a
66proper coding system is used according to the data-type as above.
67
68See also the documentation of the variable `x-select-request-type' how
69to control which data-type to request for receiving text.
70
71The default value is nil."
72  :type 'coding-system
73  :group 'mule
74  ;; Default was compound-text-with-extensions in 22.x (pre-unicode).
75  :version "23.1"
76  :set (lambda (symbol value)
77         (set-selection-coding-system value)
78         (set symbol value)))
79
80(defvar next-selection-coding-system nil
81  "Coding system for the next communication with other programs.
82Usually, `selection-coding-system' is used for communicating with
83other programs (X Windows clients or MS Windows programs).  But, if this
84variable is set, it is used for the next communication only.
85After the communication, this variable is set to nil.")
86
87;; Only declared obsolete in 23.3.
88(define-obsolete-function-alias 'x-selection 'x-get-selection "at least 19.34")
89
90(define-obsolete-variable-alias 'x-select-enable-clipboard
91  'select-enable-clipboard "25.1")
92(defcustom select-enable-clipboard t
93  "Non-nil means cutting and pasting uses the clipboard.
94This can be in addition to, but in preference to, the primary selection,
95if applicable (i.e. under X11)."
96  :type 'boolean
97  :group 'killing
98  ;; The GNU/Linux version changed in 24.1, the MS-Windows version did not.
99  :version "24.1")
100
101(define-obsolete-variable-alias 'x-select-enable-primary
102  'select-enable-primary "25.1")
103(defcustom select-enable-primary nil
104  "Non-nil means cutting and pasting uses the primary selection.
105The existence of a primary selection depends on the underlying GUI you use.
106E.g. it doesn't exist under MS-Windows."
107  :type 'boolean
108  :group 'killing
109  :version "25.1")
110
111;; We keep track of the last text selected here, so we can check the
112;; current selection against it, and avoid passing back our own text
113;; from gui-selection-value.  We track both
114;; separately in case another X application only sets one of them
115;; we aren't fooled by the PRIMARY or CLIPBOARD selection staying the same.
116
117(defvar gui--last-selected-text-clipboard nil
118  "The value of the CLIPBOARD selection last seen.")
119(defvar gui--last-selected-text-primary nil
120  "The value of the PRIMARY selection last seen.")
121
122(defun gui-select-text (text)
123  "Select TEXT, a string, according to the window system.
124if `select-enable-clipboard' is non-nil, copy TEXT to the system's clipboard.
125If `select-enable-primary' is non-nil, put TEXT in the primary selection.
126
127MS-Windows does not have a \"primary\" selection."
128  (when select-enable-primary
129    (gui-set-selection 'PRIMARY text)
130    (setq gui--last-selected-text-primary text))
131  (when select-enable-clipboard
132    ;; When cutting, the selection is cleared and PRIMARY
133    ;; set to the empty string.  Prevent that, PRIMARY
134    ;; should not be reset by cut (Bug#16382).
135    (setq saved-region-selection text)
136    (gui-set-selection 'CLIPBOARD text)
137    (setq gui--last-selected-text-clipboard text)))
138(define-obsolete-function-alias 'x-select-text 'gui-select-text "25.1")
139
140(defcustom x-select-request-type nil
141  "Data type request for X selection.
142The value is one of the following data types, a list of them, or nil:
143  `COMPOUND_TEXT', `UTF8_STRING', `STRING', `TEXT', `text/plain\\;charset=utf-8'
144
145If the value is one of the above symbols, try only the specified type.
146
147If the value is a list of them, try each of them in the specified
148order until succeed.
149
150The value nil is the same as the list (UTF8_STRING COMPOUND_TEXT STRING
151text/plain\\;charset=utf-8)."
152  :type '(choice (const :tag "Default" nil)
153		 (const COMPOUND_TEXT)
154		 (const UTF8_STRING)
155		 (const STRING)
156		 (const TEXT)
157                 (const text/plain\;charset=utf-8)
158		 (set :tag "List of values"
159		      (const COMPOUND_TEXT)
160		      (const UTF8_STRING)
161		      (const STRING)
162		      (const TEXT)
163                      (const text/plain\;charset=utf-8)))
164  :group 'killing)
165
166(defun gui--selection-value-internal (type)
167  "Get a selection value of type TYPE.
168Call `gui-get-selection' with an appropriate DATA-TYPE argument
169decided by `x-select-request-type'.  The return value is already
170decoded.  If `gui-get-selection' signals an error, return nil."
171  (let ((request-type (if (memq window-system '(x pgtk))
172                          (or x-select-request-type
173                              '(UTF8_STRING COMPOUND_TEXT STRING text/plain\;charset=utf-8))
174                        'STRING))
175	text)
176    (with-demoted-errors "gui-get-selection: %S"
177      (if (consp request-type)
178          (while (and request-type (not text))
179            (setq text (gui-get-selection type (car request-type)))
180            (setq request-type (cdr request-type)))
181        (setq text (gui-get-selection type request-type))))
182    (if text
183	(remove-text-properties 0 (length text) '(foreign-selection nil) text))
184    text))
185
186(defun gui-selection-value ()
187  (let ((clip-text
188         (when select-enable-clipboard
189           (let ((text (gui--selection-value-internal 'CLIPBOARD)))
190             (when (string= text "")
191               (setq text nil))
192             ;; When `select-enable-clipboard' is non-nil,
193             ;; killing/copying text (with, say, `C-w') will push the
194             ;; text to the clipboard (and store it in
195             ;; `gui--last-selected-text-clipboard').  We check
196             ;; whether the text on the clipboard is identical to this
197             ;; text, and if so, we report that the clipboard is
198             ;; empty.  See (bug#27442) for further discussion about
199             ;; this DWIM action, and possible ways to make this check
200             ;; less fragile, if so desired.
201             (prog1
202                 (unless (equal text gui--last-selected-text-clipboard)
203                   text)
204               (setq gui--last-selected-text-clipboard text)))))
205        (primary-text
206         (when select-enable-primary
207           (let ((text (gui--selection-value-internal 'PRIMARY)))
208             (if (string= text "") (setq text nil))
209             ;; Check the PRIMARY selection for 'newness', is it different
210             ;; from what we remembered them to be last time we did a
211             ;; cut/paste operation.
212             (prog1
213                 (unless (equal text gui--last-selected-text-primary)
214                   text)
215               (setq gui--last-selected-text-primary text))))))
216
217    ;; As we have done one selection, clear this now.
218    (setq next-selection-coding-system nil)
219
220    ;; At this point we have recorded the current values for the
221    ;; selection from clipboard (if we are supposed to) and primary.
222    ;; So return the first one that has changed
223    ;; (which is the first non-null one).
224    ;;
225    ;; NOTE: There will be cases where more than one of these has
226    ;; changed and the new values differ.  This indicates that
227    ;; something like the following has happened since the last time
228    ;; we looked at the selections: Application X set all the
229    ;; selections, then Application Y set only one of them.
230    ;; In this case since we don't have
231    ;; timestamps there is no way to know what the 'correct' value to
232    ;; return is.  The nice thing to do would be to tell the user we
233    ;; saw multiple possible selections and ask the user which was the
234    ;; one they wanted.
235    (or clip-text primary-text)
236    ))
237
238(define-obsolete-function-alias 'x-selection-value 'gui-selection-value "25.1")
239
240(defun x-get-clipboard ()
241  "Return text pasted to the clipboard."
242  (declare (obsolete gui-get-selection "25.1"))
243  (gui-backend-get-selection 'CLIPBOARD 'STRING))
244
245(defun gui-get-primary-selection ()
246  "Return the PRIMARY selection, or the best emulation thereof."
247  (or (gui--selection-value-internal 'PRIMARY)
248      (and (fboundp 'w32-get-selection-value)
249           (eq (framep (selected-frame)) 'w32)
250           ;; MS-Windows emulates PRIMARY in x-get-selection, but only
251           ;; within the Emacs session, so consult the clipboard if
252           ;; primary is not found.
253           (w32-get-selection-value))
254      (error "No selection is available")))
255(define-obsolete-function-alias 'x-get-selection-value
256  'gui-get-primary-selection "25.1")
257
258;;; Lower-level, backend dependent selection handling.
259
260(cl-defgeneric gui-backend-get-selection (_selection-symbol _target-type)
261  "Return selected text.
262SELECTION-SYMBOL is typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
263\(Those are literal upper-case symbol names, since that's what X expects.)
264TARGET-TYPE is the type of data desired, typically `STRING'."
265  nil)
266
267(cl-defgeneric gui-backend-set-selection (_selection _value)
268  "Method to assert a selection of type SELECTION and value VALUE.
269SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
270If VALUE is nil and we own the selection SELECTION, disown it instead.
271Disowning it means there is no such selection.
272\(Those are literal upper-case symbol names, since that's what X expects.)
273VALUE is typically a string, or a cons of two markers, but may be
274anything that the functions on `selection-converter-alist' know about."
275  nil)
276
277(cl-defgeneric gui-backend-selection-owner-p (_selection)
278  "Whether the current Emacs process owns the given X Selection.
279The arg should be the name of the selection in question, typically one of
280the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
281\(Those are literal upper-case symbol names, since that's what X expects.)"
282  nil)
283
284(cl-defgeneric gui-backend-selection-exists-p (_selection)
285  "Whether there is an owner for the given X Selection.
286The arg should be the name of the selection in question, typically one of
287the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
288\(Those are literal upper-case symbol names, since that's what X expects.)"
289  nil)
290
291(defun gui-get-selection (&optional type data-type)
292  "Return the value of an X Windows selection.
293The argument TYPE (default `PRIMARY') says which selection,
294and the argument DATA-TYPE (default `STRING') says
295how to convert the data.
296
297TYPE may be any symbol \(but nil stands for `PRIMARY').  However,
298only a few symbols are commonly used.  They conventionally have
299all upper-case names.  The most often used ones, in addition to
300`PRIMARY', are `SECONDARY' and `CLIPBOARD'.
301
302DATA-TYPE is usually `STRING', but can also be one of the symbols
303in `selection-converter-alist', which see.  Window systems other
304than X usually support only a small subset of these symbols, in
305addition to `STRING'; MS-Windows supports `TARGETS', which reports
306the formats available in the clipboard if TYPE is `CLIPBOARD'."
307  (let ((data (gui-backend-get-selection (or type 'PRIMARY)
308                                         (or data-type 'STRING))))
309    (when (and (stringp data)
310               ;; If this text property is set, then the data needs to
311               ;; be decoded -- otherwise it has already been decoded
312               ;; by the lower level functions.
313               (get-text-property 0 'foreign-selection data))
314      (let ((coding (or next-selection-coding-system
315                        selection-coding-system
316                        (pcase data-type
317                          ('UTF8_STRING 'utf-8)
318                          ('text/plain\;charset=utf-8 'utf-8)
319                          ('COMPOUND_TEXT 'compound-text-with-extensions)
320                          ('C_STRING nil)
321                          ('STRING 'iso-8859-1)))))
322        (setq data
323              (cond (coding (decode-coding-string data coding))
324                     ;; We want to convert each non-ASCII byte to the
325                     ;; corresponding eight-bit character, which has
326                     ;; a codepoint >= #x3FFF00.
327                    ((eq data-type 'C_STRING)
328                     (string-to-multibyte data))
329                    ;; Guess at the charset for types like text/html
330                    ;; -- it can be anything, and different
331                    ;; applications use different encodings.
332                    ((string-match-p "\\`text/" (symbol-name data-type))
333                     (decode-coding-string
334                      data (car (detect-coding-string data))))
335                    ;; Do nothing.
336                    (t data))))
337      (setq next-selection-coding-system nil)
338      (put-text-property 0 (length data) 'foreign-selection data-type data))
339    data))
340(define-obsolete-function-alias 'x-get-selection 'gui-get-selection "25.1")
341
342(defun gui-set-selection (type data)
343  "Make an X selection of type TYPE and value DATA.
344The argument TYPE (nil means `PRIMARY') says which selection, and
345DATA specifies the contents.  TYPE must be a symbol.  \(It can also
346be a string, which stands for the symbol with that name, but this
347is considered obsolete.)  DATA may be a string, a symbol, an
348integer (or a cons of two integers or list of two integers).
349
350The selection may also be a cons of two markers pointing to the same buffer,
351or an overlay.  In these cases, the selection is considered to be the text
352between the markers *at whatever time the selection is examined*.
353Thus, editing done in the buffer after you specify the selection
354can alter the effective value of the selection.
355
356The data may also be a vector of valid non-vector selection values.
357
358The return value is DATA.
359
360Interactively, this command sets the primary selection.  Without
361prefix argument, it reads the selection in the minibuffer.  With
362prefix argument, it uses the text of the region as the selection value.
363
364Note that on MS-Windows, primary and secondary selections set by Emacs
365are not available to other programs."
366  (interactive (if (not current-prefix-arg)
367		   (list 'PRIMARY (read-string "Set text for pasting: "))
368		 (list 'PRIMARY (buffer-substring (region-beginning) (region-end)))))
369  (if (stringp type) (setq type (intern type)))
370  (or (gui--valid-simple-selection-p data)
371      (and (vectorp data)
372	   (let ((valid t))
373	     (dotimes (i (length data))
374	       (or (gui--valid-simple-selection-p (aref data i))
375		   (setq valid nil)))
376	     valid))
377      (signal 'error (list "invalid selection" data)))
378  (or type (setq type 'PRIMARY))
379  (gui-backend-set-selection type data)
380  data)
381(define-obsolete-function-alias 'x-set-selection 'gui-set-selection "25.1")
382
383(defun gui--valid-simple-selection-p (data)
384  (or (bufferp data)
385      (and (consp data)
386	   (markerp (car data))
387	   (markerp (cdr data))
388	   (marker-buffer (car data))
389	   (buffer-live-p (marker-buffer (car data)))
390	   (eq (marker-buffer (car data))
391	       (marker-buffer (cdr data))))
392      (stringp data)
393      (and (overlayp data)
394	   (overlay-buffer data)
395	   (buffer-live-p (overlay-buffer data)))
396      (symbolp data)
397      (integerp data)))
398
399;; Functions to convert the selection into various other selection types.
400;; Every selection type that Emacs handles is implemented this way, except
401;; for TIMESTAMP, which is a special case.
402
403(defun xselect--selection-bounds (value)
404  "Return bounds of X selection value VALUE.
405The return value is a list (BEG END BUF) if VALUE is a cons of
406two markers or an overlay.  Otherwise, it is nil."
407  (cond ((bufferp value)
408	 (with-current-buffer value
409	   (when (mark t)
410	     (list (mark t) (point) value))))
411	((and (consp value)
412	      (markerp (car value))
413	      (markerp (cdr value)))
414	 (when (and (marker-buffer (car value))
415		    (buffer-name (marker-buffer (car value)))
416		    (eq (marker-buffer (car value))
417			(marker-buffer (cdr value))))
418	   (list (marker-position (car value))
419		 (marker-position (cdr value))
420		 (marker-buffer (car value)))))
421	((overlayp value)
422	 (when (overlay-buffer value)
423	   (list (overlay-start value)
424		 (overlay-end value)
425		 (overlay-buffer value))))))
426
427(defun xselect--int-to-cons (n)
428  (cons (ash n -16) (logand n 65535)))
429
430(defun xselect--encode-string (type str &optional can-modify)
431  (when str
432    ;; If TYPE is nil, this is a local request; return STR as-is.
433    (if (null type)
434	str
435      ;; Otherwise, encode STR.
436      (let ((coding (or next-selection-coding-system
437			selection-coding-system)))
438	(if coding
439	    (setq coding (coding-system-base coding)))
440	(let ((inhibit-read-only t))
441	  ;; Suppress producing escape sequences for compositions.
442	  ;; But avoid modifying the string if it's a buffer name etc.
443	  (unless can-modify (setq str (substring str 0)))
444	  (remove-text-properties 0 (length str) '(composition nil) str)
445	  ;; For X selections, TEXT is a polymorphic target; choose
446	  ;; the actual type from `UTF8_STRING', `COMPOUND_TEXT',
447	  ;; `STRING', and `C_STRING'.  On Nextstep, always use UTF-8
448	  ;; (see ns_string_to_pasteboard_internal in nsselect.m).
449	  (when (eq type 'TEXT)
450	    (cond
451	     ((featurep 'ns)
452	      (setq type 'UTF8_STRING))
453	     ((not (multibyte-string-p str))
454	      (setq type 'C_STRING))
455	     (t
456	      (let (non-latin-1 non-unicode eight-bit)
457                (mapc (lambda (x)
458                        (if (>= x #x100)
459                            (if (< x #x110000)
460                                (setq non-latin-1 t)
461                              (if (< x #x3FFF80)
462                                  (setq non-unicode t)
463                                (setq eight-bit t)))))
464		      str)
465		(setq type (if (or non-unicode
466				   (and
467				    non-latin-1
468				    ;; If a coding is specified for
469				    ;; selection, and that is
470				    ;; compatible with COMPOUND_TEXT,
471				    ;; use it.
472				    coding
473				    (eq (coding-system-get coding :mime-charset)
474					'x-ctext)))
475			       'COMPOUND_TEXT
476			     (if non-latin-1 'UTF8_STRING
477			       (if eight-bit 'C_STRING
478				 'STRING))))))))
479	  (cond
480	   ((eq type 'UTF8_STRING)
481	    (if (or (not coding)
482		    (not (eq (coding-system-type coding) 'utf-8)))
483		(setq coding 'utf-8))
484	    (setq str (encode-coding-string str coding)))
485
486	   ((eq type 'STRING)
487	    (if (or (not coding)
488		    (not (eq (coding-system-type coding) 'charset)))
489		(setq coding 'iso-8859-1))
490	    (setq str (encode-coding-string str coding)))
491
492	   ((eq type 'COMPOUND_TEXT)
493	    (if (or (not coding)
494		    (not (eq (coding-system-type coding) 'iso-2022)))
495		(setq coding 'compound-text-with-extensions))
496	    (setq str (encode-coding-string str coding)))
497
498	   ((eq type 'C_STRING)
499            ;; According to ICCCM Protocol v2.0 (para 2.7.1), C_STRING
500            ;; is a zero-terminated sequence of raw bytes that
501            ;; shouldn't be interpreted as text in any encoding.
502            ;; Therefore, if STR is unibyte (the normal case), we use
503            ;; it as-is; otherwise we assume some of the characters
504            ;; are eight-bit and ensure they are converted to their
505            ;; single-byte representation.
506            (or (null (multibyte-string-p str))
507                (setq str (encode-coding-string str 'raw-text-unix))))
508
509	   (t
510	    (error "Unknown selection type: %S" type)))))
511
512      ;; Most programs are unable to handle NUL bytes in strings.
513      (setq str (string-replace "\0" "\\0" str))
514
515      (setq next-selection-coding-system nil)
516      (cons type str))))
517
518(defun xselect-convert-to-string (_selection type value)
519  (let ((str (cond ((stringp value) value)
520		   ((setq value (xselect--selection-bounds value))
521		    (with-current-buffer (nth 2 value)
522		      (buffer-substring (nth 0 value)
523					(nth 1 value)))))))
524    (xselect--encode-string type str t)))
525
526(defun xselect-convert-to-length (_selection _type value)
527  (let ((len (cond ((stringp value)
528		    (length value))
529		   ((setq value (xselect--selection-bounds value))
530		    (abs (- (nth 0 value) (nth 1 value)))))))
531    (if len
532	(xselect--int-to-cons len))))
533
534(defun xselect-convert-to-targets (_selection _type _value)
535  ;; return a vector of atoms, but remove duplicates first.
536  (let* ((all (cons 'TIMESTAMP
537		    (cons 'MULTIPLE
538			  (mapcar 'car selection-converter-alist))))
539	 (rest all))
540    (while rest
541      (cond ((memq (car rest) (cdr rest))
542	     (setcdr rest (delq (car rest) (cdr rest))))
543	    ((eq (car (cdr rest)) '_EMACS_INTERNAL)  ; shh, it's a secret
544	     (setcdr rest (cdr (cdr rest))))
545	    (t
546	     (setq rest (cdr rest)))))
547    (apply 'vector all)))
548
549(defun xselect-convert-to-delete (selection _type _value)
550  (gui-backend-set-selection selection nil)
551  ;; A return value of nil means that we do not know how to do this conversion,
552  ;; and replies with an "error".  A return value of NULL means that we have
553  ;; done the conversion (and any side-effects) but have no value to return.
554  'NULL)
555
556(defun xselect-convert-to-filename (_selection _type value)
557  (when (setq value (xselect--selection-bounds value))
558    (xselect--encode-string 'TEXT (buffer-file-name (nth 2 value)))))
559
560(defun xselect-convert-to-charpos (_selection _type value)
561  (when (setq value (xselect--selection-bounds value))
562    (let ((beg (1- (nth 0 value))) ; zero-based
563	  (end (1- (nth 1 value))))
564      (cons 'SPAN (vector (xselect--int-to-cons (min beg end))
565			  (xselect--int-to-cons (max beg end)))))))
566
567(defun xselect-convert-to-lineno (_selection _type value)
568  (when (setq value (xselect--selection-bounds value))
569    (with-current-buffer (nth 2 value)
570      (let ((beg (line-number-at-pos (nth 0 value)))
571	    (end (line-number-at-pos (nth 1 value))))
572	(cons 'SPAN (vector (xselect--int-to-cons (min beg end))
573			    (xselect--int-to-cons (max beg end))))))))
574
575(defun xselect-convert-to-colno (_selection _type value)
576  (when (setq value (xselect--selection-bounds value))
577    (with-current-buffer (nth 2 value)
578      (let ((beg (progn (goto-char (nth 0 value)) (current-column)))
579	    (end (progn (goto-char (nth 1 value)) (current-column))))
580	(cons 'SPAN (vector (xselect--int-to-cons (min beg end))
581			    (xselect--int-to-cons (max beg end))))))))
582
583(defun xselect-convert-to-os (_selection _type _size)
584  (xselect--encode-string 'TEXT (symbol-name system-type)))
585
586(defun xselect-convert-to-host (_selection _type _size)
587  (xselect--encode-string 'TEXT (system-name)))
588
589(defun xselect-convert-to-user (_selection _type _size)
590  (xselect--encode-string 'TEXT (user-full-name)))
591
592(defun xselect-convert-to-class (_selection _type _size)
593  "Convert selection to class.
594This function returns the string \"Emacs\"."
595  "Emacs")
596
597;; We do not try to determine the name Emacs was invoked with,
598;; because it is not clean for a program's behavior to depend on that.
599(defun xselect-convert-to-name (_selection _type _size)
600  "Convert selection to name.
601This function returns the string \"emacs\"."
602  "emacs")
603
604(defun xselect-convert-to-integer (_selection _type value)
605  (and (integerp value)
606       (xselect--int-to-cons value)))
607
608(defun xselect-convert-to-atom (_selection _type value)
609  (and (symbolp value) value))
610
611(defun xselect-convert-to-identity (_selection _type value) ; used internally
612  (vector value))
613
614;; Null target that tells clipboard managers we support SAVE_TARGETS
615;; (see freedesktop.org Clipboard Manager spec).
616(defun xselect-convert-to-save-targets (selection _type _value)
617  (when (eq selection 'CLIPBOARD)
618    'NULL))
619
620(setq selection-converter-alist
621      '((TEXT . xselect-convert-to-string)
622	(COMPOUND_TEXT . xselect-convert-to-string)
623	(STRING . xselect-convert-to-string)
624	(UTF8_STRING . xselect-convert-to-string)
625	(TARGETS . xselect-convert-to-targets)
626	(LENGTH . xselect-convert-to-length)
627	(DELETE . xselect-convert-to-delete)
628	(FILE_NAME . xselect-convert-to-filename)
629	(CHARACTER_POSITION . xselect-convert-to-charpos)
630	(LINE_NUMBER . xselect-convert-to-lineno)
631	(COLUMN_NUMBER . xselect-convert-to-colno)
632	(OWNER_OS . xselect-convert-to-os)
633	(HOST_NAME . xselect-convert-to-host)
634	(USER . xselect-convert-to-user)
635	(CLASS . xselect-convert-to-class)
636	(NAME . xselect-convert-to-name)
637	(ATOM . xselect-convert-to-atom)
638	(INTEGER . xselect-convert-to-integer)
639	(SAVE_TARGETS . xselect-convert-to-save-targets)
640	(_EMACS_INTERNAL . xselect-convert-to-identity)))
641
642(provide 'select)
643
644;;; select.el ends here
645