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