X Selections, Cut Buffers, and Kill Rings.

© 1997, 2002 by Jamie Zawinski <jwz@jwz.org>

This document began as a description of how Emacs and XEmacs deal with copying and pasting text under X11. However, I've noticed that a lot of people are being referred here to answer more general questions of how copying and pasting work under X, or, more specifically, how they should implement copying and pasting in their own X applications. So allow me to summarize...

Selections: X11 has these things called ``selections.'' They have names. There are really only two you need to know about: the Primary selection and the Clipboard selection. An application is said to ``own'' a selection when it raises its hand and says, ``I have the Primary selection now.'' Only one application can own a selection at a time, so when one app asserts selection ownership, the previous owner loses it. Asserting ownership of a selection doesn't actually transfer data. When you want to copy text from application A to application B, it goes like this: Copy text in program A: program A asserts selection ownership.

Paste text in program B: program B notes that program A owns the selection.

program B asks A for the text.

program A responds, and sends the text to program B.

program B pastes the response. X10 had these things called ``Cut Buffers'' that were a more limited and wasteful predecessor to selections. They are obsolete; don't use them. Some applications (like xterm) still have legacy support for them, but ignore that. Clipboard: for when the user has explicitly copied something (e.g., the ``Edit/Copy'' menu item.) Primary: more ephemeral and implicit: it is for when the user has highlighted or selected something (e.g., dragging the mouse over some text.) Cut Buffers: Obsolete. Never, ever, ever use them. Ever. Copy and Paste: Contrary to what you may have come to believe, copying and pasting text under X11 works pretty much exactly the same way it does under MacOS and Windows. Really. It works like this: Select the text to copy;

Pull down the ``Edit'' menu and select ``Copy.''

This causes the text to become the Clipboard Selection.

This causes the text to become the Clipboard Selection. In another window, pull down the ``Edit'' menu and select ``Paste.''

This causes the current value of the Clipboard selection to be inserted. But what about the middle mouse button? It happens that X11 programs have a second way of copying and pasting text that is orthogonal to the Edit/Copy way described above. This causes confusion, because some people mix the two up. Here's how the other way works: Select the text to copy.

This causes the text to become the Primary Selection.

This causes the text to become the Primary Selection. In another window, click the middle mouse button.

This causes the current value of the Primary selection to be inserted. So how do I implement this? Event: Action: text highlighted

with mouse Assert PRIMARY selection ownership. Edit / Copy Make an internal copy of the highlighted text.

Assert CLIPBOARD selection ownership. Edit / Paste Retrieve and insert CLIPBOARD selection.

middle button paste,

if implemented Retrieve and insert PRIMARY selection.



another app asks

for PRIMARY Send currently-highlighted text, if any.

another app asks

for CLIPBOARD Send saved copy of last-copied text, if any.

another app becomes

PRIMARY owner De-highlight text.

another app becomes

CLIPBOARD owner Discard text copy.

Extra Credit: Content Negotiation One of the really cool, yet rarely used, features of the selection mechanism is that it can negotiate what data formats to use. It's not just about text. When one application asks another for the selection, part of their communication involves the requester asking the owner for the list of types in which they are capable of delivering the selection data; then the requester picks the format they like best, and asks for it that way. As a simple example, suppose there is a program displaying text in multiple fonts. When pasting that into a text-only program, you'd want to paste only the text. But when pasting that into a word processor, you'd want to keep the font information: if both applications spoke HTML, they could use that as the intermediate format by which they transferred the data. More complex things are possible, too: for example, when an image is selected on a web page, the web page displayer could offer to serve that up as raw image bits; or as JPEG data; or as the original URL of the image. When trying to copy and paste an image into a text editor that can't do images, the text editor might decide that the next best thing would be to paste the filename of the image, or the URL. The content negotiation mechansim is very powerful, and I wish more applications would take advantage of it. You can experiment with content negotiation with other apps from an XEmacs lisp-interaction buffer. To see what types an app will convert its selection to, make a selection in that app, and then type: (get-selection-internal 'PRIMARY 'TARGETS) ==> [ TARGETS TIMESTAMP TEXT STRING LENGTH FILE_NAME OWNER_OS HOST_NAME USER CLASS NAME CLIENT_WINDOW PROCESS COMPOUND_TEXT]

(get-selection-internal 'PRIMARY 'FILE_NAME) ==> "https://www.jwz.org/doc/x-cut-and-paste.html" ICCCM: The full technical documentation for this stuff is in the X11R6 Inter-Client Communication Conventions Manual, section 2: ``Peer-to-Peer Communication by Means of Selections.''





And now, about Emacs:





Message-ID: <3373165D.511EF610@netscape.com> Date: Fri, 09 May 1997 05:19:41 -0700 From: Jamie Zawinski <jwz@netscape.com> Newsgroups: comp.emacs Subject: Re: x-selection / kill-ring interaction

> Emacs: (car kill-ring) is always the same as the primary x selection, > therefore mouse2 and C-y pastes the same. mouse1 sets the > kill-ring as well as the primary selection.

Unless things have changed since the last time I checked, I don't believe this is true: (car kill-ring) is the same as the primary X selection only if a kill has been made in FSFmacs more recently than text has been selected in another (non-FSFmacs) X application.

> My question is: did the XEmacs people conduct a poll to determine > which behavior is preferred by the masses? (Actually, RMS asked me > this question).