Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does X11 clipboard handle multiple data formats?

It probably happened to you as well - sometimes when you copy a text from some web page into your rich-text e-mail draft in your favorite webmail client, you dislike the fact that the pasted piece has a different font/size/weight.. it somehow remembers the style (often images, when selected). How is it than that if you paste the same into your favorite text editor like Vim, there's no HTML, just the plain text?

alt text

It seems that clipboard maintains the selected data in various formats. How can one access data in any one of those formats (programmatically or with some utility)? How does the X11 clipboard work?

like image 347
mykhal Avatar asked Aug 26 '10 00:08

mykhal


1 Answers

The app you copy from advertises formats (mostly identified by MIME types) it can provide. The app you paste into has to pick its preferred format and request that one from the source app.

The reason you may not see all style info transferred is that the apps don't both support a common format that includes the style info.

You can also see issues because an app may for example try to paste HTML, but not really be able to handle all HTML. Or the apps may be buggy, or may not agree on what a particular MIME type really means.

Almost all apps can both copy and paste plain text, of course, but beyond that it's touch and go. If you don't get what seems to make sense, you could file a bug vs. one of the apps.

You may notice that if you exit the app you're copying from, you can no longer paste. (Unless you're running a "clipboard manager" or something.) This is because no data actually leaves the source app until the destination app asks for a format to paste. There are "clipboard managers" that ask for data immediately anytime you copy and store that data, so you can paste after the source app exits, but they have downsides (what if the data is huge, or is offered in 10 formats, etc.)

The following python code will show available formats for the currently-copied data, if you have pygtk installed. This app shows the ctrl+c copied data, not the middle-click easter egg. (See http://freedesktop.org/wiki/Specifications/ClipboardsWiki)

#!/usr/bin/python  import gtk; clipboard = gtk.clipboard_get() print("Current clipboard offers formats: " + str(clipboard.wait_for_targets())) 
like image 99
Havoc P Avatar answered Oct 09 '22 12:10

Havoc P