basic.py 7.1 KB

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