summaryrefslogtreecommitdiffstats
path: root/src/prompt_toolkit/key_binding/bindings/open_in_editor.py
blob: d156424f20fa8210785d68e3f4207e47d5a5e571 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Open in editor key bindings.
"""
from __future__ import annotations

from prompt_toolkit.filters import emacs_mode, has_selection, vi_navigation_mode

from ..key_bindings import KeyBindings, KeyBindingsBase, merge_key_bindings
from .named_commands import get_by_name

__all__ = [
    "load_open_in_editor_bindings",
    "load_emacs_open_in_editor_bindings",
    "load_vi_open_in_editor_bindings",
]


def load_open_in_editor_bindings() -> KeyBindingsBase:
    """
    Load both the Vi and emacs key bindings for handling edit-and-execute-command.
    """
    return merge_key_bindings(
        [
            load_emacs_open_in_editor_bindings(),
            load_vi_open_in_editor_bindings(),
        ]
    )


def load_emacs_open_in_editor_bindings() -> KeyBindings:
    """
    Pressing C-X C-E will open the buffer in an external editor.
    """
    key_bindings = KeyBindings()

    key_bindings.add("c-x", "c-e", filter=emacs_mode & ~has_selection)(
        get_by_name("edit-and-execute-command")
    )

    return key_bindings


def load_vi_open_in_editor_bindings() -> KeyBindings:
    """
    Pressing 'v' in navigation mode will open the buffer in an external editor.
    """
    key_bindings = KeyBindings()
    key_bindings.add("v", filter=vi_navigation_mode)(
        get_by_name("edit-and-execute-command")
    )
    return key_bindings