2 window-commands.c : irssi
4 Copyright (C) 2000 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "module-formats.h"
31 #include "fe-windows.h"
32 #include "window-items.h"
33 #include "windows-layout.h"
34 #include "printtext.h"
36 static void cmd_window(const char *data, void *server, WI_ITEM_REC *item)
38 if (is_numeric(data, 0)) {
39 signal_emit("command window refnum", 3, data, server, item);
43 command_runsub("window", data, server, item);
46 /* SYNTAX: WINDOW NEW [hide] */
47 static void cmd_window_new(const char *data, void *server, WI_ITEM_REC *item)
52 g_return_if_fail(data != NULL);
54 type = (g_strncasecmp(data, "hid", 3) == 0 || g_strcasecmp(data, "tab") == 0) ? 1 :
55 (g_strcasecmp(data, "split") == 0 ? 2 : 0);
56 signal_emit("gui window create override", 1, GINT_TO_POINTER(type));
58 window = window_create(NULL, FALSE);
59 window_change_server(window, server);
62 /* SYNTAX: WINDOW CLOSE [<first> [<last>] */
63 static void cmd_window_close(const char *data)
65 GSList *tmp, *destroys;
67 int first_num, last_num;
70 if (!cmd_get_params(data, &free_arg, 2, &first, &last))
73 if ((*first != '\0' && !is_numeric(first, '\0')) ||
74 ((*last != '\0') && !is_numeric(last, '\0'))) {
75 cmd_params_free(free_arg);
79 first_num = *first == '\0' ? active_win->refnum : atoi(first);
80 last_num = *last == '\0' ? active_win->refnum : atoi(last);
82 /* get list of windows to destroy */
84 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
85 WINDOW_REC *rec = tmp->data;
87 if (rec->refnum >= first_num && rec->refnum <= last_num)
88 destroys = g_slist_append(destroys, rec);
91 /* really destroy the windows */
92 while (destroys != NULL) {
93 WINDOW_REC *rec = destroys->data;
95 if (windows->next != NULL)
98 destroys = g_slist_remove(destroys, rec);
101 cmd_params_free(free_arg);
104 /* SYNTAX: WINDOW REFNUM <number> */
105 static void cmd_window_refnum(const char *data)
109 if (!is_numeric(data, 0))
112 window = window_find_refnum(atoi(data));
114 window_set_active(window);
117 /* return the first window number with the highest activity */
118 static WINDOW_REC *window_highest_activity(WINDOW_REC *window)
120 WINDOW_REC *rec, *max_win;
122 int max_act, through;
124 g_return_val_if_fail(window != NULL, NULL);
126 max_win = NULL; max_act = 0; through = FALSE;
128 tmp = g_slist_find(windows, window);
129 for (;; tmp = tmp->next) {
135 if (through && tmp->data == window)
140 if (rec->data_level > 0 && max_act < rec->data_level) {
141 max_act = rec->data_level;
149 /* SYNTAX: WINDOW GOTO active|<number>|<name> */
150 static void cmd_window_goto(const char *data)
154 g_return_if_fail(data != NULL);
156 if (is_numeric(data, 0)) {
157 cmd_window_refnum(data);
161 if (g_strcasecmp(data, "active") == 0)
162 window = window_highest_activity(active_win);
164 window = window_find_item(active_win->active_server, data);
167 window_set_active(window);
170 /* SYNTAX: WINDOW NEXT */
171 static void cmd_window_next(void)
175 num = window_refnum_next(active_win->refnum, TRUE);
176 if (num < 1) num = windows_refnum_last();
178 window_set_active(window_find_refnum(num));
181 /* SYNTAX: WINDOW LAST */
182 static void cmd_window_last(void)
184 if (windows->next != NULL)
185 window_set_active(windows->next->data);
188 /* SYNTAX: WINDOW PREVIOUS */
189 static void cmd_window_previous(void)
193 num = window_refnum_prev(active_win->refnum, TRUE);
194 if (num < 1) num = window_refnum_next(0, TRUE);
196 window_set_active(window_find_refnum(num));
199 /* SYNTAX: WINDOW LEVEL [<level>] */
200 static void cmd_window_level(const char *data)
204 g_return_if_fail(data != NULL);
206 window_set_level(active_win, combine_level(active_win->level, data));
208 level = active_win->level == 0 ? g_strdup("NONE") :
209 bits2level(active_win->level);
210 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
211 TXT_WINDOW_LEVEL, level);
215 /* SYNTAX: WINDOW SERVER [-sticky | -unsticky] <tag> */
216 static void cmd_window_server(const char *data)
223 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
224 "window server", &optlist, &tag))
228 (g_hash_table_lookup(optlist, "sticky") != NULL ||
229 g_hash_table_lookup(optlist, "unsticky") != NULL)) {
230 tag = active_win->active_server->tag;
234 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
235 server = server_find_tag(tag);
237 if (g_hash_table_lookup(optlist, "unsticky") != NULL &&
238 active_win->servertag != NULL) {
239 g_free_and_null(active_win->servertag);
240 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
241 TXT_UNSET_SERVER_STICKY, server->tag);
244 if (active_win->servertag != NULL &&
245 g_hash_table_lookup(optlist, "sticky") == NULL) {
246 printformat_window(active_win, MSGLEVEL_CLIENTERROR,
247 TXT_ERROR_SERVER_STICKY);
248 } else if (server == NULL) {
249 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
250 TXT_UNKNOWN_SERVER_TAG, tag);
251 } else if (active_win->active == NULL) {
252 window_change_server(active_win, server);
253 if (g_hash_table_lookup(optlist, "sticky") != NULL) {
254 g_free_not_null(active_win->servertag);
255 active_win->servertag = g_strdup(server->tag);
256 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
257 TXT_SET_SERVER_STICKY, server->tag);
259 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
261 server->tag, server->connrec->address,
262 server->connrec->chatnet == NULL ? "" :
263 server->connrec->chatnet);
266 cmd_params_free(free_arg);
269 static void cmd_window_item(const char *data, void *server, WI_ITEM_REC *item)
271 command_runsub("window item", data, server, item);
274 /* SYNTAX: WINDOW ITEM PREV */
275 static void cmd_window_item_prev(void)
277 window_item_prev(active_win);
280 /* SYNTAX: WINDOW ITEM NEXT */
281 static void cmd_window_item_next(void)
283 window_item_next(active_win);
286 /* SYNTAX: WINDOW ITEM GOTO <name> */
287 static void cmd_window_item_goto(const char *data, SERVER_REC *server)
291 item = window_item_find_window(active_win, server, data);
293 window_item_set_active(active_win, item);
296 /* SYNTAX: WINDOW ITEM MOVE <number>|<name> */
297 static void cmd_window_item_move(const char *data, SERVER_REC *server,
302 if (is_numeric(data, '\0')) {
303 /* move current window item to specified window */
304 window = window_find_refnum(atoi(data));
306 /* move specified window item to current window */
307 item = window_item_find(server, data);
310 if (window != NULL && item != NULL)
311 window_item_set_active(window, item);
314 /* SYNTAX: WINDOW NUMBER [-sticky] <number> */
315 static void cmd_window_number(const char *data)
322 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
323 "window number", &optlist, &refnum))
327 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
331 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
334 window_set_refnum(active_win, num);
335 active_win->sticky_refnum =
336 g_hash_table_lookup(optlist, "sticky") != NULL;
339 cmd_params_free(free_arg);
342 /* SYNTAX: WINDOW NAME <name> */
343 static void cmd_window_name(const char *data)
345 window_set_name(active_win, data);
348 /* we're moving the first window to last - move the first contiguous block
349 of refnums to left. Like if there's windows 1..5 and 7..10, move 1 to
350 11, 2..5 to 1..4 and leave 7..10 alone */
351 static void windows_move_left(WINDOW_REC *move_window)
356 window_set_refnum(move_window, windows_refnum_last()+1);
357 for (refnum = 2;; refnum++) {
358 window = window_find_refnum(refnum);
359 if (window == NULL) break;
361 window_set_refnum(window, refnum-1);
365 /* we're moving the last window to first - make some space so we can use the
367 static void windows_move_right(WINDOW_REC *move_window)
372 /* find the first unused refnum, like if there's windows
373 1..5 and 7..10, we only need to move 1..5 to 2..6 */
375 while (window_find_refnum(refnum) != NULL) refnum++;
379 window = window_find_refnum(refnum);
380 g_return_if_fail(window != NULL);
381 window_set_refnum(window, window == move_window ? 1 : refnum+1);
387 static void cmd_window_move_left(void)
391 refnum = window_refnum_prev(active_win->refnum, TRUE);
393 window_set_refnum(active_win, refnum);
397 windows_move_left(active_win);
400 static void cmd_window_move_right(void)
404 refnum = window_refnum_next(active_win->refnum, TRUE);
406 window_set_refnum(active_win, refnum);
410 windows_move_right(active_win);
413 /* SYNTAX: WINDOW MOVE <number>|left|right */
414 static void cmd_window_move(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
416 int new_refnum, refnum;
418 if (!is_numeric(data, 0)) {
419 command_runsub("window move", data, server, item);
423 new_refnum = atoi(data);
424 if (new_refnum > active_win->refnum) {
426 refnum = window_refnum_next(active_win->refnum, FALSE);
427 if (refnum == -1 || refnum > new_refnum)
430 window_set_refnum(active_win, refnum);
434 refnum = window_refnum_prev(active_win->refnum, FALSE);
435 if (refnum == -1 || refnum < new_refnum)
438 window_set_refnum(active_win, refnum);
443 /* SYNTAX: WINDOW LIST */
444 static void cmd_window_list(void)
446 GSList *tmp, *sorted;
449 sorted = windows_get_sorted();
450 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_WINDOWLIST_HEADER);
451 for (tmp = sorted; tmp != NULL; tmp = tmp->next) {
452 WINDOW_REC *rec = tmp->data;
454 levelstr = bits2level(rec->level);
455 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_WINDOWLIST_LINE,
456 rec->refnum, rec->name == NULL ? "" : rec->name,
457 rec->active == NULL ? "" : rec->active->name,
458 rec->active_server == NULL ? "" : ((SERVER_REC *) rec->active_server)->tag,
462 g_slist_free(sorted);
463 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_WINDOWLIST_FOOTER);
466 /* SYNTAX: WINDOW THEME <name> */
467 static void cmd_window_theme(const char *data)
471 g_free_not_null(active_win->theme_name);
472 active_win->theme_name = g_strdup(data);
474 active_win->theme = theme = theme_load(data);
476 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
477 TXT_WINDOW_THEME_CHANGED,
478 theme->name, theme->path);
480 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
481 TXT_THEME_NOT_FOUND, data);
485 static void cmd_layout(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
487 command_runsub("layout", data, server, item);
490 /* SYNTAX: FOREACH WINDOW <command> */
491 static void cmd_foreach_window(const char *data)
497 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
498 WINDOW_REC *rec = tmp->data;
501 signal_emit("send command", 3, data, rec->active_server,
507 void window_commands_init(void)
509 command_bind("window", NULL, (SIGNAL_FUNC) cmd_window);
510 command_bind("window new", NULL, (SIGNAL_FUNC) cmd_window_new);
511 command_bind("window close", NULL, (SIGNAL_FUNC) cmd_window_close);
512 command_bind("window kill", NULL, (SIGNAL_FUNC) cmd_window_close);
513 command_bind("window server", NULL, (SIGNAL_FUNC) cmd_window_server);
514 command_bind("window refnum", NULL, (SIGNAL_FUNC) cmd_window_refnum);
515 command_bind("window goto", NULL, (SIGNAL_FUNC) cmd_window_goto);
516 command_bind("window previous", NULL, (SIGNAL_FUNC) cmd_window_previous);
517 command_bind("window next", NULL, (SIGNAL_FUNC) cmd_window_next);
518 command_bind("window last", NULL, (SIGNAL_FUNC) cmd_window_last);
519 command_bind("window level", NULL, (SIGNAL_FUNC) cmd_window_level);
520 command_bind("window item", NULL, (SIGNAL_FUNC) cmd_window_item);
521 command_bind("window item prev", NULL, (SIGNAL_FUNC) cmd_window_item_prev);
522 command_bind("window item next", NULL, (SIGNAL_FUNC) cmd_window_item_next);
523 command_bind("window item goto", NULL, (SIGNAL_FUNC) cmd_window_item_goto);
524 command_bind("window item move", NULL, (SIGNAL_FUNC) cmd_window_item_move);
525 command_bind("window number", NULL, (SIGNAL_FUNC) cmd_window_number);
526 command_bind("window name", NULL, (SIGNAL_FUNC) cmd_window_name);
527 command_bind("window move", NULL, (SIGNAL_FUNC) cmd_window_move);
528 command_bind("window move left", NULL, (SIGNAL_FUNC) cmd_window_move_left);
529 command_bind("window move right", NULL, (SIGNAL_FUNC) cmd_window_move_right);
530 command_bind("window list", NULL, (SIGNAL_FUNC) cmd_window_list);
531 command_bind("window theme", NULL, (SIGNAL_FUNC) cmd_window_theme);
532 command_bind("layout", NULL, (SIGNAL_FUNC) cmd_layout);
533 /* SYNTAX: LAYOUT SAVE */
534 command_bind("layout save", NULL, (SIGNAL_FUNC) windows_layout_save);
535 /* SYNTAX: LAYOUT RESET */
536 command_bind("layout reset", NULL, (SIGNAL_FUNC) windows_layout_reset);
537 command_bind("foreach window", NULL, (SIGNAL_FUNC) cmd_foreach_window);
539 command_set_options("window number", "sticky");
540 command_set_options("window server", "sticky unsticky");
543 void window_commands_deinit(void)
545 command_unbind("window", (SIGNAL_FUNC) cmd_window);
546 command_unbind("window new", (SIGNAL_FUNC) cmd_window_new);
547 command_unbind("window close", (SIGNAL_FUNC) cmd_window_close);
548 command_unbind("window kill", (SIGNAL_FUNC) cmd_window_close);
549 command_unbind("window server", (SIGNAL_FUNC) cmd_window_server);
550 command_unbind("window refnum", (SIGNAL_FUNC) cmd_window_refnum);
551 command_unbind("window goto", (SIGNAL_FUNC) cmd_window_goto);
552 command_unbind("window previous", (SIGNAL_FUNC) cmd_window_previous);
553 command_unbind("window next", (SIGNAL_FUNC) cmd_window_next);
554 command_unbind("window last", (SIGNAL_FUNC) cmd_window_last);
555 command_unbind("window level", (SIGNAL_FUNC) cmd_window_level);
556 command_unbind("window item", (SIGNAL_FUNC) cmd_window_item);
557 command_unbind("window item prev", (SIGNAL_FUNC) cmd_window_item_prev);
558 command_unbind("window item next", (SIGNAL_FUNC) cmd_window_item_next);
559 command_unbind("window item goto", (SIGNAL_FUNC) cmd_window_item_goto);
560 command_unbind("window item move", (SIGNAL_FUNC) cmd_window_item_move);
561 command_unbind("window number", (SIGNAL_FUNC) cmd_window_number);
562 command_unbind("window name", (SIGNAL_FUNC) cmd_window_name);
563 command_unbind("window move", (SIGNAL_FUNC) cmd_window_move);
564 command_unbind("window move left", (SIGNAL_FUNC) cmd_window_move_left);
565 command_unbind("window move right", (SIGNAL_FUNC) cmd_window_move_right);
566 command_unbind("window list", (SIGNAL_FUNC) cmd_window_list);
567 command_unbind("window theme", (SIGNAL_FUNC) cmd_window_theme);
568 command_unbind("layout", (SIGNAL_FUNC) cmd_layout);
569 command_unbind("layout save", (SIGNAL_FUNC) windows_layout_save);
570 command_unbind("layout reset", (SIGNAL_FUNC) windows_layout_reset);
571 command_unbind("foreach window", (SIGNAL_FUNC) cmd_foreach_window);