#NEWLINE# def load_vi_search_bindings( registry , get_search_state = None , filter = None , search_buffer_name = SEARCH_BUFFER ) : #NEWLINE# #INDENT# assert ( ( get_search_state is None ) or callable ( search_buffer_is_empty ) ) #NEWLINE# if ( not get_search_state ) : #NEWLINE# #INDENT# def get_search_state( cli ) : #NEWLINE# #INDENT# return cli . search_state #NEWLINE# #UNINDENT# #UNINDENT# has_focus = filters . HasFocus ( search_buffer_name ) #NEWLINE# navigation_mode = ( ( ~ has_focus ) & ( InViMode ( InputMode . NAVIGATION ) | filters . HasSelection ( ) ) ) #NEWLINE# handle = create_handle_decorator ( registry , filter ) #NEWLINE# @ handle ( '/' , filter = navigation_mode ) #NEWLINE# @ handle ( Keys . ControlS , filter = ( ~ has_focus ) ) #NEWLINE# def _( event ) : #NEWLINE# #INDENT# '\n        Vi-style forward search.\n        ' #NEWLINE# get_search_state ( event . cli ) . direction = IncrementalSearchDirection . FORWARD #NEWLINE# event . cli . vi_state . input_mode = InputMode . INSERT #NEWLINE# event . cli . push_focus ( search_buffer_name ) #NEWLINE# #UNINDENT# @ handle ( '?' , filter = navigation_mode ) #NEWLINE# @ handle ( Keys . ControlR , filter = ( ~ has_focus ) ) #NEWLINE# def _( event ) : #NEWLINE# #INDENT# '\n        Vi-style backward search.\n        ' #NEWLINE# get_search_state ( event . cli ) . direction = IncrementalSearchDirection . BACKWARD #NEWLINE# event . cli . push_focus ( search_buffer_name ) #NEWLINE# event . cli . vi_state . input_mode = InputMode . INSERT #NEWLINE# #UNINDENT# @ handle ( Keys . ControlJ , filter = has_focus ) #NEWLINE# def _( event ) : #NEWLINE# #INDENT# '\n        Apply the search. (At the / or ? prompt.)\n        ' #NEWLINE# input_buffer = event . cli . buffers . previous ( event . cli ) #NEWLINE# search_buffer = event . cli . buffers [ search_buffer_name ] #NEWLINE# if search_buffer . text : #NEWLINE# #INDENT# get_search_state ( event . cli ) . text = search_buffer . text #NEWLINE# #UNINDENT# input_buffer . apply_search ( get_search_state ( event . cli ) ) #NEWLINE# search_buffer . append_to_history ( ) #NEWLINE# search_buffer . reset ( ) #NEWLINE# event . cli . vi_state . input_mode = InputMode . NAVIGATION #NEWLINE# event . cli . pop_focus ( ) #NEWLINE# #UNINDENT# def incremental_search( cli , direction , count = 1 ) : #NEWLINE# #INDENT# ' Apply search, but keep search buffer focussed. ' #NEWLINE# search_state = get_search_state ( cli ) #NEWLINE# direction_changed = ( search_state . direction != direction ) #NEWLINE# search_state . text = cli . buffers [ search_buffer_name ] . text #NEWLINE# search_state . direction = direction #NEWLINE# if ( not direction_changed ) : #NEWLINE# #INDENT# input_buffer = cli . buffers . previous ( cli ) #NEWLINE# input_buffer . apply_search ( search_state , include_current_position = False , count = count ) #NEWLINE# #UNINDENT# #UNINDENT# @ handle ( Keys . ControlR , filter = has_focus ) #NEWLINE# def _( event ) : #NEWLINE# #INDENT# incremental_search ( event . cli , IncrementalSearchDirection . BACKWARD , count = event . arg ) #NEWLINE# #UNINDENT# @ handle ( Keys . ControlS , filter = has_focus ) #NEWLINE# def _( event ) : #NEWLINE# #INDENT# incremental_search ( event . cli , IncrementalSearchDirection . FORWARD , count = event . arg ) #NEWLINE# #UNINDENT# def search_buffer_is_empty( cli ) : #NEWLINE# #INDENT# ' Returns True when the search buffer is empty. ' #NEWLINE# return ( cli . buffers [ search_buffer_name ] . text == '' ) #NEWLINE# #UNINDENT# @ handle ( Keys . Escape , filter = has_focus ) #NEWLINE# @ handle ( Keys . ControlC , filter = has_focus ) #NEWLINE# @ handle ( Keys . Backspace , filter = ( has_focus & Condition ( search_buffer_is_empty ) ) ) #NEWLINE# def _( event ) : #NEWLINE# #INDENT# '\n        Cancel search.\n        ' #NEWLINE# event . cli . vi_state . input_mode = InputMode . NAVIGATION #NEWLINE# event . cli . pop_focus ( ) #NEWLINE# event . cli . buffers [ search_buffer_name ] . reset ( )