junegunn / fzf

:cherry_blossom: A command-line fuzzy finder
https://junegunn.github.io/fzf/
MIT License
65.89k stars 2.41k forks source link

urxvt alternate screen left in no linewrap mode, breaking tmux/nvim after fzf #4099

Open liskin opened 1 week ago

liskin commented 1 week ago

Checklist

Output of fzf --version

0.56.3 (add1aec)

OS

Shell

Problem / Steps to reproduce

  1. start urxvt
  2. fzf <<<a, press enter
  3. tmux
  4. cat a file with long lines, they won't be split into multiple lines

I've diagnosed what's causing this and I believe it's because of https://github.com/junegunn/fzf/commit/4cd37fc02b4491d4c9a5f6a27ad41895ad1a62cc - wrapping \e[?1049l (exit alternate screen) in a pair of \e[?7l (no wrap) and \e[?7h (wrap) causes urxvt to preserve the no wrap mode in the alternate screen flags. For reasons I don't quite understand, neither tmux nor neovim reset the wrap flag but they just assume it's at its default value (wrap enabled), so once they switch to the alternate screen, the wrap is disabled but they don't expect that.

The same broken terminal state that fzf leaves behind can be reproduced by doing tput smcup; tput rmam; tput rmcup.

I think the fix might involve not using the flush wrapper for CSI sequences, as these can be output directly with no worry about line wrapping.

(It's worth noting that xterm does not save/restore the wrap flag when switching to/from the alternate screen and therefore isn't affected by this issue, and other terminals might implement this in a different way as well.)