1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Save chat room message prefix for autocomplete

This commit is contained in:
James Booth 2014-07-15 22:46:29 +01:00
parent cc62fe376c
commit d02c8af532
3 changed files with 69 additions and 33 deletions

View File

@ -1303,10 +1303,8 @@ cmd_reset_autocomplete()
autocomplete_reset(sub_ac); autocomplete_reset(sub_ac);
if (ui_current_win_type() == WIN_MUC) { if (ui_current_win_type() == WIN_MUC) {
Autocomplete nick_ac = muc_get_roster_ac(ui_current_recipient()); char *recipient = ui_current_recipient();
if (nick_ac != NULL) { muc_reset_autocomplete(recipient);
autocomplete_reset(nick_ac);
}
} }
autocomplete_reset(who_ac); autocomplete_reset(who_ac);

View File

@ -36,6 +36,7 @@ typedef struct _muc_room_t {
char *nick; // e.g. Some User char *nick; // e.g. Some User
char *password; char *password;
char *subject; char *subject;
char *autocomplete_prefix;
GList *pending_broadcasts; GList *pending_broadcasts;
gboolean autojoin; gboolean autojoin;
gboolean pending_nick_change; gboolean pending_nick_change;
@ -141,6 +142,7 @@ muc_join_room(const char * const room, const char * const nick,
ChatRoom *new_room = malloc(sizeof(ChatRoom)); ChatRoom *new_room = malloc(sizeof(ChatRoom));
new_room->room = strdup(room); new_room->room = strdup(room);
new_room->nick = strdup(nick); new_room->nick = strdup(nick);
new_room->autocomplete_prefix = NULL;
if (password != NULL) { if (password != NULL) {
new_room->password = strdup(password); new_room->password = strdup(password);
} else { } else {
@ -571,37 +573,69 @@ muc_complete_roster_nick_change(const char * const room,
void void
muc_autocomplete(char *input, int *size) muc_autocomplete(char *input, int *size)
{ {
if (rooms == NULL) {
return;
}
char *recipient = ui_current_recipient(); char *recipient = ui_current_recipient();
Autocomplete nick_ac = muc_get_roster_ac(recipient); ChatRoom *chat_room = g_hash_table_lookup(rooms, recipient);
if (nick_ac != NULL) {
input[*size] = '\0'; if (chat_room == NULL) {
gchar *last_space = g_strrstr(input, " "); return;
char *result = NULL; }
if (last_space == NULL) {
result = autocomplete_complete(nick_ac, input, FALSE); if (chat_room->nick_ac == NULL) {
if (result != NULL) { return;
ui_replace_input(input, result, size); }
g_free(result);
return; input[*size] = '\0';
} char *search_str = NULL;
} else {
char *search_str = last_space+1; gchar *last_space = g_strrstr(input, " ");
if (*search_str != '\0') { if (last_space == NULL) {
result = autocomplete_complete(nick_ac, search_str, FALSE); search_str = input;
if (result != NULL) { if (chat_room->autocomplete_prefix == NULL) {
if (g_str_has_suffix(input, result) == FALSE) { chat_room->autocomplete_prefix = strdup("");
gchar *start_str = g_strndup(input, search_str - input);
GString *replace_with = g_string_new(start_str);
g_string_append(replace_with, result);
ui_replace_input(input, replace_with->str, size);
g_string_free(replace_with, TRUE);
g_free(start_str);
g_free(result);
return;
}
}
}
} }
} else {
search_str = last_space+1;
if (chat_room->autocomplete_prefix == NULL) {
chat_room->autocomplete_prefix = g_strndup(input, search_str - input);
}
}
char *result = autocomplete_complete(chat_room->nick_ac, search_str, FALSE);
if (result != NULL) {
GString *replace_with = g_string_new(chat_room->autocomplete_prefix);
g_string_append(replace_with, result);
ui_replace_input(input, replace_with->str, size);
g_string_free(replace_with, TRUE);
g_free(result);
}
return;
}
void
muc_reset_autocomplete(const char * const room)
{
if (rooms == NULL) {
return;
}
ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room == NULL) {
return;
}
if (chat_room->nick_ac != NULL) {
autocomplete_reset(chat_room->nick_ac);
}
if (chat_room->autocomplete_prefix != NULL) {
free(chat_room->autocomplete_prefix);
chat_room->autocomplete_prefix = NULL;
} }
} }
@ -613,6 +647,9 @@ _free_room(ChatRoom *room)
free(room->nick); free(room->nick);
free(room->subject); free(room->subject);
free(room->password); free(room->password);
if (room->autocomplete_prefix != NULL) {
free(room->autocomplete_prefix);
}
if (room->roster != NULL) { if (room->roster != NULL) {
g_hash_table_destroy(room->roster); g_hash_table_destroy(room->roster);
} }

View File

@ -77,5 +77,6 @@ void muc_add_pending_broadcast(const char * const room, const char * const messa
GList * muc_get_pending_broadcasts(const char * const room); GList * muc_get_pending_broadcasts(const char * const room);
void muc_autocomplete(char *input, int *size); void muc_autocomplete(char *input, int *size);
void muc_reset_autocomplete(const char * const room);
#endif #endif