leo-arch / clifm

The shell-like, command line terminal file manager: simple, fast, extensible, and lightweight as hell.
https://github.com/leo-arch/clifm/wiki
GNU General Public License v2.0
1.29k stars 41 forks source link

On exit, CliFM breaks pasting in the terminal (xterm) #266

Closed danfe closed 6 months ago

danfe commented 6 months ago

I've noticed that after quitting CliFM, text in the clipboard would be pasted as ^[[200~SOME-TEXT^[[201~ where ^[ is your usual ESC \033. This makes it hard to work with (on command line, in the editor it's just unusable). Naïve tricks like tput reset or stty sane do not bring the terminal back to normal, however, starting and quitting mc (Midnight Commander) does. This is observed on FreeBSD/amd64 13.2, CliFM 1.16 (1.15 as well), and XTerm 382.

leo-arch commented 6 months ago

Hi @danfe! Thanks for reporting. Those are bracketed paste escape codes, so I'm pretty sure (though I've haven't reproduced the issue) that the thing is related to readline's config. Try this:

Edit readline's config file (~/.config/clifm/readline.clifm) and disable enable-bracketed-paste as follows:

set enable-bracketed-paste off

Or just comment out the line.

Restart clifm.

danfe commented 6 months ago

set enable-bracketed-paste off

This worked, thanks, nice and quick fix, albeit well-behaved programs should not leave the terminal in a troubled state regardless of the settings. :-)

Or just comment out the line.

This didn't work for some reason (tried it first).

leo-arch commented 6 months ago

Great! To be honest, I don't remember why this setting was set to on by default (indeed, the comment above -I mean, in the config file, clearly says "disable").

leo-arch commented 6 months ago

With the latest commit, bracketed-paste is disabled by default.

danfe commented 6 months ago

Thanks (referenced commit is fec3892). FWIW, issuing printf '\033[?2004l' can help one to disable bracketed paste mode if their terminal ended up in it for whatever reason.

leo-arch commented 6 months ago

well-behaved programs should not leave the terminal in a troubled state regardless of the settings

@danfe, you're right of course. Could you please give the following a try?

  1. Compile/run the latest version of clifm.
  2. Reenable bracketed-paste.
  3. Make sure the issue is gone.

The latest commit makes clifm reset the terminal to its previous state immediately before quitting.

danfe commented 6 months ago

Make sure the issue is gone.

Hmm, it's not gone, I still need to reset the terminal by hand.

leo-arch commented 6 months ago

Ok. I'm using tcsetattr(3) to reset the terminal to its original state, saved at startup. This works fine and gives us a more consistent behavior. I guess thereby that this bracketed paste thing is a readline-specific feature/issue (maybe some readline internal function might work, but I'm not sure).

Of course, I can always print \033[?2004l at exit -as you suggested- to disable the feature (as far as I know this is what the nano editor does). But, to make this the right way I should find out first whether the running terminal supports it, and secondly, print it only if the feature was actually enabled.

I'll keep this in mind. For the time being, keeping the thing off by default is quite safe.

danfe commented 6 months ago

For the time being, keeping the thing off by default is quite safe.

Fair enough, I agree.

leo-arch commented 6 months ago

Be it as it may, I still need to reproduce the issue. Could you please report the exact steps you take? How are you pasting to the terminal from the clipboard?

Running FreeBSD/amd64 14.0, Clifm 1.16.1 (tried also 1.14.6), on XTerm 385.

Personally, I'm using this on my .Xresources:

XTerm*VT100.Translations: #override \
    Ctrl Shift <Key>V: insert-selection(CLIPBOARD) \n\
    Ctrl Shift <Key>C: copy-selection(CLIPBOARD)

I can copy to the clipboard via Ctrl-C as always (in Leafpad, say) or with Ctrl-Shift-C in XTerm itself, and then Ctrl-Shift-V, either on clifm itself or, after exiting, on XTerm: works fine in both cases.

danfe commented 6 months ago

How are you pasting to the terminal from the clipboard?

I select the text with LMB, it goes to X11 primary selection buffer (others are: clipboard, secondary, buffer-cut). In the example below, I double-click on the word "true":

$ true
$ xclip -selection primary -o
true$ _

The above xclip(1) command yields the same result as clicking the MMB or pressing Shift-Insert. Now, if I run and exit CliFM:

$ true
$ xclip -selection primary -o
true$ hd
^[[200~true^[[201~
00000000  1b 5b 32 30 30 7e 74 72  75 65 1b 5b 32 30 31 7e

Interestingly, xclip(1) returns undecorated string, but not when pasting with keyboard or mouse. That being said, I do have a bunch of non-standard XTerm-related settings in my ~/.Xdefaults, I'll redo the tests in clean environment.

leo-arch commented 6 months ago

Reproduced! It depends on how you exit clifm though: it happens when you exit with Ctrl-D or F12, but not with the q/quit/exit command. Briefly put, the issue is triggered when you exit via a keybinding.

danfe commented 6 months ago

it happens when you exit with Ctrl-D or F12, but not with the q/quit/exit command.

Confirmed (I always exit with ^D from everywhere so long it's supported).

leo-arch commented 6 months ago

We're on the right track, very close! Give me a few minutes.

leo-arch commented 6 months ago

Done! Give it a try now.

danfe commented 6 months ago

Yup, commit 2262496 no longer exhibits the problem for me, thanks!

leo-arch commented 6 months ago

Thanks for your help @danfe! Closing.