Merged with Irssi 0.8.6.
[silc.git] / apps / irssi / src / fe-common / core / window-commands.c
1 /*
2  window-commands.c : irssi
3
4     Copyright (C) 2000 Timo Sirainen
5
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.
10
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.
15
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
19 */
20
21 #include "module.h"
22 #include "module-formats.h"
23 #include "signals.h"
24 #include "commands.h"
25 #include "misc.h"
26 #include "servers.h"
27
28 #include "levels.h"
29
30 #include "themes.h"
31 #include "fe-windows.h"
32 #include "window-items.h"
33 #include "windows-layout.h"
34 #include "printtext.h"
35
36 static void window_print_binds(WINDOW_REC *win)
37 {
38         GSList *tmp;
39
40         printformat_window(win, MSGLEVEL_CLIENTCRAP,
41                            TXT_WINDOW_INFO_BOUND_ITEMS_HEADER);
42         for (tmp = win->bound_items; tmp != NULL; tmp = tmp->next) {
43                 WINDOW_BIND_REC *bind = tmp->data;
44
45                 printformat_window(win, MSGLEVEL_CLIENTCRAP,
46                                    TXT_WINDOW_INFO_BOUND_ITEM,
47                                    bind->name, bind->servertag,
48                                    bind->sticky ? "sticky" : "");
49         }
50         printformat_window(win, MSGLEVEL_CLIENTCRAP,
51                            TXT_WINDOW_INFO_BOUND_ITEMS_FOOTER);
52 }
53
54 static void window_print_items(WINDOW_REC *win)
55 {
56         GSList *tmp;
57         const char *type;
58
59         printformat_window(win, MSGLEVEL_CLIENTCRAP,
60                            TXT_WINDOW_INFO_ITEMS_HEADER);
61         for (tmp = win->items; tmp != NULL; tmp = tmp->next) {
62                 WI_ITEM_REC *item = tmp->data;
63
64                 type = module_find_id_str("WINDOW ITEM TYPE", item->type);
65                 printformat_window(win, MSGLEVEL_CLIENTCRAP,
66                                    TXT_WINDOW_INFO_ITEM,
67                                    type == NULL ? "??" : type,
68                                    item->visible_name,
69                                    item->server == NULL ? "" :
70                                    item->server->tag);
71         }
72         printformat_window(win, MSGLEVEL_CLIENTCRAP,
73                            TXT_WINDOW_INFO_ITEMS_FOOTER);
74 }
75
76 static void cmd_window_info(WINDOW_REC *win)
77 {
78         char *levelstr;
79
80         printformat_window(win, MSGLEVEL_CLIENTCRAP,
81                            TXT_WINDOW_INFO_HEADER);
82
83         /* Window reference number + sticky status */
84         if (!win->sticky_refnum) {
85                 printformat_window(win, MSGLEVEL_CLIENTCRAP,
86                                    TXT_WINDOW_INFO_REFNUM, win->refnum);
87         } else {
88                 printformat_window(win, MSGLEVEL_CLIENTCRAP,
89                                    TXT_WINDOW_INFO_REFNUM_STICKY, win->refnum);
90         }
91
92         /* Window name */
93         if (win->name != NULL) {
94                 printformat_window(win, MSGLEVEL_CLIENTCRAP,
95                                    TXT_WINDOW_INFO_NAME, win->name);
96         }
97
98         /* Window width / height */
99         printformat_window(win, MSGLEVEL_CLIENTCRAP, TXT_WINDOW_INFO_SIZE,
100                            win->width, win->height);
101
102         /* Window immortality */
103         if (win->immortal) {
104                 printformat_window(win, MSGLEVEL_CLIENTCRAP,
105                                    TXT_WINDOW_INFO_IMMORTAL);
106         }
107
108         /* Window history name */
109         if (win->history_name != NULL) {
110                 printformat_window(win, MSGLEVEL_CLIENTCRAP,
111                                    TXT_WINDOW_INFO_HISTORY, win->history_name);
112         }
113
114         /* Window level */
115         levelstr = win->level == 0 ?
116                 g_strdup("NONE") : bits2level(win->level);
117         printformat_window(win, MSGLEVEL_CLIENTCRAP, TXT_WINDOW_INFO_LEVEL,
118                            levelstr);
119         g_free(levelstr);
120
121         /* Active window server + sticky status */
122         if (win->servertag == NULL) {
123                 printformat_window(win, MSGLEVEL_CLIENTCRAP,
124                                    TXT_WINDOW_INFO_SERVER,
125                                    win->active_server != NULL ?
126                                    win->active_server->tag : "NONE");
127         } else {
128                 if (win->active_server != NULL &&
129                     strcmp(win->active_server->tag, win->servertag) != 0)
130                         g_warning("Active server isn't the sticky server!");
131
132                 printformat_window(win, MSGLEVEL_CLIENTCRAP,
133                                    TXT_WINDOW_INFO_SERVER_STICKY,
134                                    win->servertag);
135         }
136
137         /* Window theme + error status */
138         if (win->theme_name != NULL) {
139                 printformat_window(win, MSGLEVEL_CLIENTCRAP,
140                                    TXT_WINDOW_INFO_THEME, win->theme_name,
141                                    win->theme != NULL ? "" : "(not loaded)");
142         }
143
144         /* Bound items in window */
145         if (win->bound_items != NULL)
146                 window_print_binds(win);
147
148         /* Item */
149         if (win->items != NULL)
150                 window_print_items(win);
151
152         signal_emit("window print info", 1, win);
153
154         printformat_window(win, MSGLEVEL_CLIENTCRAP,
155                            TXT_WINDOW_INFO_FOOTER);
156 }
157
158 static void cmd_window(const char *data, void *server, WI_ITEM_REC *item)
159 {
160         while (*data == ' ') data++;
161
162         if (*data == '\0')
163                 cmd_window_info(active_win);
164         else if (is_numeric(data, 0))
165                 signal_emit("command window refnum", 3, data, server, item);
166         else
167                 command_runsub("window", data, server, item);
168 }
169
170 /* SYNTAX: WINDOW NEW [hide] */
171 static void cmd_window_new(const char *data, void *server, WI_ITEM_REC *item)
172 {
173         WINDOW_REC *window;
174         int type;
175
176         g_return_if_fail(data != NULL);
177
178         type = (g_strncasecmp(data, "hid", 3) == 0 || g_strcasecmp(data, "tab") == 0) ? 1 :
179                 (g_strcasecmp(data, "split") == 0 ? 2 : 0);
180         signal_emit("gui window create override", 1, GINT_TO_POINTER(type));
181
182         window = window_create(NULL, FALSE);
183         window_change_server(window, server);
184 }
185
186 /* SYNTAX: WINDOW CLOSE [<first> [<last>] */
187 static void cmd_window_close(const char *data)
188 {
189         GSList *tmp, *destroys;
190         char *first, *last;
191         int first_num, last_num;
192         void *free_arg;
193
194         if (!cmd_get_params(data, &free_arg, 2, &first, &last))
195                 return;
196
197         if ((*first != '\0' && !is_numeric(first, '\0')) ||
198             ((*last != '\0') && !is_numeric(last, '\0'))) {
199                 cmd_params_free(free_arg);
200                 return;
201         }
202
203         first_num = *first == '\0' ? active_win->refnum : atoi(first);
204         last_num = *last == '\0' ? first_num : atoi(last);
205
206         /* get list of windows to destroy */
207         destroys = NULL;
208         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
209                 WINDOW_REC *rec = tmp->data;
210
211                 if (rec->refnum >= first_num && rec->refnum <= last_num)
212                         destroys = g_slist_append(destroys, rec);
213         }
214
215         /* really destroy the windows */
216         while (destroys != NULL) {
217                 WINDOW_REC *rec = destroys->data;
218
219                 if (windows->next != NULL) {
220                         if (!rec->immortal)
221                                 window_destroy(rec);
222                         else {
223                                 printformat_window(rec, MSGLEVEL_CLIENTERROR,
224                                                    TXT_WINDOW_IMMORTAL_ERROR);
225                         }
226                 }
227
228                 destroys = g_slist_remove(destroys, rec);
229         }
230
231         cmd_params_free(free_arg);
232 }
233
234 /* SYNTAX: WINDOW REFNUM <number> */
235 static void cmd_window_refnum(const char *data)
236 {
237         WINDOW_REC *window;
238
239         if (!is_numeric(data, 0))
240                 return;
241
242         window = window_find_refnum(atoi(data));
243         if (window != NULL)
244                 window_set_active(window);
245 }
246
247 /* return the first window number with the highest activity */
248 static WINDOW_REC *window_highest_activity(WINDOW_REC *window)
249 {
250         WINDOW_REC *rec, *max_win;
251         GSList *tmp;
252         int max_act, through;
253
254         g_return_val_if_fail(window != NULL, NULL);
255
256         max_win = NULL; max_act = 0; through = FALSE;
257
258         tmp = g_slist_find(windows, window);
259         for (;; tmp = tmp->next) {
260                 if (tmp == NULL) {
261                         tmp = windows;
262                         through = TRUE;
263                 }
264
265                 if (through && tmp->data == window)
266                         break;
267
268                 rec = tmp->data;
269
270                 if (rec->data_level > 0 && max_act < rec->data_level) {
271                         max_act = rec->data_level;
272                         max_win = rec;
273                 }
274         }
275
276         return max_win;
277 }
278
279 /* SYNTAX: WINDOW GOTO active|<number>|<name> */
280 static void cmd_window_goto(const char *data)
281 {
282         WINDOW_REC *window;
283         char *target;
284         void *free_arg;
285
286         g_return_if_fail(data != NULL);
287
288         if (is_numeric(data, 0)) {
289                 cmd_window_refnum(data);
290                 return;
291         }
292
293         if (!cmd_get_params(data, &free_arg, 1, &target))
294                 return;
295
296         if (g_strcasecmp(target, "active") == 0)
297                 window = window_highest_activity(active_win);
298         else
299                 window = window_find_item(active_win->active_server, target);
300
301         if (window != NULL)
302                 window_set_active(window);
303
304         cmd_params_free(free_arg);
305 }
306
307 /* SYNTAX: WINDOW NEXT */
308 static void cmd_window_next(void)
309 {
310         int num;
311
312         num = window_refnum_next(active_win->refnum, TRUE);
313         if (num < 1) num = windows_refnum_last();
314
315         window_set_active(window_find_refnum(num));
316 }
317
318 /* SYNTAX: WINDOW LAST */
319 static void cmd_window_last(void)
320 {
321         if (windows->next != NULL)
322                 window_set_active(windows->next->data);
323 }
324
325 /* SYNTAX: WINDOW PREVIOUS */
326 static void cmd_window_previous(void)
327 {
328         int num;
329
330         num = window_refnum_prev(active_win->refnum, TRUE);
331         if (num < 1) num = window_refnum_next(0, TRUE);
332
333         window_set_active(window_find_refnum(num));
334 }
335
336 /* SYNTAX: WINDOW LEVEL [<level>] */
337 static void cmd_window_level(const char *data)
338 {
339         char *level;
340
341         g_return_if_fail(data != NULL);
342
343         window_set_level(active_win, combine_level(active_win->level, data));
344
345         level = active_win->level == 0 ? g_strdup("NONE") :
346                 bits2level(active_win->level);
347         printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
348                            TXT_WINDOW_LEVEL, level);
349         g_free(level);
350 }
351
352 /* SYNTAX: WINDOW IMMORTAL on|off|toggle */
353 static void cmd_window_immortal(const char *data)
354 {
355         int set;
356
357         if (*data == '\0')
358                 set = active_win->immortal;
359         else if (g_strcasecmp(data, "ON") == 0)
360                 set = TRUE;
361         else if (g_strcasecmp(data, "OFF") == 0)
362                 set = FALSE;
363         else if (g_strcasecmp(data, "TOGGLE") == 0)
364                 set = !active_win->immortal;
365         else {
366                 printformat_window(active_win, MSGLEVEL_CLIENTERROR,
367                                    TXT_NOT_TOGGLE);
368                 return;
369         }
370
371         if (set) {
372                 window_set_immortal(active_win, TRUE);
373                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
374                                    TXT_WINDOW_SET_IMMORTAL);
375         } else {
376                 window_set_immortal(active_win, FALSE);
377                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
378                                    TXT_WINDOW_UNSET_IMMORTAL);
379         }
380 }
381
382 /* SYNTAX: WINDOW SERVER [-sticky | -unsticky] <tag> */
383 static void cmd_window_server(const char *data)
384 {
385         GHashTable *optlist;
386         SERVER_REC *server;
387         char *tag;
388         void *free_arg;
389
390         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
391                             "window server", &optlist, &tag))
392                 return;
393
394         if (*tag == '\0' && active_win->active_server != NULL &&
395             (g_hash_table_lookup(optlist, "sticky") != NULL ||
396              g_hash_table_lookup(optlist, "unsticky") != NULL)) {
397                 tag = active_win->active_server->tag;
398         }
399
400         if (*tag == '\0')
401                 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
402         server = server_find_tag(tag);
403         if (server == NULL)
404                 server = server_find_lookup_tag(tag);
405
406         if (g_hash_table_lookup(optlist, "unsticky") != NULL &&
407             active_win->servertag != NULL) {
408                 g_free_and_null(active_win->servertag);
409                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
410                                    TXT_UNSET_SERVER_STICKY);
411         }
412
413         if (active_win->servertag != NULL &&
414             g_hash_table_lookup(optlist, "sticky") == NULL) {
415                 printformat_window(active_win, MSGLEVEL_CLIENTERROR,
416                                    TXT_ERROR_SERVER_STICKY);
417         } else if (server == NULL) {
418                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
419                                    TXT_UNKNOWN_SERVER_TAG, tag);
420         } else if (active_win->active == NULL) {
421                 window_change_server(active_win, server);
422                 if (g_hash_table_lookup(optlist, "sticky") != NULL) {
423                         g_free_not_null(active_win->servertag);
424                         active_win->servertag = g_strdup(server->tag);
425                         printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
426                                            TXT_SET_SERVER_STICKY, server->tag);
427                 }
428                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
429                                    TXT_SERVER_CHANGED,
430                                    server->tag, server->connrec->address,
431                                    server->connrec->chatnet == NULL ? "" :
432                                    server->connrec->chatnet);
433         }
434
435         cmd_params_free(free_arg);
436 }
437
438 static void cmd_window_item(const char *data, void *server, WI_ITEM_REC *item)
439 {
440         while (*data == ' ') data++;
441
442         if (is_numeric(data, '\0'))
443                 signal_emit("command window item goto", 3, data, server, item);
444         else
445                 command_runsub("window item", data, server, item);
446 }
447
448 /* SYNTAX: WINDOW ITEM PREV */
449 static void cmd_window_item_prev(void)
450 {
451         window_item_prev(active_win);
452 }
453
454 /* SYNTAX: WINDOW ITEM NEXT */
455 static void cmd_window_item_next(void)
456 {
457         window_item_next(active_win);
458 }
459
460 /* SYNTAX: WINDOW ITEM GOTO <number>|<name> */
461 static void cmd_window_item_goto(const char *data, SERVER_REC *server)
462 {
463         WI_ITEM_REC *item;
464         GSList *tmp;
465
466         if (is_numeric(data, '\0')) {
467                 /* change to specified number */
468                 tmp = g_slist_nth(active_win->items, atoi(data)-1);
469                 item = tmp == NULL ? NULL : tmp->data;
470         } else {
471                 item = window_item_find_window(active_win, server, data);
472         }
473
474         if (item != NULL)
475                 window_item_set_active(active_win, item);
476 }
477
478 /* SYNTAX: WINDOW ITEM MOVE <number>|<name> */
479 static void cmd_window_item_move(const char *data, SERVER_REC *server,
480                                  WI_ITEM_REC *item)
481 {
482         WINDOW_REC *window;
483         void *free_arg;
484         char *target;
485
486         if (!cmd_get_params(data, &free_arg, 1, &target))
487                 return;
488
489         if (is_numeric(target, '\0')) {
490                 /* move current window item to specified window */
491                 window = window_find_refnum(atoi(target));
492         } else {
493                 /* move specified window item to current window */
494                 item = window_item_find(server, target);
495                 window = active_win;
496         }
497         if (window != NULL && item != NULL)
498                 window_item_set_active(window, item);
499
500         cmd_params_free(free_arg);
501 }
502
503 /* SYNTAX: WINDOW NUMBER [-sticky] <number> */
504 static void cmd_window_number(const char *data)
505 {
506         GHashTable *optlist;
507         char *refnum;
508         void *free_arg;
509         int num;
510
511         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
512                             "window number", &optlist, &refnum))
513                 return;
514
515         if (*refnum == '\0')
516                 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
517
518         num = atoi(refnum);
519         if (num < 1) {
520                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
521                                    TXT_REFNUM_TOO_LOW);
522         } else {
523                 window_set_refnum(active_win, num);
524                 active_win->sticky_refnum =
525                         g_hash_table_lookup(optlist, "sticky") != NULL;
526         }
527
528         cmd_params_free(free_arg);
529 }
530
531 /* SYNTAX: WINDOW NAME <name> */
532 static void cmd_window_name(const char *data)
533 {
534         WINDOW_REC *win;
535
536         win = window_find_name(data);
537         if (win == NULL || win == active_win)
538                 window_set_name(active_win, data);
539         else if (active_win->name == NULL ||
540                  strcmp(active_win->name, data) != 0) {
541                 printformat_window(active_win, MSGLEVEL_CLIENTERROR,
542                                    TXT_WINDOW_NAME_NOT_UNIQUE, data);
543         }
544 }
545
546 /* SYNTAX: WINDOW HISTORY <name> */
547 void cmd_window_history(const char *data)
548 {
549         window_set_history(active_win, data);
550 }
551
552 /* we're moving the first window to last - move the first contiguous block
553    of refnums to left. Like if there's windows 1..5 and 7..10, move 1 to
554    11, 2..5 to 1..4 and leave 7..10 alone */
555 static void window_refnums_move_left(WINDOW_REC *move_window)
556 {
557         WINDOW_REC *window;
558         int refnum, new_refnum;
559
560         new_refnum = windows_refnum_last();
561         for (refnum = move_window->refnum+1; refnum <= new_refnum; refnum++) {
562                 window = window_find_refnum(refnum);
563                 if (window == NULL) {
564                         new_refnum++;
565                         break;
566                 }
567
568                 window_set_refnum(window, refnum-1);
569         }
570
571         window_set_refnum(move_window, new_refnum);
572 }
573
574 /* we're moving the last window to first - make some space so we can use the
575    refnum 1 */
576 static void window_refnums_move_right(WINDOW_REC *move_window)
577 {
578         WINDOW_REC *window;
579         int refnum, new_refnum;
580
581         new_refnum = 1;
582         if (window_find_refnum(new_refnum) == NULL) {
583                 window_set_refnum(move_window, new_refnum);
584                 return;
585         }
586
587         /* find the first unused refnum, like if there's windows
588            1..5 and 7..10, we only need to move 1..5 to 2..6 */
589         refnum = new_refnum;
590         while (move_window->refnum == refnum ||
591                window_find_refnum(refnum) != NULL) refnum++;
592         refnum--;
593
594         while (refnum >= new_refnum) {
595                 window = window_find_refnum(refnum);
596                 window_set_refnum(window, refnum+1);
597
598                 refnum--;
599         }
600
601         window_set_refnum(move_window, new_refnum);
602 }
603
604 /* SYNTAX: WINDOW MOVE PREV */
605 static void cmd_window_move_prev(void)
606 {
607         int refnum;
608
609         refnum = window_refnum_prev(active_win->refnum, FALSE);
610         if (refnum != -1) {
611                 window_set_refnum(active_win, refnum);
612                 return;
613         }
614
615         window_refnums_move_left(active_win);
616 }
617
618 /* SYNTAX: WINDOW MOVE NEXT */
619 static void cmd_window_move_next(void)
620 {
621         int refnum;
622
623         refnum = window_refnum_next(active_win->refnum, FALSE);
624         if (refnum != -1) {
625                 window_set_refnum(active_win, refnum);
626                 return;
627         }
628
629         window_refnums_move_right(active_win);
630 }
631
632 static void active_window_move_to(int new_refnum)
633 {
634         int refnum;
635
636         if (new_refnum > active_win->refnum) {
637                 for (;;) {
638                         refnum = window_refnum_next(active_win->refnum, FALSE);
639                         if (refnum == -1 || refnum > new_refnum)
640                                 break;
641
642                         window_set_refnum(active_win, refnum);
643                 }
644         } else {
645                 for (;;) {
646                         refnum = window_refnum_prev(active_win->refnum, FALSE);
647                         if (refnum == -1 || refnum < new_refnum)
648                                 break;
649
650                         window_set_refnum(active_win, refnum);
651                 }
652         }
653 }
654
655 /* SYNTAX: WINDOW MOVE FIRST */
656 static void cmd_window_move_first(void)
657 {
658         active_window_move_to(1);
659 }
660
661 /* SYNTAX: WINDOW MOVE LAST */
662 static void cmd_window_move_last(void)
663 {
664         active_window_move_to(windows_refnum_last());
665 }
666
667 /* SYNTAX: WINDOW MOVE <number>|<direction> */
668 static void cmd_window_move(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
669 {
670         if (!is_numeric(data, 0)) {
671                 command_runsub("window move", data, server, item);
672                 return;
673         }
674
675         active_window_move_to(atoi(data));
676 }
677
678 /* SYNTAX: WINDOW LIST */
679 static void cmd_window_list(void)
680 {
681         GSList *tmp, *sorted;
682         char *levelstr;
683
684         sorted = windows_get_sorted();
685         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_WINDOWLIST_HEADER);
686         for (tmp = sorted; tmp != NULL; tmp = tmp->next) {
687                 WINDOW_REC *rec = tmp->data;
688
689                 levelstr = bits2level(rec->level);
690                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_WINDOWLIST_LINE,
691                             rec->refnum, rec->name == NULL ? "" : rec->name,
692                             rec->active == NULL ? "" : rec->active->visible_name,
693                             rec->active_server == NULL ? "" : ((SERVER_REC *) rec->active_server)->tag,
694                             levelstr);
695                 g_free(levelstr);
696         }
697         g_slist_free(sorted);
698         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_WINDOWLIST_FOOTER);
699 }
700
701 /* SYNTAX: WINDOW THEME [-delete] [<name>] */
702 static void cmd_window_theme(const char *data)
703 {
704         THEME_REC *theme;
705         GHashTable *optlist;
706         char *name;
707         void *free_arg;
708
709         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
710                             "window theme", &optlist, &name))
711                 return;
712
713         if (g_hash_table_lookup(optlist, "delete") != NULL) {
714                 g_free_and_null(active_win->theme_name);
715
716                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
717                                    TXT_WINDOW_THEME_REMOVED);
718         } else if (*name == '\0') {
719                 if (active_win->theme == NULL) {
720                         printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
721                                            TXT_WINDOW_THEME_DEFAULT);
722                 } else {
723                         theme = active_win->theme;
724                         printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
725                                            TXT_WINDOW_THEME,
726                                            theme->name, theme->path);
727                 }
728         } else {
729                 g_free_not_null(active_win->theme_name);
730                 active_win->theme_name = g_strdup(data);
731
732                 active_win->theme = theme = theme_load(data);
733                 if (theme != NULL) {
734                         printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
735                                            TXT_WINDOW_THEME_CHANGED,
736                                            theme->name, theme->path);
737                 } else {
738                         printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
739                                            TXT_THEME_NOT_FOUND, data);
740                 }
741         }
742
743         cmd_params_free(free_arg);
744 }
745
746 static void cmd_layout(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
747 {
748         command_runsub("layout", data, server, item);
749 }
750
751 /* SYNTAX: FOREACH WINDOW <command> */
752 static void cmd_foreach_window(const char *data)
753 {
754         WINDOW_REC *old;
755         GSList *list;
756
757         old = active_win;
758
759         list = g_slist_copy(windows);
760         while (list != NULL) {
761                 WINDOW_REC *rec = list->data;
762
763                 active_win = rec;
764                 signal_emit("send command", 3, data, rec->active_server,
765                             rec->active);
766                 list = g_slist_remove(list, list->data);
767         }
768
769         if (g_slist_find(windows, old) != NULL)
770                 active_win = old;
771 }
772
773 void window_commands_init(void)
774 {
775         command_bind("window", NULL, (SIGNAL_FUNC) cmd_window);
776         command_bind("window new", NULL, (SIGNAL_FUNC) cmd_window_new);
777         command_bind("window close", NULL, (SIGNAL_FUNC) cmd_window_close);
778         command_bind("window kill", NULL, (SIGNAL_FUNC) cmd_window_close);
779         command_bind("window server", NULL, (SIGNAL_FUNC) cmd_window_server);
780         command_bind("window refnum", NULL, (SIGNAL_FUNC) cmd_window_refnum);
781         command_bind("window goto", NULL, (SIGNAL_FUNC) cmd_window_goto);
782         command_bind("window previous", NULL, (SIGNAL_FUNC) cmd_window_previous);
783         command_bind("window next", NULL, (SIGNAL_FUNC) cmd_window_next);
784         command_bind("window last", NULL, (SIGNAL_FUNC) cmd_window_last);
785         command_bind("window level", NULL, (SIGNAL_FUNC) cmd_window_level);
786         command_bind("window immortal", NULL, (SIGNAL_FUNC) cmd_window_immortal);
787         command_bind("window item", NULL, (SIGNAL_FUNC) cmd_window_item);
788         command_bind("window item prev", NULL, (SIGNAL_FUNC) cmd_window_item_prev);
789         command_bind("window item next", NULL, (SIGNAL_FUNC) cmd_window_item_next);
790         command_bind("window item goto", NULL, (SIGNAL_FUNC) cmd_window_item_goto);
791         command_bind("window item move", NULL, (SIGNAL_FUNC) cmd_window_item_move);
792         command_bind("window number", NULL, (SIGNAL_FUNC) cmd_window_number);
793         command_bind("window name", NULL, (SIGNAL_FUNC) cmd_window_name);
794         command_bind("window history", NULL, (SIGNAL_FUNC) cmd_window_history);
795         command_bind("window move", NULL, (SIGNAL_FUNC) cmd_window_move);
796         command_bind("window move prev", NULL, (SIGNAL_FUNC) cmd_window_move_prev);
797         command_bind("window move next", NULL, (SIGNAL_FUNC) cmd_window_move_next);
798         command_bind("window move first", NULL, (SIGNAL_FUNC) cmd_window_move_first);
799         command_bind("window move last", NULL, (SIGNAL_FUNC) cmd_window_move_last);
800         command_bind("window list", NULL, (SIGNAL_FUNC) cmd_window_list);
801         command_bind("window theme", NULL, (SIGNAL_FUNC) cmd_window_theme);
802         command_bind("layout", NULL, (SIGNAL_FUNC) cmd_layout);
803         /* SYNTAX: LAYOUT SAVE */
804         command_bind("layout save", NULL, (SIGNAL_FUNC) windows_layout_save);
805         /* SYNTAX: LAYOUT RESET */
806         command_bind("layout reset", NULL, (SIGNAL_FUNC) windows_layout_reset);
807         command_bind("foreach window", NULL, (SIGNAL_FUNC) cmd_foreach_window);
808
809         command_set_options("window number", "sticky");
810         command_set_options("window server", "sticky unsticky");
811         command_set_options("window theme", "delete");
812 }
813
814 void window_commands_deinit(void)
815 {
816         command_unbind("window", (SIGNAL_FUNC) cmd_window);
817         command_unbind("window new", (SIGNAL_FUNC) cmd_window_new);
818         command_unbind("window close", (SIGNAL_FUNC) cmd_window_close);
819         command_unbind("window kill", (SIGNAL_FUNC) cmd_window_close);
820         command_unbind("window server", (SIGNAL_FUNC) cmd_window_server);
821         command_unbind("window refnum", (SIGNAL_FUNC) cmd_window_refnum);
822         command_unbind("window goto", (SIGNAL_FUNC) cmd_window_goto);
823         command_unbind("window previous", (SIGNAL_FUNC) cmd_window_previous);
824         command_unbind("window next", (SIGNAL_FUNC) cmd_window_next);
825         command_unbind("window last", (SIGNAL_FUNC) cmd_window_last);
826         command_unbind("window level", (SIGNAL_FUNC) cmd_window_level);
827         command_unbind("window immortal", (SIGNAL_FUNC) cmd_window_immortal);
828         command_unbind("window item", (SIGNAL_FUNC) cmd_window_item);
829         command_unbind("window item prev", (SIGNAL_FUNC) cmd_window_item_prev);
830         command_unbind("window item next", (SIGNAL_FUNC) cmd_window_item_next);
831         command_unbind("window item goto", (SIGNAL_FUNC) cmd_window_item_goto);
832         command_unbind("window item move", (SIGNAL_FUNC) cmd_window_item_move);
833         command_unbind("window number", (SIGNAL_FUNC) cmd_window_number);
834         command_unbind("window name", (SIGNAL_FUNC) cmd_window_name);
835         command_unbind("window history", (SIGNAL_FUNC) cmd_window_history);
836         command_unbind("window move", (SIGNAL_FUNC) cmd_window_move);
837         command_unbind("window move prev", (SIGNAL_FUNC) cmd_window_move_prev);
838         command_unbind("window move next", (SIGNAL_FUNC) cmd_window_move_next);
839         command_unbind("window move first", (SIGNAL_FUNC) cmd_window_move_first);
840         command_unbind("window move last", (SIGNAL_FUNC) cmd_window_move_last);
841         command_unbind("window list", (SIGNAL_FUNC) cmd_window_list);
842         command_unbind("window theme", (SIGNAL_FUNC) cmd_window_theme);
843         command_unbind("layout", (SIGNAL_FUNC) cmd_layout);
844         command_unbind("layout save", (SIGNAL_FUNC) windows_layout_save);
845         command_unbind("layout reset", (SIGNAL_FUNC) windows_layout_reset);
846         command_unbind("foreach window", (SIGNAL_FUNC) cmd_foreach_window);
847 }