diff options
author | mathieui <mathieui@mathieui.net> | 2012-05-10 01:26:35 +0200 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2012-05-10 01:26:35 +0200 |
commit | 3dc5c5e5b597d96aef3f7f8886316a9c76849789 (patch) | |
tree | 304e336baed3a773c108e5ecb78619c993314039 | |
parent | 70a724c1270c98607023d251787d1f727a4d07a3 (diff) | |
download | poezio-3dc5c5e5b597d96aef3f7f8886316a9c76849789.tar.gz poezio-3dc5c5e5b597d96aef3f7f8886316a9c76849789.tar.bz2 poezio-3dc5c5e5b597d96aef3f7f8886316a9c76849789.tar.xz poezio-3dc5c5e5b597d96aef3f7f8886316a9c76849789.zip |
Add a way to execute a custom command with a custom key binding
(+doc)
-rw-r--r-- | doc/en/keys.txt | 37 | ||||
-rw-r--r-- | src/core.py | 34 |
2 files changed, 66 insertions, 5 deletions
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) + + |