Skip to content

Commit 8c3c141

Browse files
Merge pull request #52 from ClickHouse/vim-default-key-handler
Add `bind_default_key` to handle keys without their own binding in an editing mode
2 parents 191e04e + d34b29e commit 8c3c141

4 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎include/replxx.hxx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,16 @@ public:
535535
*/
536536
void bind_key( char32_t code, key_press_handler_t handler, int editingMode = 0 );
537537

538+
/*! \brief Bind user defined action to handle every key-press event without its own binding.
539+
*
540+
* Without such a handler, a key-press event without a binding inserts the character
541+
* in the editing mode 0 and is ignored in other editing modes.
542+
*
543+
* \param handler - use this handler to handle key-press events without their own binding.
544+
* \param editingMode - the editing mode in which the handler is used.
545+
*/
546+
void bind_default_key( key_press_handler_t handler, int editingMode = 0 );
547+
538548
/*! \brief Bind internal `replxx` action (by name) to handle given key-press event.
539549
*
540550
* Action names are the same as names of Replxx::ACTION enumerations

‎src/replxx.cxx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ void Replxx::bind_key( char32_t keyPress_, key_press_handler_t handler_, int edi
275275
_impl->bind_key( keyPress_, handler_, editingMode_ );
276276
}
277277

278+
void Replxx::bind_default_key( key_press_handler_t handler_, int editingMode_ ) {
279+
_impl->bind_default_key( handler_, editingMode_ );
280+
}
281+
278282
void Replxx::bind_key_internal( char32_t keyPress_, char const* actionName_, int editingMode_ ) {
279283
_impl->bind_key_internal( keyPress_, actionName_, editingMode_ );
280284
}

‎src/replxx_impl.cxx‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ Replxx::ReplxxImpl::ReplxxImpl( std::istream & in_, std::ostream & out_, int in_
188188
, _namedActions()
189189
, _editingMode( 0 )
190190
, _keyPressHandlers()
191+
, _defaultKeyPressHandlers()
191192
, _terminal(in_fd_, out_fd_)
192193
, _currentThread()
193194
, _prompt( _terminal )
@@ -465,6 +466,10 @@ void Replxx::ReplxxImpl::bind_key( char32_t code_, Replxx::key_press_handler_t h
465466
_keyPressHandlers[editingMode_][code_] = handler_;
466467
}
467468

469+
void Replxx::ReplxxImpl::bind_default_key( Replxx::key_press_handler_t handler_, int editingMode_ ) {
470+
_defaultKeyPressHandlers[editingMode_] = handler_;
471+
}
472+
468473
void Replxx::ReplxxImpl::bind_key_internal( char32_t code_, char const* actionName_, int editingMode_ ) {
469474
named_actions_t::const_iterator it( _namedActions.find( actionName_ ) );
470475
if ( it == _namedActions.end() ) {
@@ -1494,9 +1499,18 @@ int Replxx::ReplxxImpl::get_input_line( void ) {
14941499
continue;
14951500
}
14961501

1502+
Replxx::key_press_handler_t* handler( nullptr );
14971503
key_press_handlers_t::iterator it( _keyPressHandlers[_editingMode].find( c ) );
14981504
if ( it != _keyPressHandlers[_editingMode].end() ) {
1499-
next = it->second( c );
1505+
handler = &it->second;
1506+
} else {
1507+
key_press_handlers_t::iterator dit( _defaultKeyPressHandlers.find( _editingMode ) );
1508+
if ( dit != _defaultKeyPressHandlers.end() ) {
1509+
handler = &dit->second;
1510+
}
1511+
}
1512+
if ( handler ) {
1513+
next = ( *handler )( c );
15001514
if ( _modifiedState ) {
15011515
refresh_line();
15021516
} else if ( _moveCursor ) {

‎src/replxx_impl.hxx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ private:
132132
named_actions_t _namedActions;
133133
int _editingMode;
134134
std::unordered_map<int, key_press_handlers_t> _keyPressHandlers;
135+
key_press_handlers_t _defaultKeyPressHandlers;
135136
Terminal _terminal;
136137
std::thread::id _currentThread;
137138
Prompt _prompt;
@@ -211,6 +212,7 @@ public:
211212
Replxx::ACTION_RESULT invoke( Replxx::ACTION, char32_t );
212213
bool history_recalled( void ) const;
213214
void bind_key( char32_t, Replxx::key_press_handler_t, int editingMode = 0 );
215+
void bind_default_key( Replxx::key_press_handler_t, int editingMode = 0 );
214216
void bind_key_internal( char32_t, char const*, int editingMode = 0 );
215217
Replxx::State get_state( void ) const;
216218
void set_state( Replxx::State const& );

0 commit comments

Comments
 (0)