diff options
author | mathieui <mathieui@mathieui.net> | 2011-11-13 19:43:31 +0100 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2011-11-13 19:43:31 +0100 |
commit | 39fcd9a4f6ace2e00f133bfaa15b1eaf0a0dce9a (patch) | |
tree | 1f776be9ad76adce37439ba22a7eeaa60355b370 /src | |
parent | ba7ee1d76c059a4d921a8f62e680dcf8d16adfa6 (diff) | |
download | poezio-39fcd9a4f6ace2e00f133bfaa15b1eaf0a0dce9a.tar.gz poezio-39fcd9a4f6ace2e00f133bfaa15b1eaf0a0dce9a.tar.bz2 poezio-39fcd9a4f6ace2e00f133bfaa15b1eaf0a0dce9a.tar.xz poezio-39fcd9a4f6ace2e00f133bfaa15b1eaf0a0dce9a.zip |
Allow a plugin to add a keybinding
Diffstat (limited to 'src')
-rw-r--r-- | src/plugin.py | 12 | ||||
-rw-r--r-- | src/plugin_manager.py | 18 |
2 files changed, 30 insertions, 0 deletions
diff --git a/src/plugin.py b/src/plugin.py index 92adbc4b..ddc3253e 100644 --- a/src/plugin.py +++ b/src/plugin.py @@ -96,6 +96,18 @@ class BasePlugin(object, metaclass=SafetyMetaclass): """ return self.plugin_manager.del_command(self.__module__, name) + def add_key(self, key, handler): + """ + Add a global keybind + """ + return self.plugin_manager.add_key(self.__module__, key, handler) + + def del_key(self, key): + """ + Remove a global keybind + """ + return self.plugin_manager.del_key(self.__module__, key) + def add_tab_command(self, tab_type, name, handler, help, completion=None): """ Add a command only for a type of tab. diff --git a/src/plugin_manager.py b/src/plugin_manager.py index 437d8ee2..76765aa8 100644 --- a/src/plugin_manager.py +++ b/src/plugin_manager.py @@ -36,6 +36,7 @@ class PluginManager(object): self.commands = {} # module name -> dict of commands loaded for the module self.event_handlers = {} # module name -> list of event_name/handler pairs loaded for the module self.tab_commands = {} #module name -> dict of tab types; tab type -> commands loaded by the module + self.keys = {} # module name → dict of keys/handlers loaded for the module def load(self, name, notify=True): if name in self.plugins: @@ -61,6 +62,7 @@ class PluginManager(object): self.modules[name] = module self.commands[name] = {} + self.keys[name] = {} self.tab_commands[name] = {} self.event_handlers[name] = [] self.plugins[name] = module.Plugin(self, self.core, plugins_conf_dir) @@ -72,6 +74,8 @@ class PluginManager(object): try: for command in self.commands[name].keys(): del self.core.commands[command] + for key in self.keys[name].keys(): + del self.core.key_func[key] for tab in list(self.tab_commands[name].keys()): for command in self.tab_commands[name][tab]: self.del_tab_command(name, getattr(tabs, tab), command[0]) @@ -82,6 +86,7 @@ class PluginManager(object): self.plugins[name].unload() del self.plugins[name] del self.commands[name] + del self.keys[name] del self.tab_commands[name] del self.event_handlers[name] if notify: @@ -122,6 +127,19 @@ class PluginManager(object): if isinstance(tab, tab_type) and name in tab.commands: del tab.commands[name] + def add_key(self, module_name, key, handler): + if key in self.core.key_func: + raise Exception(_("Key '%s' already exists") % (key,)) + keys = self.keys[module_name] + keys[key] = handler + self.core.key_func[key] = handler + + def del_key(self, module_name, key): + if key in self.keys[module_name]: + del self.keys[module_name][key] + if key in self.core.key_func: + del self.core.commands[key] + def add_command(self, module_name, name, handler, help, completion=None): if name in self.core.commands: raise Exception(_("Command '%s' already exists") % (name,)) |