how does vim distinguish ESC key from the escape sequence ?
Posted: Thu Jul 12, 2018 1:48 am
When we press home key, vim receives three ascii code: \e O H.
How does vim determine that the user pressed home, rather than pressed Esc O H.
One approach I thought of is: read() the stdin with a large count argument to drain it, test the return value. If larger than 1, we think it's an escape sequence generated by a function key.
However, there is a problem:
If vim ran on a remote server, and the user operated it through putty. The user wanted to insert a line above and insert a character H, so he pressed Esc O H. The three keystrokes were wrapped into three net packages and transferred to server. These packages arrived at the same time due to network congestion.
Thus, these three characters all appeared in the stdin buffer of vim at the same time, and were mistaken as an escape sequence of home.
I don't know how vim avoids such problem, and do you have a better approach?
How does vim determine that the user pressed home, rather than pressed Esc O H.
One approach I thought of is: read() the stdin with a large count argument to drain it, test the return value. If larger than 1, we think it's an escape sequence generated by a function key.
However, there is a problem:
If vim ran on a remote server, and the user operated it through putty. The user wanted to insert a line above and insert a character H, so he pressed Esc O H. The three keystrokes were wrapped into three net packages and transferred to server. These packages arrived at the same time due to network congestion.
Thus, these three characters all appeared in the stdin buffer of vim at the same time, and were mistaken as an escape sequence of home.
I don't know how vim avoids such problem, and do you have a better approach?