From 547b6cf4dad245876bdd1980f1ad5abfeb843af0 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 24 Apr 2014 21:50:59 +0100 Subject: [PATCH 1/3] Added command /wins swap --- src/command/command.c | 14 ++++++++------ src/command/commands.c | 20 ++++++++++++++++++++ src/ui/core.c | 7 +++++++ src/ui/ui.h | 1 + src/ui/windows.c | 29 +++++++++++++++++++++++++++++ src/ui/windows.h | 1 + 6 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index 6e884ceb..65aa2a21 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -347,13 +347,14 @@ static struct cmd_t command_defs[] = NULL } } }, { "/wins", - cmd_wins, parse_args, 0, 1, NULL, - { "/wins [tidy|prune]", "List or tidy active windows.", - { "/wins [tidy|prune]", - "------------------", + cmd_wins, parse_args, 0, 3, NULL, + { "/wins [tidy|prune|swap] [source] [target]", "List or tidy active windows.", + { "/wins [tidy|prune] [source] [target]", + "------------------------------------", "Passing no argument will list all currently active windows and information about their usage.", - "tidy : Shuffle windows so there are no gaps.", - "prune : Close all windows with no unread messages, and then tidy as above.", + "tidy : Shuffle windows so there are no gaps.", + "prune : Close all windows with no unread messages, and then tidy as above.", + "swap source target : Swap windows.", NULL } } }, { "/sub", @@ -1026,6 +1027,7 @@ cmd_init(void) wins_ac = autocomplete_new(); autocomplete_add(wins_ac, "prune"); autocomplete_add(wins_ac, "tidy"); + autocomplete_add(wins_ac, "swap"); roster_ac = autocomplete_new(); autocomplete_add(roster_ac, "add"); diff --git a/src/command/commands.c b/src/command/commands.c index c0729630..8a37305a 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -470,7 +470,27 @@ cmd_wins(gchar **args, struct cmd_help_t help) ui_tidy_wins(); } else if (strcmp(args[0], "prune") == 0) { ui_prune_wins(); + } else if (strcmp(args[0], "swap") == 0) { + if ((args[1] == NULL) || (args[2] == NULL)) { + cons_show("Usage: %s", help.usage); + } else { + int source_win = atoi(args[1]); + int target_win = atoi(args[2]); + if ((source_win == 1) || (target_win == 1)) { + cons_show("Cannot move console window."); + } else if (source_win != target_win) { + gboolean swapped = ui_swap_wins(source_win, target_win); + if (swapped) { + cons_show("Swapped windows %d <-> %d", source_win, target_win); + } else { + cons_show("Window %d does not exist", source_win); + } + } else { + cons_show("Same source and target window supplied."); + } + } } + return TRUE; } diff --git a/src/ui/core.c b/src/ui/core.c index 8f385cf2..3b7789d9 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -950,6 +950,12 @@ _ui_prune_wins(void) } } +static gboolean +_ui_swap_wins(int source_win, int target_win) +{ + return wins_swap(source_win, target_win); +} + static win_type_t _ui_current_win_type(void) { @@ -1987,4 +1993,5 @@ ui_init_module(void) ui_create_xmlconsole_win = _ui_create_xmlconsole_win; ui_xmlconsole_exists = _ui_xmlconsole_exists; ui_handle_room_join_error = _ui_handle_room_join_error; + ui_swap_wins = _ui_swap_wins; } diff --git a/src/ui/ui.h b/src/ui/ui.h index 14f7137c..d56c8f5d 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -147,6 +147,7 @@ gboolean (*ui_duck_exists)(void); void (*ui_tidy_wins)(void); void (*ui_prune_wins)(void); +gboolean (*ui_swap_wins)(int source_win, int target_win); void (*ui_auto_away)(void); void (*ui_end_auto_away)(void); diff --git a/src/ui/windows.c b/src/ui/windows.c index 82659be5..48ab9ea8 100644 --- a/src/ui/windows.c +++ b/src/ui/windows.c @@ -386,6 +386,35 @@ wins_lost_connection(void) } } +gboolean +wins_swap(int source_win, int target_win) +{ + ProfWin *source = g_hash_table_lookup(windows, GINT_TO_POINTER(source_win)); + + if (source != NULL) { + ProfWin *target = g_hash_table_lookup(windows, GINT_TO_POINTER(target_win)); + + if (target == NULL) { + g_hash_table_steal(windows, GINT_TO_POINTER(source_win)); + status_bar_inactive(source_win); + g_hash_table_insert(windows, GINT_TO_POINTER(target_win), source); + if (source->unread > 0) { + status_bar_new(target_win); + } else { + status_bar_active(target_win); + } + if ((wins_get_current_num() == source_win) || (wins_get_current_num() == target_win)) { + ui_switch_win(1); + } + return TRUE; + } else { + return FALSE; + } + } else { + return FALSE; + } +} + gboolean wins_tidy(void) { diff --git a/src/ui/windows.h b/src/ui/windows.h index cbe219df..7b3513fb 100644 --- a/src/ui/windows.h +++ b/src/ui/windows.h @@ -50,5 +50,6 @@ void wins_destroy(void); GList * wins_get_nums(void); gboolean wins_xmlconsole_exists(void); ProfWin * wins_get_xmlconsole(void); +gboolean wins_swap(int source_win, int target_win); #endif From e9ddbab5851a406ea4177ce41d4adb1c7ef850b4 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 24 Apr 2014 23:06:42 +0100 Subject: [PATCH 2/3] Allow /wins swap to move to empty position --- src/command/commands.c | 2 ++ src/ui/core.c | 1 - src/ui/windows.c | 22 +++++++++++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/command/commands.c b/src/command/commands.c index 8a37305a..83fe4322 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -478,6 +478,8 @@ cmd_wins(gchar **args, struct cmd_help_t help) int target_win = atoi(args[2]); if ((source_win == 1) || (target_win == 1)) { cons_show("Cannot move console window."); + } else if (source_win == 10 || target_win == 10) { + cons_show("Window 10 does not exist"); } else if (source_win != target_win) { gboolean swapped = ui_swap_wins(source_win, target_win); if (swapped) { diff --git a/src/ui/core.c b/src/ui/core.c index 3b7789d9..383eafef 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -691,7 +691,6 @@ static gboolean _ui_switch_win(const int i) { if (ui_win_exists(i)) { - ui_current_page_off(); ProfWin *new_current = wins_get_by_num(i); wins_set_current_by_num(i); ui_current_page_off(); diff --git a/src/ui/windows.c b/src/ui/windows.c index 48ab9ea8..44639040 100644 --- a/src/ui/windows.c +++ b/src/ui/windows.c @@ -394,6 +394,7 @@ wins_swap(int source_win, int target_win) if (source != NULL) { ProfWin *target = g_hash_table_lookup(windows, GINT_TO_POINTER(target_win)); + // target window empty if (target == NULL) { g_hash_table_steal(windows, GINT_TO_POINTER(source_win)); status_bar_inactive(source_win); @@ -407,8 +408,27 @@ wins_swap(int source_win, int target_win) ui_switch_win(1); } return TRUE; + + // target window occupied } else { - return FALSE; + g_hash_table_steal(windows, GINT_TO_POINTER(source_win)); + g_hash_table_steal(windows, GINT_TO_POINTER(target_win)); + g_hash_table_insert(windows, GINT_TO_POINTER(source_win), target); + g_hash_table_insert(windows, GINT_TO_POINTER(target_win), source); + if (source->unread > 0) { + status_bar_new(target_win); + } else { + status_bar_active(target_win); + } + if (target->unread > 0) { + status_bar_new(source_win); + } else { + status_bar_active(source_win); + } + if ((wins_get_current_num() == source_win) || (wins_get_current_num() == target_win)) { + ui_switch_win(1); + } + return TRUE; } } else { return FALSE; From f8b2e009ed1608ea99b6a757944eae956cba86be Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 24 Apr 2014 23:08:27 +0100 Subject: [PATCH 3/3] Fixed /wins swap help --- src/command/command.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index 65aa2a21..0e7c9357 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -349,12 +349,12 @@ static struct cmd_t command_defs[] = { "/wins", cmd_wins, parse_args, 0, 3, NULL, { "/wins [tidy|prune|swap] [source] [target]", "List or tidy active windows.", - { "/wins [tidy|prune] [source] [target]", - "------------------------------------", + { "/wins [tidy|prune|swap] [source] [target]", + "-----------------------------------------", "Passing no argument will list all currently active windows and information about their usage.", "tidy : Shuffle windows so there are no gaps.", "prune : Close all windows with no unread messages, and then tidy as above.", - "swap source target : Swap windows.", + "swap source target : Swap windows, target may be an empty position.", NULL } } }, { "/sub",