diff --git a/matrix.py b/matrix.py index 213ae42..2a86888 100644 --- a/matrix.py +++ b/matrix.py @@ -49,9 +49,7 @@ from matrix.commands import ( matrix_command_topic_cb, matrix_command_pgup_cb, matrix_redact_command_cb, - matrix_command_buf_clear_cb, - matrix_debug_completion_cb, - matrix_message_completion_cb + matrix_command_buf_clear_cb ) from matrix.bar_items import ( @@ -60,6 +58,14 @@ from matrix.bar_items import ( matrix_bar_item_plugin ) +from matrix.completion import ( + init_completion, + matrix_command_completion_cb, + matrix_debug_completion_cb, + matrix_message_completion_cb, + matrix_server_completion_cb +) + from matrix.utils import ( key_from_value, server_buffer_prnt, @@ -479,74 +485,6 @@ def matrix_unload_cb(): return W.WEECHAT_RC_OK -def add_servers_to_completion(completion): - for server_name in SERVERS: - W.hook_completion_list_add( - completion, - server_name, - 0, - W.WEECHAT_LIST_POS_SORT - ) - - -@utf8_decode -def server_command_completion_cb(data, completion_item, buffer, completion): - buffer_input = W.buffer_get_string(buffer, "input").split() - - args = buffer_input[1:] - commands = ['add', 'delete', 'list', 'listfull'] - - def complete_commands(): - for command in commands: - W.hook_completion_list_add( - completion, - command, - 0, - W.WEECHAT_LIST_POS_SORT - ) - - if len(args) == 1: - complete_commands() - - elif len(args) == 2: - if args[1] not in commands: - complete_commands() - else: - if args[1] == 'delete' or args[1] == 'listfull': - add_servers_to_completion(completion) - - elif len(args) == 3: - if args[1] == 'delete' or args[1] == 'listfull': - if args[2] not in SERVERS: - add_servers_to_completion(completion) - - return W.WEECHAT_RC_OK - - -@utf8_decode -def matrix_server_completion_cb(data, completion_item, buffer, completion): - add_servers_to_completion(completion) - return W.WEECHAT_RC_OK - - -@utf8_decode -def matrix_command_completion_cb(data, completion_item, buffer, completion): - for command in [ - "connect", - "disconnect", - "reconnect", - "server", - "help", - "debug" - ]: - W.hook_completion_list_add( - completion, - command, - 0, - W.WEECHAT_LIST_POS_SORT) - return W.WEECHAT_RC_OK - - def create_default_server(config_file): server = MatrixServer('matrix.org', W, config_file) SERVERS[server.name] = server @@ -577,6 +515,7 @@ if __name__ == "__main__": hook_commands() init_bar_items() + init_completion() if not SERVERS: create_default_server(CONFIG) diff --git a/matrix/commands.py b/matrix/commands.py index f5de1aa..a303aab 100644 --- a/matrix/commands.py +++ b/matrix/commands.py @@ -35,41 +35,6 @@ SERVERS = matrix.globals.SERVERS CONFIG = matrix.globals.CONFIG def hook_commands(): - W.hook_completion( - "matrix_server_commands", - "Matrix server completion", - "server_command_completion_cb", - "" - ) - - W.hook_completion( - "matrix_servers", - "Matrix server completion", - "matrix_server_completion_cb", - "" - ) - - W.hook_completion( - "matrix_commands", - "Matrix command completion", - "matrix_command_completion_cb", - "" - ) - - W.hook_completion( - "matrix_messages", - "Matrix message completion", - "matrix_message_completion_cb", - "" - ) - - W.hook_completion( - "matrix_debug_types", - "Matrix debugging type completion", - "matrix_debug_completion_cb", - "" - ) - W.hook_command( # Command name and short description 'matrix', 'Matrix chat protocol command', @@ -169,17 +134,6 @@ def hook_page_up(): ) -@utf8_decode -def matrix_debug_completion_cb(data, completion_item, buffer, completion): - for debug_type in ["messaging", "network", "timing"]: - W.hook_completion_list_add( - completion, - debug_type, - 0, - W.WEECHAT_LIST_POS_SORT) - return W.WEECHAT_RC_OK - - @utf8_decode def matrix_command_buf_clear_cb(data, buffer, command): for server in SERVERS.values(): @@ -420,57 +374,6 @@ def matrix_redact_command_cb(data, buffer, args): return W.WEECHAT_RC_OK -@utf8_decode -def matrix_message_completion_cb(data, completion_item, buffer, completion): - own_lines = W.hdata_pointer(W.hdata_get('buffer'), buffer, 'own_lines') - if own_lines: - line = W.hdata_pointer( - W.hdata_get('lines'), - own_lines, - 'last_line' - ) - - line_number = 1 - - while line: - line_data = W.hdata_pointer( - W.hdata_get('line'), - line, - 'data' - ) - - if line_data: - message = W.hdata_string(W.hdata_get('line_data'), line_data, - 'message') - - tags = tags_from_line_data(line_data) - - # Only add non redacted user messages to the completion - if (message - and 'matrix_message' in tags - and 'matrix_redacted' not in tags): - - if len(message) > GLOBAL_OPTIONS.redaction_comp_len + 2: - message = ( - message[:GLOBAL_OPTIONS.redaction_comp_len] - + '..') - - item = ("{number}:\"{message}\"").format( - number=line_number, - message=message) - - W.hook_completion_list_add( - completion, - item, - 0, - W.WEECHAT_LIST_POS_END) - line_number += 1 - - line = W.hdata_move(W.hdata_get('line'), line, -1) - - return W.WEECHAT_RC_OK - - def matrix_command_debug(args): if not args: message = ("{prefix}matrix: Too few arguments for command "