sachac / subed

subed is a subtitle editor for Emacs
177 stars 16 forks source link

Fix "I"/"O" insertion on focus events when Emacs runs in terminal #72

Closed rndusr closed 3 days ago

rndusr commented 4 days ago

If Emacs runs in a terminal emulator, the characters "I"/"O" are inserted in the buffer every time the terminal window is focused/unfocused.

Emacs tells the terminal emulator to send focus in/out events, and they are sent as "\e[I" (focus in) and "\e[O" (focus out). "\e" is the escape character.

Alt/Meta combinations are sent as "\eC" where "C" is the key pressed together with Alt. For example, if you press M-x, the terminal emulator sends an escape character and "x".

And if you press M-[, "\e[" is sent which is indistinguishable from the beginning of the "\e[I" or "\e[O" event. For some reason I don't know, Emacs doesn't bother peeking at the next character. It sees "\e[", sees that this character sequence is bound and acts on it. Then it sees "I" or "O", which are just letters, so they are inserted as if you pressed those keys.

Further reading: https://emacs.stackexchange.com/questions/1020/problems-with-keybindings-when-using-terminal/13957#13957

This should be fixable in Emacs itself, but I have no idea exactly how, and the fact that such an annoying bug hasn't been fixed yet tells me it's actually not that easy.

This PR simply doesn't map M-[ if Emacs is running in a terminal emulator and uses C-M-[ instead.

sachac commented 3 days ago

Oooh, thanks for noticing that and figuring that out!