1"""
2Key bindings for extra page navigation: bindings for up/down scrolling through
3long pages, like in Emacs or Vi.
4"""
5from __future__ import unicode_literals
6
7from prompt_toolkit.filters import buffer_has_focus, emacs_mode, vi_mode
8from prompt_toolkit.key_binding.key_bindings import (
9    ConditionalKeyBindings,
10    KeyBindings,
11    merge_key_bindings,
12)
13
14from .scroll import (
15    scroll_backward,
16    scroll_forward,
17    scroll_half_page_down,
18    scroll_half_page_up,
19    scroll_one_line_down,
20    scroll_one_line_up,
21    scroll_page_down,
22    scroll_page_up,
23)
24
25__all__ = [
26    'load_page_navigation_bindings',
27    'load_emacs_page_navigation_bindings',
28    'load_vi_page_navigation_bindings',
29]
30
31
32def load_page_navigation_bindings():
33    """
34    Load both the Vi and Emacs bindings for page navigation.
35    """
36    # Only enable when a `Buffer` is focused, otherwise, we would catch keys
37    # when another widget is focused (like for instance `c-d` in a
38    # ptterm.Terminal).
39    return ConditionalKeyBindings(
40        merge_key_bindings([
41            load_emacs_page_navigation_bindings(),
42            load_vi_page_navigation_bindings(),
43        ]), buffer_has_focus)
44
45
46def load_emacs_page_navigation_bindings():
47    """
48    Key bindings, for scrolling up and down through pages.
49    This are separate bindings, because GNU readline doesn't have them.
50    """
51    key_bindings = KeyBindings()
52    handle = key_bindings.add
53
54    handle('c-v')(scroll_page_down)
55    handle('pagedown')(scroll_page_down)
56    handle('escape', 'v')(scroll_page_up)
57    handle('pageup')(scroll_page_up)
58
59    return ConditionalKeyBindings(key_bindings, emacs_mode)
60
61
62def load_vi_page_navigation_bindings():
63    """
64    Key bindings, for scrolling up and down through pages.
65    This are separate bindings, because GNU readline doesn't have them.
66    """
67    key_bindings = KeyBindings()
68    handle = key_bindings.add
69
70    handle('c-f')(scroll_forward)
71    handle('c-b')(scroll_backward)
72    handle('c-d')(scroll_half_page_down)
73    handle('c-u')(scroll_half_page_up)
74    handle('c-e')(scroll_one_line_down)
75    handle('c-y')(scroll_one_line_up)
76    handle('pagedown')(scroll_page_down)
77    handle('pageup')(scroll_page_up)
78
79    return ConditionalKeyBindings(key_bindings, vi_mode)
80