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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
|
/*
* Copyright (C) 2003-2012 Sebastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat 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.
*
* WeeChat 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 WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* gui-key.c: keyboard functions (used by all GUI)
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "../core/weechat.h"
#include "../core/wee-config.h"
#include "../core/wee-hashtable.h"
#include "../core/wee-hdata.h"
#include "../core/wee-hook.h"
#include "../core/wee-infolist.h"
#include "../core/wee-input.h"
#include "../core/wee-list.h"
#include "../core/wee-log.h"
#include "../core/wee-string.h"
#include "../core/wee-utf8.h"
#include "../plugins/plugin.h"
#include "gui-key.h"
#include "gui-bar.h"
#include "gui-bar-window.h"
#include "gui-buffer.h"
#include "gui-chat.h"
#include "gui-color.h"
#include "gui-completion.h"
#include "gui-cursor.h"
#include "gui-focus.h"
#include "gui-input.h"
#include "gui-mouse.h"
#include "gui-window.h"
struct t_gui_key *gui_keys[GUI_KEY_NUM_CONTEXTS]; /* keys by context */
struct t_gui_key *last_gui_key[GUI_KEY_NUM_CONTEXTS]; /* last key */
struct t_gui_key *gui_default_keys[GUI_KEY_NUM_CONTEXTS]; /* default keys */
struct t_gui_key *last_gui_default_key[GUI_KEY_NUM_CONTEXTS];
int gui_keys_count[GUI_KEY_NUM_CONTEXTS]; /* keys number */
int gui_default_keys_count[GUI_KEY_NUM_CONTEXTS]; /* default keys number */
char *gui_key_context_string[GUI_KEY_NUM_CONTEXTS] =
{ "default", "search", "cursor", "mouse" };
char *gui_key_focus_string[GUI_KEY_NUM_FOCUS] =
{ "*", "chat", "bar", "item" };
int gui_key_verbose = 0; /* 1 to see some messages */
char gui_key_combo_buffer[1024]; /* 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_key_grab_command = 0; /* grab command bound to key? */
int gui_key_grab_delay = 0; /* delay for grab (default is 500) */
int *gui_key_buffer = NULL; /* input buffer (for paste detection) */
int gui_key_buffer_alloc = 0; /* input buffer allocated size */
int gui_key_buffer_size = 0; /* input buffer size in bytes */
int gui_key_paste_pending = 0; /* 1 is big paste was detected and */
/* WeeChat is asking user what to do */
int gui_key_paste_bracketed = 0; /* bracketed paste mode detected */
struct t_hook *gui_key_paste_bracketed_timer = NULL;
/* timer for bracketed paste */
int gui_key_paste_lines = 0; /* number of lines for pending paste */
time_t gui_key_last_activity_time = 0; /* last activity time (key) */
/*
* gui_key_init: init keyboard
*/
void
gui_key_init ()
{
int i;
gui_key_combo_buffer[0] = '\0';
gui_key_grab = 0;
gui_key_grab_count = 0;
gui_key_last_activity_time = time (NULL);
/* create default keys and save them in a separate list */
for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
{
gui_keys[i] = NULL;
last_gui_key[i] = NULL;
gui_default_keys[i] = NULL;
last_gui_default_key[i] = NULL;
gui_keys_count[i] = 0;
gui_default_keys_count[i] = 0;
gui_key_default_bindings (i);
gui_default_keys[i] = gui_keys[i];
last_gui_default_key[i] = last_gui_key[i];
gui_default_keys_count[i] = gui_keys_count[i];
gui_keys[i] = NULL;
last_gui_key[i] = NULL;
gui_keys_count[i] = 0;
}
}
/*
* gui_key_search_context: search context by name
*/
int
gui_key_search_context (const char *context)
{
int i;
for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
{
if (string_strcasecmp (gui_key_context_string[i], context) == 0)
return i;
}
/* context not found */
return -1;
}
/*
* gui_key_get_current_context: get current context
*/
int
gui_key_get_current_context ()
{
if (gui_cursor_mode)
return GUI_KEY_CONTEXT_CURSOR;
if (gui_current_window
&& (gui_current_window->buffer->text_search != GUI_TEXT_SEARCH_DISABLED))
return GUI_KEY_CONTEXT_SEARCH;
return GUI_KEY_CONTEXT_DEFAULT;
}
/*
* gui_key_grab_init: init "grab" mode
*/
void
gui_key_grab_init (int grab_command, const char *delay)
{
long milliseconds;
char *error;
gui_key_grab = 1;
gui_key_grab_count = 0;
gui_key_grab_command = grab_command;
gui_key_grab_delay = GUI_KEY_GRAB_DELAY_DEFAULT;
if (delay != NULL)
{
error = NULL;
milliseconds = strtol (delay, &error, 10);
if (error && !error[0] && (milliseconds >= 0))
{
gui_key_grab_delay = milliseconds;
if (gui_key_grab_delay == 0)
gui_key_grab_delay = 1;
}
}
}
/*
* gui_key_grab_end_timer_cb: insert grabbed key in input buffer
*/
int
gui_key_grab_end_timer_cb (void *data, int remaining_calls)
{
char *expanded_key, *expanded_key2;
struct t_gui_key *ptr_key;
/* make C compiler happy */
(void) data;
(void) remaining_calls;
/* get expanded name (for example: \x01+U => ctrl-u) */
expanded_key = gui_key_get_expanded_name (gui_key_combo_buffer);
if (expanded_key)
{
/*
* the expanded_key should be valid UTF-8 at this point,
* but some mouse codes can return ISO chars (for coordinates),
* then we will convert them to UTF-8 string
*/
if (!utf8_is_valid (expanded_key, NULL))
{
expanded_key2 = string_iconv_to_internal ("iso-8859-1",
expanded_key);
if (expanded_key2)
{
free (expanded_key);
expanded_key = expanded_key2;
}
else
{
/* conversion failed, then just replace invalid chars by '?' */
utf8_normalize (expanded_key, '?');
}
}
/* add expanded key to input buffer */
if (gui_current_window->buffer->input)
{
gui_input_insert_string (gui_current_window->buffer, expanded_key, -1);
if (gui_key_grab_command)
{
/* add command bound to key (if found) */
ptr_key = gui_key_search (gui_keys[GUI_KEY_CONTEXT_DEFAULT],
gui_key_combo_buffer);
if (ptr_key)
{
gui_input_insert_string (gui_current_window->buffer, " ", -1);
gui_input_insert_string (gui_current_window->buffer, ptr_key->command, -1);
}
}
if (gui_current_window->buffer->completion)
gui_completion_stop (gui_current_window->buffer->completion, 1);
gui_input_text_changed_modifier_and_signal (gui_current_window->buffer, 1);
}
free (expanded_key);
}
/* end grab mode */
gui_key_grab = 0;
gui_key_grab_count = 0;
gui_key_grab_command = 0;
gui_key_combo_buffer[0] = '\0';
return WEECHAT_RC_OK;
}
/*
* gui_key_get_internal_code: get internal code from user key name
* for example: return '\x01'+'R' for "ctrl-R"
* Note: returned value has to be free() after use
*/
char *
gui_key_get_internal_code (const char *key)
{
char *result;
if ((key[0] == '@') && strchr (key, ':'))
return strdup (key);
if ((result = malloc (strlen (key) + 1)))
{
result[0] = '\0';
while (key[0])
{
if (strncmp (key, "meta2-", 6) == 0)
{
strcat (result, "\x01[[");
key += 6;
}
if (strncmp (key, "meta-", 5) == 0)
{
strcat (result, "\x01[");
key += 5;
}
else if (strncmp (key, "ctrl-", 5) == 0)
{
strcat (result, "\x01");
key += 5;
}
else
{
strncat (result, key, 1);
key++;
}
}
}
else
return NULL;
return result;
}
/*
* gui_key_get_expanded_name: get expanded name from internal key code
* for example: return "ctrl-R" for "\x01+R"
*/
char *
gui_key_get_expanded_name (const char *key)
{
char *result;
if (!key)
return NULL;
result = malloc ((strlen (key) * 5) + 1);
if (result)
{
result[0] = '\0';
while (key[0])
{
if (strncmp (key, "\x01[[", 3) == 0)
{
strcat (result, "meta2-");
key += 3;
}
if (strncmp (key, "\x01[", 2) == 0)
{
strcat (result, "meta-");
key += 2;
}
else if ((key[0] == '\x01') && (key[1]))
{
strcat (result, "ctrl-");
key++;
}
else
{
strncat (result, key, 1);
key++;
}
}
}
return result;
}
/*
* gui_key_find_pos: find position for a key (for sorting keys list)
*/
struct t_gui_key *
gui_key_find_pos (struct t_gui_key *keys, struct t_gui_key *key)
{
struct t_gui_key *ptr_key;
for (ptr_key = keys; ptr_key; ptr_key = ptr_key->next_key)
{
if ((key->score < ptr_key->score)
|| ((key->score == ptr_key->score)
&& (strcmp (key->key, ptr_key->key) < 0)))
{
return ptr_key;
}
}
return NULL;
}
/*
* gui_key_insert_sorted: insert key into sorted list
*/
void
gui_key_insert_sorted (struct t_gui_key **keys,
struct t_gui_key **last_key,
int *keys_count,
struct t_gui_key *key)
{
struct t_gui_key *pos_key;
if (*keys)
{
pos_key = gui_key_find_pos (*keys, 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
*keys = key;
pos_key->prev_key = key;
}
else
{
/* add key to the end */
key->prev_key = *last_key;
key->next_key = NULL;
(*last_key)->next_key = key;
*last_key = key;
}
}
else
{
/* first key in list */
key->prev_key = NULL;
key->next_key = NULL;
*keys = key;
*last_key = key;
}
(*keys_count)++;
}
/*
* gui_key_set_area_type_name: set area type and name
* for example: "bar(nicklist)" will return:
* type: 2 (bar)
* name: "nicklist"
* Warning: if no area is found, values are NOT set
*/
void
gui_key_set_area_type_name (const char *area,
int *area_type, char **area_name)
{
int focus, length;
char *pos_end;
for (focus = 0; focus < GUI_KEY_NUM_FOCUS; focus++)
{
length = strlen (gui_key_focus_string[focus]);
if (strncmp (area, gui_key_focus_string[focus], length) == 0)
{
if (focus == GUI_KEY_FOCUS_ANY)
{
*area_type = focus;
*area_name = strdup ("*");
break;
}
if (!area[length])
{
*area_type = focus;
*area_name = strdup ("*");
break;
}
if ((area[length] == '(') && area[length + 1])
{
pos_end = strchr (area + length, ')');
if (pos_end)
{
*area_type = focus;
*area_name = string_strndup (area + length + 1,
pos_end - area - length - 1);
break;
}
}
}
}
}
/*
* gui_key_set_areas: set areas types (any, chat, bar or item) and names for a
* key
*/
void
gui_key_set_areas (struct t_gui_key *key)
{
int area;
char *pos_colon, *pos_area2, *areas[2];
for (area = 0; area < 2; area++)
{
key->area_type[area] = GUI_KEY_FOCUS_ANY;
key->area_name[area] = NULL;
}
key->area_key = NULL;
if (key->key[0] != '@')
return;
areas[0] = NULL;
areas[1] = NULL;
pos_colon = strchr (key->key + 1, ':');
if (!pos_colon)
return;
pos_area2 = strchr (key->key + 1, '>');
key->area_key = strdup (pos_colon + 1);
if (!pos_area2 || (pos_area2 > pos_colon))
areas[0] = string_strndup (key->key + 1, pos_colon - key->key - 1);
else
{
if (pos_area2 > key->key + 1)
areas[0] = string_strndup (key->key + 1, pos_area2 - key->key - 1);
areas[1] = string_strndup (pos_area2 + 1, pos_colon - pos_area2 - 1);
}
for (area = 0; area < 2; area++)
{
if (!areas[area])
{
key->area_name[area] = strdup ("*");
continue;
}
gui_key_set_area_type_name (areas[area],
&(key->area_type[area]),
&(key->area_name[area]));
}
if (areas[0])
free (areas[0]);
if (areas[1])
free (areas[1]);
}
/*
* gui_key_set_score: compute a score key for sorting keys
* and set it in key
* (high score == at the end of list)
*/
void
gui_key_set_score (struct t_gui_key *key)
{
int score, bonus, area;
score = 0;
bonus = 8;
key->score = score;
if (key->key[0] != '@')
return;
/* basic score for key with area */
score |= 1 << bonus;
bonus--;
/* add score for each area type */
for (area = 0; area < 2; area++)
{
/* bonus if area type is "any" */
if (key->area_name[area]
&& (key->area_type[area] == GUI_KEY_FOCUS_ANY))
{
score |= 1 << bonus;
}
bonus--;
}
/* add score for each area name */
for (area = 0; area < 2; area++)
{
/* bonus if area name is "*" */
if (key->area_name[area]
&& (strcmp (key->area_name[area], "*") == 0))
{
score |= 1 << bonus;
}
bonus--;
}
key->score = score;
}
/*
* gui_key_new: add a new key in keys list
* if buffer is not null, then key is specific to buffer
* otherwise it's general key (for most keys)
*/
struct t_gui_key *
gui_key_new (struct t_gui_buffer *buffer, int context, const char *key,
const char *command)
{
struct t_gui_key *new_key;
char *expanded_name;
if (!key || !command)
return NULL;
if ((new_key = malloc (sizeof (*new_key))))
{
new_key->key = gui_key_get_internal_code (key);
if (!new_key->key)
new_key->key = strdup (key);
if (!new_key->key)
{
free (new_key);
return NULL;
}
new_key->command = strdup (command);
if (!new_key->command)
{
free (new_key->key);
free (new_key);
return NULL;
}
gui_key_set_areas (new_key);
gui_key_set_score (new_key);
if (buffer)
{
gui_key_insert_sorted (&buffer->keys, &buffer->last_key,
&buffer->keys_count, new_key);
}
else
{
gui_key_insert_sorted (&gui_keys[context],
&last_gui_key[context],
&gui_keys_count[context], new_key);
}
expanded_name = gui_key_get_expanded_name (new_key->key);
hook_signal_send ("key_bind",
WEECHAT_HOOK_SIGNAL_STRING, expanded_name);
if (gui_key_verbose)
{
gui_chat_printf (NULL,
_("New key binding (context \"%s\"): "
"%s%s => %s%s"),
gui_key_context_string[context],
(expanded_name) ? expanded_name : new_key->key,
GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS),
GUI_COLOR(GUI_COLOR_CHAT),
new_key->command);
}
if (expanded_name)
free (expanded_name);
}
else
return NULL;
return new_key;
}
/*
* gui_key_search: search a key
*/
struct t_gui_key *
gui_key_search (struct t_gui_key *keys, const char *key)
{
struct t_gui_key *ptr_key;
for (ptr_key = keys; ptr_key; ptr_key = ptr_key->next_key)
{
if (strcmp (ptr_key->key, key) == 0)
return ptr_key;
}
/* key not found */
return NULL;
}
/*
* gui_key_cmp: compares 2 keys
*/
int
gui_key_cmp (const char *key, const char *search, int context)
{
int diff;
if (context == GUI_KEY_CONTEXT_MOUSE)
return (string_match (key, search, 1)) ? 0 : 1;
while (search[0])
{
diff = utf8_charcmp (key, search);
if (diff != 0)
return diff;
key = utf8_next_char (key);
search = utf8_next_char (search);
}
return 0;
}
/*
* gui_key_search_part: search a key (maybe part of string)
*/
struct t_gui_key *
gui_key_search_part (struct t_gui_buffer *buffer, int context,
const char *key)
{
struct t_gui_key *ptr_key;
for (ptr_key = (buffer) ? buffer->keys : gui_keys[context]; ptr_key;
ptr_key = ptr_key->next_key)
{
if (ptr_key->key
&& (((context != GUI_KEY_CONTEXT_CURSOR)
&& (context != GUI_KEY_CONTEXT_MOUSE))
|| (ptr_key->key[0] != '@')))
{
if (gui_key_cmp (ptr_key->key, key, context) == 0)
return ptr_key;
}
}
/* key not found */
return NULL;
}
/*
* gui_key_bind: bind a key to a function (command or special function)
* if buffer is not null, then key is specific to buffer
* otherwise it's general key (for most keys)
* Note: if key already exists, it is removed then added again
* with new value
*/
struct t_gui_key *
gui_key_bind (struct t_gui_buffer *buffer, int context, const char *key,
const char *command)
{
if (!key || !command || (string_strcasecmp (key, "meta-") == 0))
return NULL;
gui_key_unbind (buffer, context, key);
return gui_key_new (buffer, context, key, command);
}
/*
* gui_key_bind_plugin_hashtable_map_cb: bind keys in hashtable
*/
void
gui_key_bind_plugin_hashtable_map_cb (void *data,
struct t_hashtable *hashtable,
const void *key, const void *value)
{
int *user_data;
struct t_gui_key *ptr_key;
char *internal_code;
/* make C compiler happy */
(void) hashtable;
user_data = (int *)data;
if (user_data && key && value)
{
internal_code = gui_key_get_internal_code (key);
if (internal_code)
{
ptr_key = gui_key_search (gui_keys[user_data[0]], internal_code);
if (!ptr_key)
{
if (gui_key_new (NULL, user_data[0], key, value))
user_data[1]++;
}
free (internal_code);
}
}
}
/*
* gui_key_bind_plugin: create many keys using a hashtable
* (used by plugins only)
* return: number of keys added
* note: if key already exists, it is NOT changed (plugins
* should never overwrite user keys)
*/
int
gui_key_bind_plugin (const char *context, struct t_hashtable *keys)
{
int data[2];
data[0] = gui_key_search_context (context);
if (data[0] < 0)
return 0;
gui_key_verbose = 1;
data[1] = 0;
hashtable_map (keys, &gui_key_bind_plugin_hashtable_map_cb, data);
gui_key_verbose = 0;
return data[1];
}
/*
* gui_key_unbind: remove one key binding
* return: 1 if key removed, 0 if not removed
*/
int
gui_key_unbind (struct t_gui_buffer *buffer, int context, const char *key)
{
struct t_gui_key *ptr_key;
char *internal_code;
internal_code = gui_key_get_internal_code (key);
if (!internal_code)
return 0;
ptr_key = gui_key_search ((buffer) ? buffer->keys : gui_keys[context],
(internal_code) ? internal_code : key);
free (internal_code);
if (ptr_key)
{
if (buffer)
{
gui_key_free (&buffer->keys, &buffer->last_key,
&buffer->keys_count, ptr_key);
}
else
{
if (gui_key_verbose)
{
gui_chat_printf (NULL,
_("Key \"%s\" unbound (context: \"%s\")"),
key,
gui_key_context_string[context]);
}
gui_key_free (&gui_keys[context], &last_gui_key[context],
&gui_keys_count[context], ptr_key);
}
hook_signal_send ("key_unbind",
WEECHAT_HOOK_SIGNAL_STRING, (char *)key);
return 1;
}
return 0;
}
/*
* gui_key_unbind_plugin: remove one or more key binding(s)
* (used by plugins only)
* return: number of keys removed
*/
int
gui_key_unbind_plugin (const char *context, const char *key)
{
int ctxt, num_keys, area_type;
char *area_name;
struct t_gui_key *ptr_key;
ctxt = gui_key_search_context (context);
if (ctxt < 0)
return 0;
if (strncmp (key, "area:", 5) == 0)
{
num_keys = 0;
area_type = -1;
area_name = NULL;
gui_key_set_area_type_name (key + 5, &area_type, &area_name);
if (area_name)
{
for (ptr_key = gui_keys[ctxt]; ptr_key; ptr_key = ptr_key->next_key)
{
if (((ptr_key->area_type[0] == area_type)
&& ptr_key->area_name[0]
&& (strcmp (ptr_key->area_name[0], area_name) == 0))
|| ((ptr_key->area_type[1] == area_type)
&& ptr_key->area_name[1]
&& (strcmp (ptr_key->area_name[1], area_name) == 0)))
{
gui_key_verbose = 1;
num_keys += gui_key_unbind (NULL, ctxt, ptr_key->key);
gui_key_verbose = 0;
}
}
free (area_name);
}
}
else
{
gui_key_verbose = 1;
num_keys = gui_key_unbind (NULL, ctxt, key);
gui_key_verbose = 0;
}
return num_keys;
}
/*
* gui_key_focus_matching: return 1 if area in key is matching focus area on
* screen (cursor/mouse)
*/
int
gui_key_focus_matching (struct t_gui_key *key,
struct t_hashtable **hashtable_focus)
{
int match[2], area;
const char *chat, *buffer_full_name, *bar_name, *bar_item_name;
for (area = 0; area < 2; area++)
{
match[area] = 0;
switch (key->area_type[area])
{
case GUI_KEY_FOCUS_ANY:
match[area] = 1;
break;
case GUI_KEY_FOCUS_CHAT:
chat = hashtable_get (hashtable_focus[area], "_chat");
buffer_full_name = hashtable_get (hashtable_focus[area],
"_buffer_full_name");
if (chat && (strcmp (chat, "1") == 0)
&& buffer_full_name && buffer_full_name[0])
{
if (string_match (buffer_full_name, key->area_name[area], 0))
match[area] = 1;
}
break;
case GUI_KEY_FOCUS_BAR:
bar_name = hashtable_get (hashtable_focus[area], "_bar_name");
if (bar_name && bar_name[0]
&& (string_match (bar_name, key->area_name[area], 0)))
{
match[area] = 1;
}
break;
case GUI_KEY_FOCUS_ITEM:
bar_item_name = hashtable_get (hashtable_focus[area],
"_bar_item_name");
if (bar_item_name && bar_item_name[0]
&& (string_match (bar_item_name, key->area_name[area], 0)))
{
match[area] = 1;
}
break;
case GUI_KEY_NUM_FOCUS:
break;
}
}
return match[0] && match[1];
}
/*
* gui_key_focus_command_replace_cb: callback for replacing values in string
* with a hashtable
*/
char *
gui_key_focus_command_replace_cb (void *data, const char *text)
{
struct t_hashtable *ptr_hashtable;
const char *ptr_value;
ptr_hashtable = (struct t_hashtable *)data;
if (ptr_hashtable)
{
ptr_value = hashtable_get (ptr_hashtable, text);
if (ptr_value)
return strdup (ptr_value);
}
return NULL;
}
/*
* gui_key_focus_command: run command according to focus
* return 1 if a command was executed, otherwise 0
*/
int
gui_key_focus_command (const char *key, int context,
struct t_hashtable **hashtable_focus)
{
struct t_gui_key *ptr_key;
int i, errors, matching, debug;
char *command, **commands;
struct t_hashtable *hashtable;
struct t_weelist *list_keys;
struct t_weelist_item *ptr_item;
debug = 0;
if (gui_cursor_debug && (context == GUI_KEY_CONTEXT_CURSOR))
debug = gui_cursor_debug;
else if (gui_mouse_debug && (context == GUI_KEY_CONTEXT_MOUSE))
debug = gui_mouse_debug;
for (ptr_key = gui_keys[context]; ptr_key;
ptr_key = ptr_key->next_key)
{
/* ignore key if it has not area name or key for area */
if (!ptr_key->area_name[0] || !ptr_key->area_key)
continue;
/* the special command "-" is used to ignore key */
if (strcmp (ptr_key->command, "-") == 0)
continue;
/* ignore key if key for area is not matching */
if (gui_key_cmp (key, ptr_key->area_key, context) != 0)
continue;
/* ignore mouse event if not explicit requested */
if ((context == GUI_KEY_CONTEXT_MOUSE) &&
(string_match (key, "*-event-*", 1) != string_match (ptr_key->area_key, "*-event-*", 1)))
continue;
/* check if focus is matching with key */
matching = gui_key_focus_matching (ptr_key, hashtable_focus);
if (!matching)
continue;
hashtable = hook_focus_get_data (hashtable_focus[0],
hashtable_focus[1]);
if (!hashtable)
continue;
if ((context == GUI_KEY_CONTEXT_CURSOR) && gui_cursor_debug)
{
gui_input_delete_line (gui_current_window->buffer);
}
if (debug > 1)
{
gui_chat_printf (NULL, _("Hashtable focus:"));
list_keys = hashtable_get_list_keys (hashtable);
for (ptr_item = list_keys->items; ptr_item;
ptr_item = ptr_item->next_item)
{
gui_chat_printf (NULL, " %s: \"%s\"",
ptr_item->data,
hashtable_get (hashtable, ptr_item->data));
}
}
if (debug)
{
gui_chat_printf (NULL, _("Command for key: \"%s\""),
ptr_key->command);
}
commands = string_split_command (ptr_key->command, ';');
if (commands)
{
for (i = 0; commands[i]; i++)
{
if (string_strncasecmp (commands[i], "hsignal:", 8) == 0)
{
if (commands[i][8])
{
if (debug)
{
gui_chat_printf (NULL,
_("Sending hsignal: \"%s\""),
commands[i] + 8);
}
hook_hsignal_send (commands[i] + 8, hashtable);
}
}
else
{
command = string_replace_with_callback (commands[i],
&gui_key_focus_command_replace_cb,
hashtable,
&errors);
if (command)
{
if (errors == 0)
{
if (debug)
{
gui_chat_printf (NULL,
_("Executing command: \"%s\""),
command);
}
input_data (gui_current_window->buffer, command);
}
free (command);
}
}
}
string_free_split_command (commands);
}
hashtable_free (hashtable);
return 1;
}
return 0;
}
/*
* gui_key_focus: process key pressed in cursor or mouse mode,
* looking for keys: "{area}key" in context "cursor" or "mouse"
* return 1 if a command was executed, otherwise 0
*/
int
gui_key_focus (const char *key, int context)
{
struct t_gui_focus_info *focus_info1, *focus_info2;
struct t_hashtable *hashtable_focus[2];
int rc;
rc = 0;
focus_info1 = NULL;
focus_info2 = NULL;
hashtable_focus[0] = NULL;
hashtable_focus[1] = NULL;
if (context == GUI_KEY_CONTEXT_MOUSE)
{
focus_info1 = gui_focus_get_info (gui_mouse_event_x[0],
gui_mouse_event_y[0]);
if (!focus_info1)
goto end;
hashtable_focus[0] = gui_focus_to_hashtable (focus_info1, key);
if (!hashtable_focus[0])
goto end;
if ((gui_mouse_event_x[0] != gui_mouse_event_x[1])
|| (gui_mouse_event_y[0] != gui_mouse_event_y[1]))
{
focus_info2 = gui_focus_get_info (gui_mouse_event_x[1],
gui_mouse_event_y[1]);
if (!focus_info2)
goto end;
hashtable_focus[1] = gui_focus_to_hashtable (focus_info2, key);
if (!hashtable_focus[1])
goto end;
}
if (gui_mouse_debug)
{
gui_chat_printf (NULL, "Mouse: %s, (%d,%d) -> (%d,%d)",
key,
gui_mouse_event_x[0], gui_mouse_event_y[0],
gui_mouse_event_x[1], gui_mouse_event_y[1]);
}
}
else
{
focus_info1 = gui_focus_get_info (gui_cursor_x, gui_cursor_y);
if (!focus_info1)
goto end;
hashtable_focus[0] = gui_focus_to_hashtable (focus_info1, key);
if (!hashtable_focus[0])
goto end;
}
rc = gui_key_focus_command (key, context, hashtable_focus);
end:
if (focus_info1)
gui_focus_free_info (focus_info1);
if (focus_info2)
gui_focus_free_info (focus_info2);
if (hashtable_focus[0])
hashtable_free (hashtable_focus[0]);
if (hashtable_focus[1])
hashtable_free (hashtable_focus[1]);
return rc;
}
/*
* gui_key_pressed: process new key pressed
* return: 1 if key should be added to input buffer
* 0 otherwise
*/
int
gui_key_pressed (const char *key_str)
{
int i, first_key, context, length, length_key;
struct t_gui_key *ptr_key;
char **commands, *pos;
/* add key to buffer */
first_key = (gui_key_combo_buffer[0] == '\0');
length = strlen (gui_key_combo_buffer);
length_key = strlen (key_str);
if (length + length_key + 1 <= (int)sizeof (gui_key_combo_buffer))
strcat (gui_key_combo_buffer, key_str);
/* if we are in "show mode", increase counter and return */
if (gui_key_grab)
{
if (gui_key_grab_count == 0)
{
hook_timer (NULL, gui_key_grab_delay, 0, 1,
&gui_key_grab_end_timer_cb, NULL);
}
gui_key_grab_count++;
return 0;
}
/* mouse event pending */
if (gui_mouse_event_pending)
{
pos = strstr (gui_key_combo_buffer, "\x1B[M");
if (pos)
{
pos[0] = '\0';
gui_mouse_event_end ();
gui_mouse_event_init ();
}
return 0;
}
if (strcmp (gui_key_combo_buffer, "\x01[[M") == 0)
{
gui_key_combo_buffer[0] = '\0';
gui_mouse_event_init ();
return 0;
}
ptr_key = NULL;
context = gui_key_get_current_context ();
switch (context)
{
case GUI_KEY_CONTEXT_DEFAULT:
/* look for key combo in key table for current buffer */
ptr_key = gui_key_search_part (gui_current_window->buffer,
GUI_KEY_CONTEXT_DEFAULT,
gui_key_combo_buffer);
/* if key is not found for buffer, then look in general table */
if (!ptr_key)
ptr_key = gui_key_search_part (NULL,
GUI_KEY_CONTEXT_DEFAULT,
gui_key_combo_buffer);
break;
case GUI_KEY_CONTEXT_SEARCH:
ptr_key = gui_key_search_part (NULL,
GUI_KEY_CONTEXT_SEARCH,
gui_key_combo_buffer);
if (!ptr_key)
{
ptr_key = gui_key_search_part (NULL,
GUI_KEY_CONTEXT_DEFAULT,
gui_key_combo_buffer);
}
break;
case GUI_KEY_CONTEXT_CURSOR:
ptr_key = gui_key_search_part (NULL,
GUI_KEY_CONTEXT_CURSOR,
gui_key_combo_buffer);
break;
}
/* if key is found, then execute action */
if (ptr_key)
{
if (strcmp (ptr_key->key, gui_key_combo_buffer) == 0)
{
/* exact combo found => execute function or command */
gui_key_combo_buffer[0] = '\0';
if (ptr_key->command)
{
commands = string_split_command (ptr_key->command, ';');
if (commands)
{
for (i = 0; commands[i]; i++)
{
input_data (gui_current_window->buffer, commands[i]);
}
string_free_split_command (commands);
}
}
}
return 0;
}
else if (context == GUI_KEY_CONTEXT_CURSOR)
{
if (gui_key_focus (gui_key_combo_buffer, GUI_KEY_CONTEXT_CURSOR))
{
gui_key_combo_buffer[0] = '\0';
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_key_free: delete a key binding
*/
void
gui_key_free (struct t_gui_key **keys, struct t_gui_key **last_key,
int *keys_count, struct t_gui_key *key)
{
int i;
/* free memory */
if (key->key)
free (key->key);
for (i = 0; i < 2; i++)
{
if (key->area_name[i])
free (key->area_name[i]);
}
if (key->area_key)
free (key->area_key);
if (key->command)
free (key->command);
/* 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 (*keys == key)
*keys = key->next_key;
if (*last_key == key)
*last_key = key->prev_key;
free (key);
(*keys_count)--;
}
/*
* gui_key_free_all: delete all key bindings
*/
void
gui_key_free_all (struct t_gui_key **keys, struct t_gui_key **last_key,
int *keys_count)
{
while (*keys)
{
gui_key_free (keys, last_key, keys_count, *keys);
}
}
/*
* gui_key_buffer_optimize: optimize keyboard buffer size
*/
void
gui_key_buffer_optimize ()
{
int optimal_size, *gui_key_buffer2;
optimal_size = (((gui_key_buffer_size * sizeof (int)) /
GUI_KEY_BUFFER_BLOCK_SIZE) *
GUI_KEY_BUFFER_BLOCK_SIZE) +
GUI_KEY_BUFFER_BLOCK_SIZE;
if (gui_key_buffer_alloc != optimal_size)
{
gui_key_buffer_alloc = optimal_size;
gui_key_buffer2 = realloc (gui_key_buffer, optimal_size);
if (!gui_key_buffer2)
{
if (gui_key_buffer)
{
free (gui_key_buffer);
gui_key_buffer = NULL;
}
return;
}
gui_key_buffer = gui_key_buffer2;
}
}
/*
* gui_key_buffer_reset: reset keyboard buffer
* (create empty if never created before)
*/
void
gui_key_buffer_reset ()
{
if (!gui_key_buffer)
{
gui_key_buffer_alloc = GUI_KEY_BUFFER_BLOCK_SIZE;
gui_key_buffer_size = 0;
gui_key_buffer = malloc (gui_key_buffer_alloc);
}
else
{
gui_key_buffer_size = 0;
gui_key_buffer_optimize ();
}
gui_key_paste_lines = 0;
}
/*
* gui_key_buffer_add: add a key to keyboard buffer
*/
void
gui_key_buffer_add (unsigned char key)
{
if (!gui_key_buffer)
gui_key_buffer_reset ();
gui_key_buffer_size++;
gui_key_buffer_optimize ();
if (gui_key_buffer)
{
gui_key_buffer[gui_key_buffer_size - 1] = key;
if (((key == '\r') || (key == '\n'))
&& (gui_key_buffer_size > 1)
&& (gui_key_buffer[gui_key_buffer_size - 2] != '\r')
&& (gui_key_buffer[gui_key_buffer_size - 2] != '\n'))
{
gui_key_paste_lines++;
}
}
else
{
gui_key_buffer_alloc = 0;
gui_key_buffer_size = 0;
gui_key_paste_lines = 0;
}
}
/*
* gui_key_buffer_search: search a string in gui_key_buffer (array of integers)
* start_index must be >= 0
* if max_index is negative, the search is until end of buffer
* return index for string found in gui_key_buffer
* (not from "start_index" but from beginning of gui_key_buffer)
* or -1 if string is not found
*/
int
gui_key_buffer_search (int start_index, int max_index, const char *string)
{
int i, j, length, found;
if ((gui_key_buffer_size == 0) || !string || !string[0])
return -1;
length = strlen (string);
if (gui_key_buffer_size < length)
return -1;
if (max_index < 0)
max_index = gui_key_buffer_size - length;
else if (max_index > gui_key_buffer_size - length)
max_index = gui_key_buffer_size - length;
for (i = start_index; i <= max_index; i++)
{
found = 1;
for (j = 0; j < length; j++)
{
if (gui_key_buffer[i + j] != string[j])
{
found = 0;
break;
}
}
if (found)
return i;
}
/* string not found */
return -1;
}
/*
* gui_key_buffer_remove: remove some chars from gui_key_buffer
*/
void
gui_key_buffer_remove (int index, int number)
{
int i;
for (i = index; i < gui_key_buffer_size - number; i++)
{
gui_key_buffer[i] = gui_key_buffer[i + number];
}
gui_key_buffer_size -= number;
}
/*
* gui_key_paste_remove_newline: remove final newline at enf of paste if there
* is only one line to paste
*/
void
gui_key_paste_remove_newline ()
{
if ((gui_key_paste_lines <= 1)
&& (gui_key_buffer_size > 0)
&& ((gui_key_buffer[gui_key_buffer_size - 1] == '\r')
|| (gui_key_buffer[gui_key_buffer_size - 1] == '\n')))
{
gui_key_buffer_size--;
gui_key_paste_lines = 0;
}
}
/*
* gui_key_paste_replace_tabs: replace tabs by spaces in paste
*/
void
gui_key_paste_replace_tabs ()
{
int i;
for (i = 0; i < gui_key_buffer_size; i++)
{
if (gui_key_buffer[i] == '\t')
gui_key_buffer[i] = ' ';
}
}
/*
* gui_key_paste_start: start paste of text
*/
void
gui_key_paste_start ()
{
gui_key_paste_remove_newline ();
gui_key_paste_replace_tabs ();
gui_key_paste_pending = 1;
gui_input_paste_pending_signal ();
}
/*
* gui_key_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_key_get_paste_lines ()
{
int length;
length = gui_key_buffer_size;
if (length >= GUI_KEY_BRACKETED_PASTE_LENGTH)
{
if (gui_key_buffer_search (length - GUI_KEY_BRACKETED_PASTE_LENGTH, -1,
GUI_KEY_BRACKETED_PASTE_END) >= 0)
length -= GUI_KEY_BRACKETED_PASTE_LENGTH;
}
if ((length > 0)
&& (gui_key_buffer[length - 1] != '\r')
&& (gui_key_buffer[length - 1] != '\n'))
{
return gui_key_paste_lines + 1;
}
return (gui_key_paste_lines > 0) ? gui_key_paste_lines : 1;
}
/*
* gui_key_paste_check: check pasted lines: if more than N lines, then enable
* paste mode and ask confirmation to user
* (ctrl-Y=paste, ctrl-N=cancel)
* (N is option weechat.look.paste_max_lines)
* return 1 if paste mode has been enabled, 0 otherwise
*/
int
gui_key_paste_check (int bracketed_paste)
{
int max_lines;
max_lines = CONFIG_INTEGER(config_look_paste_max_lines);
if (max_lines >= 0)
{
if (!bracketed_paste && (max_lines == 0))
max_lines = 1;
if (gui_key_get_paste_lines () > max_lines)
{
/* ask user what to do */
gui_key_paste_start ();
return 1;
}
}
return 0;
}
/*
* gui_key_paste_bracketed_timer_cb: callback for bracketed paste timer
*/
int
gui_key_paste_bracketed_timer_cb (void *data, int remaining_calls)
{
/* make C compiler happy */
(void) data;
(void) remaining_calls;
gui_key_paste_bracketed_timer = NULL;
if (gui_key_paste_bracketed)
gui_key_paste_bracketed_stop ();
return WEECHAT_RC_OK;
}
/*
* gui_key_paste_bracketed_timer_remove: remove timer for bracketed paste
*/
void
gui_key_paste_bracketed_timer_remove ()
{
if (gui_key_paste_bracketed_timer)
{
unhook (gui_key_paste_bracketed_timer);
gui_key_paste_bracketed_timer = NULL;
}
}
/*
* gui_key_paste_bracketed_timer_add: add timer for bracketed paste
*/
void
gui_key_paste_bracketed_timer_add ()
{
gui_key_paste_bracketed_timer_remove ();
gui_key_paste_bracketed_timer = hook_timer (NULL,
CONFIG_INTEGER(config_look_paste_bracketed_timer_delay) * 1000,
0, 1,
&gui_key_paste_bracketed_timer_cb, NULL);
}
/*
* gui_key_paste_bracketed_start: start bracketed paste of text
* (ESC[200~ detected)
*/
void
gui_key_paste_bracketed_start ()
{
gui_key_paste_bracketed = 1;
gui_key_paste_bracketed_timer_add ();
}
/*
* gui_key_paste_bracketed_stop: stop bracketed paste of text
* (ESC[201~ detected or timeout while waiting for
* this code)
*/
void
gui_key_paste_bracketed_stop ()
{
gui_key_paste_check (1);
gui_key_paste_bracketed = 0;
}
/*
* gui_key_paste_accept: accept paste from user
*/
void
gui_key_paste_accept ()
{
/*
* add final newline if there is not in pasted text
* (for at least 2 lines pasted)
*/
if ((gui_key_get_paste_lines () > 1)
&& (gui_key_buffer_size > 0)
&& (gui_key_buffer[gui_key_buffer_size - 1] != '\r')
&& (gui_key_buffer[gui_key_buffer_size - 1] != '\n'))
{
gui_key_buffer_add ('\n');
}
gui_key_paste_pending = 0;
gui_input_paste_pending_signal ();
}
/*
* gui_key_paste_cancel: cancel paste from user (reset buffer)
*/
void
gui_key_paste_cancel ()
{
gui_key_buffer_reset ();
gui_key_paste_pending = 0;
gui_input_paste_pending_signal ();
}
/*
* gui_key_end: end keyboard (free some data)
*/
void
gui_key_end ()
{
int i;
/* free key buffer */
if (gui_key_buffer)
free (gui_key_buffer);
for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
{
/* free keys */
gui_key_free_all (&gui_keys[i], &last_gui_key[i],
&gui_keys_count[i]);
/* free default keys */
gui_key_free_all (&gui_default_keys[i], &last_gui_default_key[i],
&gui_default_keys_count[i]);
}
}
/*
* gui_key_hdata_key_cb: return hdata for key
*/
struct t_hdata *
gui_key_hdata_key_cb (void *data, const char *hdata_name)
{
struct t_hdata *hdata;
int i;
char str_list[128];
/* make C compiler happy */
(void) data;
hdata = hdata_new (NULL, hdata_name, "prev_key", "next_key",
0, NULL, NULL);
if (hdata)
{
HDATA_VAR(struct t_gui_key, key, STRING, 0, NULL, NULL);
HDATA_VAR(struct t_gui_key, area_type, POINTER, 0, NULL, NULL);
HDATA_VAR(struct t_gui_key, area_name, POINTER, 0, NULL, NULL);
HDATA_VAR(struct t_gui_key, area_key, STRING, 0, NULL, NULL);
HDATA_VAR(struct t_gui_key, command, STRING, 0, NULL, NULL);
HDATA_VAR(struct t_gui_key, score, INTEGER, 0, NULL, NULL);
HDATA_VAR(struct t_gui_key, prev_key, POINTER, 0, NULL, hdata_name);
HDATA_VAR(struct t_gui_key, next_key, POINTER, 0, NULL, hdata_name);
for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
{
snprintf (str_list, sizeof (str_list),
"gui_keys%s%s",
(i == GUI_KEY_CONTEXT_DEFAULT) ? "" : "_",
(i == GUI_KEY_CONTEXT_DEFAULT) ? "" : gui_key_context_string[i]);
hdata_new_list(hdata, str_list, &gui_keys[i]);
snprintf (str_list, sizeof (str_list),
"last_gui_key%s%s",
(i == GUI_KEY_CONTEXT_DEFAULT) ? "" : "_",
(i == GUI_KEY_CONTEXT_DEFAULT) ? "" : gui_key_context_string[i]);
hdata_new_list(hdata, str_list, &last_gui_key[i]);
snprintf (str_list, sizeof (str_list),
"gui_default_keys%s%s",
(i == GUI_KEY_CONTEXT_DEFAULT) ? "" : "_",
(i == GUI_KEY_CONTEXT_DEFAULT) ? "" : gui_key_context_string[i]);
hdata_new_list(hdata, str_list, &gui_default_keys[i]);
snprintf (str_list, sizeof (str_list),
"last_gui_default_key%s%s",
(i == GUI_KEY_CONTEXT_DEFAULT) ? "" : "_",
(i == GUI_KEY_CONTEXT_DEFAULT) ? "" : gui_key_context_string[i]);
hdata_new_list(hdata, str_list, &last_gui_default_key[i]);
}
}
return hdata;
}
/*
* gui_key_add_to_infolist: add a key in an infolist
* return 1 if ok, 0 if error
*/
int
gui_key_add_to_infolist (struct t_infolist *infolist, struct t_gui_key *key)
{
struct t_infolist_item *ptr_item;
char *expanded_key;
if (!infolist || !key)
return 0;
ptr_item = infolist_new_item (infolist);
if (!ptr_item)
return 0;
if (!infolist_new_var_string (ptr_item, "key_internal", key->key))
return 0;
expanded_key = gui_key_get_expanded_name (key->key);
if (expanded_key)
{
if (!infolist_new_var_string (ptr_item, "key", expanded_key))
return 0;
free (expanded_key);
}
if (!infolist_new_var_integer (ptr_item, "area_type1", key->area_type[0]))
return 0;
if (!infolist_new_var_string (ptr_item, "area_name1", key->area_name[0]))
return 0;
if (!infolist_new_var_integer (ptr_item, "area_type2", key->area_type[1]))
return 0;
if (!infolist_new_var_string (ptr_item, "area_name2", key->area_name[1]))
return 0;
if (!infolist_new_var_string (ptr_item, "area_key", key->area_key))
return 0;
if (!infolist_new_var_string (ptr_item, "command", key->command))
return 0;
if (!infolist_new_var_integer (ptr_item, "score", key->score))
return 0;
return 1;
}
/*
* gui_key_print_log: print a key info in log (usually for crash dump)
*/
void
gui_key_print_log_key (struct t_gui_key *key, const char *prefix)
{
int area;
log_printf ("%s[key (addr:0x%lx)]", prefix, key);
log_printf ("%s key. . . . . . . . : '%s'", prefix, key->key);
for (area = 0; area < 2; area++)
{
log_printf ("%s area_type[%d] . . . : %d ('%s')",
prefix, area, key->area_type[area],
gui_key_focus_string[key->area_type[area]]);
log_printf ("%s area_name[%d] . . . : '%s'",
prefix, area, key->area_name[area]);
}
log_printf ("%s area_key . . . . . : '%s'", prefix, key->area_key);
log_printf ("%s command. . . . . . : '%s'", prefix, key->command);
log_printf ("%s score. . . . . . . : %d", prefix, key->score);
log_printf ("%s prev_key . . . . . : 0x%lx", prefix, key->prev_key);
log_printf ("%s next_key . . . . . : 0x%lx", prefix, key->next_key);
}
/*
* gui_key_print_log: print key infos in log (usually for crash dump)
*/
void
gui_key_print_log (struct t_gui_buffer *buffer)
{
struct t_gui_key *ptr_key;
int i;
if (buffer)
{
log_printf (" keys . . . . . . . . : 0x%lx", buffer->keys);
log_printf (" last_key . . . . . . : 0x%lx", buffer->last_key);
log_printf (" keys_count . . . . . : %d", buffer->keys_count);
for (ptr_key = buffer->keys; ptr_key; ptr_key = ptr_key->next_key)
{
log_printf ("");
gui_key_print_log_key (ptr_key, " ");
}
}
else
{
for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
{
log_printf ("[keys for context: %s]", gui_key_context_string[i]);
log_printf ("");
log_printf (" keys . . . . . . . . : 0x%lx", gui_keys[i]);
log_printf (" last_key . . . . . . : 0x%lx", last_gui_key[i]);
log_printf (" keys_count . . . . . : %d", gui_keys_count[i]);
for (ptr_key = gui_keys[i]; ptr_key; ptr_key = ptr_key->next_key)
{
log_printf ("");
gui_key_print_log_key (ptr_key, "");
}
}
}
}
|