From 3dc5c5e5b597d96aef3f7f8886316a9c76849789 Mon Sep 17 00:00:00 2001 From: mathieui Date: Thu, 10 May 2012 01:26:35 +0200 Subject: Add a way to execute a custom command with a custom key binding (+doc) --- src/core.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/core.py b/src/core.py index 07d1f8ea..67c110eb 100644 --- a/src/core.py +++ b/src/core.py @@ -177,7 +177,8 @@ class Core(object): del self.commands['status'] del self.commands['show'] - self.key_func = { + self.key_func = KeyDict() + key_func = { "KEY_PPAGE": self.scroll_page_up, "KEY_NPAGE": self.scroll_page_down, "^B": self.scroll_line_up, @@ -231,7 +232,10 @@ class Core(object): '_chat': lambda: self.command_status('chat'), '_dnd': lambda: self.command_status('dnd'), '_xa': lambda: self.command_status('xa'), + ##### Custom actions ######## + '_exc_': lambda arg: self.try_execute(arg), } + self.key_func.update(key_func) # Add handlers self.xmpp.add_event_handler('connected', self.on_connected) @@ -1078,8 +1082,9 @@ class Core(object): else: self.command_win('%d' % nb) # search for keyboard shortcut - if char in self.key_func: - self.key_func[char]() + func = self.key_func.get(char, None) + if func: + func() else: res = self.do_command(replace_line_breaks(char), False) if res: @@ -2401,3 +2406,26 @@ class Core(object): Insert the given text into the current input """ self.do_command(text, True) + + def try_execute(self, line): + """ + Try to execute a command in the current tab + """ + line = '/' + line + try: + self.current_tab().execute_command(line) + except: + import traceback + log.debug('Execute failed:\n%s', traceback.format_exc()) + +class KeyDict(dict): + """ + A dict, with a wrapper for get() that will return a custom value + if the key starts with _exc_ + """ + def get(self, k, d=None): + if isinstance(k, str) and k.startswith('_exc_') and len(k) > 5: + return lambda: dict.get(self, '_exc_')(k[5:]) + return dict.get(self, k, d) + + -- cgit v1.2.3