From 01b5d5c1c5faca9bfcf7db97ee2eca4ca0102afe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 26 Feb 2019 16:08:39 +0100 Subject: [PATCH] matrix: Allow buffers to be switched by short names. This patch overrides the /buffer command and modifies it so we can switch to matrix buffers by their short name. --- main.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ matrix/buffer.py | 5 +++++ 2 files changed, 57 insertions(+) diff --git a/main.py b/main.py index 1a4159e..61e5c84 100644 --- a/main.py +++ b/main.py @@ -526,6 +526,57 @@ def typing_notification_cb(data, signal, buffer_ptr): return W.WEECHAT_RC_OK +def buffer_command_cb(data, _, command): + """Override the buffer command to allow switching buffers by short name.""" + command = command[7:].strip() + + buffer_subcommands = ["list", "add", "clear", "move", "swap", "cycle", + "merge", "unmerge", "hide", "unhide", "renumber", + "close", "notify", "localvar", "set", "get"] + + if not command: + return W.WEECHAT_RC_OK + + command_words = command.split() + + if len(command_words) >= 1: + if command_words[0] in buffer_subcommands: + return W.WEECHAT_RC_OK + + elif command_words[0].startswith("*"): + return W.WEECHAT_RC_OK + + try: + int(command_words[0]) + return W.WEECHAT_RC_OK + except ValueError: + pass + + room_buffers = [] + + for server in SERVERS.values(): + room_buffers.extend(server.room_buffers.values()) + + sorted_buffers = sorted( + room_buffers, + key=lambda b: b.weechat_buffer.number + ) + + for room_buffer in sorted_buffers: + buffer = room_buffer.weechat_buffer + + if command in buffer.short_name: + displayed = W.current_buffer() == buffer._ptr + + if displayed: + continue + + W.buffer_set(buffer._ptr, 'display', '1') + return W.WEECHAT_RC_OK_EAT + + return W.WEECHAT_RC_OK + + if __name__ == "__main__": if W.register(WEECHAT_SCRIPT_NAME, WEECHAT_SCRIPT_AUTHOR, WEECHAT_SCRIPT_VERSION, WEECHAT_SCRIPT_LICENSE, @@ -548,6 +599,7 @@ if __name__ == "__main__": init_bar_items() init_completion() + W.hook_command_run("/buffer", "buffer_command_cb", "") W.hook_signal("buffer_switch", "buffer_switch_cb", "") W.hook_signal("input_text_changed", "typing_notification_cb", "") diff --git a/matrix/buffer.py b/matrix/buffer.py index ca13c63..663ebaf 100644 --- a/matrix/buffer.py +++ b/matrix/buffer.py @@ -803,6 +803,11 @@ class WeechatChannelBuffer(object): def short_name(self): return W.buffer_get_string(self._ptr, "short_name") + @property + def number(self): + """Get the buffer number, starts at 1.""" + return int(W.buffer_get_integer(self._ptr, "number")) + @short_name.setter def short_name(self, name): W.buffer_set(self._ptr, "short_name", name)