Skip to content

Commit e84abde

Browse files
Made text objects of the Vi 'n' and 'N' search bindings. This allows searching in selection mode, and it will handle key bindings like cn, cN, dn, dN, yn, yN.
1 parent 8a63331 commit e84abde

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

prompt_toolkit/buffer.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,21 @@ def document_for_search(self, search_state):
10941094
working_index, cursor_position = search_result
10951095
return Document(self._working_lines[working_index], cursor_position)
10961096

1097+
def get_search_position(self, search_state, include_current_position=True, count=1):
1098+
"""
1099+
Get the cursor position for this search.
1100+
(This operation won't change the `working_index`. It's won't go through
1101+
the history. Vi text objects can't span multiple items.)
1102+
"""
1103+
search_result = self._search(
1104+
search_state, include_current_position=include_current_position, count=count)
1105+
1106+
if search_result is None:
1107+
return self.cursor_position
1108+
else:
1109+
working_index, cursor_position = search_result
1110+
return cursor_position
1111+
10971112
def apply_search(self, search_state, include_current_position=True, count=1):
10981113
"""
10991114
Apply search. If something is found, set `working_index` and

prompt_toolkit/key_binding/bindings/vi.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -476,24 +476,6 @@ def _(event):
476476
" Join selected lines without space. "
477477
event.current_buffer.join_selected_lines(separator='')
478478

479-
@handle('n', filter=navigation_mode)
480-
def _(event): # XXX: use `text_object`
481-
"""
482-
Search next.
483-
"""
484-
event.current_buffer.apply_search(
485-
get_search_state(event.cli), include_current_position=False,
486-
count=event.arg)
487-
488-
@handle('N', filter=navigation_mode)
489-
def _(event): # TODO: use `text_object`
490-
"""
491-
Search previous.
492-
"""
493-
event.current_buffer.apply_search(
494-
~get_search_state(event.cli), include_current_position=False,
495-
count=event.arg)
496-
497479
@handle('p', filter=navigation_mode)
498480
def _(event):
499481
"""
@@ -1336,6 +1318,38 @@ def _(event):
13361318
pos = len(b.document.text_after_cursor)
13371319
return TextObject(pos, type=TextObjectType.LINEWISE)
13381320

1321+
@text_object('n', no_move_handler=True)
1322+
def _(event):
1323+
" Search next. "
1324+
buff = event.current_buffer
1325+
cursor_position = buff.get_search_position(
1326+
get_search_state(event.cli), include_current_position=False,
1327+
count=event.arg)
1328+
return TextObject(cursor_position - buff.cursor_position)
1329+
1330+
@handle('n', filter=navigation_mode)
1331+
def _(event):
1332+
" Search next in navigation mode. (This goes through the history.) "
1333+
event.current_buffer.apply_search(
1334+
get_search_state(event.cli), include_current_position=False,
1335+
count=event.arg)
1336+
1337+
@text_object('N', no_move_handler=True)
1338+
def _(event):
1339+
" Search previous. "
1340+
buff = event.current_buffer
1341+
cursor_position = buff.get_search_position(
1342+
~get_search_state(event.cli), include_current_position=False,
1343+
count=event.arg)
1344+
return TextObject(cursor_position - buff.cursor_position)
1345+
1346+
@handle('N', filter=navigation_mode)
1347+
def _(event):
1348+
" Search previous in navigation mode. (This goes through the history.) "
1349+
event.current_buffer.apply_search(
1350+
~get_search_state(event.cli), include_current_position=False,
1351+
count=event.arg)
1352+
13391353
@handle('z', '+', filter=navigation_mode|selection_mode)
13401354
@handle('z', 't', filter=navigation_mode|selection_mode)
13411355
@handle('z', Keys.ControlJ, filter=navigation_mode|selection_mode)

0 commit comments

Comments
 (0)