From af5d2d95235c677ab4e80103f1e2dd4a6fd65d87 Mon Sep 17 00:00:00 2001 From: Jay McCarthy Date: Wed, 28 Nov 2018 16:27:18 -0500 Subject: [PATCH 1/5] Parse xterm-keys for motion directly Ideally, something better should be done (re #2554) but this is a decent intermediate step for some useful keys. Note: NCurses supports parsing these keys when shifted (KEY_SR, _SLEFT, S_RIGHT, etc), but it does not do the same thing for the other modifiers. --- src/keys.hh | 4 ++++ src/ncurses_ui.cc | 45 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/keys.hh b/src/keys.hh index a7684f22..57b4fffb 100644 --- a/src/keys.hh +++ b/src/keys.hh @@ -109,6 +109,10 @@ constexpr Key ctrl(Key key) { return { key.modifiers | Key::Modifiers::Control, key.key }; } +constexpr Key shift_alt(Key k) { return shift(alt(k)); } +constexpr Key shift_ctrl(Key k) { return shift(ctrl(k)); } +constexpr Key alt_ctrl(Key k) { return alt(ctrl(k)); } +constexpr Key shift_alt_ctrl(Key k) { return shift(alt(ctrl(k))); } constexpr Codepoint encode_coord(DisplayCoord coord) { return (Codepoint)(((int)coord.line << 16) | ((int)coord.column & 0x0000FFFF)); } diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index aa6b9d7d..519910c1 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -661,12 +661,51 @@ Optional NCursesUI::get_next_key() const int new_c = wgetch(m_window); if (new_c == '[') // potential CSI { - const Codepoint csi_val = wgetch(m_window); - switch (csi_val) + const Codepoint c1 = wgetch(m_window); + switch (c1) { case 'I': return {Key::FocusIn}; case 'O': return {Key::FocusOut}; - default: break; // nothing + case '1': + { + const Codepoint c2 = wgetch(m_window); + if ( c2 != ';' ) + { + ungetch(c2); ungetch(c1); break; + } + + const Codepoint c3 = wgetch(m_window); + Key (*f)(Key) = NULL; + switch (c3) + { + case '2': f = shift; break; + case '3': f = alt; break; + case '4': f = shift_alt; break; + case '5': f = ctrl; break; + case '6': f = shift_ctrl; break; + case '7': f = alt_ctrl; break; + case '8': f = shift_alt_ctrl; break; + } + if ( f == NULL ) + { + ungetch(c3); ungetch(c2); ungetch(c1); break; + } + + const Codepoint c4 = wgetch(m_window); + switch (c4) + { + case 'A': return f(Key::Up); + case 'B': return f(Key::Down); + case 'C': return f(Key::Right); + case 'D': return f(Key::Left); + case 'H': return f(Key::Home); + case 'F': return f(Key::End); + } + + ungetch(c4); ungetch(c3); ungetch(c2); ungetch(c1); break; + } + default: + ungetch(c1); break; } } wtimeout(m_window, -1); From 4cac29d98c5dd707eed226a0591a9c745d46509f Mon Sep 17 00:00:00 2001 From: Jay McCarthy Date: Thu, 29 Nov 2018 13:42:47 -0500 Subject: [PATCH 2/5] Update re lenormf's comments --- src/ncurses_ui.cc | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index 519910c1..b93f8309 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -26,6 +26,7 @@ namespace Kakoune using std::min; using std::max; +using std::function; struct NCursesWin : WINDOW {}; @@ -671,11 +672,12 @@ Optional NCursesUI::get_next_key() const Codepoint c2 = wgetch(m_window); if ( c2 != ';' ) { - ungetch(c2); ungetch(c1); break; + ungetch(c2); ungetch(c1); + break; } const Codepoint c3 = wgetch(m_window); - Key (*f)(Key) = NULL; + function f = nullptr; switch (c3) { case '2': f = shift; break; @@ -686,9 +688,10 @@ Optional NCursesUI::get_next_key() case '7': f = alt_ctrl; break; case '8': f = shift_alt_ctrl; break; } - if ( f == NULL ) + if ( f == nullptr ) { - ungetch(c3); ungetch(c2); ungetch(c1); break; + ungetch(c3); ungetch(c2); ungetch(c1); + break; } const Codepoint c4 = wgetch(m_window); @@ -702,10 +705,12 @@ Optional NCursesUI::get_next_key() case 'F': return f(Key::End); } - ungetch(c4); ungetch(c3); ungetch(c2); ungetch(c1); break; + ungetch(c4); ungetch(c3); ungetch(c2); ungetch(c1); + break; } default: - ungetch(c1); break; + ungetch(c1); + break; } } wtimeout(m_window, -1); From f36a2870b84c385b122060a3b109795464b36803 Mon Sep 17 00:00:00 2001 From: Jay McCarthy Date: Fri, 30 Nov 2018 05:47:31 -0500 Subject: [PATCH 3/5] Simplify re: lenormf --- src/ncurses_ui.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index b93f8309..be433ab5 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -670,14 +670,14 @@ Optional NCursesUI::get_next_key() case '1': { const Codepoint c2 = wgetch(m_window); - if ( c2 != ';' ) + if (c2 != ';') { ungetch(c2); ungetch(c1); break; } const Codepoint c3 = wgetch(m_window); - function f = nullptr; + function f; switch (c3) { case '2': f = shift; break; @@ -688,7 +688,7 @@ Optional NCursesUI::get_next_key() case '7': f = alt_ctrl; break; case '8': f = shift_alt_ctrl; break; } - if ( f == nullptr ) + if (f) { ungetch(c3); ungetch(c2); ungetch(c1); break; From dde81019a2a329928469d841be1a92924e889356 Mon Sep 17 00:00:00 2001 From: Jay McCarthy Date: Thu, 6 Dec 2018 04:46:42 -0500 Subject: [PATCH 4/5] Fix dumb typo --- src/ncurses_ui.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index be433ab5..bd185aa4 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -688,7 +688,7 @@ Optional NCursesUI::get_next_key() case '7': f = alt_ctrl; break; case '8': f = shift_alt_ctrl; break; } - if (f) + if (!f) { ungetch(c3); ungetch(c2); ungetch(c1); break;