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