Added SILC Thread Queue API
[runtime.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         void *free_arg;
466         char *target;
467         
468         if (!cmd_get_params(data, &free_arg, 1, &target))
469                 return;
470
471         if (is_numeric(target, '\0')) {
472                 /* change to specified number */
473                 tmp = g_slist_nth(active_win->items, atoi(target)-1);
474                 item = tmp == NULL ? NULL : tmp->data;
475         } else {
476                 item = window_item_find_window(active_win, server, target);
477         }
478
479         if (item != NULL)
480                 window_item_set_active(active_win, item);
481
482         cmd_params_free(free_arg);
483 }
484
485 /* SYNTAX: WINDOW ITEM MOVE <number>|<name> */
486 static void cmd_window_item_move(const char *data, SERVER_REC *server,
487                                  WI_ITEM_REC *item)
488 {
489         WINDOW_REC *window;
490         void *free_arg;
491         char *target;
492
493         if (!cmd_get_params(data, &free_arg, 1, &target))
494                 return;
495
496         if (is_numeric(target, '\0')) {
497                 /* move current window item to specified window */
498                 window = window_find_refnum(atoi(target));
499         } else {
500                 /* move specified window item to current window */
501                 item = window_item_find(server, target);
502                 window = active_win;
503         }
504         if (window != NULL && item != NULL)
505                 window_item_set_active(window, item);
506
507         cmd_params_free(free_arg);
508 }
509
510 /* SYNTAX: WINDOW NUMBER [-sticky] <number> */
511 static void cmd_window_number(const char *data)
512 {
513         GHashTable *optlist;
514         char *refnum;
515         void *free_arg;
516         int num;
517
518         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
519                             "window number", &optlist, &refnum))
520                 return;
521
522         if (*refnum == '\0')
523                 cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
524
525         num = atoi(refnum);
526         if (num < 1) {
527                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
528                                    TXT_REFNUM_TOO_LOW);
529         } else {
530                 window_set_refnum(active_win, num);
531                 active_win->sticky_refnum =
532                         g_hash_table_lookup(optlist, "sticky") != NULL;
533         }
534
535         cmd_params_free(free_arg);
536 }
537
538 /* SYNTAX: WINDOW NAME <name> */
539 static void cmd_window_name(const char *data)
540 {
541         WINDOW_REC *win;
542
543         win = window_find_name(data);
544         if (win == NULL || win == active_win)
545                 window_set_name(active_win, data);
546         else if (active_win->name == NULL ||
547                  strcmp(active_win->name, data) != 0) {
548                 printformat_window(active_win, MSGLEVEL_CLIENTERROR,
549                                    TXT_WINDOW_NAME_NOT_UNIQUE, data);
550         }
551 }
552
553 /* SYNTAX: WINDOW HISTORY <name> */
554 void cmd_window_history(const char *data)
555 {
556         window_set_history(active_win, data);
557 }
558
559 /* we're moving the first window to last - move the first contiguous block
560    of refnums to left. Like if there's windows 1..5 and 7..10, move 1 to
561    11, 2..5 to 1..4 and leave 7..10 alone */
562 static void window_refnums_move_left(WINDOW_REC *move_window)
563 {
564         WINDOW_REC *window;
565         int refnum, new_refnum;
566
567         new_refnum = windows_refnum_last();
568         for (refnum = move_window->refnum+1; refnum <= new_refnum; refnum++) {
569                 window = window_find_refnum(refnum);
570                 if (window == NULL) {
571                         new_refnum++;
572                         break;
573                 }
574
575                 window_set_refnum(window, refnum-1);
576         }
577
578         window_set_refnum(move_window, new_refnum);
579 }
580
581 /* we're moving the last window to first - make some space so we can use the
582    refnum 1 */
583 static void window_refnums_move_right(WINDOW_REC *move_window)
584 {
585         WINDOW_REC *window;
586         int refnum, new_refnum;
587
588         new_refnum = 1;
589         if (window_find_refnum(new_refnum) == NULL) {
590                 window_set_refnum(move_window, new_refnum);
591                 return;
592         }
593
594         /* find the first unused refnum, like if there's windows
595            1..5 and 7..10, we only need to move 1..5 to 2..6 */
596         refnum = new_refnum;
597         while (move_window->refnum == refnum ||
598                window_find_refnum(refnum) != NULL) refnum++;
599         refnum--;
600
601         while (refnum >= new_refnum) {
602                 window = window_find_refnum(refnum);
603                 window_set_refnum(window, refnum+1);
604
605                 refnum--;
606         }
607
608         window_set_refnum(move_window, new_refnum);
609 }
610
611 /* SYNTAX: WINDOW MOVE PREV */
612 static void cmd_window_move_prev(void)
613 {
614         int refnum;
615
616         refnum = window_refnum_prev(active_win->refnum, FALSE);
617         if (refnum != -1) {
618                 window_set_refnum(active_win, refnum);
619                 return;
620         }
621
622         window_refnums_move_left(active_win);
623 }
624
625 /* SYNTAX: WINDOW MOVE NEXT */
626 static void cmd_window_move_next(void)
627 {
628         int refnum;
629
630         refnum = window_refnum_next(active_win->refnum, FALSE);
631         if (refnum != -1) {
632                 window_set_refnum(active_win, refnum);
633                 return;
634         }
635
636         window_refnums_move_right(active_win);
637 }
638
639 static void active_window_move_to(int new_refnum)
640 {
641         int refnum;
642
643         if (new_refnum > active_win->refnum) {
644                 for (;;) {
645                         refnum = window_refnum_next(active_win->refnum, FALSE);
646                         if (refnum == -1 || refnum > new_refnum)
647                                 break;
648
649                         window_set_refnum(active_win, refnum);
650                 }
651         } else {
652                 for (;;) {
653                         refnum = window_refnum_prev(active_win->refnum, FALSE);
654                         if (refnum == -1 || refnum < new_refnum)
655                                 break;
656
657                         window_set_refnum(active_win, refnum);
658                 }
659         }
660 }
661
662 /* SYNTAX: WINDOW MOVE FIRST */
663 static void cmd_window_move_first(void)
664 {
665         active_window_move_to(1);
666 }
667
668 /* SYNTAX: WINDOW MOVE LAST */
669 static void cmd_window_move_last(void)
670 {
671         active_window_move_to(windows_refnum_last());
672 }
673
674 /* SYNTAX: WINDOW MOVE <number>|<direction> */
675 static void cmd_window_move(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
676 {
677         if (!is_numeric(data, 0)) {
678                 command_runsub("window move", data, server, item);
679                 return;
680         }
681
682         active_window_move_to(atoi(data));
683 }
684
685 /* SYNTAX: WINDOW LIST */
686 static void cmd_window_list(void)
687 {
688         GSList *tmp, *sorted;
689         char *levelstr;
690
691         sorted = windows_get_sorted();
692         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_WINDOWLIST_HEADER);
693         for (tmp = sorted; tmp != NULL; tmp = tmp->next) {
694                 WINDOW_REC *rec = tmp->data;
695
696                 levelstr = bits2level(rec->level);
697                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_WINDOWLIST_LINE,
698                             rec->refnum, rec->name == NULL ? "" : rec->name,
699                             rec->active == NULL ? "" : rec->active->visible_name,
700                             rec->active_server == NULL ? "" : ((SERVER_REC *) rec->active_server)->tag,
701                             levelstr);
702                 g_free(levelstr);
703         }
704         g_slist_free(sorted);
705         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_WINDOWLIST_FOOTER);
706 }
707
708 /* SYNTAX: WINDOW THEME [-delete] [<name>] */
709 static void cmd_window_theme(const char *data)
710 {
711         THEME_REC *theme;
712         GHashTable *optlist;
713         char *name;
714         void *free_arg;
715
716         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
717                             "window theme", &optlist, &name))
718                 return;
719
720         if (g_hash_table_lookup(optlist, "delete") != NULL) {
721                 g_free_and_null(active_win->theme_name);
722
723                 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
724                                    TXT_WINDOW_THEME_REMOVED);
725         } else if (*name == '\0') {
726                 if (active_win->theme == NULL) {
727                         printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
728                                            TXT_WINDOW_THEME_DEFAULT);
729                 } else {
730                         theme = active_win->theme;
731                         printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
732                                            TXT_WINDOW_THEME,
733                                            theme->name, theme->path);
734                 }
735         } else {
736                 g_free_not_null(active_win->theme_name);
737                 active_win->theme_name = g_strdup(data);
738
739                 active_win->theme = theme = theme_load(data);
740                 if (theme != NULL) {
741                         printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
742                                            TXT_WINDOW_THEME_CHANGED,
743                                            theme->name, theme->path);
744                 } else {
745                         printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
746                                            TXT_THEME_NOT_FOUND, data);
747                 }
748         }
749
750         cmd_params_free(free_arg);
751 }
752
753 static void cmd_layout(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
754 {
755         command_runsub("layout", data, server, item);
756 }
757
758 /* SYNTAX: FOREACH WINDOW <command> */
759 static void cmd_foreach_window(const char *data)
760 {
761         WINDOW_REC *old;
762         GSList *list;
763
764         old = active_win;
765
766         list = g_slist_copy(windows);
767         while (list != NULL) {
768                 WINDOW_REC *rec = list->data;
769
770                 active_win = rec;
771                 signal_emit("send command", 3, data, rec->active_server,
772                             rec->active);
773                 list = g_slist_remove(list, list->data);
774         }
775
776         if (g_slist_find(windows, old) != NULL)
777                 active_win = old;
778 }
779
780 void window_commands_init(void)
781 {
782         command_bind("window", NULL, (SIGNAL_FUNC) cmd_window);
783         command_bind("window new", NULL, (SIGNAL_FUNC) cmd_window_new);
784         command_bind("window close", NULL, (SIGNAL_FUNC) cmd_window_close);
785         command_bind("window kill", NULL, (SIGNAL_FUNC) cmd_window_close);
786         command_bind("window server", NULL, (SIGNAL_FUNC) cmd_window_server);
787         command_bind("window refnum", NULL, (SIGNAL_FUNC) cmd_window_refnum);
788         command_bind("window goto", NULL, (SIGNAL_FUNC) cmd_window_goto);
789         command_bind("window previous", NULL, (SIGNAL_FUNC) cmd_window_previous);
790         command_bind("window next", NULL, (SIGNAL_FUNC) cmd_window_next);
791         command_bind("window last", NULL, (SIGNAL_FUNC) cmd_window_last);
792         command_bind("window level", NULL, (SIGNAL_FUNC) cmd_window_level);
793         command_bind("window immortal", NULL, (SIGNAL_FUNC) cmd_window_immortal);
794         command_bind("window item", NULL, (SIGNAL_FUNC) cmd_window_item);
795         command_bind("window item prev", NULL, (SIGNAL_FUNC) cmd_window_item_prev);
796         command_bind("window item next", NULL, (SIGNAL_FUNC) cmd_window_item_next);
797         command_bind("window item goto", NULL, (SIGNAL_FUNC) cmd_window_item_goto);
798         command_bind("window item move", NULL, (SIGNAL_FUNC) cmd_window_item_move);
799         command_bind("window number", NULL, (SIGNAL_FUNC) cmd_window_number);
800         command_bind("window name", NULL, (SIGNAL_FUNC) cmd_window_name);
801         command_bind("window history", NULL, (SIGNAL_FUNC) cmd_window_history);
802         command_bind("window move", NULL, (SIGNAL_FUNC) cmd_window_move);
803         command_bind("window move prev", NULL, (SIGNAL_FUNC) cmd_window_move_prev);
804         command_bind("window move next", NULL, (SIGNAL_FUNC) cmd_window_move_next);
805         command_bind("window move first", NULL, (SIGNAL_FUNC) cmd_window_move_first);
806         command_bind("window move last", NULL, (SIGNAL_FUNC) cmd_window_move_last);
807         command_bind("window list", NULL, (SIGNAL_FUNC) cmd_window_list);
808         command_bind("window theme", NULL, (SIGNAL_FUNC) cmd_window_theme);
809         command_bind("layout", NULL, (SIGNAL_FUNC) cmd_layout);
810         /* SYNTAX: LAYOUT SAVE */
811         command_bind("layout save", NULL, (SIGNAL_FUNC) windows_layout_save);
812         /* SYNTAX: LAYOUT RESET */
813         command_bind("layout reset", NULL, (SIGNAL_FUNC) windows_layout_reset);
814         command_bind("foreach window", NULL, (SIGNAL_FUNC) cmd_foreach_window);
815
816         command_set_options("window number", "sticky");
817         command_set_options("window server", "sticky unsticky");
818         command_set_options("window theme", "delete");
819 }
820
821 void window_commands_deinit(void)
822 {
823         command_unbind("window", (SIGNAL_FUNC) cmd_window);
824         command_unbind("window new", (SIGNAL_FUNC) cmd_window_new);
825         command_unbind("window close", (SIGNAL_FUNC) cmd_window_close);
826         command_unbind("window kill", (SIGNAL_FUNC) cmd_window_close);
827         command_unbind("window server", (SIGNAL_FUNC) cmd_window_server);
828         command_unbind("window refnum", (SIGNAL_FUNC) cmd_window_refnum);
829         command_unbind("window goto", (SIGNAL_FUNC) cmd_window_goto);
830         command_unbind("window previous", (SIGNAL_FUNC) cmd_window_previous);
831         command_unbind("window next", (SIGNAL_FUNC) cmd_window_next);
832         command_unbind("window last", (SIGNAL_FUNC) cmd_window_last);
833         command_unbind("window level", (SIGNAL_FUNC) cmd_window_level);
834         command_unbind("window immortal", (SIGNAL_FUNC) cmd_window_immortal);
835         command_unbind("window item", (SIGNAL_FUNC) cmd_window_item);
836         command_unbind("window item prev", (SIGNAL_FUNC) cmd_window_item_prev);
837         command_unbind("window item next", (SIGNAL_FUNC) cmd_window_item_next);
838         command_unbind("window item goto", (SIGNAL_FUNC) cmd_window_item_goto);
839         command_unbind("window item move", (SIGNAL_FUNC) cmd_window_item_move);
840         command_unbind("window number", (SIGNAL_FUNC) cmd_window_number);
841         command_unbind("window name", (SIGNAL_FUNC) cmd_window_name);
842         command_unbind("window history", (SIGNAL_FUNC) cmd_window_history);
843         command_unbind("window move", (SIGNAL_FUNC) cmd_window_move);
844         command_unbind("window move prev", (SIGNAL_FUNC) cmd_window_move_prev);
845         command_unbind("window move next", (SIGNAL_FUNC) cmd_window_move_next);
846         command_unbind("window move first", (SIGNAL_FUNC) cmd_window_move_first);
847         command_unbind("window move last", (SIGNAL_FUNC) cmd_window_move_last);
848         command_unbind("window list", (SIGNAL_FUNC) cmd_window_list);
849         command_unbind("window theme", (SIGNAL_FUNC) cmd_window_theme);
850         command_unbind("layout", (SIGNAL_FUNC) cmd_layout);
851         command_unbind("layout save", (SIGNAL_FUNC) windows_layout_save);
852         command_unbind("layout reset", (SIGNAL_FUNC) windows_layout_reset);
853         command_unbind("foreach window", (SIGNAL_FUNC) cmd_foreach_window);
854 }