Skip to content

Conversation

@engival
Copy link

@engival engival commented Jun 13, 2025

Add universal MIME type support

New Features

  • --type MIMETYPE for any MIME type (e.g., --type "image/png", --type "text/html")
  • --html shortcut for --type "text/html"
  • --query option to list available clipboard targets
  • Smart text/binary distinction with proper fallback handling

Examples

Binary data (images):

# Copy PNG image to clipboard
cat /etc/kde/xdg/slackware_logo.png | xsel --type 'image/png' --input -b

# Now paste directly into GIMP

HTML content:
# Copy rich HTML to clipboard
echo '<b>Hello <i>World</i></b>' | xsel --html --input -b

# Now paste into Gmail compose window

Diagnostic to query clipboard contents:
# See what's available in clipboard
xsel --query -b
# Output might show: TARGETS, image/png, STRING, UTF8_STRING, etc.

Technical Details

  • Binary data fix: Saves input length instead of relying on strlen() (but only for mime type, doesn't change old behavior)
  • Smart fallbacks: Text-like MIME types (text/*, application/json, etc.) provide STRING/UTF8_STRING fallbacks for compatibility -- not a comprehensive list!
  • Binary barrier: Binary MIME types block text atom registration to prevent unwanted conversions

Compatibility

  • All existing functionality preserved
  • MIME type is only added when requested

Tested with GIMP (PNG images), Gmail (HTML), and various text editors.

engival added 2 commits June 13, 2025 10:00
- Add --type MIMETYPE option for any MIME type (e.g. image/png)
- Add --html shortcut for --type text/html
- Add --query option to list available clipboard targets
- Implement text/binary MIME type whitelist with fallback logic
- Binary MIME types (image/png) only register specific atom
- Text MIME types (text/html, application/json) register both specific + text fallbacks
- All existing functionality preserved for backwards compatibility
- Add sel_length global variable to track actual binary data size
- Update handle_mime_string() to use sel_length for binary data
- Use xs_strlen() for text data, actual byte count for binary data
- Fixes issue where xs_strlen() truncated binary data at null bytes
- GIMP can now successfully paste PNG images from xsel
@kratusch
Copy link

This is great—looks like I can finally drop xclip now that xsel supports MIME types and is also prioritized by default in neovim due to known issues with xclip (See neovim#20862 and neovim-docs).

I tested copying image/png data to the clipboard, and it works as expected—it can be pasted into other applications.

One request: could MIME types support also be extended to output operations? For example:

xsel --type 'image/png' --output --clipboard

Currently, this results in the following error:

X Error of failed request:  BadAtom (invalid Atom parameter)
  Major opcode of failed request:  24 (X_ConvertSelection)
  Atom id in failed request:  0x1c0
  Serial number of failed request:  25
  Current serial number in output stream:  26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants