Closes https://github.com/kovidgoyal/kitty/issues/1884. From a337c56848.
a337c56848
Reduces the difference to upstream. From ab118b2529.
ab118b2529