basic.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # pylint: disable=function-redefined
  2. from __future__ import annotations
  3. from prompt_toolkit.application.current import get_app
  4. from prompt_toolkit.filters import (
  5. Condition,
  6. emacs_insert_mode,
  7. has_selection,
  8. in_paste_mode,
  9. is_multiline,
  10. vi_insert_mode,
  11. )
  12. from prompt_toolkit.key_binding.key_processor import KeyPress, KeyPressEvent
  13. from prompt_toolkit.keys import Keys
  14. from ..key_bindings import KeyBindings
  15. from .named_commands import get_by_name
  16. __all__ = [
  17. "load_basic_bindings",
  18. ]
  19. E = KeyPressEvent
  20. def if_no_repeat(event: E) -> bool:
  21. """Callable that returns True when the previous event was delivered to
  22. another handler."""
  23. return not event.is_repeat
  24. def load_basic_bindings() -> KeyBindings:
  25. key_bindings = KeyBindings()
  26. insert_mode = vi_insert_mode | emacs_insert_mode
  27. handle = key_bindings.add
  28. @handle("c-a")
  29. @handle("c-b")
  30. @handle("c-c")
  31. @handle("c-d")
  32. @handle("c-e")
  33. @handle("c-f")
  34. @handle("c-g")
  35. @handle("c-h")
  36. @handle("c-i")
  37. @handle("c-j")
  38. @handle("c-k")
  39. @handle("c-l")
  40. @handle("c-m")
  41. @handle("c-n")
  42. @handle("c-o")
  43. @handle("c-p")
  44. @handle("c-q")
  45. @handle("c-r")
  46. @handle("c-s")
  47. @handle("c-t")
  48. @handle("c-u")
  49. @handle("c-v")
  50. @handle("c-w")
  51. @handle("c-x")
  52. @handle("c-y")
  53. @handle("c-z")
  54. @handle("f1")
  55. @handle("f2")
  56. @handle("f3")
  57. @handle("f4")
  58. @handle("f5")
  59. @handle("f6")
  60. @handle("f7")
  61. @handle("f8")
  62. @handle("f9")
  63. @handle("f10")
  64. @handle("f11")
  65. @handle("f12")
  66. @handle("f13")
  67. @handle("f14")
  68. @handle("f15")
  69. @handle("f16")
  70. @handle("f17")
  71. @handle("f18")
  72. @handle("f19")
  73. @handle("f20")
  74. @handle("f21")
  75. @handle("f22")
  76. @handle("f23")
  77. @handle("f24")
  78. @handle("c-@") # Also c-space.
  79. @handle("c-\\")
  80. @handle("c-]")
  81. @handle("c-^")
  82. @handle("c-_")
  83. @handle("backspace")
  84. @handle("up")
  85. @handle("down")
  86. @handle("right")
  87. @handle("left")
  88. @handle("s-up")
  89. @handle("s-down")
  90. @handle("s-right")
  91. @handle("s-left")
  92. @handle("home")
  93. @handle("end")
  94. @handle("s-home")
  95. @handle("s-end")
  96. @handle("delete")
  97. @handle("s-delete")
  98. @handle("c-delete")
  99. @handle("pageup")
  100. @handle("pagedown")
  101. @handle("s-tab")
  102. @handle("tab")
  103. @handle("c-s-left")
  104. @handle("c-s-right")
  105. @handle("c-s-home")
  106. @handle("c-s-end")
  107. @handle("c-left")
  108. @handle("c-right")
  109. @handle("c-up")
  110. @handle("c-down")
  111. @handle("c-home")
  112. @handle("c-end")
  113. @handle("insert")
  114. @handle("s-insert")
  115. @handle("c-insert")
  116. @handle("<sigint>")
  117. @handle(Keys.Ignore)
  118. def _ignore(event: E) -> None:
  119. """
  120. First, for any of these keys, Don't do anything by default. Also don't
  121. catch them in the 'Any' handler which will insert them as data.
  122. If people want to insert these characters as a literal, they can always
  123. do by doing a quoted insert. (ControlQ in emacs mode, ControlV in Vi
  124. mode.)
  125. """
  126. pass
  127. # Readline-style bindings.
  128. handle("home")(get_by_name("beginning-of-line"))
  129. handle("end")(get_by_name("end-of-line"))
  130. handle("left")(get_by_name("backward-char"))
  131. handle("right")(get_by_name("forward-char"))
  132. handle("c-up")(get_by_name("previous-history"))
  133. handle("c-down")(get_by_name("next-history"))
  134. handle("c-l")(get_by_name("clear-screen"))
  135. handle("c-k", filter=insert_mode)(get_by_name("kill-line"))
  136. handle("c-u", filter=insert_mode)(get_by_name("unix-line-discard"))
  137. handle("backspace", filter=insert_mode, save_before=if_no_repeat)(
  138. get_by_name("backward-delete-char")
  139. )
  140. handle("delete", filter=insert_mode, save_before=if_no_repeat)(
  141. get_by_name("delete-char")
  142. )
  143. handle("c-delete", filter=insert_mode, save_before=if_no_repeat)(
  144. get_by_name("delete-char")
  145. )
  146. handle(Keys.Any, filter=insert_mode, save_before=if_no_repeat)(
  147. get_by_name("self-insert")
  148. )
  149. handle("c-t", filter=insert_mode)(get_by_name("transpose-chars"))
  150. handle("c-i", filter=insert_mode)(get_by_name("menu-complete"))
  151. handle("s-tab", filter=insert_mode)(get_by_name("menu-complete-backward"))
  152. # Control-W should delete, using whitespace as separator, while M-Del
  153. # should delete using [^a-zA-Z0-9] as a boundary.
  154. handle("c-w", filter=insert_mode)(get_by_name("unix-word-rubout"))
  155. handle("pageup", filter=~has_selection)(get_by_name("previous-history"))
  156. handle("pagedown", filter=~has_selection)(get_by_name("next-history"))
  157. # CTRL keys.
  158. @Condition
  159. def has_text_before_cursor() -> bool:
  160. return bool(get_app().current_buffer.text)
  161. handle("c-d", filter=has_text_before_cursor & insert_mode)(
  162. get_by_name("delete-char")
  163. )
  164. @handle("enter", filter=insert_mode & is_multiline)
  165. def _newline(event: E) -> None:
  166. """
  167. Newline (in case of multiline input.
  168. """
  169. event.current_buffer.newline(copy_margin=not in_paste_mode())
  170. @handle("c-j")
  171. def _newline2(event: E) -> None:
  172. r"""
  173. By default, handle \n as if it were a \r (enter).
  174. (It appears that some terminals send \n instead of \r when pressing
  175. enter. - at least the Linux subsystem for Windows.)
  176. """
  177. event.key_processor.feed(KeyPress(Keys.ControlM, "\r"), first=True)
  178. # Delete the word before the cursor.
  179. @handle("up")
  180. def _go_up(event: E) -> None:
  181. event.current_buffer.auto_up(count=event.arg)
  182. @handle("down")
  183. def _go_down(event: E) -> None:
  184. event.current_buffer.auto_down(count=event.arg)
  185. @handle("delete", filter=has_selection)
  186. def _cut(event: E) -> None:
  187. data = event.current_buffer.cut_selection()
  188. event.app.clipboard.set_data(data)
  189. # Global bindings.
  190. @handle("c-z")
  191. def _insert_ctrl_z(event: E) -> None:
  192. """
  193. By default, control-Z should literally insert Ctrl-Z.
  194. (Ansi Ctrl-Z, code 26 in MSDOS means End-Of-File.
  195. In a Python REPL for instance, it's possible to type
  196. Control-Z followed by enter to quit.)
  197. When the system bindings are loaded and suspend-to-background is
  198. supported, that will override this binding.
  199. """
  200. event.current_buffer.insert_text(event.data)
  201. @handle(Keys.BracketedPaste)
  202. def _paste(event: E) -> None:
  203. """
  204. Pasting from clipboard.
  205. """
  206. data = event.data
  207. # Be sure to use \n as line ending.
  208. # Some terminals (Like iTerm2) seem to paste \r\n line endings in a
  209. # bracketed paste. See: https://github.com/ipython/ipython/issues/9737
  210. data = data.replace("\r\n", "\n")
  211. data = data.replace("\r", "\n")
  212. event.current_buffer.insert_text(data)
  213. @Condition
  214. def in_quoted_insert() -> bool:
  215. return get_app().quoted_insert
  216. @handle(Keys.Any, filter=in_quoted_insert, eager=True)
  217. def _insert_text(event: E) -> None:
  218. """
  219. Handle quoted insert.
  220. """
  221. event.current_buffer.insert_text(event.data, overwrite=False)
  222. event.app.quoted_insert = False
  223. return key_bindings