The behavior I'm going to describe is not necessarily a bug or problem with iterm2 but it might be.
First, let me describe the activity:
(1) open an emacs buffer and edit some code that uses tabs for indentation
(2) copy/paste some of that code into another app, like slack, into a fixed-width-font area (e.g., a code block ``` ```)
Observe:
Sometimes, you end up with wrong indentation in the destination.
I believe the "wrong indentation" is because the paste uses spaces for the indentation of most lines, but certain lines contain actual tab characters.
Obviously, iterm2 can't know the contents of the file being displayed by emacs and must have a model of what's displayed on screen. If I were to "cat" a text file that contains tabs and then copy/paste the contents of the terminal, I might reasonably expect to get tabs copy/pasted. On the other hand, it is also reasonable to expect that I get one space for each displayed fixed-width blank area in the terminal region I'm copying from.
I also can't expect iterm2 to know how I expect it to deal with tabs vs spaces in its copy/paste buffer when pasting. It has an "Advanced Paste..." menu and a "Prompt to convert tabs to spaces" feature -- but these are useful only when pasting into iterm2.
It certainly would be nice to be able to affect the paste behavior when pulling data out of iterm2 as well. E.g., by telling it to prepare the paste buffer so that it reflects the physical layout by having represented all whitespace as spaces.
Separately, there's the question of why iterm2 only sometimes (vs consistently) thinks there's a tab in the indentation whitespace of a displayed emacs buffer. It must have to do with what emacs emits to draw the screen.
When I first load a buffer that is tabified, the copy/paste output from iterm2 seems to consistently NOT include tabs. *IF* you then switch to another buffer (e.g., using `electric-buffer-list`) and *IF* there is a blank line in the destination buffer then, when you switch back to the original buffer, and *IF* there is a tabified indented line in the original buffer that is at the same physical line location where the blank line was *THEN* iterm2 will end up with an actual physical tab in the paste buffer if you try to copy/paste that region.
I don't know if maybe emacs is emitting a tab vs a space to draw a blank on the previously empty line. It seems iterm2 believes there to be only one tab on the line, adjacent to the left border of the terminal. The rest of the whitespace on that line is copy/pasted as spaces.
So, it seems that something about the terminal sequences emitted by emacs to redraw the original buffer, filling in the blank line with the original tabified source, causes a single physical tab to exist in iterm2's model of what's on screen. The location of the cursor does not seem to be relevant.
Thoughts?