|
11 | 11 | )
|
12 | 12 | from prompt_toolkit.key_binding import KeyBindings
|
13 | 13 | from prompt_toolkit.keys import Keys
|
| 14 | +from prompt_toolkit.key_binding.bindings import named_commands as nc |
14 | 15 |
|
15 | 16 | from .utils import document_is_multiline_python
|
16 | 17 |
|
@@ -256,6 +257,73 @@ def _(event):
|
256 | 257 | python_input.show_sidebar = False
|
257 | 258 | event.app.layout.focus_last()
|
258 | 259 |
|
| 260 | + @Condition |
| 261 | + def ebivim(): |
| 262 | + return True #repl.emacs_bindings_in_vi_insert_mode |
| 263 | + |
| 264 | + focused_insert = has_focus(DEFAULT_BUFFER) & vi_insert_mode |
| 265 | + |
| 266 | + # Needed for to accept autosuggestions in vi insert mode |
| 267 | + @handle("c-e", filter=focused_insert & ebivim) |
| 268 | + def _(event): |
| 269 | + b = event.current_buffer |
| 270 | + suggestion = b.suggestion |
| 271 | + if suggestion: |
| 272 | + b.insert_text(suggestion.text) |
| 273 | + else: |
| 274 | + nc.end_of_line(event) |
| 275 | + |
| 276 | + @handle("c-f", filter=focused_insert & ebivim) |
| 277 | + def _(event): |
| 278 | + b = event.current_buffer |
| 279 | + suggestion = b.suggestion |
| 280 | + if suggestion: |
| 281 | + b.insert_text(suggestion.text) |
| 282 | + else: |
| 283 | + nc.forward_char(event) |
| 284 | + |
| 285 | + @handle("escape", "f", filter=focused_insert & ebivim) |
| 286 | + def _(event): |
| 287 | + b = event.current_buffer |
| 288 | + suggestion = b.suggestion |
| 289 | + if suggestion: |
| 290 | + t = re.split(r"(\S+\s+)", suggestion.text) |
| 291 | + b.insert_text(next((x for x in t if x), "")) |
| 292 | + else: |
| 293 | + nc.forward_word(event) |
| 294 | + |
| 295 | + # Simple Control keybindings |
| 296 | + key_cmd_dict = { |
| 297 | + "c-a": nc.beginning_of_line, |
| 298 | + "c-b": nc.backward_char, |
| 299 | + "c-k": nc.kill_line, |
| 300 | + "c-w": nc.backward_kill_word, |
| 301 | + "c-y": nc.yank, |
| 302 | + "c-_": nc.undo, |
| 303 | + } |
| 304 | + |
| 305 | + for key, cmd in key_cmd_dict.items(): |
| 306 | + handle(key, filter=focused_insert & ebivim)(cmd) |
| 307 | + |
| 308 | + # Alt and Combo Control keybindings |
| 309 | + keys_cmd_dict = { |
| 310 | + # Control Combos |
| 311 | + ("c-x", "c-e"): nc.edit_and_execute, |
| 312 | + ("c-x", "e"): nc.edit_and_execute, |
| 313 | + # Alt |
| 314 | + ("escape", "b"): nc.backward_word, |
| 315 | + ("escape", "c"): nc.capitalize_word, |
| 316 | + ("escape", "d"): nc.kill_word, |
| 317 | + ("escape", "h"): nc.backward_kill_word, |
| 318 | + ("escape", "l"): nc.downcase_word, |
| 319 | + ("escape", "u"): nc.uppercase_word, |
| 320 | + ("escape", "y"): nc.yank_pop, |
| 321 | + ("escape", "."): nc.yank_last_arg, |
| 322 | + } |
| 323 | + |
| 324 | + for keys, cmd in keys_cmd_dict.items(): |
| 325 | + handle(*keys, filter=focused_insert & ebivim)(cmd) |
| 326 | + |
259 | 327 | return bindings
|
260 | 328 |
|
261 | 329 |
|
|
0 commit comments