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