named_commands.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. """
  2. Key bindings which are also known by GNU readline by the given names.
  3. See: http://www.delorie.com/gnu/docs/readline/rlman_13.html
  4. """
  5. from __future__ import unicode_literals
  6. from prompt_toolkit.enums import IncrementalSearchDirection, SEARCH_BUFFER
  7. from prompt_toolkit.selection import PasteMode
  8. from six.moves import range
  9. import six
  10. from .completion import generate_completions, display_completions_like_readline
  11. from prompt_toolkit.document import Document
  12. from prompt_toolkit.enums import EditingMode
  13. from prompt_toolkit.key_binding.input_processor import KeyPress
  14. from prompt_toolkit.keys import Keys
  15. __all__ = (
  16. 'get_by_name',
  17. )
  18. # Registry that maps the Readline command names to their handlers.
  19. _readline_commands = {}
  20. def register(name):
  21. """
  22. Store handler in the `_readline_commands` dictionary.
  23. """
  24. assert isinstance(name, six.text_type)
  25. def decorator(handler):
  26. assert callable(handler)
  27. _readline_commands[name] = handler
  28. return handler
  29. return decorator
  30. def get_by_name(name):
  31. """
  32. Return the handler for the (Readline) command with the given name.
  33. """
  34. try:
  35. return _readline_commands[name]
  36. except KeyError:
  37. raise KeyError('Unknown readline command: %r' % name)
  38. #
  39. # Commands for moving
  40. # See: http://www.delorie.com/gnu/docs/readline/rlman_14.html
  41. #
  42. @register('beginning-of-line')
  43. def beginning_of_line(event):
  44. " Move to the start of the current line. "
  45. buff = event.current_buffer
  46. buff.cursor_position += buff.document.get_start_of_line_position(after_whitespace=False)
  47. @register('end-of-line')
  48. def end_of_line(event):
  49. " Move to the end of the line. "
  50. buff = event.current_buffer
  51. buff.cursor_position += buff.document.get_end_of_line_position()
  52. @register('forward-char')
  53. def forward_char(event):
  54. " Move forward a character. "
  55. buff = event.current_buffer
  56. buff.cursor_position += buff.document.get_cursor_right_position(count=event.arg)
  57. @register('backward-char')
  58. def backward_char(event):
  59. " Move back a character. "
  60. buff = event.current_buffer
  61. buff.cursor_position += buff.document.get_cursor_left_position(count=event.arg)
  62. @register('forward-word')
  63. def forward_word(event):
  64. """
  65. Move forward to the end of the next word. Words are composed of letters and
  66. digits.
  67. """
  68. buff = event.current_buffer
  69. pos = buff.document.find_next_word_ending(count=event.arg)
  70. if pos:
  71. buff.cursor_position += pos
  72. @register('backward-word')
  73. def backward_word(event):
  74. """
  75. Move back to the start of the current or previous word. Words are composed
  76. of letters and digits.
  77. """
  78. buff = event.current_buffer
  79. pos = buff.document.find_previous_word_beginning(count=event.arg)
  80. if pos:
  81. buff.cursor_position += pos
  82. @register('clear-screen')
  83. def clear_screen(event):
  84. """
  85. Clear the screen and redraw everything at the top of the screen.
  86. """
  87. event.cli.renderer.clear()
  88. @register('redraw-current-line')
  89. def redraw_current_line(event):
  90. """
  91. Refresh the current line.
  92. (Readline defines this command, but prompt-toolkit doesn't have it.)
  93. """
  94. pass
  95. #
  96. # Commands for manipulating the history.
  97. # See: http://www.delorie.com/gnu/docs/readline/rlman_15.html
  98. #
  99. @register('accept-line')
  100. def accept_line(event):
  101. " Accept the line regardless of where the cursor is. "
  102. b = event.current_buffer
  103. b.accept_action.validate_and_handle(event.cli, b)
  104. @register('previous-history')
  105. def previous_history(event):
  106. " Move `back` through the history list, fetching the previous command. "
  107. event.current_buffer.history_backward(count=event.arg)
  108. @register('next-history')
  109. def next_history(event):
  110. " Move `forward` through the history list, fetching the next command. "
  111. event.current_buffer.history_forward(count=event.arg)
  112. @register('beginning-of-history')
  113. def beginning_of_history(event):
  114. " Move to the first line in the history. "
  115. event.current_buffer.go_to_history(0)
  116. @register('end-of-history')
  117. def end_of_history(event):
  118. """
  119. Move to the end of the input history, i.e., the line currently being entered.
  120. """
  121. event.current_buffer.history_forward(count=10**100)
  122. buff = event.current_buffer
  123. buff.go_to_history(len(buff._working_lines) - 1)
  124. @register('reverse-search-history')
  125. def reverse_search_history(event):
  126. """
  127. Search backward starting at the current line and moving `up` through
  128. the history as necessary. This is an incremental search.
  129. """
  130. event.cli.current_search_state.direction = IncrementalSearchDirection.BACKWARD
  131. event.cli.push_focus(SEARCH_BUFFER)
  132. #
  133. # Commands for changing text
  134. #
  135. @register('end-of-file')
  136. def end_of_file(event):
  137. """
  138. Exit.
  139. """
  140. event.cli.exit()
  141. @register('delete-char')
  142. def delete_char(event):
  143. " Delete character before the cursor. "
  144. deleted = event.current_buffer.delete(count=event.arg)
  145. if not deleted:
  146. event.cli.output.bell()
  147. @register('backward-delete-char')
  148. def backward_delete_char(event):
  149. " Delete the character behind the cursor. "
  150. if event.arg < 0:
  151. # When a negative argument has been given, this should delete in front
  152. # of the cursor.
  153. deleted = event.current_buffer.delete(count=-event.arg)
  154. else:
  155. deleted = event.current_buffer.delete_before_cursor(count=event.arg)
  156. if not deleted:
  157. event.cli.output.bell()
  158. @register('self-insert')
  159. def self_insert(event):
  160. " Insert yourself. "
  161. event.current_buffer.insert_text(event.data * event.arg)
  162. @register('transpose-chars')
  163. def transpose_chars(event):
  164. """
  165. Emulate Emacs transpose-char behavior: at the beginning of the buffer,
  166. do nothing. At the end of a line or buffer, swap the characters before
  167. the cursor. Otherwise, move the cursor right, and then swap the
  168. characters before the cursor.
  169. """
  170. b = event.current_buffer
  171. p = b.cursor_position
  172. if p == 0:
  173. return
  174. elif p == len(b.text) or b.text[p] == '\n':
  175. b.swap_characters_before_cursor()
  176. else:
  177. b.cursor_position += b.document.get_cursor_right_position()
  178. b.swap_characters_before_cursor()
  179. @register('uppercase-word')
  180. def uppercase_word(event):
  181. """
  182. Uppercase the current (or following) word.
  183. """
  184. buff = event.current_buffer
  185. for i in range(event.arg):
  186. pos = buff.document.find_next_word_ending()
  187. words = buff.document.text_after_cursor[:pos]
  188. buff.insert_text(words.upper(), overwrite=True)
  189. @register('downcase-word')
  190. def downcase_word(event):
  191. """
  192. Lowercase the current (or following) word.
  193. """
  194. buff = event.current_buffer
  195. for i in range(event.arg): # XXX: not DRY: see meta_c and meta_u!!
  196. pos = buff.document.find_next_word_ending()
  197. words = buff.document.text_after_cursor[:pos]
  198. buff.insert_text(words.lower(), overwrite=True)
  199. @register('capitalize-word')
  200. def capitalize_word(event):
  201. """
  202. Capitalize the current (or following) word.
  203. """
  204. buff = event.current_buffer
  205. for i in range(event.arg):
  206. pos = buff.document.find_next_word_ending()
  207. words = buff.document.text_after_cursor[:pos]
  208. buff.insert_text(words.title(), overwrite=True)
  209. @register('quoted-insert')
  210. def quoted_insert(event):
  211. """
  212. Add the next character typed to the line verbatim. This is how to insert
  213. key sequences like C-q, for example.
  214. """
  215. event.cli.quoted_insert = True
  216. #
  217. # Killing and yanking.
  218. #
  219. @register('kill-line')
  220. def kill_line(event):
  221. """
  222. Kill the text from the cursor to the end of the line.
  223. If we are at the end of the line, this should remove the newline.
  224. (That way, it is possible to delete multiple lines by executing this
  225. command multiple times.)
  226. """
  227. buff = event.current_buffer
  228. if event.arg < 0:
  229. deleted = buff.delete_before_cursor(count=-buff.document.get_start_of_line_position())
  230. else:
  231. if buff.document.current_char == '\n':
  232. deleted = buff.delete(1)
  233. else:
  234. deleted = buff.delete(count=buff.document.get_end_of_line_position())
  235. event.cli.clipboard.set_text(deleted)
  236. @register('kill-word')
  237. def kill_word(event):
  238. """
  239. Kill from point to the end of the current word, or if between words, to the
  240. end of the next word. Word boundaries are the same as forward-word.
  241. """
  242. buff = event.current_buffer
  243. pos = buff.document.find_next_word_ending(count=event.arg)
  244. if pos:
  245. deleted = buff.delete(count=pos)
  246. event.cli.clipboard.set_text(deleted)
  247. @register('unix-word-rubout')
  248. def unix_word_rubout(event, WORD=True):
  249. """
  250. Kill the word behind point, using whitespace as a word boundary.
  251. Usually bound to ControlW.
  252. """
  253. buff = event.current_buffer
  254. pos = buff.document.find_start_of_previous_word(count=event.arg, WORD=WORD)
  255. if pos is None:
  256. # Nothing found? delete until the start of the document. (The
  257. # input starts with whitespace and no words were found before the
  258. # cursor.)
  259. pos = - buff.cursor_position
  260. if pos:
  261. deleted = buff.delete_before_cursor(count=-pos)
  262. # If the previous key press was also Control-W, concatenate deleted
  263. # text.
  264. if event.is_repeat:
  265. deleted += event.cli.clipboard.get_data().text
  266. event.cli.clipboard.set_text(deleted)
  267. else:
  268. # Nothing to delete. Bell.
  269. event.cli.output.bell()
  270. @register('backward-kill-word')
  271. def backward_kill_word(event):
  272. """
  273. Kills the word before point, using "not a letter nor a digit" as a word boundary.
  274. Usually bound to M-Del or M-Backspace.
  275. """
  276. unix_word_rubout(event, WORD=False)
  277. @register('delete-horizontal-space')
  278. def delete_horizontal_space(event):
  279. " Delete all spaces and tabs around point. "
  280. buff = event.current_buffer
  281. text_before_cursor = buff.document.text_before_cursor
  282. text_after_cursor = buff.document.text_after_cursor
  283. delete_before = len(text_before_cursor) - len(text_before_cursor.rstrip('\t '))
  284. delete_after = len(text_after_cursor) - len(text_after_cursor.lstrip('\t '))
  285. buff.delete_before_cursor(count=delete_before)
  286. buff.delete(count=delete_after)
  287. @register('unix-line-discard')
  288. def unix_line_discard(event):
  289. """
  290. Kill backward from the cursor to the beginning of the current line.
  291. """
  292. buff = event.current_buffer
  293. if buff.document.cursor_position_col == 0 and buff.document.cursor_position > 0:
  294. buff.delete_before_cursor(count=1)
  295. else:
  296. deleted = buff.delete_before_cursor(count=-buff.document.get_start_of_line_position())
  297. event.cli.clipboard.set_text(deleted)
  298. @register('yank')
  299. def yank(event):
  300. """
  301. Paste before cursor.
  302. """
  303. event.current_buffer.paste_clipboard_data(
  304. event.cli.clipboard.get_data(), count=event.arg, paste_mode=PasteMode.EMACS)
  305. @register('yank-nth-arg')
  306. def yank_nth_arg(event):
  307. """
  308. Insert the first argument of the previous command. With an argument, insert
  309. the nth word from the previous command (start counting at 0).
  310. """
  311. n = (event.arg if event.arg_present else None)
  312. event.current_buffer.yank_nth_arg(n)
  313. @register('yank-last-arg')
  314. def yank_last_arg(event):
  315. """
  316. Like `yank_nth_arg`, but if no argument has been given, yank the last word
  317. of each line.
  318. """
  319. n = (event.arg if event.arg_present else None)
  320. event.current_buffer.yank_last_arg(n)
  321. @register('yank-pop')
  322. def yank_pop(event):
  323. """
  324. Rotate the kill ring, and yank the new top. Only works following yank or
  325. yank-pop.
  326. """
  327. buff = event.current_buffer
  328. doc_before_paste = buff.document_before_paste
  329. clipboard = event.cli.clipboard
  330. if doc_before_paste is not None:
  331. buff.document = doc_before_paste
  332. clipboard.rotate()
  333. buff.paste_clipboard_data(
  334. clipboard.get_data(), paste_mode=PasteMode.EMACS)
  335. #
  336. # Completion.
  337. #
  338. @register('complete')
  339. def complete(event):
  340. " Attempt to perform completion. "
  341. display_completions_like_readline(event)
  342. @register('menu-complete')
  343. def menu_complete(event):
  344. """
  345. Generate completions, or go to the next completion. (This is the default
  346. way of completing input in prompt_toolkit.)
  347. """
  348. generate_completions(event)
  349. @register('menu-complete-backward')
  350. def menu_complete_backward(event):
  351. " Move backward through the list of possible completions. "
  352. event.current_buffer.complete_previous()
  353. #
  354. # Keyboard macros.
  355. #
  356. @register('start-kbd-macro')
  357. def start_kbd_macro(event):
  358. """
  359. Begin saving the characters typed into the current keyboard macro.
  360. """
  361. event.cli.input_processor.start_macro()
  362. @register('end-kbd-macro')
  363. def start_kbd_macro(event):
  364. """
  365. Stop saving the characters typed into the current keyboard macro and save
  366. the definition.
  367. """
  368. event.cli.input_processor.end_macro()
  369. @register('call-last-kbd-macro')
  370. def start_kbd_macro(event):
  371. """
  372. Re-execute the last keyboard macro defined, by making the characters in the
  373. macro appear as if typed at the keyboard.
  374. """
  375. event.cli.input_processor.call_macro()
  376. @register('print-last-kbd-macro')
  377. def print_last_kbd_macro(event):
  378. " Print the last keboard macro. "
  379. # TODO: Make the format suitable for the inputrc file.
  380. def print_macro():
  381. for k in event.cli.input_processor.macro:
  382. print(k)
  383. event.cli.run_in_terminal(print_macro)
  384. #
  385. # Miscellaneous Commands.
  386. #
  387. @register('undo')
  388. def undo(event):
  389. " Incremental undo. "
  390. event.current_buffer.undo()
  391. @register('insert-comment')
  392. def insert_comment(event):
  393. """
  394. Without numeric argument, comment all lines.
  395. With numeric argument, uncomment all lines.
  396. In any case accept the input.
  397. """
  398. buff = event.current_buffer
  399. # Transform all lines.
  400. if event.arg != 1:
  401. def change(line):
  402. return line[1:] if line.startswith('#') else line
  403. else:
  404. def change(line):
  405. return '#' + line
  406. buff.document = Document(
  407. text='\n'.join(map(change, buff.text.splitlines())),
  408. cursor_position=0)
  409. # Accept input.
  410. buff.accept_action.validate_and_handle(event.cli, buff)
  411. @register('vi-editing-mode')
  412. def vi_editing_mode(event):
  413. " Switch to Vi editing mode. "
  414. event.cli.editing_mode = EditingMode.VI
  415. @register('emacs-editing-mode')
  416. def emacs_editing_mode(event):
  417. " Switch to Emacs editing mode. "
  418. event.cli.editing_mode = EditingMode.EMACS
  419. @register('prefix-meta')
  420. def prefix_meta(event):
  421. """
  422. Metafy the next character typed. This is for keyboards without a meta key.
  423. Sometimes people also want to bind other keys to Meta, e.g. 'jj'::
  424. registry.add_key_binding('j', 'j', filter=ViInsertMode())(prefix_meta)
  425. """
  426. event.cli.input_processor.feed(KeyPress(Keys.Escape))
  427. @register('operate-and-get-next')
  428. def operate_and_get_next(event):
  429. """
  430. Accept the current line for execution and fetch the next line relative to
  431. the current line from the history for editing.
  432. """
  433. buff = event.current_buffer
  434. new_index = buff.working_index + 1
  435. # Accept the current input. (This will also redraw the interface in the
  436. # 'done' state.)
  437. buff.accept_action.validate_and_handle(event.cli, buff)
  438. # Set the new index at the start of the next run.
  439. def set_working_index():
  440. if new_index < len(buff._working_lines):
  441. buff.working_index = new_index
  442. event.cli.pre_run_callables.append(set_working_index)
  443. @register('edit-and-execute-command')
  444. def edit_and_execute(event):
  445. """
  446. Invoke an editor on the current command line, and accept the result.
  447. """
  448. buff = event.current_buffer
  449. buff.open_in_editor(event.cli)
  450. buff.accept_action.validate_and_handle(event.cli, buff)