reddit / emacs

Weird behaviour when yanking text copied from outside of Emacs running in WSL

FOSHavoc · 2022-04-08 07:56
This issue occurs in stock emacs (27.2 for me) launched with an `emacs -Q` running in WSL. This issue does not occur when I run Emacs and everything else natively on Linux. I run Emacs with a gui in WSL through an X server running on Windows. Emacs launches in WSL and then thanks to the magic of export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0 export LIBGL_ALWAYS_INDIRECT=1 it runs on the X server running on WIndows. I do this, as opposed to running Emacs on Windows, because all the code I develop and run targets Linux which I do through WSL so it makes my life easier to run everything through WSL - for example it makes it easier to launch vterm directly within WSL in the right place, not to mention the fact that this way only the display must cross the WSL/WIndows boundary and not the file contents itself. I don't use Emacs to edit files on the Windows side. When I yank I expect Emacs to yank the latest item I cut/copied/killed/copied-to-kill-ring. Generally, I found that Emacs's choice of what to yank matched my intuitive expectation, but I've recently found an exception to this which happens when running in WSL. If I copy some text from outside Emacs (e.g. a URL I want to paste) (and so also outside of WSL), if I yank immediately in Emacs it will yank that copied text as expected. However, if I first select a region (e.g. because I want to insert-link with C-c C-l) then Emacs will yank the text from that region instead (even if I don't run any copy-to-kill-ring command). This is reproducible in the scratch buffer in fundamental-mode. In a scratch buffer, I get this unexpected behaviour if I copy the URL from outside of Emacs (and so also outside WSL), switch to Emacs, highlight a region, cancel my selection with `C-g`, and then yank, Emacs will yank the text that was briefly highlighted, not the text from outside of Emacs. I've narrowed down the issue to the return value of `gui-selection-value`. This function is called from `current-kill` which calls `interprogram-paste-function` which is by default set to `gui-selection-value`. I found that the unintuitive behaviour stems from the fact that `gui-selection-value` returns the Emacs highlighted region of the previous thing I copied was outside of Emacs (if the previous thing I copied was in Emacs, it returns `nil` as expected). It's clearly an issue with the Emacs/WSL combination, but I don't really know where I would look to fix this. Any ideas?
yep808 · 2022-04-08 18:25
Have you tried upgrading to the current stable 28.1? Also, do you see the same problem with WSLg? What was the x server you used, vcxsrv?