Jonix

The Clipboard That Spoke Latin-1

I write in Swedish a lot. Swedish has three extra letters: å, ä and ö.

They are not decorative. Without them, “ägg” (eggs) becomes “agg” (a grudge), and nobody wants to let their partner eat a grudge at the breakfast table.

So when I copied a line from Emacs and pasted it into Signal, and got this:

jag �r tr�tt med �gren

I took it personally. Being a swedish developer, this has been haunting me from day 1, and 30+ years later it still haunts me.

The usual suspects

My first theory was Flatpak. Signal and Threema both run as Flatpaks on my machine, and Flatpak is a reliable scapegoat for anything strange on a Linux desktop, right?. Locales missing in the sandbox? No, both en_US.utf8 and sv_SE.utf8 were there. A Swedish keyboard layout lost on its way through Xwayland? No, the layout was Swedish everywhere, and typing worked fine.

Then came the clue that actually mattered. Copying the same sentence from gedit or from my terminal worked. Copying it from Emacs did not, not even from a bare emacs -Q. So it was not Flatpak, it was not my complex configuration, and it was not typing. It was the clipboard, and it was Emacs.

What is actually on the clipboard

On Wayland, a program that copies something does not hand over text. It hands over a promise: “I can give you this in the following formats.” The program that pastes picks one, and asks for it.

Emacs offers a generous menu:

TEXT  COMPOUND_TEXT  STRING  UTF8_STRING  text/plain  text/plain;charset=utf-8  ...

The upper-case names are X11 heirlooms from the 1980s. On X11, STRING means ISO-8859-1, also known as Latin-1, by definition. The lower-case ones are MIME types, which is how Wayland programs are supposed to talk.

So I asked for each format with wl-paste and looked at the bytes. In UTF-8, “ä” is two bytes, 195 and 164. In Latin-1 it is one byte, 228.

text/plain;charset=utf-8   106 97 103 32 195 164 ...   jag är
UTF8_STRING                106 97 103 32 195 164 ...   jag är
text/plain                 106 97 103 32 228 ...       jag �r
STRING                     106 97 103 32 228 ...       jag �r
TEXT                       106 97 103 32 228 ...       jag �r

There it was. Emacs was speaking Latin-1, fluently, to programs that only understand UTF-8.

Down the rabbit hole

The obvious fix was to make the text/plain converter in Emacs speak UTF-8. I did, and nothing changed. A little tracing showed why: Emacs never received a request for text/plain at all. It received a request for STRING.

The culprit sits in GTK 3, in its Wayland backend (gdk/wayland/gdkselection-wayland.c). When Emacs offers STRING, GTK helpfully offers it to Wayland as text/plain too. When somebody then asks for text/plain, GTK walks through Emacs’s list, finds STRING first, and turns the request back into STRING. Emacs, being a faithful X11 citizen, answers in Latin-1.

And Signal and Threema? They are Electron apps, built on Chromium, and Chromium picks the first text format it is offered. The first one in Emacs’s list was TEXT. Latin-1 again.

So it was nobody’s bug and everybody’s bug: a 1980s rule in Emacs, a helpful shortcut in GTK, and a browser that takes the first thing on the menu.

It was already reported

Before writing a patch for Emacs, I checked the bug tracker. Robert Pluim had reported exactly this in December 2025, as bug#79966. Po Lu, who maintains the PGTK code, had pushed back, and with a good reason: X11 programs running under Xwayland still ask for STRING and TEXT, and they expect Latin-1. Change that, and you break them instead.

That turned out to be the most useful thing I read all evening, because it ruled out my first patch.

The fix that keeps everyone happy

The second attempt changes the order, not the meaning. If the menu lists the MIME types first, Chromium picks text/plain;charset=utf-8, which Emacs already encodes correctly, and GTK finds the real text/plain before it ever reaches STRING:

(setq selection-converter-alist
      '((text/plain\;charset=utf-8 . xselect-convert-to-string)
        (text/plain . xselect-convert-to-string)
        (UTF8_STRING . xselect-convert-to-string)
        (TEXT . xselect-convert-to-string)
        (COMPOUND_TEXT . xselect-convert-to-string)
        (STRING . xselect-convert-to-string)
        ...))

A second, smaller change makes a bare text/plain UTF-8 when Emacs is on Wayland. Before, it was encoded as plain ASCII, which would have turned every “ä” into a “?”. STRING and TEXT keep their old meaning, so the Xwayland programs never notice.

The test that counted was not a unit test. It was pasting into Threema and seeing:

jag är trött med ågren

I am about to send the two patches to the bug thread. Until they land in a release, my own Emacs configuration carries a small workaround module that makes STRING and TEXT answer in UTF-8 as well. That is the blunt version of the fix, fine on a Wayland-only machine like mine, and it can go away once Emacs gets the polite one.

What I take away from it

I did most of the digging together with an AI assistant, which was very good at reading GTK source code at midnight. It was less good at noticing that its own test file was broken, I pushed back at the AI Assistant analysis and pointed it to the bug report, we came up with a better solution.