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