When I use emacs in a terminal window on linux, I am often getting spaces unexpectedly converted to tabs when I cut and paste using the X11 primary selection. What reproduces the problem for me is the following:
Open a terminal window. (I tested using both lxterminal and aterm.) Start emacs with the -nw option. Type the following text character by character on the keyboard:
1234567 1234567
(If you copy and paste it from the stackexchange question, it won't reproduce the problem.) Triple-click on this line of text to copy it to the X11 primary selection. Open a new terminal window, and in that window do od -a
. Click the middle mouse button to paste the text into the terminal. This is echoed by the od command with an ht
rather than an sp
.
I'm pretty sure this is emacs doing this, because if I use the lightweight editor mg instead, it doesn't happen. I've also tested that this still happens if I hide my .emacs file, so that it's not resulting from any customizations that I've done there.
Can anyone explain how to get rid of this behavior?
Update: Gilles' comment shows that this happens at line 265 of this file https://github.com/emacs-mirror/emacs/blob/master/src/cm.c , which has the following code: if (tabcost < (deltax * tty->Wcm->cc_right)) {
. I would like to find a solution for this problem that doesn't involve my having to make my own private fork of emacs. I would be happy to see an answer that would suggest an approach to doing this by submitting a patch to emacs. Perhaps this behavior could be turned off by setting a certain flag in lisp. If that seems like a reasonable approach, then it would be great to see an answer saying so, and maybe pointing to an example of how well-behaved emacs C code would access such a flag. If I was an emacs maintainer, I would just rip all the code out, because I just don't think this is reasonable behavior for a terminal-based application in the year 2020, but possibly some people depend on this behavior and it would cause problems if it were simply removed.
indent-tabs-mode
to nil have any effect on it?lxterminal -e strace -o $(tty) -e read,write emacs -Q -nw
then in Emacs type1234567
and a space. When you type the space (read(6, " ", 1)
), Emacs writes a tab (write(6, "\t", 1)
). Whether this happens depends on how Emacs repaints the screen, for example it doesn't happen if you switch to another buffer then switch back, or if you scroll away and back.