Skip to content

Instantly share code, notes, and snippets.

@tfheen
Created July 18, 2015 16:13
Show Gist options
  • Save tfheen/39d2c5b371735d776d42 to your computer and use it in GitHub Desktop.
Save tfheen/39d2c5b371735d776d42 to your computer and use it in GitHub Desktop.
diff --git a/wee_slack.py b/wee_slack.py
index 251d5bf..ad5c92b 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -1634,8 +1634,55 @@ def script_unloaded():
# END Utility Methods
+def nick_completion_cb(data, completion_item, buffer, completion):
+ """
+ Adds all @-prefixed nicks to completion list
+ """
+
+ channel = channels.find(buffer)
+ for m in channel.members:
+ user = channel.server.users.find(m)
+ w.hook_completion_list_add(completion, "@" + user.name, 1, w.WEECHAT_LIST_POS_SORT)
+ return w.WEECHAT_RC_OK
+
+
+def complete_next_cb(data, buffer, command):
+ """Extract current word, if it matches a nick, prefix it with @ and
+ rely on nick_completion_cb adding the @-prefixed versions to the
+ completion lists, then let Weechat's internal completion do its
+ thing
+
+ """
+
+ channel = channels.find(buffer)
+ input = w.buffer_get_string(buffer, "input")
+ current_pos = w.buffer_get_integer(buffer, "input_pos") - 1
+ input_length = w.buffer_get_integer(buffer, "input_length")
+ word_start = 0
+ word_end = input_length
+ for l in range(current_pos, 0, -1):
+ if not input[l].isalnum():
+ word_start = l + 1
+ break
+ for l in range(current_pos, input_length):
+ if not input[l].isalnum():
+ word_end = l
+ break
+ word = input[word_start:word_end]
+ for m in channel.members:
+ user = channel.server.users.find(m)
+ if user.name.startswith(word):
+ # Here, we cheat. Insert a @ in front and rely in the @
+ # nicks being in the completion list
+ w.buffer_set(buffer, "input", input[:word_start] + "@" + input[word_start:])
+ w.buffer_set(buffer, "input_pos", str(w.buffer_get_integer(buffer, "input_pos") + 1))
+ break
+ return w.WEECHAT_RC_OK
+
# Main
-if __name__ == "__main__":
+if __name__ =
+
+ = "__main__":
if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
SCRIPT_DESC, "script_unloaded", ""):
@@ -1732,5 +1779,8 @@ if __name__ == "__main__":
w.hook_command_run('/join', 'join_command_cb', '')
w.hook_command_run('/part', 'part_command_cb', '')
w.hook_command_run('/leave', 'part_command_cb', '')
+ w.hook_command_run("/input complete_next", "complete_next_cb", "")
+ w.hook_completion("nicks", "complete @-nicks for slack",
+ "nick_completion_cb", "")
w.bar_item_new('slack_typing_notice', 'typing_bar_item_cb', '')
# END attach to the weechat hooks we need
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment