From 0fa68ff11d0bfb7a764b3952e0d08a64554fbba9 Mon Sep 17 00:00:00 2001 From: Marcin Konarski Date: Thu, 18 Nov 2021 15:59:30 +0100 Subject: [PATCH] ReplxxImpl: Allow **silently** aborting user input. --- include/replxx.h | 1 + include/replxx.hxx | 6 ++++++ src/replxx_impl.cxx | 7 +++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/replxx.h b/include/replxx.h index 635b3c1..1dcfddc 100644 --- a/include/replxx.h +++ b/include/replxx.h @@ -134,6 +134,7 @@ enum { REPLXX_KEY_PASTE_FINISH = REPLXX_KEY_PASTE_START + 1 }; enum { REPLXX_KEY_BACKSPACE = REPLXX_KEY_CONTROL( 'H' ) }; enum { REPLXX_KEY_TAB = REPLXX_KEY_CONTROL( 'I' ) }; enum { REPLXX_KEY_ENTER = REPLXX_KEY_CONTROL( 'M' ) }; +enum { REPLXX_KEY_ABORT = REPLXX_KEY_META( REPLXX_KEY_CONTROL( 'M' ) ) }; /*! \brief List of built-in actions that act upon user input. */ diff --git a/include/replxx.hxx b/include/replxx.hxx index 065bb09..ce48139 100644 --- a/include/replxx.hxx +++ b/include/replxx.hxx @@ -60,6 +60,11 @@ enum { ERROR_BB1CA97EC761FC37101737BA0AA2E7C5 = ERROR }; #undef ERROR enum { ERROR = ERROR_BB1CA97EC761FC37101737BA0AA2E7C5 }; #endif +#ifdef ABORT +enum { ABORT_8D12A2CA7E5A64036D7251A3EDA51A38 = ABORT }; +#undef ABORT +enum { ABORT = ABORT_8D12A2CA7E5A64036D7251A3EDA51A38 }; +#endif #ifdef DELETE enum { DELETE_32F68A60CEF40FAEDBC6AF20298C1A1E = DELETE }; #undef DELETE @@ -144,6 +149,7 @@ public: static char32_t const BACKSPACE = 'H' | BASE_CONTROL; static char32_t const TAB = 'I' | BASE_CONTROL; static char32_t const ENTER = 'M' | BASE_CONTROL; + static char32_t const ABORT = 'C' | BASE_CONTROL | BASE_META; }; /*! \brief List of built-in actions that act upon user input. */ diff --git a/src/replxx_impl.cxx b/src/replxx_impl.cxx index 4bc81e6..a38986f 100644 --- a/src/replxx_impl.cxx +++ b/src/replxx_impl.cxx @@ -288,6 +288,7 @@ Replxx::ReplxxImpl::ReplxxImpl( FILE*, FILE*, FILE* ) bind_key( Replxx::KEY::meta( 'U' ), _namedActions.at( action_names::UPPERCASE_SUBWORD ) ); bind_key( Replxx::KEY::control( 'T' ), _namedActions.at( action_names::TRANSPOSE_CHARACTERS ) ); bind_key( Replxx::KEY::control( 'C' ), _namedActions.at( action_names::ABORT_LINE ) ); + bind_key( Replxx::KEY::ABORT, _namedActions.at( action_names::ABORT_LINE ) ); bind_key( Replxx::KEY::control( 'D' ), _namedActions.at( action_names::SEND_EOF ) ); bind_key( Replxx::KEY::INSERT + 0, _namedActions.at( action_names::TOGGLE_OVERWRITE_MODE ) ); bind_key( 127, _namedActions.at( action_names::DELETE_CHARACTER_UNDER_CURSOR ) ); @@ -1772,7 +1773,7 @@ Replxx::ACTION_RESULT Replxx::ReplxxImpl::transpose_characters( char32_t ) { } // ctrl-C, abort this line -Replxx::ACTION_RESULT Replxx::ReplxxImpl::abort_line( char32_t ) { +Replxx::ACTION_RESULT Replxx::ReplxxImpl::abort_line( char32_t keyCode_ ) { errno = EAGAIN; _history.drop_last(); // we need one last refresh with the cursor at the end of the line @@ -1780,7 +1781,9 @@ Replxx::ACTION_RESULT Replxx::ReplxxImpl::abort_line( char32_t ) { _pos = _data.length(); // pass _data.length() as _pos for EOL _lastRefreshTime = 0; refresh_line( _refreshSkipped ? HINT_ACTION::REGENERATE : HINT_ACTION::TRIM ); - _terminal.write8( "^C\r\n", 4 ); + if ( keyCode_ == Replxx::KEY::control( 'C' ) ) { + _terminal.write8( "^C\r\n", 4 ); + } return ( Replxx::ACTION_RESULT::BAIL ); }