manager.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """
  2. DEPRECATED:
  3. Use `prompt_toolkit.key_binding.defaults.load_key_bindings` instead.
  4. :class:`KeyBindingManager` is a utility (or shortcut) for loading all the key
  5. bindings in a key binding registry, with a logic set of filters to quickly to
  6. quickly change from Vi to Emacs key bindings at runtime.
  7. You don't have to use this, but it's practical.
  8. Usage::
  9. manager = KeyBindingManager()
  10. app = Application(key_bindings_registry=manager.registry)
  11. """
  12. from __future__ import unicode_literals
  13. from .defaults import load_key_bindings
  14. from prompt_toolkit.filters import to_cli_filter
  15. from prompt_toolkit.key_binding.registry import Registry, ConditionalRegistry, MergedRegistry
  16. __all__ = (
  17. 'KeyBindingManager',
  18. )
  19. class KeyBindingManager(object):
  20. """
  21. Utility for loading all key bindings into memory.
  22. :param registry: Optional `Registry` instance.
  23. :param enable_abort_and_exit_bindings: Filter to enable Ctrl-C and Ctrl-D.
  24. :param enable_system_bindings: Filter to enable the system bindings
  25. (meta-! prompt and Control-Z suspension.)
  26. :param enable_search: Filter to enable the search bindings.
  27. :param enable_open_in_editor: Filter to enable open-in-editor.
  28. :param enable_open_in_editor: Filter to enable open-in-editor.
  29. :param enable_extra_page_navigation: Filter for enabling extra page navigation.
  30. (Bindings for up/down scrolling through long pages, like in Emacs or Vi.)
  31. :param enable_auto_suggest_bindings: Filter to enable fish-style suggestions.
  32. :param enable_vi_mode: Deprecated!
  33. """
  34. def __init__(self,
  35. registry=None, # XXX: not used anymore.
  36. enable_vi_mode=None, # (`enable_vi_mode` is deprecated.)
  37. enable_all=True, #
  38. get_search_state=None,
  39. enable_abort_and_exit_bindings=False,
  40. enable_system_bindings=False,
  41. enable_search=False,
  42. enable_open_in_editor=False,
  43. enable_extra_page_navigation=False,
  44. enable_auto_suggest_bindings=False):
  45. assert registry is None or isinstance(registry, Registry)
  46. assert get_search_state is None or callable(get_search_state)
  47. enable_all = to_cli_filter(enable_all)
  48. defaults = load_key_bindings(
  49. get_search_state=get_search_state,
  50. enable_abort_and_exit_bindings=enable_abort_and_exit_bindings,
  51. enable_system_bindings=enable_system_bindings,
  52. enable_search=enable_search,
  53. enable_open_in_editor=enable_open_in_editor,
  54. enable_extra_page_navigation=enable_extra_page_navigation,
  55. enable_auto_suggest_bindings=enable_auto_suggest_bindings)
  56. # Note, we wrap this whole thing again in a MergedRegistry, because we
  57. # don't want the `enable_all` settings to apply on items that were
  58. # added to the registry as a whole.
  59. self.registry = MergedRegistry([
  60. ConditionalRegistry(defaults, enable_all)
  61. ])
  62. @classmethod
  63. def for_prompt(cls, **kw):
  64. """
  65. Create a ``KeyBindingManager`` with the defaults for an input prompt.
  66. This activates the key bindings for abort/exit (Ctrl-C/Ctrl-D),
  67. incremental search and auto suggestions.
  68. (Not for full screen applications.)
  69. """
  70. kw.setdefault('enable_abort_and_exit_bindings', True)
  71. kw.setdefault('enable_search', True)
  72. kw.setdefault('enable_auto_suggest_bindings', True)
  73. return cls(**kw)
  74. def reset(self, cli):
  75. # For backwards compatibility.
  76. pass
  77. def get_vi_state(self, cli):
  78. # Deprecated!
  79. return cli.vi_state