Skip to content

Commit 8cf908a

Browse files
committed
Add config options related to Vi input mode
Setting `vi_start_in_nav_mode` to `True` enables `NAVIGATION` mode on startup. The issue is that due to the current behaviour of `ViState.reset()` input mode gets resetted back to `INSERT` on the every iteration of the main loop. In order to at one hand to provide the user with desired behaviour and on the other hand doesn't introduce breaking changes the other option `vi_keep_last_used_mode` was introduced which sets `input_mode` to the state observed before reset. `vi_keep_last_used_mode` can be useful even with `vi_start_in_nav_mode` set to `False` in the case the user prefer to start in `INSERT` mode but still wants to maintain the last mode he was in. In the case of `vi_keep_last_used_mode` set to `False` and `vi_start_in_nav_mode` to `True` `NAVIGATION` mode is set on every iteration the same way `INSERT` was set before this commit. Fixes #258.
1 parent 27f5bcd commit 8cf908a

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

examples/ptpython_config/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ def configure(repl):
119119
# Syntax.
120120
repl.enable_syntax_highlighting = True
121121

122+
# Get into Vi navigation mode at startup
123+
repl.vi_start_in_nav_mode = False
124+
125+
# Preserve last used Vi input mode between main loop iterations
126+
repl.vi_keep_last_used_mode = False
127+
122128
# Install custom colorscheme named 'my-colorscheme' and use it.
123129
"""
124130
repl.install_ui_colorscheme('my-colorscheme', _custom_ui_colorscheme)

ptpython/python_input.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,12 @@ def __init__(
298298
# (Never run more than one at the same time.)
299299
self._get_signatures_thread_running: bool = False
300300

301+
# Get into Vi navigation mode at startup
302+
self.vi_start_in_nav_mode: bool = False
303+
304+
# Preserve last used Vi input mode between main loop iterations
305+
self.vi_keep_last_used_mode: bool = False
306+
301307
self.style_transformation = merge_style_transformations(
302308
[
303309
ConditionalStyleTransformation(
@@ -327,6 +333,9 @@ def __init__(
327333
if vi_mode:
328334
self.app.editing_mode = EditingMode.VI
329335

336+
if vi_mode and self.vi_start_in_nav_mode:
337+
self.app.vi_state.input_mode = InputMode.NAVIGATION
338+
330339
def _accept_handler(self, buff: Buffer) -> bool:
331340
app = get_app()
332341
app.exit(result=buff.text)

ptpython/repl.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
merge_formatted_text,
2323
)
2424
from prompt_toolkit.formatted_text.utils import fragment_list_width
25+
from prompt_toolkit.key_binding.vi_state import InputMode
2526
from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context
2627
from prompt_toolkit.shortcuts import clear_title, print_formatted_text, set_title
2728
from prompt_toolkit.utils import DummyContext
@@ -70,9 +71,19 @@ def prompt() -> str:
7071
# This happens when the user used `asyncio.run()`.
7172
old_loop = None
7273

74+
# Capture the current input_mode in order to restore it after reset,
75+
# for ViState.reset() sets it to InputMode.INSERT unconditionally and
76+
# doesn't accept any arguments despite the docstring says otherwise.
77+
def pre_run(last_input_mode=self.app.vi_state.input_mode):
78+
if self.vi_keep_last_used_mode:
79+
self.app.vi_state.input_mode = last_input_mode
80+
81+
if not self.vi_keep_last_used_mode and self.vi_start_in_nav_mode:
82+
self.app.vi_state.input_mode = InputMode.NAVIGATION
83+
7384
asyncio.set_event_loop(self.pt_loop)
7485
try:
75-
return self.app.run() # inputhook=inputhook)
86+
return self.app.run(pre_run) # inputhook=inputhook)
7687
finally:
7788
# Restore the original event loop.
7889
asyncio.set_event_loop(old_loop)

0 commit comments

Comments
 (0)