diff --git a/doc/en/keys.txt b/doc/en/keys.txt index b823c619..c1b9b7fd 100644 --- a/doc/en/keys.txt +++ b/doc/en/keys.txt @@ -370,8 +370,27 @@ Status actions Similar to /status xa. -Example -~~~~~~~ +Command execution +~~~~~~~~~~~~~~~~~ + +With that kind of actions, you can also execute arbitrary commands, with the + __exc__ keyword. + + +You only have to prefix your command line with *\_exc\_*, and without the _/_. + +./kick Partauche added on Ctrl-w +======================== + + ^W = _exc_kick Partauche + +======================== + +That key binding will only work in the tabs defining the command (here, the +MUC tab), and will show an error message in the others. + +Examples +~~~~~~~~ .Config with user-defined actions ================================= @@ -384,3 +403,17 @@ M-i = _show_important_room M-p = _toggle_pane ------------- ================================= + +.Config with commands mapped +================================= +[source,conf] +------------- +[bindings] +M-c = _exc_configure +^Q = _exc_part RAGE QUIT +^J = _exc_join +^F = _exc_load figlet +^R = _exc_load rainbow +^S = _exc_say llollllllllllll +------------- +================================= 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) + +