basic.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. # pylint: disable=function-redefined
  2. from __future__ import unicode_literals
  3. from prompt_toolkit.enums import DEFAULT_BUFFER
  4. from prompt_toolkit.filters import HasSelection, Condition, EmacsInsertMode, ViInsertMode
  5. from prompt_toolkit.keys import Keys
  6. from prompt_toolkit.layout.screen import Point
  7. from prompt_toolkit.mouse_events import MouseEventType, MouseEvent
  8. from prompt_toolkit.renderer import HeightIsUnknownError
  9. from prompt_toolkit.utils import suspend_to_background_supported, is_windows
  10. from .named_commands import get_by_name
  11. from ..registry import Registry
  12. __all__ = (
  13. 'load_basic_bindings',
  14. 'load_abort_and_exit_bindings',
  15. 'load_basic_system_bindings',
  16. 'load_auto_suggestion_bindings',
  17. )
  18. def if_no_repeat(event):
  19. """ Callable that returns True when the previous event was delivered to
  20. another handler. """
  21. return not event.is_repeat
  22. def load_basic_bindings():
  23. registry = Registry()
  24. insert_mode = ViInsertMode() | EmacsInsertMode()
  25. handle = registry.add_binding
  26. has_selection = HasSelection()
  27. @handle(Keys.ControlA)
  28. @handle(Keys.ControlB)
  29. @handle(Keys.ControlC)
  30. @handle(Keys.ControlD)
  31. @handle(Keys.ControlE)
  32. @handle(Keys.ControlF)
  33. @handle(Keys.ControlG)
  34. @handle(Keys.ControlH)
  35. @handle(Keys.ControlI)
  36. @handle(Keys.ControlJ)
  37. @handle(Keys.ControlK)
  38. @handle(Keys.ControlL)
  39. @handle(Keys.ControlM)
  40. @handle(Keys.ControlN)
  41. @handle(Keys.ControlO)
  42. @handle(Keys.ControlP)
  43. @handle(Keys.ControlQ)
  44. @handle(Keys.ControlR)
  45. @handle(Keys.ControlS)
  46. @handle(Keys.ControlT)
  47. @handle(Keys.ControlU)
  48. @handle(Keys.ControlV)
  49. @handle(Keys.ControlW)
  50. @handle(Keys.ControlX)
  51. @handle(Keys.ControlY)
  52. @handle(Keys.ControlZ)
  53. @handle(Keys.F1)
  54. @handle(Keys.F2)
  55. @handle(Keys.F3)
  56. @handle(Keys.F4)
  57. @handle(Keys.F5)
  58. @handle(Keys.F6)
  59. @handle(Keys.F7)
  60. @handle(Keys.F8)
  61. @handle(Keys.F9)
  62. @handle(Keys.F10)
  63. @handle(Keys.F11)
  64. @handle(Keys.F12)
  65. @handle(Keys.F13)
  66. @handle(Keys.F14)
  67. @handle(Keys.F15)
  68. @handle(Keys.F16)
  69. @handle(Keys.F17)
  70. @handle(Keys.F18)
  71. @handle(Keys.F19)
  72. @handle(Keys.F20)
  73. @handle(Keys.ControlSpace)
  74. @handle(Keys.ControlBackslash)
  75. @handle(Keys.ControlSquareClose)
  76. @handle(Keys.ControlCircumflex)
  77. @handle(Keys.ControlUnderscore)
  78. @handle(Keys.Backspace)
  79. @handle(Keys.Up)
  80. @handle(Keys.Down)
  81. @handle(Keys.Right)
  82. @handle(Keys.Left)
  83. @handle(Keys.ShiftUp)
  84. @handle(Keys.ShiftDown)
  85. @handle(Keys.ShiftRight)
  86. @handle(Keys.ShiftLeft)
  87. @handle(Keys.Home)
  88. @handle(Keys.End)
  89. @handle(Keys.Delete)
  90. @handle(Keys.ShiftDelete)
  91. @handle(Keys.ControlDelete)
  92. @handle(Keys.PageUp)
  93. @handle(Keys.PageDown)
  94. @handle(Keys.BackTab)
  95. @handle(Keys.Tab)
  96. @handle(Keys.ControlLeft)
  97. @handle(Keys.ControlRight)
  98. @handle(Keys.ControlUp)
  99. @handle(Keys.ControlDown)
  100. @handle(Keys.Insert)
  101. @handle(Keys.Ignore)
  102. def _(event):
  103. """
  104. First, for any of these keys, Don't do anything by default. Also don't
  105. catch them in the 'Any' handler which will insert them as data.
  106. If people want to insert these characters as a literal, they can always
  107. do by doing a quoted insert. (ControlQ in emacs mode, ControlV in Vi
  108. mode.)
  109. """
  110. pass
  111. # Readline-style bindings.
  112. handle(Keys.Home)(get_by_name('beginning-of-line'))
  113. handle(Keys.End)(get_by_name('end-of-line'))
  114. handle(Keys.Left)(get_by_name('backward-char'))
  115. handle(Keys.Right)(get_by_name('forward-char'))
  116. handle(Keys.ControlUp)(get_by_name('previous-history'))
  117. handle(Keys.ControlDown)(get_by_name('next-history'))
  118. handle(Keys.ControlL)(get_by_name('clear-screen'))
  119. handle(Keys.ControlK, filter=insert_mode)(get_by_name('kill-line'))
  120. handle(Keys.ControlU, filter=insert_mode)(get_by_name('unix-line-discard'))
  121. handle(Keys.ControlH, filter=insert_mode, save_before=if_no_repeat)(
  122. get_by_name('backward-delete-char'))
  123. handle(Keys.Backspace, filter=insert_mode, save_before=if_no_repeat)(
  124. get_by_name('backward-delete-char'))
  125. handle(Keys.Delete, filter=insert_mode, save_before=if_no_repeat)(
  126. get_by_name('delete-char'))
  127. handle(Keys.ShiftDelete, filter=insert_mode, save_before=if_no_repeat)(
  128. get_by_name('delete-char'))
  129. handle(Keys.Any, filter=insert_mode, save_before=if_no_repeat)(
  130. get_by_name('self-insert'))
  131. handle(Keys.ControlT, filter=insert_mode)(get_by_name('transpose-chars'))
  132. handle(Keys.ControlW, filter=insert_mode)(get_by_name('unix-word-rubout'))
  133. handle(Keys.ControlI, filter=insert_mode)(get_by_name('menu-complete'))
  134. handle(Keys.BackTab, filter=insert_mode)(get_by_name('menu-complete-backward'))
  135. handle(Keys.PageUp, filter= ~has_selection)(get_by_name('previous-history'))
  136. handle(Keys.PageDown, filter= ~has_selection)(get_by_name('next-history'))
  137. # CTRL keys.
  138. text_before_cursor = Condition(lambda cli: cli.current_buffer.text)
  139. handle(Keys.ControlD, filter=text_before_cursor & insert_mode)(get_by_name('delete-char'))
  140. is_multiline = Condition(lambda cli: cli.current_buffer.is_multiline())
  141. is_returnable = Condition(lambda cli: cli.current_buffer.accept_action.is_returnable)
  142. @handle(Keys.ControlJ, filter=is_multiline & insert_mode)
  143. def _(event):
  144. " Newline (in case of multiline input. "
  145. event.current_buffer.newline(copy_margin=not event.cli.in_paste_mode)
  146. @handle(Keys.ControlJ, filter=~is_multiline & is_returnable)
  147. def _(event):
  148. " Enter, accept input. "
  149. buff = event.current_buffer
  150. buff.accept_action.validate_and_handle(event.cli, buff)
  151. # Delete the word before the cursor.
  152. @handle(Keys.Up)
  153. def _(event):
  154. event.current_buffer.auto_up(count=event.arg)
  155. @handle(Keys.Down)
  156. def _(event):
  157. event.current_buffer.auto_down(count=event.arg)
  158. @handle(Keys.Delete, filter=has_selection)
  159. def _(event):
  160. data = event.current_buffer.cut_selection()
  161. event.cli.clipboard.set_data(data)
  162. # Global bindings.
  163. @handle(Keys.ControlZ)
  164. def _(event):
  165. """
  166. By default, control-Z should literally insert Ctrl-Z.
  167. (Ansi Ctrl-Z, code 26 in MSDOS means End-Of-File.
  168. In a Python REPL for instance, it's possible to type
  169. Control-Z followed by enter to quit.)
  170. When the system bindings are loaded and suspend-to-background is
  171. supported, that will override this binding.
  172. """
  173. event.current_buffer.insert_text(event.data)
  174. @handle(Keys.CPRResponse, save_before=lambda e: False)
  175. def _(event):
  176. """
  177. Handle incoming Cursor-Position-Request response.
  178. """
  179. # The incoming data looks like u'\x1b[35;1R'
  180. # Parse row/col information.
  181. row, col = map(int, event.data[2:-1].split(';'))
  182. # Report absolute cursor position to the renderer.
  183. event.cli.renderer.report_absolute_cursor_row(row)
  184. @handle(Keys.BracketedPaste)
  185. def _(event):
  186. " Pasting from clipboard. "
  187. data = event.data
  188. # Be sure to use \n as line ending.
  189. # Some terminals (Like iTerm2) seem to paste \r\n line endings in a
  190. # bracketed paste. See: https://github.com/ipython/ipython/issues/9737
  191. data = data.replace('\r\n', '\n')
  192. data = data.replace('\r', '\n')
  193. event.current_buffer.insert_text(data)
  194. @handle(Keys.Any, filter=Condition(lambda cli: cli.quoted_insert), eager=True)
  195. def _(event):
  196. """
  197. Handle quoted insert.
  198. """
  199. event.current_buffer.insert_text(event.data, overwrite=False)
  200. event.cli.quoted_insert = False
  201. return registry
  202. def load_mouse_bindings():
  203. """
  204. Key bindings, required for mouse support.
  205. (Mouse events enter through the key binding system.)
  206. """
  207. registry = Registry()
  208. @registry.add_binding(Keys.Vt100MouseEvent)
  209. def _(event):
  210. """
  211. Handling of incoming mouse event.
  212. """
  213. # Typical: "Esc[MaB*"
  214. # Urxvt: "Esc[96;14;13M"
  215. # Xterm SGR: "Esc[<64;85;12M"
  216. # Parse incoming packet.
  217. if event.data[2] == 'M':
  218. # Typical.
  219. mouse_event, x, y = map(ord, event.data[3:])
  220. mouse_event = {
  221. 32: MouseEventType.MOUSE_DOWN,
  222. 35: MouseEventType.MOUSE_UP,
  223. 96: MouseEventType.SCROLL_UP,
  224. 97: MouseEventType.SCROLL_DOWN,
  225. }.get(mouse_event)
  226. # Handle situations where `PosixStdinReader` used surrogateescapes.
  227. if x >= 0xdc00: x-= 0xdc00
  228. if y >= 0xdc00: y-= 0xdc00
  229. x -= 32
  230. y -= 32
  231. else:
  232. # Urxvt and Xterm SGR.
  233. # When the '<' is not present, we are not using the Xterm SGR mode,
  234. # but Urxvt instead.
  235. data = event.data[2:]
  236. if data[:1] == '<':
  237. sgr = True
  238. data = data[1:]
  239. else:
  240. sgr = False
  241. # Extract coordinates.
  242. mouse_event, x, y = map(int, data[:-1].split(';'))
  243. m = data[-1]
  244. # Parse event type.
  245. if sgr:
  246. mouse_event = {
  247. (0, 'M'): MouseEventType.MOUSE_DOWN,
  248. (0, 'm'): MouseEventType.MOUSE_UP,
  249. (64, 'M'): MouseEventType.SCROLL_UP,
  250. (65, 'M'): MouseEventType.SCROLL_DOWN,
  251. }.get((mouse_event, m))
  252. else:
  253. mouse_event = {
  254. 32: MouseEventType.MOUSE_DOWN,
  255. 35: MouseEventType.MOUSE_UP,
  256. 96: MouseEventType.SCROLL_UP,
  257. 97: MouseEventType.SCROLL_DOWN,
  258. }.get(mouse_event)
  259. x -= 1
  260. y -= 1
  261. # Only handle mouse events when we know the window height.
  262. if event.cli.renderer.height_is_known and mouse_event is not None:
  263. # Take region above the layout into account. The reported
  264. # coordinates are absolute to the visible part of the terminal.
  265. try:
  266. y -= event.cli.renderer.rows_above_layout
  267. except HeightIsUnknownError:
  268. return
  269. # Call the mouse handler from the renderer.
  270. handler = event.cli.renderer.mouse_handlers.mouse_handlers[x,y]
  271. handler(event.cli, MouseEvent(position=Point(x=x, y=y),
  272. event_type=mouse_event))
  273. @registry.add_binding(Keys.WindowsMouseEvent)
  274. def _(event):
  275. """
  276. Handling of mouse events for Windows.
  277. """
  278. assert is_windows() # This key binding should only exist for Windows.
  279. # Parse data.
  280. event_type, x, y = event.data.split(';')
  281. x = int(x)
  282. y = int(y)
  283. # Make coordinates absolute to the visible part of the terminal.
  284. screen_buffer_info = event.cli.renderer.output.get_win32_screen_buffer_info()
  285. rows_above_cursor = screen_buffer_info.dwCursorPosition.Y - event.cli.renderer._cursor_pos.y
  286. y -= rows_above_cursor
  287. # Call the mouse event handler.
  288. handler = event.cli.renderer.mouse_handlers.mouse_handlers[x,y]
  289. handler(event.cli, MouseEvent(position=Point(x=x, y=y),
  290. event_type=event_type))
  291. return registry
  292. def load_abort_and_exit_bindings():
  293. """
  294. Basic bindings for abort (Ctrl-C) and exit (Ctrl-D).
  295. """
  296. registry = Registry()
  297. handle = registry.add_binding
  298. @handle(Keys.ControlC)
  299. def _(event):
  300. " Abort when Control-C has been pressed. "
  301. event.cli.abort()
  302. @Condition
  303. def ctrl_d_condition(cli):
  304. """ Ctrl-D binding is only active when the default buffer is selected
  305. and empty. """
  306. return (cli.current_buffer_name == DEFAULT_BUFFER and
  307. not cli.current_buffer.text)
  308. handle(Keys.ControlD, filter=ctrl_d_condition)(get_by_name('end-of-file'))
  309. return registry
  310. def load_basic_system_bindings():
  311. """
  312. Basic system bindings (For both Emacs and Vi mode.)
  313. """
  314. registry = Registry()
  315. suspend_supported = Condition(
  316. lambda cli: suspend_to_background_supported())
  317. @registry.add_binding(Keys.ControlZ, filter=suspend_supported)
  318. def _(event):
  319. """
  320. Suspend process to background.
  321. """
  322. event.cli.suspend_to_background()
  323. return registry
  324. def load_auto_suggestion_bindings():
  325. """
  326. Key bindings for accepting auto suggestion text.
  327. """
  328. registry = Registry()
  329. handle = registry.add_binding
  330. suggestion_available = Condition(
  331. lambda cli:
  332. cli.current_buffer.suggestion is not None and
  333. cli.current_buffer.document.is_cursor_at_the_end)
  334. @handle(Keys.ControlF, filter=suggestion_available)
  335. @handle(Keys.ControlE, filter=suggestion_available)
  336. @handle(Keys.Right, filter=suggestion_available)
  337. def _(event):
  338. " Accept suggestion. "
  339. b = event.current_buffer
  340. suggestion = b.suggestion
  341. if suggestion:
  342. b.insert_text(suggestion.text)
  343. return registry