1"""
2Open in editor key bindings.
3"""
4
5from __future__ import annotations
6
7from prompt_toolkit.filters import emacs_mode, has_selection, vi_navigation_mode
8
9from ..key_bindings import KeyBindings, KeyBindingsBase, merge_key_bindings
10from .named_commands import get_by_name
11
12__all__ = [
13 "load_open_in_editor_bindings",
14 "load_emacs_open_in_editor_bindings",
15 "load_vi_open_in_editor_bindings",
16]
17
18
19def load_open_in_editor_bindings() -> KeyBindingsBase:
20 """
21 Load both the Vi and emacs key bindings for handling edit-and-execute-command.
22 """
23 return merge_key_bindings(
24 [
25 load_emacs_open_in_editor_bindings(),
26 load_vi_open_in_editor_bindings(),
27 ]
28 )
29
30
31def load_emacs_open_in_editor_bindings() -> KeyBindings:
32 """
33 Pressing C-X C-E will open the buffer in an external editor.
34 """
35 key_bindings = KeyBindings()
36
37 key_bindings.add("c-x", "c-e", filter=emacs_mode & ~has_selection)(
38 get_by_name("edit-and-execute-command")
39 )
40
41 return key_bindings
42
43
44def load_vi_open_in_editor_bindings() -> KeyBindings:
45 """
46 Pressing 'v' in navigation mode will open the buffer in an external editor.
47 """
48 key_bindings = KeyBindings()
49 key_bindings.add("v", filter=vi_navigation_mode)(
50 get_by_name("edit-and-execute-command")
51 )
52 return key_bindings