Script: coords.pl

Select text, nicks or URLs on screen with mouse or keyboard.
Author: Nei — Version: 0.7.3.2 — License: GPL3
For WeeChat ≥ 0.4.2.
Tags: url, mouse
Added: 2012-11-26 — Updated: 2018-09-18

Download GitHub Repository

   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
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
use strict; use warnings;
$INC{'Encode/ConfigLocal.pm'}=1;
require Encode;

# coords.pl is written by Nei <anti.teamidiot.de>
# and licensed under the under GNU General Public License v3
# or any later version

# to read the following docs, you can use "perldoc coords.pl"

=head1 NAME

coords - weechat script to map screen coordinates (weechat edition)

=head1 SYNOPSIS

first, copy the file to your
F<.weechat/perl> directory. Then you can type

  /script load coords.pl

in weechat to load the script. Use

  /coords

to open the coords screen, or conveniently

  /key bind meta-/ /coords /

to open it with the Alt+/ keybinding.

=head1 DESCRIPTION

coords will hilight links, allow text selection and tries to send the
selection to xterm

=head1 SETUP

if you would like urls to be copied into the selection clipboard, add

  xterm*disallowedWindowOps:20,21,SetXprop

to your F<.Xresources> file for B<xterm>.

For B<rxvt-unicode>, get the F<osc-xterm-clipboard> script from
L<http://anti.teamidiot.de/static/nei/*/Code/urxvt/>, install it to
your F<perl-lib> directory and add it to your F<.Xresources> like such:

  URxvt.perl-ext-common: default,osc-xterm-clipboard

=head1 USAGE

to open the url overlay on a window, type C</coords> or use the
keybinding you created as explained in the L</SYNOPSIS>.

=head2 Selection Mode

by default, the copy window will be in selection mode. you can move
the text cursor with the arrow keys and open a selection with the
Space key. The selection content will be transfered into clipboard.

=head2 URL Mode

to switch between selection mode and URL mode, use the C</> key or
type the command C</coords /> to directly start in URL mode.

inside the overlay, you can use Arrow-Up and Arrow-Down keys to select
URLs. This script will try to copy them into your selection clipboard
(see L</SETUP>) so you should be able to open the selected link by
clicking the middle mouse button in your browser.

once you click or hit enter, an url open signal will be sent to
WeeChat. Use an appropriate script such as F<urlopener.pl> from
L<http://anti.teamidiot.de/static/nei/*/Code/WeeChat/> if you would
like to associate this with some application (web browser etc.)

to leave the overlay, hit the C<q> key.

for mouse support, this script will listen to mouse input
signals. Another script is needed to supply these signals, such as
F<mouse.pl> which can be found in the same place as F<urlopener.pl>
and this script.

=head1 CAVEATS

=over

=item *

WeeChat does not allow for visual feedback during mouse operation
unless you patch it and use the F<mouse.pl> or F<mouse_var.pl> script
instead.

=item *

double-click word select does not work in an unpatched WeeChat for the
same reason -- unless you set a very high close_on_release timeout (with
the added inconvenience).

=item *

unfortunately, WeeChat scrolls back a buffer to the end when you
switch to any other buffer, including the copy window
overlay. F<coords.pl> will work together with the F<keepscroll.pl>
script to try and remedy this a little bit, but the perfect scrolling
position cannot be restored due to internal limitations. (no longer
the case in recent versions of WeeChat.)

=item *

whether other terminal emulators support selection storage, depends on
how well they emulate B<xterm>. Set C<xterm_compatible> to your
I<$TERM> if you think it does.

=item *

B<xterm> will not allow selection storage unless you enable it in the
Xresources as described in L</SETUP>

=item *

B<rxvt-unicode> will need a script to handle the selection operating
system control. querying the selection is only supported if the
selection is rxvt-unicodeE<apos>s (not needed for this script)

=item *

GNU B<screen> will not allow to pass through the operating system
control needed for selection storage to the underlying term, try
B<tmux> instead (L<http://tmux.sourceforge.net/>)

=item *

it would be possible to provide a remote clipboard, but that will
require a clipboard server. Also see B<xsel>, B<xclip>, command mode

=back

=head1 TODO

=over

=item *

set the text input cursor on click in the input bar

=back

=head1 BUGS

=over

=item *

my local B<xterm> exhibits a bug where the selection is only copied if
you I<click> into the xterm window first. still trying to figure this
one out...

=item *

missing handling of scroll_beyond_end feature in weechat

=item *

broken handling of Day Changed messages in newer weechat

=item *

possibly more...

=back

=head1 SETTINGS

the settings are usually found in the

  plugins.var.perl.coords

namespace, that is, type

  /set plugins.var.perl.coords.*

to see them and

  /set plugins.var.perl.coords.SETTINGNAME VALUE

to change a setting C<SETTINGNAME> to a new value C<VALUE>. Finally,

  /unset plugins.var.perl.coords.SETTINGNAME

will reset a setting to its default value.

the following settings are available:

=head2 url_regex

a regular expression to identify URLs in the text. See L<perlre> for
more information about Perl regular expressions.

=head2 url_braces

parenthesis-like characters which nest and should be excluded when
found around an URL. make sure the variable setting nests properly
when modifying this.

=head2 url_non_endings

this is matched against the end of a link and removed

=head2 hyper_nicks

make nicks to hyperlinks for menu/pm

=head2 hyper_channels

make channels to hyperlinks for join

=head2 hyper_show

set to types of hyperlinks that are shown by default

=head2 use_nick_menu

use nick menu when opening nick hyperlink (see I<hyper_nicks>,
requires menu.pl script). otherwise open private message. this setting
only applies to text mode selection, for mouse see
I<mouse.nick_2nd_click>

=head2 color.url_highlight

the weechat color and/or attribute to be used for highlighting URLs in
the copy window. seperate multiple attributes with C<.>

=head2 color.url_highlight_active

the same as I<color.url_highlight> except for the currently (using
arrow keys) selected link.

=head2 color.selection_cursor

the weechat color and/or attribute to be used for the text cursor.

=head2 color.selection

the color of the currently selected text in selection mode

=head2 copybuf_short_name

short_name to use for coords buffer. it is set to the copy sign by
default to not disturb buffers bar width, set to the empty string to
have window position and size shown

=head2 mouse.copy_on_click

set to on if it should be possible to directly click on URLs and
select text, set to off if mouse should only work in open coords
buffer

=head2 mouse.close_on_release

set to on or a delay (in ms) to autoclose coords buffer opened by
I<copy_on_click> on button release, set to off if the coords buffer
should stay open after click

=head2 mouse.click_select_pane

set to on to use the mouse to select windows

=head2 mouse.click_through_pane

set to on if I<copy_on_click> should work on inactive windows (works
only if I<click_select_pane> is set too). set to off if window needs
to be active

=head2 mouse.url_open_2nd_click

if this is set, URLs are only opened when clicked twice (in the same
incarnation of a coords buffer) instead of on first click. it can be set to
a delay (in ms) that will be added to the I<close_on_release> delay if
the script is waiting for a second click on the URL to happen

=head2 mouse.handle_scroll

set to on if coords should handle scrolling inside windows. the script
will try to guess non-chat areas to be nicklist, top to be title and
bottom to be status and scroll the respective bars if the cursor is in
that area. set to off if scrolling should be handled by the default
F<mouse.pl> script or another mouse scrolling script

=head2 mouse.scroll_inactive_pane

set to on if inactive windows should be scrolled instead of active
window if the mouse cursor is over it (requires I<handle_scroll> to be
enabled)

=head2 clipboard_command

if you set this, an external program may be executed to store the
selection or URL. begin with C<|> to pipe into program or use
parameters C<%s> for text, C<%q> for quoted text or C<%x> for quoted
escape sequence.

=head2 copywin_custom_keys

You can define custom key bindings to use inside the copywin here. syntax is:
command-letter:weechat-keycode. available commands: -+>< (up/down/left/right)
fbae (forward word/backward word/beginning/end) !@ (open/start selection)
/UNCunc (toggle highlights/urls/nicks/channels) q (close window)

=head1 FUNCTION DESCRIPTION

for full pod documentation, filter this script with

  perl -pE'
  (s/^## (.*?) -- (.*)/=head2 $1\n\n$2\n\n=over\n/ and $o=1) or
   s/^## (.*?) - (.*)/=item I<$1>\n\n$2\n/ or
  (s/^## (.*)/=back\n\n$1\n\n=cut\n/ and $o=0,1) or
  ($o and $o=0,1 and s/^sub /=back\n\n=cut\n\nsub /)'

=cut

use MIME::Base64;

use constant SCRIPT_NAME => 'coords';
weechat::register(SCRIPT_NAME, 'Nei <anti.teamidiot.de>', '0.7.3.2', 'GPL3', 'copy text and urls', 'stop_coords', '') || return;
sub SCRIPT_FILE() {
	my $infolistptr = weechat::infolist_get('perl_script', '', SCRIPT_NAME);
	my $filename = weechat::infolist_string($infolistptr, 'filename') if weechat::infolist_next($infolistptr);
	weechat::infolist_free($infolistptr);
	return $filename unless @_;
}

{
package Nlib;
# this is a weechat perl library
use strict; use warnings; no warnings 'redefine';

## i2h -- copy weechat infolist content into perl hash
## $infolist - name of the infolist in weechat
## $ptr - pointer argument (infolist dependend)
## @args - arguments to the infolist (list dependend)
## $fields - string of ref type "fields" if only certain keys are needed (optional)
## returns perl list with perl hashes for each infolist entry
sub i2h {
	my %i2htm = (i => 'integer', s => 'string', p => 'pointer', b => 'buffer', t => 'time');
	local *weechat::infolist_buffer = sub { '(not implemented)' };
	my ($infolist, $ptr, @args) = @_;
	$ptr ||= "";
	my $fields = ref $args[-1] eq 'fields' ? ${ pop @args } : undef;
	my $infptr = weechat::infolist_get($infolist, $ptr, do { local $" = ','; "@args" });
	my @infolist;
	while (weechat::infolist_next($infptr)) {
		my @fields = map {
			my ($t, $v) = split ':', $_, 2;
			bless \$v, $i2htm{$t};
		}
		split ',',
			($fields || weechat::infolist_fields($infptr));
		push @infolist, +{ do {
			my (%list, %local, @local);
			map {
				my $fn = 'weechat::infolist_'.ref $_;
				my $r = do { no strict 'refs'; &$fn($infptr, $$_) };
				if ($$_ =~ /^localvar_name_(\d+)$/) {
					$local[$1] = $r;
					()
				}
				elsif ($$_ =~ /^(localvar)_value_(\d+)$/) {
					$local{$local[$2]} = $r;
					$1 => \%local
				}
				elsif ($$_ =~ /(.*?)((?:_\d+)+)$/) {
					my ($key, $idx) = ($1, $2);
					my @idx = split '_', $idx; shift @idx;
					my $target = \$list{$key};
					for my $x (@idx) {
						my $o = 1;
						if ($key eq 'key' or $key eq 'key_command') {
							$o = 0;
						}
						if ($x-$o < 0) {
							local $" = '|';
							weechat::print('',"list error: $target/$$_/$key/$x/$idx/@idx(@_)");
							$o = 0;
						}
						$target = \$$target->[$x-$o]
					}
					$$target = $r;

					my $code = qq{
						local \$[=1;
						\$list{"\Q$key\E"}$idx = \$r
					};
					$key => $list{$key}
				}
				else {
					$$_ => $r
				}
			} @fields
		} };
	}
	weechat::infolist_free($infptr);
	!wantarray && @infolist ? \@infolist : @infolist
}

## hdh -- hdata helper
sub hdh {
	if (@_ > 1 && $_[0] !~ /^0x/ && $_[0] !~ /^\d+$/) {
		my $arg = shift;
		unshift @_, weechat::hdata_get_list(weechat::hdata_get($_[0]), $arg);
	}
	while (@_ > 2) {
		my ($arg, $name, $var) = splice @_, 0, 3;
		my $hdata = weechat::hdata_get($name);

		$var =~ s/!(.*)/weechat::hdata_get_string($hdata, $1)/e;
		(my $plain_var = $var) =~ s/^\d+\|//;
		my $type = weechat::hdata_get_var_type_string($hdata, $plain_var);
		if ($type eq 'pointer') {
			my $name = weechat::hdata_get_var_hdata($hdata, $var);
			unshift @_, $name if $name;
		}
		if ($type eq 'shared_string') {
			$type =~ s/shared_//;
		}

		my $fn = "weechat::hdata_$type";
		unshift @_, do { no strict 'refs';
						 &$fn($hdata, $arg, $var) };
	}
	wantarray ? @_ : $_[0]
}

## l2l -- copy weechat list into perl list
## $ptr - weechat list pointer
## $clear - if true, clear weechat list
## returns perl list
sub l2l {
	my ($ptr, $clear) = @_;
	my $itemptr = weechat::list_get($ptr, 0);
	my @list;
	while ($itemptr) {
		push @list, weechat::list_string($itemptr);
		$itemptr = weechat::list_next($itemptr);
	}
	weechat::list_remove_all($ptr) if $clear;
	@list
}

## find_bar_window -- find the bar window where the coordinates belong to
## $row - row
## $col - column
## returns bar window infolist and bar infolist in a array ref if found
sub find_bar_window {
	my ($row, $col) = @_;

	my $barwinptr;
	my $bar_info;
	for (i2h('bar_window')) {
		return [ $_, $bar_info ] if
			$row > $_->{'y'} && $row <= $_->{'y'}+$_->{'height'} &&
				$col > $_->{'x'} && $col <= $_->{'x'}+$_->{'width'} &&
					(($bar_info)=i2h('bar', $_->{'bar'})) && !$bar_info->{'hidden'};
	}
	
}

## in_window -- check if given coordinates are in a window
## $row - row
## $col - column
## $wininfo - infolist of window to check
## returns true if in window
sub in_window {
	my ($row, $col, $wininfo) = @_;

	# in window?
	$row > $wininfo->{'y'} &&
		$row <= $wininfo->{'y'}+$wininfo->{'height'} &&
			$col > $wininfo->{'x'} &&
				$col <= $wininfo->{'x'}+$wininfo->{'width'}
}

## in_chat_window -- check if given coordinates are in the chat part of a window
## $row - row
## $col - column
## $wininfo - infolist of window to check
## returns true if in chat part of window
sub in_chat_window {
	my ($row, $col, $wininfo) = @_;

	# in chat window?
	$row > $wininfo->{'chat_y'} &&
		$row <= $wininfo->{'chat_y'}+$wininfo->{'chat_height'} &&
			$col > $wininfo->{'chat_x'} &&
				$col <= $wininfo->{'chat_x'}+$wininfo->{'chat_width'}
}

## has_true_value -- some constants for "true"
## $v - value string
## returns true if string looks like a true thing
sub has_true_value {
	my $v = shift || '';
	$v =~ /^(?:on|yes|y|true|t|1)$/i
}

## has_false_value -- some constants for "false"
## $v - value string
## returns true if string looks like a B<false> thing
sub has_false_value {
	my $v = shift || '';
	$v =~ /^(?:off|no|n|false|f|0)?$/i
}

## bar_filling -- get current filling according to position
## $bar_infos - info about bar (from find_bar_window)
## returns filling as an integer number
sub bar_filling {
	my ($bar_infos) = @_;
	($bar_infos->[-1]{'position'} <= 1 ? $bar_infos->[-1]{'filling_top_bottom'}
	 : $bar_infos->[-1]{'filling_left_right'})
}

sub fu8on(@) {
	Encode::_utf8_on($_) for @_; wantarray ? @_ : shift
}

sub screen_length($) {
	weechat::strlen_screen($_[0])
}

## bar_column_max_length -- get max item length for column based filling
## $bar_infos - info about bar (from find_bar_window)
## returns max item length
sub bar_column_max_length {
	my ($bar_infos) = @_;
	my @items;
	for (@{ $bar_infos->[0]{'items_content'} }) {
		push @items, split "\n", join "\n", @$_;
	}
	my $max_length = 0;
	for (@items) {
		my $item_length = screen_length fu8on weechat::string_remove_color($_, '');
		$max_length = $item_length if $max_length < $item_length;
	}
	$max_length;
}

## find_bar_item_pos -- get position of an item in a bar structure
## $bar_infos - instance and general info about bar (from find_bar_window)
## $search - search pattern for item name
## returns (outer position, inner position, true if found)
sub find_bar_item_pos {
	my ($bar_infos, $search) = @_;
	my $item_pos_a = 0;
	my $item_pos_b;
	for (@{ $bar_infos->[-1]{'items_array'} }) {
		$item_pos_b = 0;
		for (@$_) {
			return ($item_pos_a, $item_pos_b, 1)
				if $_ =~ $search;
			++$item_pos_b;
		}
		++$item_pos_a;
	}
	(undef, undef, undef)
}

## bar_line_wrap_horiz -- apply linebreak for horizontal bar filling
## $prefix_col_r - reference to column counter
## $prefix_y_r - reference to row counter
## $bar_infos - info about bar (from find_bar_window)
sub bar_line_wrap_horiz {
	my ($prefix_col_r, $prefix_y_r, $bar_infos) = @_;
	while ($$prefix_col_r > $bar_infos->[0]{'width'}) {
		++$$prefix_y_r;
		$$prefix_col_r -= $bar_infos->[0]{'width'};
	}
}

## bar_lines_column_vert -- count lines in column layout
## $bar_infos - info about bar (from find_bar_window)
## returns lines needed for columns_horizontal layout
sub bar_lines_column_vert {
	my ($bar_infos) = @_;
	my @items;
	for (@{ $bar_infos->[0]{'items_content'} }) {
		push @items, split "\n", join "\n", @$_;
	}
	my $max_length = bar_column_max_length($bar_infos);
	my $dummy_col = 1;
	my $lines = 1;
	for (@items) {
		if ($dummy_col+$max_length > 1+$bar_infos->[0]{'width'}) {
			++$lines;
			$dummy_col = 1;
		}
		$dummy_col += 1+$max_length;
	}
	$lines;
}

## bar_items_skip_to -- skip several bar items on search for subitem position
## $bar_infos - info about bar (from find_bar_window)
## $search - patter of item to skip to
## $col - pointer column
## $row - pointer row
sub bar_items_skip_to {
	my ($bar_infos, $search, $col, $row) = @_;
	$col += $bar_infos->[0]{'scroll_x'};
	$row += $bar_infos->[0]{'scroll_y'};
	my ($item_pos_a, $item_pos_b, $found) = 
		find_bar_item_pos($bar_infos, $search);

	return 'item position not found' unless $found;

	# extract items to skip
	my $item_join = 
		(bar_filling($bar_infos) <= 1 ? '' : "\n");
	my @prefix;
	for (my $i = 0; $i < $item_pos_a; ++$i) {
		push @prefix, split "\n", join $item_join, @{ $bar_infos->[0]{'items_content'}[$i] };
	}
	push @prefix, split "\n", join $item_join, @{ $bar_infos->[0]{'items_content'}[$item_pos_a] }[0..$item_pos_b-1] if $item_pos_b;

	# cursor
	my $prefix_col = 1;
	my $prefix_y = 1;
	my $item_max_length;
	my $col_vert_lines;

	# forward cursor
	if (!bar_filling($bar_infos)) {
		my $prefix = join ' ', @prefix;
		$prefix_col += screen_length fu8on weechat::string_remove_color($prefix, '');
		++$prefix_col if @prefix && !$item_pos_b;
		bar_line_wrap_horiz(\($prefix_col, $prefix_y), $bar_infos);
	}
	elsif (bar_filling($bar_infos) == 1) {
		$prefix_y += @prefix;
		if ($item_pos_b) {
			--$prefix_y;
			$prefix_col += screen_length fu8on weechat::string_remove_color($prefix[-1], '');
		}
	}
	elsif (bar_filling($bar_infos) == 2) {
		$item_max_length = bar_column_max_length($bar_infos);
		for (@prefix) {
			$prefix_col += 1+$item_max_length;
			if ($prefix_col+$item_max_length > 1+$bar_infos->[0]{'width'}) {
				++$prefix_y;
				$prefix_col = 1;
			}
		}
	}
	elsif (bar_filling($bar_infos) == 3) {
		$item_max_length = bar_column_max_length($bar_infos);
		$col_vert_lines = $bar_infos->[-1]{'position'} <= 1 ? bar_lines_column_vert($bar_infos) : $bar_infos->[0]{'height'};
		my $pfx_idx = 0;
		for (@prefix) {
			$prefix_y = 1+($pfx_idx % $col_vert_lines);
			$prefix_col = 1+(1+$item_max_length)*(int($pfx_idx / $col_vert_lines)+1);
			return 'in prefix'
				if ($prefix_y == $row && $prefix_col > $col);
			++$pfx_idx;
		}
		$prefix_y = 1+(@prefix % $col_vert_lines);
		$prefix_col = 1+(1+$item_max_length)*int(@prefix / $col_vert_lines);
	}

	(undef,
	 $item_pos_a, $item_pos_b,
	 $prefix_col, $prefix_y,
	 (scalar @prefix),
	 $item_max_length, $col_vert_lines)
}

## bar_item_get_subitem_at -- extract subitem from a bar item at given coords
## $bar_infos - info about bar
## $search - search pattern for item whose subitems to get
## $col - pointer column
## $row - pointer row
## returns error message, subitem index, subitem text
sub bar_item_get_subitem_at {
	my ($bar_infos, $search, $col, $row) = @_;

	my ($error,
		$item_pos_a, $item_pos_b,
		$prefix_col, $prefix_y,
		$prefix_cnt,
		$item_max_length, $col_vert_lines) = 
			bar_items_skip_to($bar_infos, $search, $col, $row);

	$col += $bar_infos->[0]{'scroll_x'};
	$row += $bar_infos->[0]{'scroll_y'};

	return $error if $error;
	
	return 'no viable position'
		unless (($row == $prefix_y  && $col >= $prefix_col) || $row > $prefix_y || bar_filling($bar_infos) >= 3);

	my @subitems = split "\n", $bar_infos->[0]{'items_content'}[$item_pos_a][$item_pos_b];
	my $idx = 0;
	for (@subitems) {
		my ($beg_col, $beg_y) = ($prefix_col, $prefix_y);
		$prefix_col += screen_length fu8on weechat::string_remove_color($_, '');
		if (!bar_filling($bar_infos)) {
			bar_line_wrap_horiz(\($prefix_col, $prefix_y), $bar_infos);
		}

		return (undef, $idx, $_, [$beg_col, $col, $prefix_col, $beg_y, $row, $prefix_y])
			if (($prefix_col > $col && $row == $prefix_y) || ($row < $prefix_y && bar_filling($bar_infos) < 3));

		++$idx;

		if (!bar_filling($bar_infos)) {
			++$prefix_col;
			return ('outside', $idx-1, $_)
				if ($prefix_y == $row && $prefix_col > $col);
		}
		elsif (bar_filling($bar_infos) == 1) {
			return ('outside', $idx-1, $_)
				if ($prefix_y == $row && $col >= $prefix_col);
			++$prefix_y;
			$prefix_col = 1;
		}
		elsif (bar_filling($bar_infos) == 2) {
			$prefix_col += 1+$item_max_length-(($prefix_col-1)%($item_max_length+1));

			return ('outside', $idx-1, $_)
				if ($prefix_y == $row && $prefix_col > $col);

			if ($prefix_col+$item_max_length > 1+$bar_infos->[0]{'width'}) {
				return ('outside item', $idx-1, $_)
					if ($prefix_y == $row && $col >= $prefix_col);
				
				++$prefix_y;
				$prefix_col = 1;
			}
		}
		elsif (bar_filling($bar_infos) == 3) {
			$prefix_col += 1+$item_max_length-(($prefix_col-1)%($item_max_length+1));
			return ('outside', $idx-1, $_)
				if ($prefix_y == $row && $prefix_col > $col);
			$prefix_y = 1+(($idx+$prefix_cnt) % $col_vert_lines);
			$prefix_col = 1+(1+$item_max_length)*int(($idx+$prefix_cnt) / $col_vert_lines);

		}
	}
	'not found';
}

use Pod::Select qw();
use Pod::Simple::TextContent;

## get_desc_from_pod -- return setting description from pod documentation
## $file - filename with pod
## $setting - name of setting
## returns description as text
sub get_desc_from_pod {
	my $file = shift;
	return unless -s $file;
	my $setting = shift;

	open my $pod_sel, '>', \my $ss;
	Pod::Select::podselect({
	   -output => $pod_sel,
	   -sections => ["SETTINGS/$setting"]}, $file);

	my $pt = new Pod::Simple::TextContent;
	$pt->output_string(\my $ss_f);
	$pt->parse_string_document($ss);

	my ($res) = $ss_f =~ /^\s*\Q$setting\E\s+(.*)\s*/;
	$res
}

## get_settings_from_pod -- retrieve all settings in settings section of pod
## $file - file with pod
## returns list of all settings
sub get_settings_from_pod {
	my $file = shift;
	return unless -s $file;

	open my $pod_sel, '>', \my $ss;
	Pod::Select::podselect({
	   -output => $pod_sel,
	   -sections => ["SETTINGS//!.+"]}, $file);

	$ss =~ /^=head2\s+(.*)\s*$/mg
}

## mangle_man_for_wee -- turn man output into weechat codes
sub mangle_man_for_wee {
	for (@_) {
		s/_\x08(.)/weechat::color('underline').$1.weechat::color('-underline')/ge;
		s/(.)\x08\1/weechat::color('bold').$1.weechat::color('-bold')/ge;
	}
	wantarray ? @_ : $_[0]
}

## read_manpage -- read a man page in weechat window
## $file - file with pod
## $name - buffer name
sub read_manpage {
	my $caller_package = (caller)[0];
	my $file = shift;
	my $name = shift;

	if (my $obuf = weechat::buffer_search('perl', "man $name")) {
		eval qq{
			package $caller_package;
			weechat::buffer_close(\$obuf);
		};
	}

	my @wee_keys = Nlib::i2h('key');
	my @keys;

	my $winptr = weechat::current_window();
	my ($wininfo) = Nlib::i2h('window', $winptr);
	my $buf = weechat::buffer_new("man $name", '', '', '', '');
	return weechat::WEECHAT_RC_OK unless $buf;

	my $width = $wininfo->{'chat_width'};
	--$width if $wininfo->{'chat_width'} < $wininfo->{'width'} || ($wininfo->{'width_pct'} < 100 && (grep { $_->{'y'} == $wininfo->{'y'} } Nlib::i2h('window'))[-1]{'x'} > $wininfo->{'x'});

	weechat::buffer_set($buf, 'time_for_each_line', 0);
	eval qq{
		package $caller_package;
		weechat::buffer_set(\$buf, 'display', 'auto');
	};
	die $@ if $@;

	@keys = map { $_->{'key'} }
		grep { $_->{'command'} eq '/input history_previous' ||
			   $_->{'command'} eq '/input history_global_previous' } @wee_keys;
	@keys = 'meta2-A' unless @keys;
	weechat::buffer_set($buf, "key_bind_$_", '/window scroll -1') for @keys;

	@keys = map { $_->{'key'} }
		grep { $_->{'command'} eq '/input history_next' ||
			   $_->{'command'} eq '/input history_global_next' } @wee_keys;
	@keys = 'meta2-B' unless @keys;
	weechat::buffer_set($buf, "key_bind_$_", '/window scroll +1') for @keys;

	weechat::buffer_set($buf, 'key_bind_ ', '/window page_down');

	@keys = map { $_->{'key'} }
		grep { $_->{'command'} eq '/input delete_previous_char' } @wee_keys;
	@keys = ('ctrl-?', 'ctrl-H') unless @keys;
	weechat::buffer_set($buf, "key_bind_$_", '/window page_up') for @keys;

	weechat::buffer_set($buf, 'key_bind_g', '/window scroll_top');
	weechat::buffer_set($buf, 'key_bind_G', '/window scroll_bottom');

	weechat::buffer_set($buf, 'key_bind_q', '/buffer close');

	weechat::print($buf, " \t".mangle_man_for_wee($_))
			for `pod2man \Q$file\E 2>/dev/null | GROFF_NO_SGR=1 nroff -mandoc -rLL=${width}n -rLT=${width}n -Tutf8 2>/dev/null`;
	weechat::command($buf, '/window scroll_top');

	unless (hdh($buf, 'buffer', 'lines', 'lines_count') > 0) {
		weechat::print($buf, weechat::prefix('error').$_)
				for "Unfortunately, your @{[weechat::color('underline')]}nroff".
					"@{[weechat::color('-underline')]} command did not produce".
					" any output.",
					"Working pod2man and nroff commands are required for the ".
					"help viewer to work.",
					"In the meantime, please use the command ", '',
					"\tperldoc $file", '',
					"on your shell instead in order to read the manual.",
					"Thank you and sorry for the inconvenience."
	}
}

1
}

use constant CMD_COPYWIN => SCRIPT_NAME;
weechat::hook_command(CMD_COPYWIN, 'copy active window for hyperlink operations or free selection of text',
					  '[/] || url nicks channels',
					  'if any or more arguments are given, go into hyperlink mode and set this filter.'."\n".
						  'in the title bar of opened copy window, the possible key bindings are displayed.'."\n".
							  'use '.weechat::color('bold').'/'.CMD_COPYWIN.' help'.weechat::color('-bold').
								  ' to read the manual',
					  '|| / %- || url %- || nicks %- || channels %- || url,nicks %- || url,channels %- '.
						  '|| url,nicks,channels %- || nicks,channels %- || help',
					  'copywin_cmd', '');
weechat::hook_signal('buffer_closed', 'garbage_str', '');
weechat::hook_signal('upgrade', 'close_copywin', '');
weechat::hook_config('plugins.var.perl.'.SCRIPT_NAME.'.*', 'default_options', '');
weechat::hook_signal('mouse', 'mouse_evt', '');
weechat::hook_signal('input_flow_free', 'binding_mouse_fix', '');
weechat::hook_modifier('input_text_display_with_cursor', 'input_text_hlsel', '');
# is there builtin mouse support?
weechat::hook_hsignal(SCRIPT_NAME, 'hsignal_evt', '');
weechat::key_bind('mouse', +{
	map { $_ => 'hsignal:'.SCRIPT_NAME }
	'@chat:button1*', '@chat:button1-event-*', '@chat(perl.[*):button1'
});
weechat::command('', '/alias add copywin '.CMD_COPYWIN)
	if 'copywin' ne CMD_COPYWIN && !Nlib::i2h('alias', '', 'copywin') && Nlib::i2h('hook', '', 'command,alias');

# downloaded line fields
use constant TRACE => -1;
use constant LINE => 0;
use constant LAYOUT => 1;

# URLS structure fields
use constant MSG => 0;
use constant URL_S => 1;
use constant URL => 2;
use constant URL_E => 3;
use constant URL_LINE => 4;
use constant URL_INFO => 5;

# captured control codes fields
use constant RES => 0;
use constant POS_S => 1;
use constant POS_E => 2;

# trace weelist container
our $listptr;

# global storage & fields
our %STR;
use constant BUFPTR => 0;
use constant LINES => 1;
use constant WINDOWS => 2;
use constant A_LINK => 3;
use constant URLS => 4;
use constant MODE => 5;
use constant CUR => 6;
use constant MOUSE_AUTOMODE => 7;
use constant URL_TYPE_FILTER => 8;
# currently active storage key
our $ACT_STR;

our $last_mouse_seq;
our $script_old_mouse_scroll;
our $mouse_2nd_click;
our $autoclose_in_progress;
our $drag_speed_timer;
our $delayed_nick_menu_timer;

our $hsignal_mouse_down_sent;

our $input_sel;

our $LAYOUT_OK;

my @SIMPLE_MODE_KEYS = ('/', 'U', 'N', 'C', 'P', 'u', 'n', 'c', 'p');

init_coords();

## hdh_get_lineinfo -- get lineinfo from hdata
## $_[0] - line pointer
## $_[1] - 'line' (automatic when using result from Nlib::hdh)
## returns lineinfo hashref
sub hdh_get_lineinfo {
	my @line_data = Nlib::hdh(@_, 'data');
	+{
		next_line => scalar Nlib::hdh(@_, 'next_line'),
		(map { $_ => scalar Nlib::hdh(@line_data,  $_) }
			qw(displayed message prefix str_time buffer date highlight)),
		tag => [ map { Nlib::hdh(@line_data, "$_|tags_array") }
					 0 .. Nlib::hdh(@line_data, 'tags_count')-1 ]
	   }
}

sub screen_length($) {
	weechat::strlen_screen($_[0])
}

sub fu8on(@) {
	Encode::_utf8_on($_) for @_; wantarray ? @_ : shift
}

## calculate_trace -- create a trace profile witgh word splits
## $lineinfo - lineinfo with message to break into lines
## $wininfo - window info hash with chat_width
## $base_trace - ref to base trace with # fields
## $prev_lineinfo - lineinfo of previous message for prefix erasure
## returns (layoutinfo hashref, partial trace arrayref)
sub calculate_trace {
	# b : pos // break between words
	# n : pos // new line
	# x : pos // cut word
	# t : x # j // time
	# u : x # j // buffer name
	# p : x # j // prefix (nick etc.)
	# q : x # j // separator
	# q : x . line
	my ($lineinfo, $wininfo, $base_trace, $prev_lineinfo) = @_;
	my $msg = fu8on $lineinfo->{'message'};
	my $layoutinfo = +{ count => 1 };
	my @trace;
	my $show_time = $base_trace->[1];
	my $no_prefix_align;
	if ($base_trace->[-1] < 0) {
		$no_prefix_align = $base_trace->[5];
		$base_trace->[5] = $base_trace->[-1] = 0;
		my $prefix = $lineinfo->{'prefix'};
		if (defined $prefix) {
			
			my ($nick_tag) = grep { /^nick_/ } @{$lineinfo->{'tag'}||[]};
			my ($pnick_tag) = grep { /^nick_/ } @{$prev_lineinfo->{'tag'}||[]};
			if ($nick_tag && $pnick_tag && $nick_tag eq $pnick_tag && !$lineinfo->{'highlight'} &&
					(grep { /_action$/ && !/^nick_/ } @{$lineinfo->{'tag'}||[]}) == 0 &&
					$prev_lineinfo && $lineinfo->{'prefix'} eq $prev_lineinfo->{'prefix'}) {
				my $repl = weechat::config_string(weechat::config_get(
					'weechat.look.prefix_same_nick'));
				$prefix = $repl if length $repl;
				$prefix = '' if $repl eq ' ';
			}
			$base_trace->[5] = (sort { $a <=> $b } #($no_prefix_align?$no_prefix_align+1:0),
									screen_length fu8on weechat::string_remove_color($prefix, ''))[0];
		}
	}
	$base_trace->[1] = $show_time ? screen_length fu8on weechat::string_remove_color($lineinfo->{'str_time'}, '') : 0;
	for (my ($i, $pos) = (0, 0); $i < @$base_trace; $i+=2) {
		$pos += $base_trace->[$i+1] + 1 if $base_trace->[$i+1];
		push @trace, $base_trace->[$i] . ':' . $pos . '#' if $base_trace->[$i+1];
	}
	my ($ctrl, $plain_msg) = capture_color_codes($msg);
	my @words = split /(\s+)/, $plain_msg;
	my $screen = 0;
	if ($base_trace->[-1]) {
		($screen) = ((trace_cond('q', [\@trace], 0))[0] =~ /:(\d+)/);
	}
	elsif ($base_trace->[5]) {
		($screen) = ((trace_cond('p', [\@trace], 0))[0] =~ /:(\d+)/);
	}
	elsif ($base_trace->[3]) {
		($screen) = ((trace_cond('u', [\@trace], 0))[0] =~ /:(\d+)/);
	}
	elsif ($base_trace->[1]) {
		($screen) = ((trace_cond('t', [\@trace], 0))[0] =~ /:(\d+)/);
	}
	my $new_screen = 0;
	my $eol_pos = weechat::config_string(weechat::config_get('weechat.look.align_end_of_lines'));
	unless ($eol_pos eq 'time') {
		($new_screen) = ((trace_cond('t', [\@trace], 0))[0] =~ /:(\d+)/)
			if $base_trace->[1];
		unless ($eol_pos eq 'buffer') {
			($new_screen) = ((trace_cond('u', [\@trace], 0))[0] =~ /:(\d+)/)
				if $base_trace->[3];
			unless ($eol_pos eq 'prefix') {
				($new_screen) = ((trace_cond('p', [\@trace], 0))[0] =~ /:(\d+)/)
					if $base_trace->[5];
				unless ($eol_pos eq 'suffix') {
					$new_screen = $screen;
				}
			}
		}
	}
	unless ($lineinfo->{'date'}) {
		$screen = $new_screen = 0;
		@trace = ();
		if ($base_trace->[3]) {
			$screen = $base_trace->[3] + 1;
			push @trace, "u:$screen#";
		}
	}
	$base_trace->[1] = $show_time;
	if (defined $no_prefix_align) {
		$base_trace->[5] = $no_prefix_align;
		$base_trace->[-1] = -1;
	}

	# XXX missing special case:  $wininfo->{'chat_width'} - $screen < 4
	# `--> ignore all line break rules, just X every screenful
	my $pos = 0;
	my $width = $wininfo->{'chat_width'};
	--$width if $wininfo->{'chat_width'} < $wininfo->{'width'} || ($wininfo->{'width_pct'} < 100 && (grep { $_->{'y'} == $wininfo->{'y'} } Nlib::i2h('window'))[-1]{'x'} > $wininfo->{'x'});
	for (my $i = 0; $i < @words; $i+=2) {
		my $len = defined $words[$i] ? screen_length $words[$i] : 0;
		my $len2 = $i == $#words ? 0 : screen_length $words[$i+1];
		if ($len <= $width - $screen) {
			# no action needed
			$screen += $len + $len2;
		}
		elsif ($len > $width - $screen) {
			if ($len <= $width - $new_screen && $pos) { #cannot break before first word
				push @trace, 'b:'.$pos;
				push @trace, 'q:'.$new_screen.'.'.$layoutinfo->{'count'}++;
				$screen = $new_screen + $len + $len2;
			}
			else {
				my $pump = $width - $screen;
				if ($pump <= 0) {
					push @trace, 'b:'.$pos;
					push @trace, 'q:'.$new_screen.'.'.$layoutinfo->{'count'}++;
					$pump = $width - $new_screen;
				}
				if ($pump > 0) {
					my $ipos = $pos;
					while ($pump < $len) {
						my $i = 0;
						for (;;) {
							my $clen = screen_length substr $plain_msg, $ipos, 1;
							last if $i + $clen > $pump;
							$i += $clen;
							++$ipos;
						}
						push @trace, 'x:'.$ipos;
						push @trace, 'q:'.$new_screen.'.'.$layoutinfo->{'count'}++;
						$len -= $i;
						$pump = $width - $new_screen;
					}
				}
				$screen = $new_screen + $len + $len2;
			}
		}
		$pos += ($len ? length $words[$i] : 0) + ($len2 ? length $words[$i+1] : 0);
	}
	while (@{$ctrl->[POS_S]}) {
		my $ctrl_s = shift @{$ctrl->[POS_S]};
		my $ctrl_e = shift @{$ctrl->[POS_E]};
		my $ctrl_r = shift @{$ctrl->[RES]};
		for (@trace) {
			s{^([bnx]:)(\d+)}{
				$1 . ( $2 + ($2 > $ctrl_s ? length $ctrl_r : 0) )
			}ge;
		}
	}
	($layoutinfo, \@trace)
}

## download_lines -- load all (partially) visible lines & infos
## $wininfo -  hash ref with windowinfo of window to grab
## returns reference to all downloaded lines
sub download_lines {
	my ($wininfo) = @_;

	my @scroll_area = Nlib::hdh($wininfo->{'pointer'}, 'window', 'scroll');
	my @lines_in = Nlib::hdh($wininfo->{'buffer'}, 'buffer', 'lines');
	my $lineinfo =  hdh_get_lineinfo(Nlib::hdh(@lines_in, 'last_line'));

	my $show_time = Nlib::hdh($wininfo->{'buffer'}, 'buffer', 'time_for_each_line');
	my $buffer_len = (sort { $a <=> $b } Nlib::hdh(@lines_in, 'buffer_max_length'), grep { $_ > 0 } weechat::config_integer(weechat::config_get('weechat.look.prefix_buffer_align_max')))[0];
	my $prefix_len = (sort { $a <=> $b } Nlib::hdh(@lines_in, 'prefix_max_length'), grep { $_ > 0 } weechat::config_integer(weechat::config_get('weechat.look.prefix_align_max')))[0];
	my $separator_len = weechat::config_string(weechat::config_get('weechat.look.prefix_align')) eq 'none' ? -1 : screen_length fu8on weechat::config_string(weechat::config_get('weechat.look.prefix_suffix'));

	my @base_trace = (t => $show_time, u => $buffer_len, p => $prefix_len, q => $separator_len);

	my @line = Nlib::hdh(@lines_in, 'last_line');
	my $last_read_line = weechat::config_string(weechat::config_get('weechat.look.read_marker')) eq 'line'
		? Nlib::hdh(@lines_in, 'last_read_line')
			: '';
	my $read_marker_always = weechat::config_boolean(weechat::config_get('weechat.look.read_marker_always_show'));
	$last_read_line = '' if $last_read_line eq $line[0]
		&& !$read_marker_always;
	my $lp;
	my @scroll_line = Nlib::hdh(@scroll_area, 'start_line');
	if ($scroll_line[0]) {
		@line = @scroll_line;
		$lineinfo = hdh_get_lineinfo(@line);
		$wininfo->{'start_line_pos'} = Nlib::hdh
			(@scroll_area, 'start_line_pos');
	}
	else {
		$wininfo->{'start_line_pos'} = 0;
		my $total = Nlib::hdh(@lines_in, 'lines_count');
		return [] unless $total;
		my $not_last_line = 0;
		for (my ($i, $j) = (0, 0); $j < $wininfo->{'chat_height'} && $i < $total; ++$i) {

			$line[0] = Nlib::hdh(@line, 'prev_line') if $i > 0;

			if ($line[0] eq $last_read_line) {
				if ($not_last_line || $read_marker_always) {
					++$j;
				}
				else {
					$last_read_line = '';
				}
			}
			my @line_data = Nlib::hdh(@line, 'data');
			if (Nlib::hdh(@line_data, 'displayed')) {
				my $lineinfo = +{ map { $_ => Nlib::hdh(@line_data, $_) } qw(message str_time date highlight) };
				my $prev_lineinfo;
				if ($base_trace[-1] < 0 && $i + 1 < $total) {
				HAS_PREV: {
						my @prev_line = @line;
						my @prev_linedata;
						do {
							@prev_line = Nlib::hdh(@prev_line, 'prev_line');
							last HAS_PREV unless $prev_line[0];
							@prev_linedata = Nlib::hdh(@prev_line, 'data');
						} until (Nlib::hdh(@prev_linedata, 'displayed'));
						$prev_lineinfo = +{
							(map { $_ => Nlib::hdh(@prev_linedata, $_) } qw(message str_time date highlight prefix)),
							tag => [
								map { Nlib::hdh(@prev_linedata, "$_|tags_array") }
									0 .. Nlib::hdh(@prev_linedata, 'tags_count')-1 ]
						   };
						my ($prev_layoutinfo) = calculate_trace($prev_lineinfo,
																$wininfo, \@base_trace);
						my ($this_layoutinfo) = calculate_trace($lineinfo,
																$wininfo, \@base_trace);
						#$prev_lineinfo = undef if $prev_layoutinfo->{'count'} + $this_layoutinfo->{'count'} + $j > $wininfo->{'chat_height'};
					}
					$lineinfo->{'prefix'} = Nlib::hdh(@line_data, 'prefix');
					$lineinfo->{'tag'} = [
						map { Nlib::hdh(@line_data, "$_|tags_array") }
							0 .. Nlib::hdh(@line_data, 'tags_count')-1 ];
				}
				my ($layout_info) = calculate_trace($lineinfo,
													$wininfo, \@base_trace,
													$prev_lineinfo);
				$prev_lineinfo = $lineinfo;
				$j += $layout_info->{'count'};
				$not_last_line = 1 if $j;
				$wininfo->{'start_line_pos'} -= $wininfo->{'chat_height'} - $j
					if $j > $wininfo->{'chat_height'};
			}
		}
		$lineinfo = hdh_get_lineinfo(@line);
	}
	$lp = $line[0];

	my @lines;
	my $current_line = 0;
	if ($lineinfo->{'displayed'}) {
		push @lines, [+{%$lineinfo}, calculate_trace($lineinfo, $wininfo, \@base_trace)];
		$current_line = $lines[0][LAYOUT]{'count'}-$wininfo->{'start_line_pos'};
	}
	my $prev_lineinfo;
	$prev_lineinfo = $lineinfo unless $wininfo->{'start_line_pos'};
	# XXX start_line_pos is buggy under yet uncertain multi-line messages
	#$wininfo->{'start_line_pos'} = 6;

	do {
		if ($lp eq $last_read_line) {
			push @lines, [+{message=>'',prefix=>''}, +{count=>1}, []];
			++$current_line;
		}

		$lp = $lineinfo->{'next_line'};
		$lineinfo = hdh_get_lineinfo($lineinfo->{'next_line'}, 'line');
		
		if ($lineinfo->{'displayed'}) {
			push @lines, [+{%$lineinfo}, calculate_trace($lineinfo, $wininfo, \@base_trace, $prev_lineinfo)];
			$prev_lineinfo = $lineinfo;
			$current_line += $lines[-1][LAYOUT]{'count'};
		}
	}
	while ($lineinfo->{'next_line'} && $current_line < $wininfo->{'chat_height'});

	\@lines;
}

## OLD_download_lines -- load all (partially) visible lines & infos
## $wininfo -  hash ref with windowinfo of window to grab
## returns reference to all downloaded lines
sub OLD_download_lines {
	my ($wininfo) = @_;

	my @lines;

	# get first line of buffer
	return \@lines unless $wininfo->{'start_line'};
	my ($lineinfo) = Nlib::i2h('buffer_lines', @{$wininfo}{'buffer','start_line'});
	my ($layoutinfo) = Nlib::i2h('layout', $wininfo->{'pointer'}, $lineinfo->{'line'}, $listptr);
	my $current_line = $layoutinfo->{'count'}-$wininfo->{'start_line_pos'};

	push @lines, [+{%$lineinfo}, $layoutinfo, [Nlib::l2l($listptr, 1)]];

	while ($lineinfo->{'next_line'} && $current_line < $wininfo->{'chat_height'}) {
		($lineinfo) = Nlib::i2h('buffer_lines', @{$lineinfo}{'buffer','next_line'});		
		next unless $lineinfo->{'displayed'};
		my ($layoutinfo) = Nlib::i2h('layout', $wininfo->{'pointer'}, $lineinfo->{'line'}, $listptr);
		$current_line += $layoutinfo->{'count'};

		push @lines, [+{%$lineinfo}, $layoutinfo, [Nlib::l2l($listptr, 1)]];
	}

	\@lines
}

## message_splits -- extract word splits in the message from trace
## $line - one line from download_lines
## returns all split events from trace as list of [position, event]
sub message_splits {
	my ($line) = @_;
	map {
		my ($l, $d) = /(.):(\d+)/;
		#($l =~ /[bn]/ ? 1 : 0) + 
		[ $d, $l ]
	}
	grep { /^[bnx]:/ } # b: break between words, n: new line, x: cut word
	@{ $line->[TRACE] }
}

## trace_cond -- filter trace for certain condition
## $what - event to match
## $line - downloaded line (with trace section)
## $lineno - line number for event
## returns matching trace event
sub trace_cond {
	my ($what, $line, $lineno) = @_;
	my $ext = $lineno ? qr/[.]$lineno$/ : qr/#/;
	grep { /^$what:/ && /$ext/ } @{ $line->[TRACE] }
}

## to_pos -- pad string to reach a certain position
## $tr_info - trace event from trace_cond, contains position
## $c_ref - string reference where padding is added
sub to_pos {
	my ($tr_info, $c_ref) = @_;
	$tr_info =~ /:(\d+)/;
	my $pos = $1;
	my $current_length = screen_length fu8on weechat::string_remove_color($$c_ref, '');
	if ($pos-$current_length < 0) {
		chop $$c_ref while length $$c_ref && (screen_length fu8on weechat::string_remove_color($$c_ref, '')) > $pos;
	}
	else {
		$$c_ref .= ' 'x($pos-$current_length);
	}
}

## right_align -- right align some text
## $tr - trace event, contains position for right alignment
## $text_ref - text reference to right align
## $c_ref - string reference where padding is added, so that $text_ref would be right aligned
sub right_align {
	my ($tr, $text_ref, $c_ref) = @_;
	$tr =~ /:(\d+)/;
	my $pos1 = $1 -1;
	my $text_length = screen_length fu8on weechat::string_remove_color($$text_ref, '');
	my $current_length = screen_length fu8on weechat::string_remove_color($$c_ref, '');
	$$c_ref .= ' 'x($pos1-$text_length-$current_length)
}

## show_time -- output timestamp and pad to next position
## $line - downloaded line
## $c_ref - string reference on which to output
sub show_time {
	my ($line, $c_ref) = @_;
	if (my ($tr) = trace_cond('t', $line)) {
		$$c_ref .= $line->[LINE]{'str_time'};
		to_pos($tr, $c_ref);
	}
}

## show_buffername -- output buffer name and pad to next position
## $line - downloaded line
## $c_ref - string reference on which to output
sub show_buffername {
	my ($line, $c_ref) = @_;
	if (my ($tr) = trace_cond('u', $line)) {
		my $buffer = weechat::color('chat_prefix_buffer').
			         (Nlib::i2h('buffer', $line->[LINE]{'buffer'}))[0]{'short_name'}.
				     weechat::color('reset');
		if (weechat::config_string(weechat::config_get(
		   'weechat.look.prefix_buffer_align')) =~ /right/) {
			right_align($tr, \$buffer, $c_ref);
		}
		$$c_ref .= $buffer;
		to_pos($tr, $c_ref);
	}
}

## show_prefix -- output prefix and pad to next position
## $line - downloaded line
## $c_ref - string reference on which to output
sub show_prefix {
	my ($line, $c_ref, $prev_line) = @_;
	if (my ($tr) = trace_cond('p', $line)) {
		my $prefix = fu8on $line->[LINE]{'prefix'};
		my ($nick_tag) = grep { /^nick_/ } @{$line->[LINE]{'tag'}||[]};
		my ($pnick_tag) = grep { /^nick_/ } @{$prev_line->[LINE]{'tag'}||[]};
		if ($nick_tag && $pnick_tag && $nick_tag eq $pnick_tag && !$line->[LINE]{'highlight'} &&
				(grep { /_action$/ && !/^nick_/ } @{$line->[LINE]{'tag'}||[]}) == 0 &&
				$prev_line && $line->[LINE]{'prefix'} eq $prev_line->[LINE]{'prefix'}) {
			my $repl = fu8on weechat::config_string(weechat::config_get(
					'weechat.look.prefix_same_nick'));
			$prefix = repeat_control_codes($prefix) . $repl if $repl;
		}
		if (weechat::config_string(weechat::config_get(
		   'weechat.look.prefix_align')) =~ /right/) {
			right_align($tr, \$prefix, $c_ref);
		}
		$$c_ref .= $prefix;
		to_pos($tr, $c_ref);
	}
}

## show_separator -- output separator and pad to next position
## $line - downloaded line
## $c_ref - string reference on which to output
sub show_separator {
	my ($line, $c_ref, $lineno) = @_;
	if (my ($tr) = trace_cond('q', $line, $lineno)) {
		my $separator = fu8on weechat::color('chat_prefix_suffix').
			            weechat::config_string(weechat::config_get('weechat.look.prefix_suffix'));
		right_align($tr, \$separator, $c_ref);
		$$c_ref .= $separator; # if XXX
		to_pos($tr, $c_ref);
	}
}

## repeat_control_codes -- repeat control codes previously seen (for linebreaks)
## $text - all codes in this text will be repeated
## returns control codes as a string
sub repeat_control_codes {
	my ($text) = @_;
	my $id_control = quotemeta fu8on weechat::string_remove_color($text, "\1");
	$id_control =~ s/\\\01/(.+?)/g;
	my @res = $text =~ /^()$id_control()$/;
	join '', @res
}

## calc_free_image -- create image of free window buffer
## $wininfo - window info hash with start_line_pos & chat_height
## $lines_ref - array ref of lines from download_lines
## returns a array reference to all lines in the image
sub calc_free_image {
	my ($wininfo, $lines_ref) = @_;
	my @image;
	my $i = 0;
	my $first_line = $wininfo->{'start_line_pos'};
	my $prev_line;
	for my $line (@$lines_ref) {
		my $max_length = [(length $line->[LINE]{'message'}),''];
		my @splits = ([0,''], message_splits($line), $max_length, $max_length);
		for (0 .. $line->[LAYOUT]{'count'}-1) {
			shift @splits if $splits[0][0] - $splits[1][0] >= 0;
			last if $i >= $wininfo->{'chat_height'};
			my $subm = substr $line->[LINE]{'message'}, $splits[0][0], $splits[1][0]-$splits[0][0];
			if ($_ >= $first_line) {
 				my $construction = '';
				unless ($_) {
					show_time($line, \$construction);
					show_buffername($line, \$construction);
					show_prefix($line, \$construction, $prev_line);
					$prev_line = $line if exists $line->[LINE]{'date'};
				}
				show_separator($line, \$construction, $_);
				$construction .= weechat::color('reset');
 				$construction .= repeat_control_codes(substr $line->[LINE]{'message'}, 0, $splits[0][0]);
				$subm =~ s/^ +// if $splits[0][1] =~ /[bn]/;
 				$construction .= $subm;
				$construction .= weechat::color('reset');
				
				push @image, $construction;
				$i++;
			}
			shift @splits;
		}
		$first_line = 0;
	}
	\@image
}

## capture_color_codes -- extract all weechat control codes from a message and store them with their original positions
## $msg - message from which to start
## returns a array ref with C<RES> (control codes), C<POS_S> (start position), C<POS_E> (end position) and optionally string without all codes
sub capture_color_codes {
	my ($msg) = @_;
	my $id_control = quotemeta fu8on weechat::string_remove_color($msg, "\1");
	$id_control =~ s/(\\\01)+/(.+?)/g;
	my @ctrl_res = $msg =~ /^()$id_control()$/;
	my @ctrl_pos_s = @-;
	my @ctrl_pos_e = @+;

	shift @ctrl_pos_s; shift @ctrl_pos_e;
	
	#my @chunks = split "\01+", $color_1, -1;

	my @ret = \( @ctrl_res, @ctrl_pos_s, @ctrl_pos_e );
	if (wantarray) {
		( \@ret, fu8on weechat::string_remove_color($msg, '') );
	}
	else {
		\@ret
	}
}

sub DEBUG_bracketing {
	my ($bracketing_r, $msg_r, $fx) = @_;
	return unless $fx;
	my $dumpline = ' ' x length $$msg_r;
	my @pair_pool = ('a'..'z');
	for (sort { $a eq 'oc' ? -1 : $b eq 'oc' ? 1 : $a <=> $b } keys %$bracketing_r) {
		my @l = @{ $bracketing_r->{$_} };
		if (@l) {
			my $ch = shift @pair_pool;
			substr $dumpline, $l[0], 1, $ch;
			substr $dumpline, $l[-2], 1, "\U$ch";
			push @pair_pool, $ch;
		}
	}
	print $fx $$msg_r."\n$dumpline\n";
}

## calculate_bracketing -- try to detect bracketing pairs
## $msg_r - reference to bracketed string 
## returns hash ref to detected bracketing
sub calculate_bracketing {
	my ($msg_r) = @_;
	my $braces = weechat::config_get_plugin('url_braces');
	my %br_open;
	while ($braces =~ s/^(.)(.*)(.)$/$2/) {
		$br_open{$3} = $1;
	}
	$br_open{$braces} = $braces if length $braces;
	my %bracketing;
	my $br_open = join '|', map { quotemeta } values %br_open;
	my $br_close = join '|', map { quotemeta } keys %br_open;
	while ($$msg_r =~ /($br_open|$br_close)/g) {
		my $char = $1;
		my $pos = $-[0];
		if ($char =~ /$br_close/ && @{ $bracketing{'oc'} || [] } && $bracketing{'oc'}[-1][-1] eq $br_open{$char}) {
			my $match = pop @{ $bracketing{'oc'} };
			push @$match, $pos, $char;
			$bracketing{$pos} = $bracketing{$match->[0]} = $match;
		}
		elsif ($char =~ /$br_open/) {
			push @{ $bracketing{'oc'} }, [ $pos, $char ];
		}
		else {
			$bracketing{$pos} = [ $pos, $char ];
		}
	}
	while (@{ $bracketing{'oc'} || [] }) {
		my $match = shift @{ $bracketing{'oc'} };
		$bracketing{$match->[0]} = $match;
	}
	\%bracketing
}

sub DEBUG_hyperlink {
	my ($s, $plain_msg, $e, $fx) = @_;
	return unless $fx;
	my $dumpline = ' ' x length $plain_msg;
	substr $dumpline, $s, 1, "\\";
	substr $dumpline, $e-1, 1, '/';
	print $fx '  '.$dumpline."\n";
	print $fx "----------\n";
}

## hyperlink_adjust_region -- removes bracketing and word end markers if detected around/at the end of region
## $sr - reference to start position of hyperlink
## $msg_r - reference to string with hyperlink
## $er - reference to end position of hyperlink
## $bracketing_r - calculated bracketing for this string
sub hyperlink_adjust_region {
	my ($sr, $msg_r, $er, $bracketing_r, $no_url) = @_;
	my $non_endings = weechat::config_get_plugin('url_non_endings');
	my $non_beginnings = weechat::config_get_plugin('url_non_beginnings');
	for (undef) {
		if (exists $bracketing_r->{$$er-1} && $bracketing_r->{$$er-1}[0] == $$sr) {
			++$$sr; --$$er; redo;
		}
		elsif (exists $bracketing_r->{$$er-1} && $bracketing_r->{$$er-1}[0] < $$sr) {
			--$$er; redo;
		}
		elsif (exists $bracketing_r->{$$sr} && $bracketing_r->{$$sr}[-2] > $$er-1) {
			++$$sr; redo;
		}
		unless ($no_url) {
			if ((substr $$msg_r, $$er-1, 1) =~ /$non_endings/) {
				--$$er; redo;
			}
			elsif ((substr $$msg_r, $$sr, 1) =~ /$non_beginnings/) {
				++$$sr; redo;
			}
		}
	}
}

sub trace_to_u8 {}

sub OLD_trace_to_u8 {
	my ($line) = @_;
	my $bytes_msg = $line->[LINE]{'message'};
	Encode::_utf8_off($bytes_msg);
	
	for (@{ $line->[TRACE] }) {
		s{^([bnx]:)(\d+)}{
			$1 . length fu8on substr $bytes_msg, 0, $2;
		}ge;
	}
}

## hyperlink_replay_code1 -- insert start/end of marking and adjust positions/trace
## $line - downloaded line with trace info to adjust
## $msg_r - reference to msg in which to insert
## $coder - reference to code to insert
## $tr - seek this much to the left when correcting trace profile
## @advs - references to arrays of positions to advance, first one will be shifted
## $fx - debug file handle
## $DBG - output debug info, prefix with $DBG
sub hyperlink_replay_code1 {
	my ($line, $msg_r, $coder, $tr, @advs, $fx, $DBG) = @_;
	unless (ref $advs[-1]) {
		$DBG = pop @advs;
		$fx = pop @advs;
	}
	my $pos_r = $advs[0];
	my $pos = shift @$pos_r;
	print $fx '[1]'."code:$$coder pos:$pos\n" if $fx;
	substr $$msg_r, $pos, 0, $$coder;
	print $fx $DBG.' '.$$msg_r."\n" if $fx;
	for (@advs) {
		$_ += length $$coder for @$_;
	}
	for (@{ $line->[TRACE] }) {
		s{^([bnx]:)(\d+)}{
			$1 . ( $2 + ($2 > $pos-$tr ? length $$coder : 0) )
		}ge;
	}
}

## hyperlink_replay_code2 -- reinsert color codes and adjust positions
## $ctrl - captured control codes from capture_color_codes
## $msg_r - reference to msg in which to insert
## @advs - references to arrays of positions to advance
## $fx - debug file handle
## $DBG - output debug info, prefix with $DBG
sub hyperlink_replay_code2 {
	my ($ctrl, $msg_r, @advs, $fx, $DBG) = @_;
	unless (ref $advs[-1]) {
		$DBG = pop @advs;
		$fx = pop @advs;
	}
	my $ctrl_s = shift @{$ctrl->[POS_S]};
	my $ctrl_e = shift @{$ctrl->[POS_E]};
	my $ctrl_r = shift @{$ctrl->[RES]};
	substr $$msg_r, $ctrl_s, 0, $ctrl_r;
	print $fx $DBG.' '.$$msg_r."\n" if $fx;
	for (@advs) {
		$_ += $ctrl_e-$ctrl_s for @$_;
	}
}


## hyperlink_replay_codes -- insert marker codes, reinsert control codes into plain msg
## $line - line with trace data to adjust
## $url_sr - reference to hyperlink starting position
## $msg_r - reference to message where positions apply
## $url_er - reference to hyperlink ending positions
## $ctrl - capture_color_codes result of control codes
## $active - reference that counts no of links and actives on "0"
## $fx - debug file handle
sub hyperlink_replay_codes {
	my ($line, $url_sr, $msg_r, $url_er, $ctrl, $active, $fx) = @_;

	my $ul = join '', map { weechat::color($_) } split '[.]',
		weechat::config_get_plugin('color.url_highlight');
	#my $UL = weechat::color('-underline').weechat::color('-reverse');

	my $ul_active = join '', map { weechat::color($_) } split '[.]',
		weechat::config_get_plugin('color.url_highlight_active');
	my $UL_active = weechat::color('reset');

	my $last_url_s;

	my $max_loop = 2*( @{$ctrl->[POS_S]} + @$url_sr + @$url_er );
	while (@{$ctrl->[POS_S]} || @$url_sr || @$url_er) {
		print $fx "<S> @$url_sr\n<E> @$url_er\n<C> @{$ctrl->[POS_S]}\n" if $fx;
	   #if (@$url_sr && $url_sr->[0] <= $url_er->[0] && (!@{$ctrl->[POS_S]} || $url_sr->[0] <= $ctrl->[POS_S][0])) # code goes before original ctl code
		if (@$url_sr && $url_sr->[0] <= $url_er->[0] && (!@{$ctrl->[POS_S]} || $url_sr->[0] < $ctrl->[POS_S][0])) {
			$last_url_s = $url_sr->[0];
			hyperlink_replay_code1($line, $msg_r, ($$active ? \$ul : \$ul_active), 0,
				$url_sr, $url_er, @{$ctrl}[POS_S,POS_E], $fx, 'S');
		}
		elsif (@$url_er && (!@{$ctrl->[POS_S]} || $url_er->[0] <= $ctrl->[POS_S][0])) {
			my $UL_active1 = $UL_active;
			if (defined $last_url_s) {
				# get part of message constructed thus far, and remove starting bracket
				my $msg_part1 = substr $$msg_r, 0, $url_er->[0];
				substr $msg_part1, $last_url_s, length ($$active ? $ul : $ul_active), '';
				$UL_active1 .= repeat_control_codes($msg_part1);
				$last_url_s = undef;
			}
			hyperlink_replay_code1($line, $msg_r, ($$active ? \$UL_active1 : \$UL_active1), 1,
				$url_er, $url_sr, @{$ctrl}[POS_S,POS_E], $fx, 'E');
			--$$active;
		}
		else { # ($ctrl->[POS_S][0] <= $url_sr->[0] && $ctrl->[POS_S][0] <= $url_er->[0])
			my $ip = $ctrl->[POS_E][0];
			my $needs_fixup = defined $last_url_s && $ctrl->[RES][0] =~ $UL_active;
			hyperlink_replay_code2($ctrl, $msg_r, $url_sr, $url_er, $fx, 'C');
			if ($needs_fixup) {
				$last_url_s = $ip;
				hyperlink_replay_code1($line, $msg_r, ($$active ? \$ul : \$ul_active), 0,
				[$ip], $url_sr, $url_er, @{$ctrl}[POS_S,POS_E], $fx, 'F');
			}
		}
		--$max_loop; die 'endless loop' if $max_loop < 0;
	}
}

## hyperlink_match_type_filter -- checks if current type filter applies to url info
## $url_info - hashref with a type property
## returns true or false
sub hyperlink_match_type_filter {
	my ($url_info) = @_;
	my $t = substr $url_info->{'type'}, 0, 1;
	$ACT_STR->[URL_TYPE_FILTER] =~ $t
}

## hyperlink_function -- highlight hyperlinks
## $lines_ref - downloaded lines
sub hyperlink_function {
	my ($lines_ref) = @_;
	my $fx;
	#open $fx, '>', ... || weechat::print('', "error:$!");
	my ($nicklist, $channels);
	if (Nlib::has_true_value(weechat::config_get_plugin('hyper_nicks'))) {
		$nicklist = join '|',
			map { quotemeta }
			sort { length $b <=> length $a }
			map { $_->{'name'} }
			grep { $_->{'type'} eq 'nick' && $_->{'visible'} && length $_->{'name'} }
			Nlib::i2h('nicklist', $ACT_STR->[WINDOWS]{'buffer'});
	}
	else {
		$nicklist = '(?!)';
	}
	$nicklist = '(?!)' unless length $nicklist; # stop infinite loop on empty pair
	if (Nlib::has_true_value(weechat::config_get_plugin('hyper_channels'))) {
		$channels = qr,[#]+(?:\w|[][./+^!&|~}{)(:\\*@?'-])+,;
	}
	else {
		$channels = '(?!)';
	}

	my $re = weechat::config_get_plugin('url_regex');
	$ACT_STR->[A_LINK] = -1 unless defined ${$ACT_STR}[A_LINK];
	my $a_link = -1;
	if (defined $ACT_STR->[URLS] && $ACT_STR->[A_LINK] < @{$ACT_STR->[URLS]}
	   && hyperlink_match_type_filter($ACT_STR->[URLS][$ACT_STR->[A_LINK]][URL_INFO])) {
		for my $i (0 .. $ACT_STR->[A_LINK]) {
			++$a_link
				if hyperlink_match_type_filter($ACT_STR->[URLS][$i][URL_INFO]);
		}
	}
	my @urls;
	my $i = 0; # line index
	for my $line (@$lines_ref) {
		my %prefix_type = (type => 'prefix');
		my ($pfx_nick) = grep { /^nick_/ } @{ $line->[LINE]{'tag'} };

		if (Nlib::has_true_value(weechat::config_get_plugin('hyper_prefix')) &&
				$line->[LINE]{'prefix'} &&
					defined $pfx_nick && $pfx_nick =~ s/^nick_//) {
			push @urls, [ $line, -1, $pfx_nick, -1, $i, \%prefix_type ];

			if (hyperlink_match_type_filter(\%prefix_type)) {
				my $ul = join '', map { weechat::color($_) } split '[.]',
					weechat::config_get_plugin('color.url_highlight');

				my $ul_active = join '', map { weechat::color($_) } split '[.]',
					weechat::config_get_plugin('color.url_highlight_active');
				my $UL_active = weechat::color('reset');

				my ($ctrl, $plain_pfx) = capture_color_codes(fu8on $line->[LINE]{'prefix'});
				my $my_ul = $a_link ? $ul : $ul_active;
				substr $line->[LINE]{'prefix'}, $ctrl->[POS_E][-2], 0, $my_ul;
				substr $line->[LINE]{'prefix'}, $ctrl->[POS_E][-1]+length $my_ul, 0, $UL_active;
				--$a_link;
			}
		}
		my $msg = fu8on $line->[LINE]{'message'};
		my ($ctrl_codes, $plain_msg) = capture_color_codes($msg);
		my $bracketing_r = calculate_bracketing(\$plain_msg);
		DEBUG_bracketing($bracketing_r, \$plain_msg, $fx);
		my (@url_s, @url_res, @url_e);
		while ($plain_msg =~ /\b($nicklist)(?:(?=\W)|$)|(?:^|(?<=\W))($channels)\b|$re/gx) {
			my %typeinfo = (type => defined $1 ? 'nick' : defined $2 ? 'channel' : 'url');
			my ($s, $e) = ($-[0], $+[0]);
			DEBUG_hyperlink($s, $plain_msg, $e, $fx);
			
			hyperlink_adjust_region(\($s, $plain_msg, $e), $bracketing_r, $typeinfo{'type'} ne 'url')
				unless $typeinfo{'type'} eq 'nick';
			my $t = substr $plain_msg, $s, $e-$s;
			if (hyperlink_match_type_filter(\%typeinfo)) {
				push @url_s, $s;
				push @url_res, $t;
				push @url_e, $e;
			}
			DEBUG_hyperlink($s, $plain_msg, $e, $fx);
			push @urls, [ $line, $s, $t, $e, $i, \%typeinfo ];
		}
		
		print $fx "X $plain_msg\n" if $fx;
		trace_to_u8($line);
		hyperlink_replay_codes($line, \(@url_s, $plain_msg, @url_e), $ctrl_codes, \$a_link, $fx);
		$line->[LINE]{'message'} = $plain_msg;

		++$i;
	}
	$ACT_STR->[URLS] = \@urls;
	$ACT_STR->[A_LINK] = @{ $ACT_STR->[URLS] }
		if $ACT_STR->[A_LINK] < 0;
}

## copy_lines -- make a copy of downloaded lines (dumb dclone)
## $lines_ref - reference to copy
## returns a reference copy of the reference content
sub copy_lines {
	my ($lines_ref) = @_;
	my $lines_ref2 = [];
	push @$lines_ref2, [ +{%{$_->[LINE]}}, +{%{$_->[LAYOUT]}}, [@{$_->[TRACE]}] ]
		for @$lines_ref;
	$lines_ref2
}

## send_clip_external -- send clipboard to external app
## $text - text for clipboard
## $xterm_osc - xterm-compatible escape sequence
sub send_clip_external {
	my ($text, $xterm_osc) = @_;
	if (weechat::config_is_set_plugin('clipboard_command')) {
		my %presets = ( xsel => '|xsel -c', xclip => '|xclip' );
		my $external = weechat::config_get_plugin('clipboard_command');
		$external = $presets{$external} if exists $presets{$external};
		if ($external =~ /^\|/) {
			if (open my $ext, $external) {
				print $ext $text;
			}
			else {
				weechat::print('', weechat::prefix('error').'Clipboard: '.$!);
			}
		}
		elsif ($external =~ s/%q/\Q$text/ || $external =~ s/%x/\Q$xterm_osc/) {
			unless (system($external) >= 0) {
				weechat::print('', weechat::prefix('error').'Clipboard: '.$!);
			}
		}
		else {
			if ($external eq 'tmux') {
				unless (( system { $external } $external, 'deleteb' ) >= 0) {
					weechat::print('', weechat::prefix('error').'Clipboard: '.$!);
				}
				$external .= ' setb';
			}
			my @cmd = split ' ', $external;
			if (grep { $_ eq '%s' } @cmd) {
				@cmd = map { $_ eq '%s' ? $text : $_ } @cmd;
			}
			else {
				push @cmd, $text;
			}
			unless (( system { $cmd[0] } @cmd ) >= 0)  {
				weechat::print('', weechat::prefix('error').'Clipboard: '.$!);
			}
		}
	}
}

## send_clip -- send text to selection clipboard
## $text - text for clipboard
## $stor - storage unit (optional, default s0)
sub send_clip {
	my ($text, $stor) = @_;
	$stor = '' unless $stor;
	my $text_nu = $text;
	Encode::_utf8_off($text_nu);
	my $xterm_osc = "\e]52;$stor;".encode_base64($text_nu, '')."\a";
	my $compatible_terms = join '|', map { split /[,;]/ } split ' ',
		weechat::config_get_plugin('xterm_compatible');
	print STDERR $xterm_osc if $ENV{'TERM'} =~ /^xterm|$compatible_terms/;
	if ($ENV{'TMUX'}) {
		my @tmux_clients = `tmux lsc`;
		my $active_term;
		my $last_time = 0;
		for (@tmux_clients) {
			chomp;
			my ($path, $rest) = split ':', $_;
			next unless $rest =~ / (?:xterm|$compatible_terms)/;
			my $atime = -A $path;
			if ($last_time >= $atime) {
				$last_time = $atime;
				$active_term = $path;
			}
		}
		if ($active_term) {
			open my $pty, '>>', $active_term;
			print $pty $xterm_osc;
		}
	}	
	send_clip_external($text, $xterm_osc);
}

## hyperlink_to_clip -- send currently active link to clipboard
sub hyperlink_to_clip {
	if ($ACT_STR->[A_LINK] >= 0 && $ACT_STR->[A_LINK] < @{ $ACT_STR->[URLS] }) {
		my $url = $ACT_STR->[URLS][$ACT_STR->[A_LINK]][URL];
		send_clip($url);
	}
}

## hyperlink_urlopen -- send url open signal for currently active link
sub hyperlink_urlopen {
	my $url = $ACT_STR->[URLS][$ACT_STR->[A_LINK]][URL];
	send_clip($url, 's0x');
	weechat::hook_signal_send('urlopen', weechat::WEECHAT_HOOK_SIGNAL_STRING, $url);
}

## selection_to_clip -- send currently active selection to clipboard
sub selection_to_clip {
	if ($ACT_STR->[CUR][0*2] > -1 && $ACT_STR->[CUR][1*2] > -1) {
		my @range = sort { $a <=> $b } @{$ACT_STR->[CUR]}[0*2,1*2];
		my @lines = map { fu8on weechat::string_remove_color($_->[LINE]{'message'},'') } @{$ACT_STR->[LINES]}[$range[0]..$range[1]];
		my @prefixes = map { fu8on weechat::string_remove_color($_->[LINE]{'prefix'},'') } @{$ACT_STR->[LINES]}[$range[0]..$range[1]];
		my @cuts = map { $_->[1] } sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } ([@{$ACT_STR->[CUR]}[0*2,0*2+1]], [@{$ACT_STR->[CUR]}[1*2,1*2+1]]);
		$lines[0] = length $lines[0] >= $cuts[0] ? substr $lines[0], $cuts[0] : ''; #(substr outside of string?)
		$prefixes[0] = undef;# if $cuts[0];
		$lines[-1] = substr $lines[-1], 0, $range[0]==$range[1] ? $cuts[1]-$cuts[0] : $cuts[1];
		my $sel_text = join "\n", map { my $pfx = shift @prefixes; ($pfx ? "$pfx\t" : '') . $_ } @lines;
		send_clip($sel_text);
	}
}

## hyperlink_dispatch_input -- dispatch input from ** commands in hyperlink mode
## $args - input argument
## returns true value if processing is continued, false otherwise
sub hyperlink_dispatch_input {
	my ($args) = @_;
	if ($args eq '+') {
		++$ACT_STR->[A_LINK];
		$ACT_STR->[A_LINK] = 0
			if $ACT_STR->[A_LINK] > @{ $ACT_STR->[URLS] };
		until ($ACT_STR->[A_LINK] >= @{$ACT_STR->[URLS]}
				   || hyperlink_match_type_filter($ACT_STR->[URLS][$ACT_STR->[A_LINK]][URL_INFO])) {
			++$ACT_STR->[A_LINK];
		}
		hyperlink_to_clip();
	}
	elsif ($args eq '-') {	
		--$ACT_STR->[A_LINK];
		until ($ACT_STR->[A_LINK] < 0
					 || hyperlink_match_type_filter($ACT_STR->[URLS][$ACT_STR->[A_LINK]][URL_INFO])) {
			--$ACT_STR->[A_LINK];
		}
		$ACT_STR->[A_LINK] = @{ $ACT_STR->[URLS] }
			if $ACT_STR->[A_LINK] < 0;
		hyperlink_to_clip();
	}
	elsif ($args eq '!') {
		if ($ACT_STR->[A_LINK] >= 0 && $ACT_STR->[A_LINK] < @{ $ACT_STR->[URLS] }) {
			my $link_type = $ACT_STR->[URLS][$ACT_STR->[A_LINK]][URL_INFO]{'type'};
			if ($link_type eq 'nick' || $link_type eq 'prefix') {
				my $nick = $ACT_STR->[URLS][$ACT_STR->[A_LINK]][URL];
				if (Nlib::has_false_value(weechat::config_get_plugin('use_nick_menu'))) {
					weechat::command($ACT_STR->[WINDOWS]{'buffer'}, "/query $nick");
				}
				else {
					delayed_nick_menu($nick);
					close_copywin(); # XXX
				}
			}
			elsif ($link_type eq 'channel') {
				my $channel = $ACT_STR->[URLS][$ACT_STR->[A_LINK]][URL];
				weechat::command($ACT_STR->[WINDOWS]{'buffer'}, "/join $channel");
			}
			else {
				hyperlink_urlopen();
			}
		}
		else {
			weechat::command($ACT_STR->[BUFPTR], '/input return');
		}
		return;
	}
	else {
		return;
	}
	1
}

## selection_dispatch_input -- dispatch input from ** commands in selectionmode
## $args - input argument
## returns true value if processing is continued, false otherwise
sub selection_dispatch_input {
	my ($args) = @_;
	if ($args eq '+') {
		++$ACT_STR->[CUR][0];
		$ACT_STR->[CUR][0] = -1 if $ACT_STR->[CUR][0] >= @{$ACT_STR->[LINES]};
	}
	elsif ($args eq '-') {	
		--$ACT_STR->[CUR][0];
		$ACT_STR->[CUR][0] = @{$ACT_STR->[LINES]}-1 if $ACT_STR->[CUR][0] < -1;
	}
	elsif ($args eq '>') {
		++$ACT_STR->[CUR][1];
		if ($ACT_STR->[CUR][0] < 0 && $ACT_STR->[CUR][1] < 0) {
			$ACT_STR->[CUR][1] = -1;
		}
		else {
			my $msg = $ACT_STR->[LINES][$ACT_STR->[CUR][0]][LINE]{'message'};
			my $plain_msg = fu8on weechat::string_remove_color($msg, '');
			my $msglen = length $plain_msg;
			$ACT_STR->[CUR][1] = $msglen if $ACT_STR->[CUR][1] > $msglen;
		}
	}
	elsif ($args eq '<') {	
		--$ACT_STR->[CUR][1];
		if ($ACT_STR->[CUR][0] < 0) {
			$ACT_STR->[CUR][1] = -1 ;
		}
		elsif ($ACT_STR->[CUR][1] < 0) {
			$ACT_STR->[CUR][1] = 0;
		}
		else {
			my $msg = $ACT_STR->[LINES][$ACT_STR->[CUR][0]][LINE]{'message'};
			my $plain_msg = fu8on weechat::string_remove_color($msg, '');
			my $msglen = length $plain_msg;
			$ACT_STR->[CUR][1] = $msglen-1 if $ACT_STR->[CUR][1] > $msglen;
		}
	}
	elsif ($args eq 'f') {
		if ($ACT_STR->[CUR][0] < 0) {
			$ACT_STR->[CUR][1] = -1 ;
		}
		else {
			my $msg = $ACT_STR->[LINES][$ACT_STR->[CUR][0]][LINE]{'message'};
			my $plain_msg = fu8on weechat::string_remove_color($msg, '');
			my @breaks;
			push @breaks, @- while $plain_msg =~ /\b/g;
			$ACT_STR->[CUR][1] = (grep { $_ > $ACT_STR->[CUR][1] } @breaks)[0];
			unless (defined $ACT_STR->[CUR][1]) {
				my $msglen = length $plain_msg;
				$ACT_STR->[CUR][1] = $msglen;
			}
		}
	}
	elsif ($args eq 'b') {
		if ($ACT_STR->[CUR][0] < 0) {
			$ACT_STR->[CUR][1] = -1 ;
		}
		else {
			my $msg = $ACT_STR->[LINES][$ACT_STR->[CUR][0]][LINE]{'message'};
			my $plain_msg = fu8on weechat::string_remove_color($msg, '');
			my @breaks;
			push @breaks, @- while $plain_msg =~ /\b/g;
			$ACT_STR->[CUR][1] = (grep { $_ < $ACT_STR->[CUR][1] } @breaks)[-1];
			unless (defined $ACT_STR->[CUR][1]) {
				$ACT_STR->[CUR][1] = 0;
			}
		}
	}
	elsif ($args eq 'e') {
		if ($ACT_STR->[CUR][0] < 0) {
			$ACT_STR->[CUR][1] = -1 ;
		}
		else {
			my $msg = $ACT_STR->[LINES][$ACT_STR->[CUR][0]][LINE]{'message'};
			my $plain_msg = fu8on weechat::string_remove_color($msg, '');
			my $msglen = length $plain_msg;
			$ACT_STR->[CUR][1] = $msglen;
		}
	}
	elsif ($args eq 'a') {
		if ($ACT_STR->[CUR][0] < 0) {
			$ACT_STR->[CUR][1] = -1 ;
		}
		else {
			$ACT_STR->[CUR][1] = 0;
		}
	}
	elsif ($args eq '@') {
		if ($ACT_STR->[CUR][2] > -1) {
			@{$ACT_STR->[CUR]}[2,3] = (-1, -1);
		}
		else {
			@{$ACT_STR->[CUR]}[2,3] = @{$ACT_STR->[CUR]}[0,1];
		}
	}
	else {
		return;
	}
	if ($args =~ /^[><+fbea-]$/) {
		selection_to_clip();
	}
	1
}

## selection_replay_codes -- insert marker codes, reinsert control codes into plain msg
## $line - line with trace data to adjust
## $sel_s - start position of selection
## $msg_r - reference to message where positions apply
## $sel_e - end position of selection
## $ctrl - capture_color_codes result of control codes
## $cup - cursor position
## $fx - debug file handle
sub selection_replay_codes {
	my ($line, $sel_s, $msg_r, $sel_e, $ctrl, $cup, $fx) = @_;
	my @cup = grep { $_ > -1 } ($cup);

	my $se = join '', map { weechat::color($_) } split '[.]',
		weechat::config_get_plugin('color.selection');

	my $cu = join '', map { weechat::color($_) } split '[.]',
		weechat::config_get_plugin('color.selection_cursor');

	my @starts = grep { $_ > -1 } sort { $a <=> $b } ($sel_s, $cup);
	my @ends = grep { $_ > -1 } sort { $a <=> $b } ($sel_e, $cup+1);

	my $last_seq_s;

	print $fx "s<@starts> e<@ends> sel_s:$sel_s sel_e:$sel_e cup:$cup\n" if $fx;

	my $max_loop = 2*( @{$ctrl->[POS_S]} + @starts + @ends );
	while (@{$ctrl->[POS_S]} || @starts || @ends) {
		#if (@starts && $starts[0] <= $ends[0] && (!@{$ctrl->[POS_S]} || $starts[0] <= $ctrl->[POS_S][0])) #urlonly
		if (@starts && $starts[0] < $ends[0] && (!@{$ctrl->[POS_S]} || $starts[0] <= $ctrl->[POS_S][0])) {
			$last_seq_s = $starts[0];
			hyperlink_replay_code1($line, $msg_r, (@cup && $starts[0] == $cup[0] ? \$cu : \$se), 0,
				\@starts, \@ends, \@cup, @{$ctrl}[POS_S,POS_E], $fx, 'S');
		}
		elsif (@ends && (!@{$ctrl->[POS_S]} || $ends[0] <= $ctrl->[POS_S][0])) {
			my $active1 = weechat::color('reset');
			if (defined $last_seq_s) {
				# get part of message constructed thus far, and remove starting bracket
				my $msg_part1 = substr $$msg_r, 0, $ends[0];
				substr $msg_part1, $last_seq_s, length (@cup && $last_seq_s == $cup[0] ? $cu : $se), '';
				$active1 .= repeat_control_codes($msg_part1);
				$last_seq_s = undef;
			}
			hyperlink_replay_code1($line, $msg_r, \$active1, 1,
				\@ends, \@starts, \@cup, @{$ctrl}[POS_S,POS_E], $fx, 'E');
		}
		else { # ($ctrl->[POS_S][0] <= $starts[0] && $ctrl->[POS_S][0] <= $ends[0])
			my $ip = $ctrl->[POS_E][0];
			my $needs_fixup = defined $last_seq_s && $ctrl->[RES][0] =~ weechat::color('reset');
			hyperlink_replay_code2($ctrl, $msg_r, \@starts, \@ends, \@cup, $fx, 'C');
			if ($needs_fixup) {
				$last_seq_s = $ip;
				hyperlink_replay_code1($line, $msg_r, (@cup && $last_seq_s == $cup[0] ? \$cu : \$se), 0,
				[$ip], \@starts, \@ends, \@cup, @{$ctrl}[POS_S,POS_E], $fx, 'F');
			}
		}
		--$max_loop; die 'endless loop' if $max_loop < 0;
	}
}

## selection_function -- select text with cursor
## $lines_ref - downloaded lines
sub selection_function {
	my ($lines_ref) = @_;
	$ACT_STR->[CUR] = [ -1, -1, -1, -1 ] unless defined ${$ACT_STR}[CUR];
	my $cur = $ACT_STR->[CUR];
	my $lineno = 0;
	my $fx;
	#open $fx, '>', ...;
	print $fx "cur: @$cur\n" if $fx;
	for my $line (@$lines_ref) {
		my $msg = fu8on $line->[LINE]{'message'};
		my ($ctrl_codes, $plain_msg) = capture_color_codes($msg);
		my ($sel_s, $sel_e) = (-1, -1);
		my $lcur = $cur->[0*2] == $lineno ? $cur->[0*2+1] : -2;
		my $msglen = length $plain_msg;
		$lcur = -1+$msglen if $lcur >= $msglen;
		if ($cur->[0*2] > -1 && $cur->[1*2] > -1) { # we have a selection
			if ($cur->[0*2] < $cur->[1*2]) { # cursor is on line before selection
				if ($cur->[0*2] == $lineno) {
					#($sel_s, $sel_e) = ($cur->[0*2+1]+1, $msglen);
					($sel_s, $sel_e) = ($lcur+1, $msglen);
				}
				elsif ($cur->[0*2] < $lineno && $cur->[1*2] > $lineno) {
					($sel_s, $sel_e) = ('0 but true', $msglen);
				}
				elsif ($cur->[1*2] == $lineno) {
					($sel_s, $sel_e) = ('0 but true', $cur->[1*2+1]);
				}
			}
			elsif ($cur->[0*2] > $cur->[1*2]) { # cursor is on line after selection
				if ($cur->[0*2] == $lineno) {
					($sel_s, $sel_e) = ('0 but true', $cur->[0*2+1]);
				}
				elsif ($cur->[0*2] > $lineno && $cur->[1*2] < $lineno) {
					($sel_s, $sel_e) = ('0 but true', $msglen);
				}
				elsif ($cur->[1*2] == $lineno) {
					($sel_s, $sel_e) = ($cur->[1*2+1], $msglen);
				}
			}
			elsif ($cur->[0*2] == $lineno && $cur->[1*2] == $lineno) { # cursor is on same line as selection
				if ($cur->[0*2+1] < $cur->[1*2+1]) {
					($sel_s, $sel_e) = ($cur->[0*2+1]+1, $cur->[1*2+1]);
				}
				elsif ($cur->[0*2+1] > $cur->[1*2+1]) {
					($sel_s, $sel_e) = ($cur->[1*2+1], $cur->[0*2+1]);
				}
				else {
					($sel_s, $sel_e) = ($cur->[0*2+1], $cur->[0*2+1]+1);
					$lcur = -2;
				}
			}
		}
		if ($sel_s && $sel_s == 0) {
			$sel_s+=0;
			my $hl = weechat::color('reverse');
			my $HL = weechat::color('-reverse');
			if ($line->[LINE]{'prefix'}) {
				my $ctrl = capture_color_codes(fu8on $line->[LINE]{'prefix'});
				substr $line->[LINE]{'prefix'}, $ctrl->[POS_E][-2], 0, $hl;
				substr $line->[LINE]{'prefix'}, $ctrl->[POS_E][-1]+length $hl, 0, $HL;
			}
		}
		$sel_e = $msglen if $sel_e > $msglen;
		($sel_s, $sel_e) = (-1, -1)
			if $sel_s == $sel_e;
		trace_to_u8($line);
		selection_replay_codes($line, $sel_s, \$plain_msg, $sel_e, $ctrl_codes, $lcur, $fx);
		$line->[LINE]{'message'} = $plain_msg;
		++$lineno;
	}
}

## switchmode -- toggle between modes
sub switchmode {
	$ACT_STR->[MODE] = $ACT_STR->[MODE] eq 'hyperlink' ? 'selection' : 'hyperlink';
	my ($r_, $R_) = (weechat::color('reverse'), weechat::color('-reverse'));
	my $I = '▐';
	my $t_flt = hyper_get_valid_keys('t');
	$t_flt =~ s/$_/$_/i for split '', $ACT_STR->[URL_TYPE_FILTER];
	weechat::buffer_set($ACT_STR->[BUFPTR], 'title',
		($ACT_STR->[MODE] eq 'hyperlink' ?
		    $r_.' ↑ ↓'.$R_.'move to url'.
			$I.$r_.'RET' .$I.$R_.'send open'.
			$I.$r_.'/'   .$I.$R_.'sel.mode'.
			$I.$r_.$t_flt.$I.$R_.
			$I.$r_.'q'   .$I.$R_.'close'
						:
		    $r_.'←↑→↓'.$R_.'move cursor'.
			$I.$r_.'SPC'   .$I.$R_.'start selection'.
			$I.$r_.'/'     .$I.$R_.'url mode'.
			$I.$r_.'q'     .$I.$R_.'close').' '.
		$r_.$I.$R_.
			 weechat::buffer_get_string($ACT_STR->[WINDOWS]{'buffer'}, 'short_name').
		$I);
}

## apply_keybindings -- set up key bindings for copy buffer
sub apply_keybindings {
	my @wee_keys = Nlib::i2h('key');
	my @keys;
	my $custom_keys = weechat::config_get_plugin('copywin_custom_keys');
	my %custom_keys;
	if ($custom_keys) {
	    %custom_keys = ('' => split /(\S+):/, $custom_keys);
	    for (keys %custom_keys) {
		$custom_keys{$_} = [ grep { length } split ' ', $custom_keys{$_} ];
	    }
	}
	@keys = map { $_->{'key'} }
		grep { $_->{'command'} eq '/input history_previous' ||
			   $_->{'command'} eq '/input history_global_previous' } @wee_keys;
	@keys = 'meta2-A' unless @keys;
	weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **-') for @keys, @{$custom_keys{'-'}//[]}; # up arrow

	@keys = map { $_->{'key'} }
		grep { $_->{'command'} eq '/input history_next' ||
			   $_->{'command'} eq '/input history_global_next' } @wee_keys;
	@keys = 'meta2-B' unless @keys;
	weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **+') for @keys, @{$custom_keys{'+'}//[]}; # down arrow

	@keys = map { $_->{'key'} }
		grep { $_->{'command'} eq '/input move_next_char' } @wee_keys;
	@keys = 'meta2-C' unless @keys;
	weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **>') for @keys, @{$custom_keys{'>'}//[]}; # right arrow

	@keys = map { $_->{'key'} }
		grep { $_->{'command'} eq '/input move_previous_char' } @wee_keys;
	@keys = 'meta2-D' unless @keys;
	weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **<') for @keys, @{$custom_keys{'<'}//[]}; # left arrow

	@keys = map { $_->{'key'} }
		grep { $_->{'command'} eq '/input move_next_word' } @wee_keys;
	@keys = 'meta-f' unless @keys;
	weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **f') for @keys, @{$custom_keys{f}//[]}; # back word

	@keys = map { $_->{'key'} }
		grep { $_->{'command'} eq '/input move_previous_word' } @wee_keys;
	@keys = 'meta-b' unless @keys;
	weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **b') for @keys, @{$custom_keys{b}//[]}; # forward word

	@keys = map { $_->{'key'} }
		grep { $_->{'command'} eq '/input move_end_of_line' } @wee_keys;
	@keys = 'ctrl-E' unless @keys;
	weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **e') for @keys, @{$custom_keys{e}//[]};

	@keys = map { $_->{'key'} }
		grep { $_->{'command'} eq '/input move_beginning_of_line' } @wee_keys;
	@keys = 'ctrl-A' unless @keys;
	weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **a') for @keys, @{$custom_keys{a}//[]};

	@keys = map { $_->{'key'} }
		grep { $_->{'command'} eq '/input return' || $_->{'command'} eq '/input magic_enter' } @wee_keys;
	@keys = 'ctrl-M' unless @keys;
	weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **!') for @keys, @{$custom_keys{'!'}//[]}; # enter key

	@keys = ('ctrl-@', ' ');
	weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **@') for @keys, @{$custom_keys{'@'}//[]}; # ctrl+space or ctrl+@

	for my $cmd (@SIMPLE_MODE_KEYS) {
	    weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **'.$cmd) for $cmd, @{$custom_keys{$cmd}//[]};
	}

	@keys = map { $_->{'key'} }
		grep { $_->{'command'} =~ "^/@{[CMD_COPYWIN]}" } @wee_keys;
	push @keys, 'q' unless @{$custom_keys{q}//[]};
	weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **q') for @keys, @{$custom_keys{q}//[]};
}

## binding_mouse_fix -- disable one key bindings on mouse input
## () - signal handler
## $data - signal has true value if mouse input in progress, false if mouse input finished
sub binding_mouse_fix {
	my (undef, undef, $data) = @_;
	return weechat::WEECHAT_RC_OK unless $ACT_STR && $ACT_STR->[BUFPTR] && weechat::current_buffer() eq $ACT_STR->[BUFPTR];
	my $custom_keys = weechat::config_get_plugin('copywin_custom_keys');
	my %custom_keys;
	if ($custom_keys) {
	    %custom_keys = ('' => split /(\S+):/, $custom_keys);
	    for (keys %custom_keys) {
		$custom_keys{$_} = [ grep { length } split ' ', $custom_keys{$_} ];
	    }
	}
	if ($data) {
	    weechat::buffer_set($ACT_STR->[BUFPTR], "key_unbind_$_", '') for ' ', @SIMPLE_MODE_KEYS, 'q',
		    grep { 1 == length } map { @$_ } values %custom_keys;
	}
	else {
		weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **@') for ' ';
		weechat::buffer_set($ACT_STR->[BUFPTR], 'key_bind_/', '/'.CMD_COPYWIN.' **/');
		for my $cmd (@SIMPLE_MODE_KEYS) {
		    weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **'.$cmd) for $cmd, grep { 1 == length } @{$custom_keys{$cmd}//[]};
		}
		for my $cmd (split //, '@!aebf<>+-/') {
		    weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **'.$cmd) for grep { 1 == length } @{$custom_keys{$cmd}//[]};
		}
		if (@{$custom_keys{q}//[]}) {
		    weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_$_", '/'.CMD_COPYWIN.' **q') for @{$custom_keys{q}//[]};
		}
		else {
		    weechat::buffer_set($ACT_STR->[BUFPTR], "key_bind_q", '/'.CMD_COPYWIN.' **q');
		}
	}
	weechat::WEECHAT_RC_OK
}

## hyper_get_valid_keys -- get keys for type filter according to enabled settings
## $res - 't' for title, 'u' for upcase
sub hyper_get_valid_keys {
	my ($res) = @_;
	$res = '' unless defined $res;
	my $title = $res eq 't';
	my $uc = $res eq 'u';
	my $keys = 'u';
	$keys .= 'n' if Nlib::has_true_value(weechat::config_get_plugin('hyper_nicks'));
	$keys .= 'c' if Nlib::has_true_value(weechat::config_get_plugin('hyper_channels'));
	$keys .= 'p' if Nlib::has_true_value(weechat::config_get_plugin('hyper_prefix'));
	$keys = uc $keys if $title || $uc;
	if ($title) {
		length $keys == 1 ? " $keys  " :
			length $keys == 2 ? (substr $keys, 0, 1).' '.(substr $keys, 1).' ' :
				length $keys == 3 ? "$keys " :
					$keys;
	}
	else {
		$keys = "[$keys]";
		qr/^$keys$/;
	}
}

## hyper_set_type_filter -- sets the type of links shown in url view based on setting and args
## $args - command line arguments
## returns new args
sub hyper_set_type_filter {
	my ($args) = @_;
	my $valid_keys = hyper_get_valid_keys();
	$ACT_STR->[URL_TYPE_FILTER] = join '',
		keys %{+{ map { $_ => undef } grep { /$valid_keys/ } map { lc substr $_, 0, 1 } map { split /[,;]/ } split ' ',
				  weechat::config_get_plugin('hyper_show') }};
	$args = '' unless defined $args;
	my $urlfilter = $args;
	if ($urlfilter =~ s/^\/// && length $urlfilter) {
		$args = '/';
		$ACT_STR->[URL_TYPE_FILTER] = join '',
			keys %{+{ map { $_ => undef } grep { /$valid_keys/ } map { lc } split '', $urlfilter }};
	}
	elsif (length $urlfilter) {
		$ACT_STR->[URL_TYPE_FILTER] = join '',
			keys %{+{ map { $_ => undef } grep { /$valid_keys/ } map { lc substr $_, 0, 1 }
						  map { split /[,;]/ } split ' ', $urlfilter }};
		$args = '/' if length $ACT_STR->[URL_TYPE_FILTER];
	}
	$ACT_STR->[URL_TYPE_FILTER] = 'u' unless length $ACT_STR->[URL_TYPE_FILTER];
	$args
}

## make_new_copybuf -- creates a new copywin buffer from the current window
## $args - arguments to command, can set mode
sub make_new_copybuf {
	my ($args) = @_;
	my $winptr = weechat::current_window();
	my ($wininfo) = Nlib::i2h('window', $winptr);

	my $copybuf = weechat::buffer_new('['.$wininfo->{'width'}.'x'.$wininfo->{'height'}.
									  '+'.$wininfo->{'x'}.'+'.$wininfo->{'y'}.']',
									  '', '', '', '');
	# apply scroll keeping to current buffer if possible
	weechat::hook_signal_send('buffer_sk', weechat::WEECHAT_HOOK_SIGNAL_POINTER, $copybuf);
	$STR{$copybuf} = $ACT_STR = [ $copybuf ];
	$ACT_STR->[LINES] = download_lines($wininfo);	
	$ACT_STR->[WINDOWS] = $wininfo;
	unless ($copybuf) {
		$ACT_STR->[MODE] = 'FAIL';
		return;
	}
	$ACT_STR->[MODE] = 'hyperlink';
	$args = hyper_set_type_filter($args);
	weechat::buffer_set($copybuf, 'short_name', weechat::config_get_plugin('copybuf_short_name')) if $copybuf;
	weechat::buffer_set($copybuf, 'type', 'free');
	apply_keybindings();
	switchmode();
	switchmode() if $args eq '/';
}

sub copywin_cmd {
	my (undef, $bufptr, $args, $int) = @_;

	if ($int) {
	}
	elsif ($args =~ s/^\*\*//) {
		return weechat::WEECHAT_RC_OK unless exists $STR{$bufptr};
		$ACT_STR = $STR{$bufptr};
		if ($args eq 'q') {
			weechat::buffer_close($ACT_STR->[BUFPTR]);
			return weechat::WEECHAT_RC_OK;
		}
		elsif ($args eq '/') {
			hyper_set_type_filter()
				unless length $ACT_STR->[URL_TYPE_FILTER];
			switchmode();
		}
		elsif ($args =~ hyper_get_valid_keys()) {
			switchmode() if $ACT_STR->[MODE] eq 'hyperlink';
			$ACT_STR->[URL_TYPE_FILTER] = $args;
			switchmode();
		}
		elsif ($args =~ hyper_get_valid_keys('u')) {
			switchmode() if $ACT_STR->[MODE] eq 'hyperlink';
			my $t = lc $args;
			$ACT_STR->[URL_TYPE_FILTER] .= $t
				unless $ACT_STR->[URL_TYPE_FILTER] =~ s/$t//;
			switchmode() if length $ACT_STR->[URL_TYPE_FILTER];
		}
		elsif ($ACT_STR->[MODE] eq 'hyperlink') {
			return weechat::WEECHAT_RC_OK
				unless hyperlink_dispatch_input($args);
		}
		else {
			return weechat::WEECHAT_RC_OK
				unless selection_dispatch_input($args);			
		}
	}
	elsif ($args =~ /^\s*help\s*$/i) {
		Nlib::read_manpage(SCRIPT_FILE, SCRIPT_NAME);
		return weechat::WEECHAT_RC_OK
	}
	else {
		check_layout()
			unless $LAYOUT_OK;
		make_new_copybuf($args);
	}
	
	my $wininfo = $ACT_STR->[WINDOWS];
	my $lines_ref2 = copy_lines($ACT_STR->[LINES]);
	my $copybuf = $ACT_STR->[BUFPTR];

	if ($ACT_STR->[MODE] eq 'hyperlink') {
		hyperlink_function($lines_ref2);
	}
	elsif ($ACT_STR->[MODE] eq 'selection') {
		selection_function($lines_ref2);
	}


	my $printy = calc_free_image($wininfo, $lines_ref2);

	for my $i (0..$#$printy) {
		weechat::print_y($copybuf, $i, $printy->[$i]);
	}

	weechat::buffer_set($copybuf, 'display', 'auto');
	weechat::WEECHAT_RC_OK
}

sub real_screen {
	my ($screen_cursor, $text) = @_;
	my $l = length $text;
	for (my ($i, $j) = (0, 0); $i < $l; ++$i) {
		$j += screen_length(substr $text, $i, 1);
		return $i if $j > $screen_cursor;
	}
	$l
}

## mouse_coords_to_cursor -- calculate in line cursor according to rendering
## $r - local (in window) row of mouse cursor
## $c - local (in window) column of mouse cursor
## returns line index and position in plain text if found
sub mouse_coords_to_cursor {
	my ($r, $c) = @_; # wanted row & column

	my $wininfo = $ACT_STR->[WINDOWS];
	my $lines_ref = copy_lines($ACT_STR->[LINES]);

	my ($i, $l, $p) = (1, 0, 0); # current row, current line index, current line position
	my $first_line = $wininfo->{'start_line_pos'};
	my $prev_line;
	for my $line (@$lines_ref) {
		trace_to_u8($line);
		my $max_length = [(length fu8on $line->[LINE]{'message'}),''];
		my @splits = ([0,''], message_splits($line), $max_length, $max_length);
		for (0 .. $line->[LAYOUT]{'count'}-1) {
			shift @splits if $splits[0][0] - $splits[1][0] >= 0;
			last if $i > $wininfo->{'chat_height'};
			my $subm = substr $line->[LINE]{'message'}, $splits[0][0], $splits[1][0]-$splits[0][0];
			my $subm_u = fu8on weechat::string_remove_color($subm, '');
			my $subm_length = length $subm_u;
			if ($_ >= $first_line) {
				if ($r == $i) {
					my $subm_length_screen = screen_length $subm_u;
					my $construction = '';
					unless ($_) {
						show_time($line, \$construction);
						return ($l, undef, 'time')
							if (screen_length fu8on weechat::string_remove_color($construction, '')) >= $c;
						show_buffername($line, \$construction);
						return ($l, undef, 'buffername')
							if (screen_length fu8on weechat::string_remove_color($construction, '')) >= $c;
						show_prefix($line, \$construction, $prev_line);
						return ($l, undef, 'prefix')
							if (screen_length fu8on weechat::string_remove_color($construction, '')) >= $c;
						$prev_line = $line if exists $line->[LINE]{'date'};
					}
					show_separator($line, \$construction, $_);
					my $message_start_screen = screen_length fu8on weechat::string_remove_color($construction, '');
					return ($l) if $message_start_screen >= $c;
					if ($splits[0][1] =~ /[bn]/ && $subm =~ s/^( +)//) {
						my $l = length $1;
						$p += $l;
						$subm_u = substr $subm_u, $l;
					}
					if ($subm_length_screen >= $c - 1 - $message_start_screen) {
						return ($l, $p + real_screen($c - 1 - $message_start_screen, $subm_u));
					}
				}
				++$i;
			}
			$p += $subm_length;
			shift @splits;
		}
		$first_line = 0;
		++$l; $p = 0;
	}
}

## copywin_autoclose -- close copywin buffer
## () - this small function is a timer handler
sub copywin_autoclose {
	weechat::buffer_close($ACT_STR->[BUFPTR])
			if $ACT_STR && $ACT_STR->[MOUSE_AUTOMODE];
	$autoclose_in_progress = undef;
	weechat::WEECHAT_RC_OK
}

## get_autoclose_delay -- checks delay for autoclose of copywin buffer
## returns delay or false if autoclose is disabled
sub get_autoclose_delay {
	my $autoclose_delay = weechat::config_get_plugin('mouse.close_on_release');
	if (Nlib::has_false_value($autoclose_delay)) { 0 }
	elsif ($autoclose_delay =~ /\D/ || $autoclose_delay < 100) { 100 }
	else { $autoclose_delay }
}

## get_2nd_click_delay -- checks additional delay for autoclose of copywin buffer if 2nd click is needed to open url
## returns additional delay
sub get_2nd_click_delay {
	my $conf_2nd_click_delay = weechat::config_get_plugin('mouse.url_open_2nd_click');
	if (Nlib::has_false_value($conf_2nd_click_delay)) { 0 }
	elsif ($conf_2nd_click_delay =~ /\D/) { 2000 }
	else { $conf_2nd_click_delay }
}

## get_nick_2nd_click_delay -- checks additional delay for autoclose of copywin buffer if 2nd click is needed to query nick
## returns additional delay
sub get_nick_2nd_click_delay {
	my $conf_2nd_click_delay = weechat::config_get_plugin('mouse.nick_2nd_click');
	if (Nlib::has_false_value($conf_2nd_click_delay)) { 0 }
	elsif ($conf_2nd_click_delay =~ /\D/) { 500 }
	else { $conf_2nd_click_delay }
}

## mouse_window_at_pointer -- switch to window where clicked
## $row - mouse row
## $col - mouse column
## $wininfo - currently active window info
## returns true value if successful
sub mouse_window_at_pointer {
	my ($row, $col, $wininfo) = @_;
	my @all_windows = Nlib::i2h('window');
	my $in_any_win;
	for (@all_windows) {
		$in_any_win = $_ if Nlib::in_window($row, $col, $_);
	}
	return unless $in_any_win;
	my $steps = 0;
	for (@all_windows) {
		weechat::command('', '/window +1');
		++$steps;
		last if weechat::current_window() eq $in_any_win->{'pointer'};
	}
	my ($act_win_ind) = map { $_->[0] }
		grep { $_->[-1]{'pointer'} eq $wininfo->{'pointer'} }
			do {
				my $i = 0;
				map { [ $i++, $_ ] } @all_windows
			};
	my ($click_win_ind) = map { $_->[0] }
		grep { $_->[-1]{'pointer'} eq $in_any_win->{'pointer'} }
			do {
				my $i = 0;
				map { [ $i++, $_ ] } @all_windows
			};
	#weechat::print('',"steps:$steps, a:$act_win_ind, c:$click_win_ind, d:@{[$act_win_ind-$click_win_ind]}/@{[$click_win_ind-$act_win_ind]} #:".scalar @all_windows);
	1
}

## mouse_scroll_action -- handle mouse scroll
## () - signal passed on from mouse event handler
## $_[2] - mouse code
## $row - mouse row
## $col - mouse column
sub mouse_scroll_action {
	my (undef, undef, undef, $row, $col) = @_;
	return weechat::WEECHAT_RC_OK unless Nlib::has_true_value(weechat::config_get_plugin('mouse.handle_scroll'));

	my $winptr = weechat::current_window();
	my ($wininfo) = Nlib::i2h('window', $winptr);

	my $had_to_switch_win;

	unless (Nlib::in_window($row, $col, $wininfo)) {
		if (Nlib::has_true_value(weechat::config_get_plugin('mouse.scroll_inactive_pane'))) {
			return weechat::WEECHAT_RC_OK
				unless mouse_window_at_pointer($row, $col, $wininfo);
			$had_to_switch_win = $winptr;
			$winptr = weechat::current_window();
			($wininfo) = Nlib::i2h('window', $winptr);
		}
		else {
			if ($_[2] =~ /^`/) { weechat::command('', '/window scroll_up'); } #`
			elsif ($_[2] =~ /^a/) { weechat::command('', '/window scroll_down'); }			
		}
	}

	if (Nlib::in_window($row, $col, $wininfo)) {
		if (Nlib::in_chat_window($row, $col, $wininfo)) {
			if ($_[2] =~ /^`/) { weechat::command('', '/window scroll_up'); } #`
			elsif ($_[2] =~ /^a/) { weechat::command('', '/window scroll_down'); }
		}
		elsif (my $bar_infos = Nlib::find_bar_window($row, $col)) {
			my $dir = $bar_infos->[-1]{'filling_'.
		   			 ($bar_infos->[-1]{'position'} <= 1 ?
					  'top_bottom' : 'left_right')} ? 'y' : 'x';
			if ($_[2] =~ /^`/) { $dir .= '-' } #`
			elsif ($_[2] =~ /^a/) { $dir .= '+' }
			for ($bar_infos->[-1]{'name'}) {
				weechat::command('', '/bar scroll '.$_.' * '.$dir.'10%')
						if ($_ eq 'title' or $_ eq 'status' or $_ eq 'nicklist')
			}
		}
	}

	if ($had_to_switch_win) {
		my $steps = 0;
		for (Nlib::i2h('window')) {
			weechat::command('', '/window +1');
			++$steps;
			last if weechat::current_window() eq $had_to_switch_win;
		}
	}
	weechat::WEECHAT_RC_OK
}

## drag_speed_hack -- delay drag events
## () - timer handler
sub drag_speed_hack {
	$drag_speed_timer = undef;
	mouse_evt(undef, undef, $_[0]);
	weechat::WEECHAT_RC_OK
}

sub input_text_hlsel {
	my (undef, undef, $bufptr, $text) = @_;
	return $text unless defined $input_sel && $input_sel->[0] eq $bufptr;
	my ($ctrl_codes, $plain_msg) = capture_color_codes($text);
	my $npos = weechat::buffer_get_integer($bufptr, 'input_pos');
	my ($sel_s, $sel_e) = sort { $a <=> $b } ($npos, $input_sel->[-1]);
	return $text unless $sel_s < $sel_e;
	selection_replay_codes([[]], $sel_s, \$plain_msg, $sel_e, $ctrl_codes, -2);
	$plain_msg
}

sub window_of_bar {
	my ($barinfo) = @_;
	my @all_windows = Nlib::i2h('window');
	my $in_any_win;
	for (@all_windows) {
		$in_any_win = $_ if Nlib::in_window(1+$barinfo->[0]{'y'}, 1+$barinfo->[0]{'x'}, $_);
	}
	$in_any_win
}

## mouse_input_sel -- mouse select in input bar
## () - signal passed on from mouse event handler
## $_[2] - mouse code
## $row - mouse row
## $col - mouse column
sub mouse_input_sel {
	my (undef, undef, undef, $row, $col) = @_;
	my $plain;
	if (my $bar_infos = Nlib::find_bar_window($row, $col)) {
		if ($bar_infos->[-1]{'name'} eq 'input') {
			my $win = window_of_bar($bar_infos);
			my ($error, $idx, $content, $coords) = Nlib::bar_item_get_subitem_at
				($bar_infos, 'input_text', $col - $bar_infos->[0]{'x'}, $row - $bar_infos->[0]{'y'});
			if ($coords) {
				my $col_pos = $coords->[1]-$coords->[0] + ($coords->[4]-$coords->[3])*$bar_infos->[0]{'width'};
				$plain = fu8on weechat::string_remove_color($content, '');
				$input_sel = [ $win->{'buffer'}, real_screen($col_pos, $plain) ];
			}
			elsif ($error eq 'outside') {
				$plain = fu8on weechat::string_remove_color($content, '');
				$input_sel = [ $win->{'buffer'}, length $plain ];
			}
		}
	}
	if (defined $plain) {
		if ($_[2] =~ /^ /) {
			weechat::buffer_set($input_sel->[0], 'input_pos', $input_sel->[-1]);
		}
		else {
			my $npos = weechat::buffer_get_integer($input_sel->[0], 'input_pos');
			my ($sel_s, $sel_e) = sort { $a <=> $b } ($npos, $input_sel->[-1]);
			send_clip((substr $plain, $sel_s, $sel_e-$sel_s))
				if $sel_s < $sel_e;
		}
	}
	$input_sel = undef if $_[2] =~ /^#/;
}

## mouse_click_into_copy -- click when not in copy mode
## () - signal passed on from mouse event handler
## $_[2] - mouse code
## $row - mouse row
## $col - mouse column
## returns true value if copy mode should be turned on
sub mouse_click_into_copy {
	my (undef, undef, undef, $row, $col) = @_;

	mouse_input_sel(@_[0..2], $row, $col);
	return unless $_[2] =~ /^ /;

	my $winptr = weechat::current_window();
	my ($wininfo) = Nlib::i2h('window', $winptr);

	unless (Nlib::in_window($row, $col, $wininfo)) {
		return unless Nlib::has_true_value(weechat::config_get_plugin('mouse.click_select_pane'));
		return unless mouse_window_at_pointer($row, $col, $wininfo);
		$winptr = weechat::current_window();
		($wininfo) = Nlib::i2h('window', $winptr);
		return unless Nlib::has_true_value(weechat::config_get_plugin('mouse.click_through_pane'));
	}
	return unless Nlib::has_true_value(weechat::config_get_plugin('mouse.copy_on_click'));
	return unless Nlib::in_chat_window($row, $col, $wininfo);
	1
}

## mouse_delay_drag -- delay drag events
## () - signal passed on from mouse event handler
## $_[2] - mouse code
## returns true if this event should be delayed
sub mouse_delay_drag {
	my $drag_speed = weechat::config_get_plugin('mouse.drag_speed');
	unless (Nlib::has_false_value($drag_speed)) {
		$drag_speed = 50 if $drag_speed =~ /\D/;
		weechat::unhook($drag_speed_timer) if defined $drag_speed_timer;
		$drag_speed_timer = weechat::hook_timer($drag_speed, 0, 1, 'drag_speed_hack', $_[2]);
		return 1;
	}
	undef
}

## delayed_nick_menu -- open the nick menu
## () - timer handler
sub delayed_nick_menu {
	$delayed_nick_menu_timer = undef;
	weechat::command($ACT_STR->[WINDOWS]{'buffer'}, "/menu nick $_[0]")
			if $ACT_STR && Nlib::i2h('hook', '', 'command,menu');
	weechat::WEECHAT_RC_OK
}

## mouse_evt -- handle mouse clicks
## () - signal handler
## $_[2] - mouse code
sub mouse_evt {
	Encode::_utf8_on($_[2]);

	my $this_last_mouse_seq = $last_mouse_seq || '';
	if ($_[1] && $this_last_mouse_seq =~ /^@/ && $_[2] =~ /^@/) {
		return weechat::WEECHAT_RC_OK if mouse_delay_drag(@_[0..2])
	}
	$last_mouse_seq = $_[2];

	#return weechat::WEECHAT_RC_OK unless defined $ACT_STR && $ACT_STR->[BUFPTR] eq weechat::current_buffer();
	
	my $curbufptr = weechat::current_buffer();

    if ($_[2] =~ /^[#@ `a](.)(.)$/) {
        my $row = ord($2)-32;
		my $col = ord($1)-32;

		weechat::unhook($delayed_nick_menu_timer) if defined $delayed_nick_menu_timer;
		$delayed_nick_menu_timer = undef;

		return mouse_scroll_action(@_[0..2], $row, $col) if $_[2] =~ /^[`a]/;

		if (!defined $ACT_STR || $ACT_STR->[BUFPTR] ne $curbufptr) {
			return weechat::WEECHAT_RC_OK
				unless mouse_click_into_copy(@_[0..2], $row, $col);
			copywin_cmd(undef, $curbufptr, '/');
			return weechat::WEECHAT_RC_OK unless $ACT_STR->[BUFPTR] eq weechat::current_buffer();
			$ACT_STR->[MOUSE_AUTOMODE] = 1;
		}
		$ACT_STR->[CUR] = [ -1, -1, -1, -1 ] unless defined ${$ACT_STR}[CUR];

		my $winptr = weechat::current_window();
		my ($wininfo) = Nlib::i2h('window', $winptr);
		return weechat::WEECHAT_RC_OK unless Nlib::in_chat_window($row, $col, $wininfo);

		my $lrow = $row - $wininfo->{'chat_y'};
		my $lcol = $col - $wininfo->{'chat_x'};
		#weechat::print_y($ACT_STR->[BUFPTR], $lrow-1, ' 'x($lcol-1).'x'.$lrow.'/'.$lcol);
		my @cursor = mouse_coords_to_cursor($lrow, $lcol);
		#weechat::print($ACT_STR->[WINDOWS]{'buffer'}, 'cursor: '.join ',', map { defined $_ ? $_ : '?' } @cursor );

		my $one_click;
		if (@cursor == 3 && $cursor[2] eq 'prefix' &&
				$this_last_mouse_seq =~ /^ / && $_[2] =~ /^#/ &&
					((substr $this_last_mouse_seq, 1) eq (substr $_[2], 1))) { # click

			my @link = grep { $_->[-1][URL_LINE] == $cursor[LINE] &&
								  $_->[-1][URL_S] == -1 && $_->[-1][URL_E] == -1 }
				do {
					my $i = 0;
					map { [ $i++, $_ ] } @{$ACT_STR->[URLS]}
				};

			if (@link == 1) {
				$ACT_STR->[A_LINK] = $link[0][0];
				my $t = substr $ACT_STR->[URLS][$ACT_STR->[A_LINK]][URL_INFO]{'type'}, 0, 1;
				unless ($ACT_STR->[URL_TYPE_FILTER] =~ $t) {
					switchmode();
					$ACT_STR->[URL_TYPE_FILTER] = $t;
					switchmode();
				}
			}
			if ($ACT_STR->[MODE] eq 'hyperlink' && @link == 1) {
				hyperlink_to_clip();

				my ($nick) = grep { /^nick_/ } @{ $ACT_STR->[LINES][$cursor[LINE]][LINE]{'tag'} };
				if (defined $nick && $nick =~ s/^nick_//) {
					if (Nlib::has_false_value(weechat::config_get_plugin('mouse.nick_2nd_click'))) {
						delayed_nick_menu($nick);
					}
					elsif ($mouse_2nd_click && $mouse_2nd_click->[0] eq 'nick' &&
							   $mouse_2nd_click->[1] == $ACT_STR && $mouse_2nd_click->[2] eq $ACT_STR->[A_LINK]) {
						weechat::command($ACT_STR->[WINDOWS]{'buffer'}, "/query $nick");
					}
					elsif (!$mouse_2nd_click) {
						$one_click = [ 'nick', $ACT_STR, $ACT_STR->[A_LINK] ];
						$delayed_nick_menu_timer = weechat::hook_timer(get_nick_2nd_click_delay(), 0, 1, 'delayed_nick_menu', $nick);
					}
					else {
						delayed_nick_menu($nick);
					}
				}
			}
			copywin_cmd(undef, $ACT_STR->[BUFPTR], '**', 1);
		}

		unless (@cursor == 2) { # no valid text here
			$mouse_2nd_click = $one_click if $_[2] =~ /^#/;
			my $autoclose_delay = get_autoclose_delay();
			$autoclose_delay += get_nick_2nd_click_delay() if $one_click;
			$autoclose_in_progress = weechat::hook_timer($autoclose_delay, 0, 1, 'copywin_autoclose', '')
					if $autoclose_delay && $ACT_STR->[MOUSE_AUTOMODE] && $_[2] =~ /^#/ && !$autoclose_in_progress;
			return weechat::WEECHAT_RC_OK;
		}

		if ($_[2] =~ /^ /) { # button down
			@{$ACT_STR->[CUR]}[2,3] = (-1, -1);
			weechat::unhook($autoclose_in_progress) if $autoclose_in_progress;
			$autoclose_in_progress = undef;
		}
		elsif ($this_last_mouse_seq =~ /^ / &&
			   ($_[2] =~ /^@/ ||
				($_[2] =~ /^#/ && ((substr $this_last_mouse_seq, 1) ne (substr $_[2], 1)))
			   )) { # switch to drag
			switchmode() if $ACT_STR->[MOUSE_AUTOMODE] && $ACT_STR->[MODE] eq 'hyperlink';
			selection_dispatch_input('@');
		}
		if ($this_last_mouse_seq =~ /^ / && $_[2] =~ /^#/ &&
				((substr $this_last_mouse_seq, 1) eq (substr $_[2], 1))) { # click
			if ($mouse_2nd_click && $mouse_2nd_click->[0] eq 'sel' &&
				 $mouse_2nd_click->[1] == $ACT_STR) {
			}
			elsif (!$mouse_2nd_click) {
				$one_click = [ 'sel', $ACT_STR, [ @cursor ] ];
			}
		}
		@{$ACT_STR->[CUR]}[0,1] = @cursor;
		if ($mouse_2nd_click && $mouse_2nd_click->[0] eq 'sel' &&
			$mouse_2nd_click->[1] == $ACT_STR &&
			$ACT_STR->[MODE] eq 'selection' &&
			$ACT_STR->[CUR][2] >= 0 && $ACT_STR->[CUR][3] >= 0) {
			{ # fix cursor to word boundary
			my $msg = $ACT_STR->[LINES][$ACT_STR->[CUR][0]][LINE]{'message'};
			my $plain_msg = fu8on weechat::string_remove_color($msg, '');
			my @breaks;
			push @breaks, @- while $plain_msg =~ /\b/g;
			if (($ACT_STR->[CUR][0] == $mouse_2nd_click->[-1][0] && $ACT_STR->[CUR][1] > $mouse_2nd_click->[-1][1])
			   || $ACT_STR->[CUR][0] > $mouse_2nd_click->[-1][0]) { #forward
				$ACT_STR->[CUR][1] = (grep { $_ >= $ACT_STR->[CUR][1] } @breaks)[0];
				unless (defined $ACT_STR->[CUR][1]) {
					my $msglen = length $plain_msg;
					$ACT_STR->[CUR][1] = $msglen;
				}
			}
			else { #backward
				$ACT_STR->[CUR][1] = (grep { $_ <= $ACT_STR->[CUR][1] } @breaks)[-1];
				unless (defined $ACT_STR->[CUR][1]) {
					$ACT_STR->[CUR][1] = 0;
				}
			} }
		
			{ # fix selection to word boundary
			my $msg = $ACT_STR->[LINES][$mouse_2nd_click->[-1][0]][LINE]{'message'};
			my $plain_msg = fu8on weechat::string_remove_color($msg, '');
			my @breaks;
			push @breaks, @- while $plain_msg =~ /\b/g;
			if (($mouse_2nd_click->[-1][0] == $ACT_STR->[CUR][0] && $mouse_2nd_click->[-1][1] > $ACT_STR->[CUR][1])
			   || $mouse_2nd_click->[-1][0] > $ACT_STR->[CUR][0]) { #forward
				$ACT_STR->[CUR][3] = (grep { $_ >= $mouse_2nd_click->[-1][1] } @breaks)[0];
				unless (defined $ACT_STR->[CUR][3]) {
					my $msglen = length $plain_msg;
					$ACT_STR->[CUR][3] = $msglen;
				}
			}
			else { #backward
				$ACT_STR->[CUR][3] = (grep { $_ <= $mouse_2nd_click->[-1][1] } @breaks)[-1];
				unless (defined $ACT_STR->[CUR][3]) {
					$ACT_STR->[CUR][3] = 0;
				}
			} }
		}
		my @link = grep { $_->[-1][URL_LINE] == $cursor[LINE] &&
						  $_->[-1][URL_S] <= $cursor[1] && $_->[-1][URL_E] > $cursor[1] }
			do {
				my $i = 0;
				map { [ $i++, $_ ] } @{$ACT_STR->[URLS]}
			};

# 		open my $fx, '>', ...;
# 		print $fx "link:@link cur:@cursor\n";
# 		map { print $fx $_->[0].'#url_line:'.$_->[-1][URL_LINE].' url_s:'.$_->[-1][URL_S].' url_e:'.$_->[-1][URL_E].' url:'.$_->[-1][URL]."\n" }
# 			do {
# 				my $i = 0;
# 				map { [ $i++, $_ ] } @{$ACT_STR->[URLS]}
# 			};

		if (@link == 1) {
			$ACT_STR->[A_LINK] = $link[0][0];
			my $t = substr $ACT_STR->[URLS][$ACT_STR->[A_LINK]][URL_INFO]{'type'}, 0, 1;
			unless ($ACT_STR->[URL_TYPE_FILTER] =~ $t) {
				switchmode();
				$ACT_STR->[URL_TYPE_FILTER] = $t;
				switchmode();
			}
		}
		if ($ACT_STR->[MODE] eq 'hyperlink' && @link == 1) {
			hyperlink_to_clip();
			if ($this_last_mouse_seq =~ /^ / && $_[2] =~ /^#/ &&
				((substr $this_last_mouse_seq, 1) eq (substr $_[2], 1))) { # click
				my $link_type = $ACT_STR->[URLS][$ACT_STR->[A_LINK]][URL_INFO]{'type'};
				if ($link_type eq 'nick') {
					my $nick = $ACT_STR->[URLS][$ACT_STR->[A_LINK]][URL];
					if (Nlib::has_false_value(weechat::config_get_plugin('mouse.nick_2nd_click'))) {
						delayed_nick_menu($nick);
					}
					elsif ($mouse_2nd_click && $mouse_2nd_click->[0] eq 'link' &&
						   $mouse_2nd_click->[1] == $ACT_STR && $mouse_2nd_click->[2] == $ACT_STR->[A_LINK]) {
						weechat::command($ACT_STR->[WINDOWS]{'buffer'}, "/query $nick");
					}
					elsif (!$mouse_2nd_click) {
						$one_click = [ 'link', $ACT_STR, $ACT_STR->[A_LINK] ];
						$delayed_nick_menu_timer = weechat::hook_timer(get_nick_2nd_click_delay(), 0, 1, 'delayed_nick_menu', $nick);
					}
					else {
						delayed_nick_menu($nick);
					}
				}
				elsif ($link_type eq 'channel') {
					my $channel = $ACT_STR->[URLS][$ACT_STR->[A_LINK]][URL];
					if ($mouse_2nd_click && $mouse_2nd_click->[0] eq 'link' &&
					    $mouse_2nd_click->[1] == $ACT_STR && $mouse_2nd_click->[2] == $ACT_STR->[A_LINK]) {
						weechat::command($ACT_STR->[WINDOWS]{'buffer'}, "/join $channel");
					}
					elsif (!$mouse_2nd_click) {
						$one_click = [ 'link', $ACT_STR, $ACT_STR->[A_LINK] ];
					}
				}
				else {
					if (Nlib::has_false_value(weechat::config_get_plugin('mouse.url_open_2nd_click')) ||
							($mouse_2nd_click && $mouse_2nd_click->[0] eq 'link' &&
							 $mouse_2nd_click->[1] == $ACT_STR && $mouse_2nd_click->[2] == $ACT_STR->[A_LINK])) {
						hyperlink_dispatch_input('!');
					}
					elsif (!$mouse_2nd_click) {
						$one_click = [ 'link', $ACT_STR, $ACT_STR->[A_LINK] ];
					}
				}
			}
		}
		else {
			selection_to_clip();
		}
		copywin_cmd(undef, $ACT_STR->[BUFPTR], '**', 1);
		if ($_[2] =~ /^#/) { # button up
			$mouse_2nd_click = $one_click;
			my $autoclose_delay = get_autoclose_delay();
			if ($one_click && $one_click->[0] eq 'link') {
				if ($one_click->[1][URLS][$one_click->[2]][URL_INFO]{'type'} eq 'nick') {
					$autoclose_delay += get_nick_2nd_click_delay();
				}
				else {
					$autoclose_delay += get_2nd_click_delay();
				}
			}
			$autoclose_in_progress = weechat::hook_timer($autoclose_delay, 0, 1, 'copywin_autoclose', '')
					if $autoclose_delay && $ACT_STR->[MOUSE_AUTOMODE] && !$autoclose_in_progress;
		}
	}
	weechat::WEECHAT_RC_OK
}

sub hsignal_evt {
	my %data = %{$_[2]};
	if ($data{_key} =~ /^(.*)-event-/) {
		my $msg = "\@chat($data{_buffer_full_name}):$1";
		for my $k (Nlib::i2h('key', '', 'mouse')) {
			next if $k->{'key'} =~ /-event/;
			(my $match = '^'.(quotemeta $k->{'key'})) =~ s/\\\*/.*/g;
			my $close = $msg =~ $match;
			last if $close and $k->{'command'} =~ /hsignal:@{[SCRIPT_NAME]}/;
			return weechat::WEECHAT_RC_OK if $close;
		}
	}
	if ($data{_key} =~ /-event-down/) {
		mouse_evt(undef, undef, join '', ' ', (pack 'U', 33+$data{_x2}), (pack 'U', 33+$data{_y2}));
		$hsignal_mouse_down_sent = 1;
	}
	elsif ($data{_key} =~ /-event-drag/) {
		mouse_evt(undef, undef, join '', '@', (pack 'U', 33+$data{_x2}), (pack 'U', 33+$data{_y2}));
	}
	else {
		mouse_evt(undef, undef, join '', ' ', (pack 'U', 33+$data{_x}), (pack 'U', 33+$data{_y}))
			unless $hsignal_mouse_down_sent;
		mouse_evt(undef, undef, join '', '#', (pack 'U', 33+$data{_x2}), (pack 'U', 33+$data{_y2}));
		$hsignal_mouse_down_sent = undef;
	}
	weechat::WEECHAT_RC_OK
}

## check_layout -- check if this weechat version knows about layout infolist
sub check_layout {
	return weechat::WEECHAT_RC_OK if $LAYOUT_OK;
#	my $winptr = weechat::current_window();
#	my ($wininfo) = Nlib::i2h('window', $winptr);
#	my ($lineinfo) = Nlib::i2h('buffer_lines', @{$wininfo}{'buffer','start_line'});
#	my ($layoutinfo) = Nlib::i2h('layout', $wininfo->{'pointer'}, $lineinfo->{'line'}, $listptr);
#	Nlib::l2l($listptr, 1);
# 	unless ($layoutinfo) {
# 		weechat::print('', weechat::prefix('error').
# 					   "You will need to have layout trace support in your WeeChat. Get it at\n".
# 					   " http://anti.teamidiot.de/static/nei/*/Code/WeeChat/display_traces.diff");
# 	}
# 	else {
		$LAYOUT_OK = 1;
#	}
# 	if (($ENV{'STY'} || $ENV{'TERM'} =~ /screen/) && !exists $ENV{'TMUX'}) {
# 		weechat::print('', weechat::prefix('error').
# 					  "Your terminal most likely doesn't support the selection clipboard control.");
# 	}
	weechat::WEECHAT_RC_OK
}


## garbage_str -- remove copywin storage when copywin buffer is closed
## () - signal handler
## $bufptr - signal comes with pointer of closed buffer
sub garbage_str {
	my (undef, undef, $bufptr) = @_;
	$ACT_STR = undef if $ACT_STR && $ACT_STR->[BUFPTR] eq $bufptr;
	delete $STR{$bufptr};
	weechat::WEECHAT_RC_OK
}

## decouple_mouse_scroll -- add coords script as mouse scrolling handler
sub decouple_mouse_scroll {
	my $main = weechat::buffer_search_main();
	my $mouse_scroll_ext = weechat::buffer_string_replace_local_var($main, '$mousescroll');
	unless (grep { $_ eq SCRIPT_NAME } split ',', $mouse_scroll_ext) {
		$script_old_mouse_scroll = $mouse_scroll_ext;
		if ($mouse_scroll_ext =~ /^\$/) {
			weechat::buffer_set($main, 'localvar_set_mousescroll', SCRIPT_NAME);
		}
		else {
			weechat::buffer_set($main, 'localvar_set_mousescroll', $mouse_scroll_ext.','.SCRIPT_NAME);
		}
	}
}

## restore_mouse_scroll -- restore mouse scrolling handler
sub restore_mouse_scroll {
	if (defined $script_old_mouse_scroll) {
		my $main = weechat::buffer_search_main();
		if ($script_old_mouse_scroll =~ /^\$/) {
			weechat::buffer_set($main, 'localvar_del_mousescroll', '');
		}
		else {
			weechat::buffer_set($main, 'localvar_set_mousescroll',
								join ',', grep { $_ ne SCRIPT_NAME } split ',', $script_old_mouse_scroll);
		}
	}
}

## default_options -- set up default option values on start and when unset
sub default_options {
	my %defaults = (
		url_braces => '[({<"'."''".'">})]',
		url_regex  =>
			 '\w+://\S+ | '.
			 '(?:^|(?<=\s))(?:\S+\.){2,}\w{2,5}(?:/\S*|(?=\s)|$) | '.
			 '(?:^|(?<=\s))(?:\S+\.)+\w{2,5}/(?:\S+)?',
		url_non_endings    => '[.,;:?!_-]',
		url_non_beginnings => '\W',
		hyper_nicks    => 'off',
		hyper_channels => 'off',
		hyper_prefix   => 'on',
		hyper_show     => 'url',
		use_nick_menu  => 'off',
		xterm_compatible => 'rxvt-uni',
		'mouse.copy_on_click'        => 'on',
		'mouse.close_on_release'     => '110',
		'mouse.click_select_pane'    => 'on',
		'mouse.click_through_pane'   => 'off',
		'mouse.url_open_2nd_click'   => 'off',
		'mouse.handle_scroll'        => 'off',
		'mouse.scroll_inactive_pane' => 'on',
		copybuf_short_name => '©',
		'color.selection_cursor' => 'reverse.underline',
		'color.selection'        => 'reverse.brown,black',
		'color.url_highlight'        => 'reverse.underline',
		'color.url_highlight_active' => 'reverse.brown,black',
	);
	for (keys %defaults) {
		weechat::config_set_plugin($_, $defaults{$_})
			unless weechat::config_is_set_plugin($_);
	}
	my $sf = SCRIPT_FILE;
	for (Nlib::get_settings_from_pod($sf)) {
		weechat::config_set_desc_plugin($_, Nlib::get_desc_from_pod($sf, $_));
	}
	if (Nlib::has_true_value(weechat::config_get_plugin('mouse.handle_scroll'))) {
		decouple_mouse_scroll();
	}
	else {
		restore_mouse_scroll();
	}
	weechat::WEECHAT_RC_OK
}

sub close_copywin {
	copywin_cmd(undef, $ACT_STR->[BUFPTR], '**q') if $ACT_STR;
	weechat::WEECHAT_RC_OK
}

sub init_coords {
	$listptr = weechat::list_new();
	weechat::hook_timer(1000, 0, 1, 'check_layout', '');
	default_options();
	weechat::WEECHAT_RC_OK
}

sub stop_coords {
	close_copywin();
	restore_mouse_scroll();
	weechat::list_free($listptr);
	weechat::WEECHAT_RC_OK
}