defaults.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """
  2. Default key bindings.::
  3. registry = load_key_bindings()
  4. app = Application(key_bindings_registry=registry)
  5. """
  6. from __future__ import unicode_literals
  7. from prompt_toolkit.key_binding.registry import ConditionalRegistry, MergedRegistry
  8. from prompt_toolkit.key_binding.bindings.basic import load_basic_bindings, load_abort_and_exit_bindings, load_basic_system_bindings, load_auto_suggestion_bindings, load_mouse_bindings
  9. from prompt_toolkit.key_binding.bindings.emacs import load_emacs_bindings, load_emacs_system_bindings, load_emacs_search_bindings, load_emacs_open_in_editor_bindings, load_extra_emacs_page_navigation_bindings
  10. from prompt_toolkit.key_binding.bindings.vi import load_vi_bindings, load_vi_system_bindings, load_vi_search_bindings, load_vi_open_in_editor_bindings, load_extra_vi_page_navigation_bindings
  11. from prompt_toolkit.filters import to_cli_filter
  12. __all__ = (
  13. 'load_key_bindings',
  14. 'load_key_bindings_for_prompt',
  15. )
  16. def load_key_bindings(
  17. get_search_state=None,
  18. enable_abort_and_exit_bindings=False,
  19. enable_system_bindings=False,
  20. enable_search=False,
  21. enable_open_in_editor=False,
  22. enable_extra_page_navigation=False,
  23. enable_auto_suggest_bindings=False):
  24. """
  25. Create a Registry object that contains the default key bindings.
  26. :param enable_abort_and_exit_bindings: Filter to enable Ctrl-C and Ctrl-D.
  27. :param enable_system_bindings: Filter to enable the system bindings (meta-!
  28. prompt and Control-Z suspension.)
  29. :param enable_search: Filter to enable the search bindings.
  30. :param enable_open_in_editor: Filter to enable open-in-editor.
  31. :param enable_open_in_editor: Filter to enable open-in-editor.
  32. :param enable_extra_page_navigation: Filter for enabling extra page
  33. navigation. (Bindings for up/down scrolling through long pages, like in
  34. Emacs or Vi.)
  35. :param enable_auto_suggest_bindings: Filter to enable fish-style suggestions.
  36. """
  37. assert get_search_state is None or callable(get_search_state)
  38. # Accept both Filters and booleans as input.
  39. enable_abort_and_exit_bindings = to_cli_filter(enable_abort_and_exit_bindings)
  40. enable_system_bindings = to_cli_filter(enable_system_bindings)
  41. enable_search = to_cli_filter(enable_search)
  42. enable_open_in_editor = to_cli_filter(enable_open_in_editor)
  43. enable_extra_page_navigation = to_cli_filter(enable_extra_page_navigation)
  44. enable_auto_suggest_bindings = to_cli_filter(enable_auto_suggest_bindings)
  45. registry = MergedRegistry([
  46. # Load basic bindings.
  47. load_basic_bindings(),
  48. load_mouse_bindings(),
  49. ConditionalRegistry(load_abort_and_exit_bindings(),
  50. enable_abort_and_exit_bindings),
  51. ConditionalRegistry(load_basic_system_bindings(),
  52. enable_system_bindings),
  53. # Load emacs bindings.
  54. load_emacs_bindings(),
  55. ConditionalRegistry(load_emacs_open_in_editor_bindings(),
  56. enable_open_in_editor),
  57. ConditionalRegistry(load_emacs_search_bindings(get_search_state=get_search_state),
  58. enable_search),
  59. ConditionalRegistry(load_emacs_system_bindings(),
  60. enable_system_bindings),
  61. ConditionalRegistry(load_extra_emacs_page_navigation_bindings(),
  62. enable_extra_page_navigation),
  63. # Load Vi bindings.
  64. load_vi_bindings(get_search_state=get_search_state),
  65. ConditionalRegistry(load_vi_open_in_editor_bindings(),
  66. enable_open_in_editor),
  67. ConditionalRegistry(load_vi_search_bindings(get_search_state=get_search_state),
  68. enable_search),
  69. ConditionalRegistry(load_vi_system_bindings(),
  70. enable_system_bindings),
  71. ConditionalRegistry(load_extra_vi_page_navigation_bindings(),
  72. enable_extra_page_navigation),
  73. # Suggestion bindings.
  74. # (This has to come at the end, because the Vi bindings also have an
  75. # implementation for the "right arrow", but we really want the
  76. # suggestion binding when a suggestion is available.)
  77. ConditionalRegistry(load_auto_suggestion_bindings(),
  78. enable_auto_suggest_bindings),
  79. ])
  80. return registry
  81. def load_key_bindings_for_prompt(**kw):
  82. """
  83. Create a ``Registry`` object with the defaults key bindings for an input
  84. prompt.
  85. This activates the key bindings for abort/exit (Ctrl-C/Ctrl-D),
  86. incremental search and auto suggestions.
  87. (Not for full screen applications.)
  88. """
  89. kw.setdefault('enable_abort_and_exit_bindings', True)
  90. kw.setdefault('enable_search', True)
  91. kw.setdefault('enable_auto_suggest_bindings', True)
  92. return load_key_bindings(**kw)