1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
|
/*
* Copyright (c) 2003-2008 by FlashCode <flashcode@flashtux.org>
* See README for License detail, AUTHORS for developers list.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* gui-keyboard: keyboard functions, used by all GUI */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../core/weechat.h"
#include "../core/wee-input.h"
#include "../core/wee-log.h"
#include "../core/wee-string.h"
#include "../plugins/plugin.h"
#include "gui-keyboard.h"
#include "gui-action.h"
#include "gui-buffer.h"
#include "gui-completion.h"
#include "gui-input.h"
#include "gui-window.h"
t_gui_key *gui_keys = NULL; /* key bindings */
t_gui_key *last_gui_key = NULL; /* last key binding */
char gui_key_combo_buffer[128]; /* buffer used for combos */
int gui_key_grab = 0; /* 1 if grab mode enabled (alt-k) */
int gui_key_grab_count = 0; /* number of keys pressed in grab mode */
int *gui_keyboard_buffer = NULL; /* input buffer (for paste detection) */
int gui_keyboard_buffer_alloc = 0; /* input buffer allocated size */
int gui_keyboard_buffer_size = 0; /* input buffer size in bytes */
int gui_keyboard_paste_pending = 0; /* 1 is big paste was detected and */
/* WeeChat is asking user what to do */
int gui_keyboard_paste_lines = 0; /* number of lines for pending paste */
time_t gui_keyboard_last_activity_time = 0; /* last activity time (key) */
t_gui_key_function gui_key_functions[] =
{ { "return", gui_action_return,
N_("terminate line") },
{ "tab", gui_action_tab,
N_("complete word") },
{ "tab_previous", gui_action_tab_previous,
N_("find previous completion for word") },
{ "backspace", gui_action_backspace,
N_("delete previous char") },
{ "delete", gui_action_delete,
N_("delete next char") },
{ "delete_end_line", gui_action_delete_end_of_line,
N_("delete until end of line") },
{ "delete_beginning_line", gui_action_delete_begin_of_line,
N_("delete until beginning of line") },
{ "delete_line", gui_action_delete_line,
N_("delete entire line") },
{ "delete_previous_word", gui_action_delete_previous_word,
N_("delete previous word") },
{ "delete_next_word", gui_action_delete_next_word,
N_("delete next word") },
{ "clipboard_paste", gui_action_clipboard_paste,
N_("paste current clipboard content") },
{ "transpose_chars", gui_action_transpose_chars,
N_("transpose chars") },
{ "home", gui_action_home,
N_("go to beginning of line") },
{ "end", gui_action_end,
N_("go to end of line") },
{ "left", gui_action_left,
N_("move one char left") },
{ "previous_word", gui_action_previous_word,
N_("move to previous word") },
{ "right", gui_action_right,
N_("move one char right") },
{ "next_word", gui_action_next_word,
N_("move to next word") },
{ "up", gui_action_up,
N_("call previous command in history") },
{ "up_global", gui_action_up_global,
N_("call previous command in global history") },
{ "down", gui_action_down,
N_("call next command in history") },
{ "down_global", gui_action_down_global,
N_("call next command in global history") },
{ "page_up", gui_action_page_up,
N_("scroll one page up") },
{ "page_down", gui_action_page_down,
N_("scroll one page down") },
{ "scroll_up", gui_action_scroll_up,
N_("scroll a few lines up") },
{ "scroll_down", gui_action_scroll_down,
N_("scroll a few lines down") },
{ "scroll_top", gui_action_scroll_top,
N_("scroll to top of buffer") },
{ "scroll_bottom", gui_action_scroll_bottom,
N_("scroll to bottom of buffer") },
{ "scroll_topic_left", gui_action_scroll_topic_left,
N_("scroll left topic") },
{ "scroll_topic_right", gui_action_scroll_topic_right,
N_("scroll right topic") },
{ "nick_beginning", gui_action_nick_beginning,
N_("display beginning of nicklist") },
{ "nick_end", gui_action_nick_end,
N_("display end of nicklist") },
{ "nick_page_up", gui_action_nick_page_up,
N_("scroll nicklist one page up") },
{ "nick_page_down", gui_action_nick_page_down,
N_("scroll nicklist one page down") },
{ "jump_smart", gui_action_jump_smart,
N_("jump to buffer with activity") },
{ "jump_dcc", gui_action_jump_dcc,
N_("jump to DCC buffer") },
{ "jump_raw_data", gui_action_jump_raw_data,
N_("jump to raw IRC data buffer") },
{ "jump_last_buffer", gui_action_jump_last_buffer,
N_("jump to last buffer") },
{ "jump_previous_buffer", gui_action_jump_previous_buffer,
N_("jump to previous buffer") },
{ "jump_server", gui_action_jump_server,
N_("jump to server buffer") },
{ "jump_next_server", gui_action_jump_next_server,
N_("jump to next server") },
{ "switch_server", gui_action_switch_server,
N_("switch active server on servers buffer") },
{ "scroll_previous_highlight", gui_action_scroll_previous_highlight,
N_("scroll to previous highlight in buffer") },
{ "scroll_next_highlight", gui_action_scroll_next_highlight,
N_("scroll to next highlight in buffer") },
{ "scroll_unread", gui_action_scroll_unread,
N_("scroll to first unread line in buffer") },
{ "set_unread", gui_action_set_unread,
N_("set unread marker on all buffers") },
{ "hotlist_clear", gui_action_hotlist_clear,
N_("clear hotlist") },
{ "infobar_clear", gui_action_infobar_clear,
N_("clear infobar") },
{ "refresh", gui_action_refresh_screen,
N_("refresh screen") },
{ "grab_key", gui_action_grab_key,
N_("grab a key") },
{ "insert", gui_action_insert_string,
N_("insert a string in command line") },
{ "search_text", gui_action_search_text,
N_("search text in buffer history") },
{ NULL, NULL, NULL }
};
/*
* gui_keyboard_init: init keyboard (create default key bindings)
*/
void
gui_keyboard_init ()
{
gui_key_combo_buffer[0] = '\0';
gui_key_grab = 0;
gui_key_grab_count = 0;
gui_keyboard_default_bindings ();
}
/*
* gui_keyboard_init_last_activity_time: init last activity time with current
* time
*/
void
gui_keyboard_init_last_activity_time ()
{
struct timeval tv_time;
gettimeofday (&tv_time, NULL);
gui_keyboard_last_activity_time = tv_time.tv_sec;
}
/*
* gui_keyboard_grab_init: init "grab" mode
*/
void
gui_keyboard_grab_init ()
{
gui_key_grab = 1;
gui_key_grab_count = 0;
}
/*
* gui_keyboard_grab_end: insert grabbed key in input buffer
*/
void
gui_keyboard_grab_end ()
{
char *expanded_key;
/* get expanded name (for example: ^U => ctrl-u) */
expanded_key = gui_keyboard_get_expanded_name (gui_key_combo_buffer);
if (expanded_key)
{
if (gui_current_window->buffer->input)
{
gui_input_insert_string (gui_current_window->buffer, expanded_key, -1);
if (gui_current_window->buffer->completion)
gui_current_window->buffer->completion->position = -1;
gui_input_draw (gui_current_window->buffer, 0);
}
free (expanded_key);
}
/* end grab mode */
gui_key_grab = 0;
gui_key_grab_count = 0;
gui_key_combo_buffer[0] = '\0';
}
/*
* gui_keyboard_get_internal_code: get internal code from user key name
* for example: return "^R" for "ctrl-R"
*/
char *
gui_keyboard_get_internal_code (char *key)
{
char *result;
if ((result = (char *)malloc ((strlen (key) + 1) * sizeof (char))))
{
result[0] = '\0';
while (key[0])
{
if (string_strncasecmp (key, "meta2-", 6) == 0)
{
strcat (result, "^[[");
key += 6;
}
if (string_strncasecmp (key, "meta-", 5) == 0)
{
strcat (result, "^[");
key += 5;
}
else if (string_strncasecmp (key, "ctrl-", 5) == 0)
{
strcat (result, "^");
key += 5;
}
else
{
strncat (result, key, 1);
key++;
}
}
}
else
return NULL;
return result;
}
/*
* gui_keyboard_get_expanded_name: get expanded name from internal key code
* for example: return "ctrl-R" for "^R"
*/
char *
gui_keyboard_get_expanded_name (char *key)
{
char *result;
if ((result = (char *)malloc (((strlen (key) * 5) + 1) * sizeof (char))))
{
result[0] = '\0';
while (key[0])
{
if (string_strncasecmp (key, "^[[", 3) == 0)
{
strcat (result, "meta2-");
key += 3;
}
if (string_strncasecmp (key, "^[", 2) == 0)
{
strcat (result, "meta-");
key += 2;
}
else if ((key[0] == '^') && (key[1]))
{
strcat (result, "ctrl-");
key++;
}
else
{
strncat (result, key, 1);
key++;
}
}
}
else
return NULL;
return result;
}
/*
* gui_keyboard_find_pos: find position for a key (for sorting keys list)
*/
t_gui_key *
gui_keyboard_find_pos (t_gui_key *key)
{
t_gui_key *ptr_key;
for (ptr_key = gui_keys; ptr_key; ptr_key = ptr_key->next_key)
{
if (string_strcasecmp (key->key, ptr_key->key) < 0)
return ptr_key;
}
return NULL;
}
/*
* gui_keyboard_insert_sorted: insert key into sorted list
*/
void
gui_keyboard_insert_sorted (t_gui_key *key)
{
t_gui_key *pos_key;
if (gui_keys)
{
pos_key = gui_keyboard_find_pos (key);
if (pos_key)
{
/* insert key into the list (before key found) */
key->prev_key = pos_key->prev_key;
key->next_key = pos_key;
if (pos_key->prev_key)
pos_key->prev_key->next_key = key;
else
gui_keys = key;
pos_key->prev_key = key;
}
else
{
/* add key to the end */
key->prev_key = last_gui_key;
key->next_key = NULL;
last_gui_key->next_key = key;
last_gui_key = key;
}
}
else
{
key->prev_key = NULL;
key->next_key = NULL;
gui_keys = key;
last_gui_key = key;
}
}
/*
* gui_keyboard_new: add a new key in keys list
*/
t_gui_key *
gui_keyboard_new (char *key, char *command, t_gui_key_func *function, char *args)
{
t_gui_key *new_key;
char *internal_code;
int length;
if ((new_key = (t_gui_key *)malloc (sizeof (t_gui_key))))
{
internal_code = gui_keyboard_get_internal_code (key);
new_key->key = (internal_code) ? strdup (internal_code) : strdup (key);
if (internal_code)
free (internal_code);
new_key->command = (command) ? strdup (command) : NULL;
new_key->function = function;
if (args)
{
if (args[0] == '"')
{
length = strlen (args);
if ((length > 1) && (args[length - 1] == '"'))
new_key->args = string_strndup (args + 1, length - 2);
else
new_key->args = strdup (args);
}
else
new_key->args = strdup (args);
}
else
new_key->args = NULL;
gui_keyboard_insert_sorted (new_key);
}
else
return NULL;
return new_key;
}
/*
* gui_keyboard_search: search a key
*/
t_gui_key *
gui_keyboard_search (char *key)
{
t_gui_key *ptr_key;
for (ptr_key = gui_keys; ptr_key; ptr_key = ptr_key->next_key)
{
if (string_strcasecmp (ptr_key->key, key) == 0)
return ptr_key;
}
/* key not found */
return NULL;
}
/*
* gui_keyboard_cmp: compares 2 keys
*/
int
gui_keyboard_cmp (char *key, char *search)
{
while (search[0])
{
if (toupper(key[0]) != toupper(search[0]))
return search[0] - key[0];
key++;
search++;
}
return 0;
}
/*
* gui_keyboard_search_part: search a key (maybe part of string)
*/
t_gui_key *
gui_keyboard_search_part (char *key)
{
t_gui_key *ptr_key;
for (ptr_key = gui_keys; ptr_key; ptr_key = ptr_key->next_key)
{
if (gui_keyboard_cmp (ptr_key->key, key) == 0)
return ptr_key;
}
/* key not found */
return NULL;
}
/*
* gui_keyboard_function_search_by_name: search a function by name
*/
t_gui_key_func *
gui_keyboard_function_search_by_name (char *name)
{
int i;
i = 0;
while (gui_key_functions[i].function_name)
{
if (string_strcasecmp (gui_key_functions[i].function_name, name) == 0)
return gui_key_functions[i].function;
i++;
}
/* function not found */
return NULL;
}
/*
* gui_keyboard_function_search_by_ptr: search a function by pointer
*/
char *
gui_keyboard_function_search_by_ptr (t_gui_key_func *function)
{
int i;
i = 0;
while (gui_key_functions[i].function_name)
{
if (gui_key_functions[i].function == function)
return gui_key_functions[i].function_name;
i++;
}
/* function not found */
return NULL;
}
/*
* gui_keyboard_bind: bind a key to a function (command or special function)
*/
t_gui_key *
gui_keyboard_bind (char *key, char *command)
{
t_gui_key_func *ptr_function;
t_gui_key *new_key;
char *command2, *ptr_args;
if (!key || !command)
{
log_printf (_("Error: unable to bind key \"%s\""), key);
return NULL;
}
ptr_function = NULL;
ptr_args = NULL;
if (command[0] != '/')
{
ptr_args = strchr (command, ' ');
if (ptr_args)
command2 = string_strndup (command, ptr_args - command);
else
command2 = strdup (command);
if (command2)
{
ptr_function = gui_keyboard_function_search_by_name (command2);
if (ptr_args)
{
ptr_args++;
while (ptr_args[0] == ' ')
ptr_args++;
}
free (command2);
}
if (!ptr_function)
{
log_printf (_("Error: unable to bind key \"%s\" "
"(invalid function name: \"%s\")"),
key, command);
return NULL;
}
}
gui_keyboard_unbind (key);
new_key = gui_keyboard_new (key,
(ptr_function) ? NULL : command,
ptr_function,
ptr_args);
if (!new_key)
{
log_printf (_("Error: not enough memory for key binding"));
return NULL;
}
return new_key;
}
/*
* gui_keyboard_unbind: remove a key binding
*/
int
gui_keyboard_unbind (char *key)
{
t_gui_key *ptr_key;
char *internal_code;
internal_code = gui_keyboard_get_internal_code (key);
ptr_key = gui_keyboard_search ((internal_code) ? internal_code : key);
if (ptr_key)
gui_keyboard_free (ptr_key);
if (internal_code)
free (internal_code);
return (ptr_key != NULL);
}
/*
* gui_keyboard_pressed: treat new key pressed
* return: 1 if key should be added to input buffer
* 0 otherwise
*/
int
gui_keyboard_pressed (char *key_str)
{
int first_key;
t_gui_key *ptr_key;
char *buffer_before_key;
char **commands, **ptr_cmd;
/* add key to buffer */
first_key = (gui_key_combo_buffer[0] == '\0');
strcat (gui_key_combo_buffer, key_str);
/* if we are in "show mode", increase counter and return */
if (gui_key_grab)
{
gui_key_grab_count++;
return 0;
}
/* look for key combo in key table */
ptr_key = gui_keyboard_search_part (gui_key_combo_buffer);
if (ptr_key)
{
if (string_strcasecmp (ptr_key->key, gui_key_combo_buffer) == 0)
{
/* exact combo found => execute function or command */
buffer_before_key =
(gui_current_window->buffer->input_buffer) ?
strdup (gui_current_window->buffer->input_buffer) : strdup ("");
gui_key_combo_buffer[0] = '\0';
if (ptr_key->command)
{
commands = string_split_command (ptr_key->command, ';');
if (commands)
{
for (ptr_cmd = commands; *ptr_cmd; ptr_cmd++)
{
input_data (gui_current_window->buffer,
*ptr_cmd, 0);
}
string_free_splitted_command (commands);
}
}
else
(void)(ptr_key->function)(ptr_key->args);
if (gui_current_window->buffer->text_search == GUI_TEXT_SEARCH_DISABLED)
{
/* TODO: execute keyboard hooks */
/*(void) plugin_keyboard_handler_exec (
(ptr_key->command) ?
ptr_key->command : gui_keyboard_function_search_by_ptr (ptr_key->function),
buffer_before_key,
gui_current_window->buffer->input_buffer);*/
}
if (buffer_before_key)
free (buffer_before_key);
}
return 0;
}
gui_key_combo_buffer[0] = '\0';
/* if this is first key and not found (even partial) => return 1
else return 0 (= silently discard sequence of bad keys) */
return first_key;
}
/*
* gui_keyboard_free: delete a key binding
*/
void
gui_keyboard_free (t_gui_key *key)
{
/* free memory */
if (key->key)
free (key->key);
if (key->command)
free (key->command);
if (key->args)
free (key->args);
/* remove key from keys list */
if (key->prev_key)
key->prev_key->next_key = key->next_key;
if (key->next_key)
key->next_key->prev_key = key->prev_key;
if (gui_keys == key)
gui_keys = key->next_key;
if (last_gui_key == key)
last_gui_key = key->prev_key;
free (key);
}
/*
* gui_keyboard_free_all: delete all key bindings
*/
void
gui_keyboard_free_all ()
{
while (gui_keys)
gui_keyboard_free (gui_keys);
}
/*
* gui_keyboard_buffer_optimize: optimize keyboard buffer size
*/
void
gui_keyboard_buffer_optimize ()
{
int optimal_size;
optimal_size = (((gui_keyboard_buffer_size * sizeof (int)) /
GUI_KEYBOARD_BUFFER_BLOCK_SIZE) *
GUI_KEYBOARD_BUFFER_BLOCK_SIZE) +
GUI_KEYBOARD_BUFFER_BLOCK_SIZE;
if (gui_keyboard_buffer_alloc != optimal_size)
{
gui_keyboard_buffer_alloc = optimal_size;
gui_keyboard_buffer = realloc (gui_keyboard_buffer, optimal_size * sizeof (char));
}
}
/*
* gui_keyboard_buffer_reset: reset keyboard buffer
* (create empty if never created before)
*/
void
gui_keyboard_buffer_reset ()
{
if (!gui_keyboard_buffer)
{
gui_keyboard_buffer_alloc = GUI_KEYBOARD_BUFFER_BLOCK_SIZE;
gui_keyboard_buffer_size = 0;
gui_keyboard_buffer = (int *)malloc (gui_keyboard_buffer_alloc);
}
else
{
gui_keyboard_buffer_size = 0;
gui_keyboard_buffer_optimize ();
}
gui_keyboard_paste_lines = 0;
}
/*
* gui_keyboard_buffer_add: add a key to keyboard buffer
*/
void
gui_keyboard_buffer_add (int key)
{
if (!gui_keyboard_buffer)
gui_keyboard_buffer_reset ();
gui_keyboard_buffer_size++;
gui_keyboard_buffer_optimize ();
if (gui_keyboard_buffer)
{
gui_keyboard_buffer[gui_keyboard_buffer_size - 1] = key;
if ((key == 10)
&& (gui_keyboard_buffer_size > 1)
&& (gui_keyboard_buffer[gui_keyboard_buffer_size - 2] != 10))
gui_keyboard_paste_lines++;
}
else
{
gui_keyboard_buffer_alloc = 0;
gui_keyboard_buffer_size = 0;
gui_keyboard_paste_lines = 0;
}
}
/*
* gui_keyboard_get_paste_lines: return real number of lines in buffer
* if last key is not Return, then this is lines + 1
* else it's lines
*/
int
gui_keyboard_get_paste_lines ()
{
if ((gui_keyboard_buffer_size > 0)
&& (gui_keyboard_buffer[gui_keyboard_buffer_size - 1] != 10))
return gui_keyboard_paste_lines + 1;
return gui_keyboard_paste_lines;
}
/*
* gui_keyboard_paste_accept: accept paste from user
*/
void
gui_keyboard_paste_accept ()
{
gui_keyboard_paste_pending = 0;
}
/*
* gui_keyboard_paste_cancel: cancel paste from user (reset buffer)
*/
void
gui_keyboard_paste_cancel ()
{
gui_keyboard_buffer_reset ();
gui_keyboard_paste_pending = 0;
}
|