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
|
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read a list of people who contributed.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* Implementation of popup windows. See ":help popup".
*/
#include "vim.h"
#ifdef FEAT_TEXT_PROP
typedef struct {
char *pp_name;
poppos_T pp_val;
} poppos_entry_T;
static poppos_entry_T poppos_entries[] = {
{"botleft", POPPOS_BOTLEFT},
{"topleft", POPPOS_TOPLEFT},
{"botright", POPPOS_BOTRIGHT},
{"topright", POPPOS_TOPRIGHT},
{"center", POPPOS_CENTER}
};
/*
* Get option value for "key", which is "line" or "col".
* Handles "cursor+N" and "cursor-N".
*/
static int
popup_options_one(dict_T *dict, char_u *key)
{
dictitem_T *di;
char_u *val;
char_u *s;
char_u *endp;
int n = 0;
di = dict_find(dict, key, -1);
if (di == NULL)
return 0;
val = tv_get_string(&di->di_tv);
if (STRNCMP(val, "cursor", 6) != 0)
return dict_get_number_check(dict, key);
setcursor_mayforce(TRUE);
s = val + 6;
if (*s != NUL)
{
endp = s;
if (*skipwhite(s) == '+' || *skipwhite(s) == '-')
n = strtol((char *)s, (char **)&endp, 10);
if (endp != NULL && *skipwhite(endp) != NUL)
{
semsg(_(e_invexpr2), val);
return 0;
}
}
if (STRCMP(key, "line") == 0)
n = screen_screenrow() + 1 + n;
else // "col"
n = screen_screencol() + 1 + n;
if (n < 1)
n = 1;
return n;
}
static void
get_pos_options(win_T *wp, dict_T *dict)
{
char_u *str;
int nr;
nr = popup_options_one(dict, (char_u *)"line");
if (nr > 0)
wp->w_wantline = nr;
nr = popup_options_one(dict, (char_u *)"col");
if (nr > 0)
wp->w_wantcol = nr;
wp->w_popup_fixed = dict_get_number(dict, (char_u *)"fixed") != 0;
str = dict_get_string(dict, (char_u *)"pos", FALSE);
if (str != NULL)
{
for (nr = 0;
nr < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
++nr)
if (STRCMP(str, poppos_entries[nr].pp_name) == 0)
{
wp->w_popup_pos = poppos_entries[nr].pp_val;
nr = -1;
break;
}
if (nr != -1)
semsg(_(e_invarg2), str);
}
}
static void
set_padding_border(dict_T *dict, int *array, char *name, int max_val)
{
dictitem_T *di;
di = dict_find(dict, (char_u *)name, -1);
if (di != NULL)
{
if (di->di_tv.v_type != VAR_LIST)
emsg(_(e_listreq));
else
{
list_T *list = di->di_tv.vval.v_list;
listitem_T *li;
int i;
int nr;
for (i = 0; i < 4; ++i)
array[i] = 1;
if (list != NULL)
for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
++i, li = li->li_next)
{
nr = (int)tv_get_number(&li->li_tv);
if (nr >= 0)
array[i] = nr > max_val ? max_val : nr;
}
}
}
}
/*
* Used when popup options contain "moved": set default moved values.
*/
static void
set_moved_values(win_T *wp)
{
wp->w_popup_curwin = curwin;
wp->w_popup_lnum = curwin->w_cursor.lnum;
wp->w_popup_mincol = curwin->w_cursor.col;
wp->w_popup_maxcol = curwin->w_cursor.col;
}
/*
* Used when popup options contain "moved" with "word" or "WORD".
*/
static void
set_moved_columns(win_T *wp, int flags)
{
char_u *ptr;
int len = find_ident_under_cursor(&ptr, flags | FIND_NOERROR);
if (len > 0)
{
wp->w_popup_mincol = (int)(ptr - ml_get_curline());
wp->w_popup_maxcol = wp->w_popup_mincol + len - 1;
}
}
/*
* Return TRUE if "row"/"col" is on the border of the popup.
* The values are relative to the top-left corner.
*/
int
popup_on_border(win_T *wp, int row, int col)
{
return (row == 0 && wp->w_popup_border[0] > 0)
|| (row == popup_height(wp) - 1 && wp->w_popup_border[2] > 0)
|| (col == 0 && wp->w_popup_border[3] > 0)
|| (col == popup_width(wp) - 1 && wp->w_popup_border[1] > 0);
}
// Values set when dragging a popup window starts.
static int drag_start_row;
static int drag_start_col;
static int drag_start_wantline;
static int drag_start_wantcol;
/*
* Mouse down on border of popup window: start dragging it.
* Uses mouse_col and mouse_row.
*/
void
popup_start_drag(win_T *wp)
{
drag_start_row = mouse_row;
drag_start_col = mouse_col;
// TODO: handle using different corner
if (wp->w_wantline == 0)
drag_start_wantline = wp->w_winrow + 1;
else
drag_start_wantline = wp->w_wantline;
if (wp->w_wantcol == 0)
drag_start_wantcol = wp->w_wincol + 1;
else
drag_start_wantcol = wp->w_wantcol;
// Stop centering the popup
if (wp->w_popup_pos == POPPOS_CENTER)
wp->w_popup_pos = POPPOS_TOPLEFT;
}
/*
* Mouse moved while dragging a popup window: adjust the window popup position.
*/
void
popup_drag(win_T *wp)
{
// The popup may be closed before dragging stops.
if (!win_valid_popup(wp))
return;
wp->w_wantline = drag_start_wantline + (mouse_row - drag_start_row);
if (wp->w_wantline < 1)
wp->w_wantline = 1;
if (wp->w_wantline > Rows)
wp->w_wantline = Rows;
wp->w_wantcol = drag_start_wantcol + (mouse_col - drag_start_col);
if (wp->w_wantcol < 1)
wp->w_wantcol = 1;
if (wp->w_wantcol > Columns)
wp->w_wantcol = Columns;
popup_adjust_position(wp);
}
#if defined(FEAT_TIMERS)
static void
popup_add_timeout(win_T *wp, int time)
{
char_u cbbuf[50];
char_u *ptr = cbbuf;
typval_T tv;
vim_snprintf((char *)cbbuf, sizeof(cbbuf),
"{_ -> popup_close(%d)}", wp->w_id);
if (get_lambda_tv(&ptr, &tv, TRUE) == OK)
{
wp->w_popup_timer = create_timer(time, 0);
wp->w_popup_timer->tr_callback = get_callback(&tv);
clear_tv(&tv);
}
}
#endif
/*
* Shared between popup_create() and f_popup_move().
*/
static void
apply_move_options(win_T *wp, dict_T *d)
{
int nr;
if ((nr = dict_get_number(d, (char_u *)"minwidth")) > 0)
wp->w_minwidth = nr;
if ((nr = dict_get_number(d, (char_u *)"minheight")) > 0)
wp->w_minheight = nr;
if ((nr = dict_get_number(d, (char_u *)"maxwidth")) > 0)
wp->w_maxwidth = nr;
if ((nr = dict_get_number(d, (char_u *)"maxheight")) > 0)
wp->w_maxheight = nr;
get_pos_options(wp, d);
}
/*
* Shared between popup_create() and f_popup_setoptions().
*/
static void
apply_general_options(win_T *wp, dict_T *dict)
{
dictitem_T *di;
int nr;
char_u *str;
// TODO: flip
di = dict_find(dict, (char_u *)"firstline", -1);
if (di != NULL)
wp->w_firstline = dict_get_number(dict, (char_u *)"firstline");
if (wp->w_firstline < 1)
wp->w_firstline = 1;
str = dict_get_string(dict, (char_u *)"title", FALSE);
if (str != NULL)
{
vim_free(wp->w_popup_title);
wp->w_popup_title = vim_strsave(str);
}
di = dict_find(dict, (char_u *)"wrap", -1);
if (di != NULL)
{
nr = dict_get_number(dict, (char_u *)"wrap");
wp->w_p_wrap = nr != 0;
}
di = dict_find(dict, (char_u *)"drag", -1);
if (di != NULL)
wp->w_popup_drag = dict_get_number(dict, (char_u *)"drag");
str = dict_get_string(dict, (char_u *)"highlight", FALSE);
if (str != NULL)
set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
str, OPT_FREE|OPT_LOCAL, 0);
set_padding_border(dict, wp->w_popup_padding, "padding", 999);
set_padding_border(dict, wp->w_popup_border, "border", 1);
di = dict_find(dict, (char_u *)"borderhighlight", -1);
if (di != NULL)
{
if (di->di_tv.v_type != VAR_LIST)
emsg(_(e_listreq));
else
{
list_T *list = di->di_tv.vval.v_list;
listitem_T *li;
int i;
if (list != NULL)
for (i = 0, li = list->lv_first; i < 4 && i < list->lv_len;
++i, li = li->li_next)
{
str = tv_get_string(&li->li_tv);
if (*str != NUL)
wp->w_border_highlight[i] = vim_strsave(str);
}
if (list->lv_len == 1 && wp->w_border_highlight[0] != NULL)
for (i = 1; i < 4; ++i)
wp->w_border_highlight[i] =
vim_strsave(wp->w_border_highlight[0]);
}
}
di = dict_find(dict, (char_u *)"borderchars", -1);
if (di != NULL)
{
if (di->di_tv.v_type != VAR_LIST)
emsg(_(e_listreq));
else
{
list_T *list = di->di_tv.vval.v_list;
listitem_T *li;
int i;
if (list != NULL)
for (i = 0, li = list->lv_first; i < 8 && i < list->lv_len;
++i, li = li->li_next)
{
str = tv_get_string(&li->li_tv);
if (*str != NUL)
wp->w_border_char[i] = mb_ptr2char(str);
}
if (list->lv_len == 1)
for (i = 1; i < 8; ++i)
wp->w_border_char[i] = wp->w_border_char[0];
if (list->lv_len == 2)
{
for (i = 4; i < 8; ++i)
wp->w_border_char[i] = wp->w_border_char[1];
for (i = 1; i < 4; ++i)
wp->w_border_char[i] = wp->w_border_char[0];
}
}
}
di = dict_find(dict, (char_u *)"zindex", -1);
if (di != NULL)
{
wp->w_zindex = dict_get_number(dict, (char_u *)"zindex");
if (wp->w_zindex < 1)
wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
if (wp->w_zindex > 32000)
wp->w_zindex = 32000;
}
di = dict_find(dict, (char_u *)"mask", -1);
if (di != NULL)
{
int ok = TRUE;
if (di->di_tv.v_type != VAR_LIST)
ok = FALSE;
else if (di->di_tv.vval.v_list != NULL)
{
listitem_T *li;
for (li = di->di_tv.vval.v_list->lv_first; li != NULL;
li = li->li_next)
{
if (li->li_tv.v_type != VAR_LIST
|| li->li_tv.vval.v_list == NULL
|| li->li_tv.vval.v_list->lv_len != 4)
{
ok = FALSE;
break;
}
}
}
if (ok)
{
wp->w_popup_mask = di->di_tv.vval.v_list;
++wp->w_popup_mask->lv_refcount;
}
else
semsg(_(e_invargval), "mask");
}
#if defined(FEAT_TIMERS)
// Add timer to close the popup after some time.
nr = dict_get_number(dict, (char_u *)"time");
if (nr > 0)
popup_add_timeout(wp, nr);
#endif
di = dict_find(dict, (char_u *)"moved", -1);
if (di != NULL)
{
set_moved_values(wp);
if (di->di_tv.v_type == VAR_STRING && di->di_tv.vval.v_string != NULL)
{
char_u *s = di->di_tv.vval.v_string;
int flags = 0;
if (STRCMP(s, "word") == 0)
flags = FIND_IDENT | FIND_STRING;
else if (STRCMP(s, "WORD") == 0)
flags = FIND_STRING;
else if (STRCMP(s, "any") != 0)
semsg(_(e_invarg2), s);
if (flags != 0)
set_moved_columns(wp, flags);
}
else if (di->di_tv.v_type == VAR_LIST
&& di->di_tv.vval.v_list != NULL
&& di->di_tv.vval.v_list->lv_len == 2)
{
list_T *l = di->di_tv.vval.v_list;
wp->w_popup_mincol = tv_get_number(&l->lv_first->li_tv);
wp->w_popup_maxcol = tv_get_number(&l->lv_first->li_next->li_tv);
}
else
semsg(_(e_invarg2), tv_get_string(&di->di_tv));
}
di = dict_find(dict, (char_u *)"filter", -1);
if (di != NULL)
{
callback_T callback = get_callback(&di->di_tv);
if (callback.cb_name != NULL)
{
free_callback(&wp->w_filter_cb);
set_callback(&wp->w_filter_cb, &callback);
}
}
di = dict_find(dict, (char_u *)"callback", -1);
if (di != NULL)
{
callback_T callback = get_callback(&di->di_tv);
if (callback.cb_name != NULL)
{
free_callback(&wp->w_close_cb);
set_callback(&wp->w_close_cb, &callback);
}
}
}
/*
* Go through the options in "dict" and apply them to popup window "wp".
*/
static void
apply_options(win_T *wp, dict_T *dict)
{
int nr;
apply_move_options(wp, dict);
apply_general_options(wp, dict);
nr = dict_get_number(dict, (char_u *)"hidden");
if (nr > 0)
{
wp->w_popup_flags |= POPF_HIDDEN;
--wp->w_buffer->b_nwindows;
}
popup_mask_refresh = TRUE;
}
/*
* Add lines to the popup from a list of strings.
*/
static void
add_popup_strings(buf_T *buf, list_T *l)
{
listitem_T *li;
linenr_T lnum = 0;
char_u *p;
for (li = l->lv_first; li != NULL; li = li->li_next)
if (li->li_tv.v_type == VAR_STRING)
{
p = li->li_tv.vval.v_string;
ml_append_buf(buf, lnum++,
p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
}
}
/*
* Add lines to the popup from a list of dictionaries.
*/
static void
add_popup_dicts(buf_T *buf, list_T *l)
{
listitem_T *li;
listitem_T *pli;
linenr_T lnum = 0;
char_u *p;
dict_T *dict;
// first add the text lines
for (li = l->lv_first; li != NULL; li = li->li_next)
{
if (li->li_tv.v_type != VAR_DICT)
{
emsg(_(e_dictreq));
return;
}
dict = li->li_tv.vval.v_dict;
p = dict == NULL ? NULL
: dict_get_string(dict, (char_u *)"text", FALSE);
ml_append_buf(buf, lnum++,
p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
}
// add the text properties
lnum = 1;
for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
{
dictitem_T *di;
list_T *plist;
dict = li->li_tv.vval.v_dict;
di = dict_find(dict, (char_u *)"props", -1);
if (di != NULL)
{
if (di->di_tv.v_type != VAR_LIST)
{
emsg(_(e_listreq));
return;
}
plist = di->di_tv.vval.v_list;
if (plist != NULL)
{
for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
{
if (pli->li_tv.v_type != VAR_DICT)
{
emsg(_(e_dictreq));
return;
}
dict = pli->li_tv.vval.v_dict;
if (dict != NULL)
{
int col = dict_get_number(dict, (char_u *)"col");
prop_add_common( lnum, col, dict, buf, NULL);
}
}
}
}
}
}
/*
* Return the height of popup window "wp", including border and padding.
*/
int
popup_height(win_T *wp)
{
return wp->w_height
+ wp->w_popup_padding[0] + wp->w_popup_border[0]
+ wp->w_popup_padding[2] + wp->w_popup_border[2];
}
/*
* Return the width of popup window "wp", including border and padding.
*/
int
popup_width(win_T *wp)
{
return wp->w_width
+ wp->w_popup_padding[3] + wp->w_popup_border[3]
+ wp->w_popup_padding[1] + wp->w_popup_border[1];
}
/*
* Get the padding plus border at the top, adjusted to 1 if there is a title.
*/
static int
popup_top_extra(win_T *wp)
{
int extra = wp->w_popup_border[0] + wp->w_popup_padding[0];
if (extra == 0 && wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
return 1;
return extra;
}
/*
* Adjust the position and size of the popup to fit on the screen.
*/
void
popup_adjust_position(win_T *wp)
{
linenr_T lnum;
int wrapped = 0;
int maxwidth;
int center_vert = FALSE;
int center_hor = FALSE;
int allow_adjust_left = !wp->w_popup_fixed;
int top_extra = popup_top_extra(wp);
int right_extra = wp->w_popup_border[1] + wp->w_popup_padding[1];
int bot_extra = wp->w_popup_border[2] + wp->w_popup_padding[2];
int left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
int extra_height = top_extra + bot_extra;
int extra_width = left_extra + right_extra;
int org_winrow = wp->w_winrow;
int org_wincol = wp->w_wincol;
int org_width = wp->w_width;
int org_height = wp->w_height;
int minwidth;
wp->w_winrow = 0;
wp->w_wincol = 0;
if (wp->w_popup_pos == POPPOS_CENTER)
{
// center after computing the size
center_vert = TRUE;
center_hor = TRUE;
}
else
{
if (wp->w_wantline == 0)
center_vert = TRUE;
else if (wp->w_popup_pos == POPPOS_TOPLEFT
|| wp->w_popup_pos == POPPOS_TOPRIGHT)
{
wp->w_winrow = wp->w_wantline - 1;
if (wp->w_winrow >= Rows)
wp->w_winrow = Rows - 1;
}
if (wp->w_wantcol == 0)
center_hor = TRUE;
else if (wp->w_popup_pos == POPPOS_TOPLEFT
|| wp->w_popup_pos == POPPOS_BOTLEFT)
{
wp->w_wincol = wp->w_wantcol - 1;
if (wp->w_wincol >= Columns - 3)
wp->w_wincol = Columns - 3;
}
}
// When centering or right aligned, use maximum width.
// When left aligned use the space available, but shift to the left when we
// hit the right of the screen.
maxwidth = Columns - wp->w_wincol - left_extra;
if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
{
allow_adjust_left = FALSE;
maxwidth = wp->w_maxwidth;
}
// start at the desired first line
wp->w_topline = wp->w_firstline;
if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count)
wp->w_topline = wp->w_buffer->b_ml.ml_line_count;
// Compute width based on longest text line and the 'wrap' option.
// Use a minimum width of one, so that something shows when there is no
// text.
// TODO: more accurate wrapping
wp->w_width = 1;
for (lnum = wp->w_topline; lnum <= wp->w_buffer->b_ml.ml_line_count; ++lnum)
{
int len = vim_strsize(ml_get_buf(wp->w_buffer, lnum, FALSE));
if (wp->w_p_wrap)
{
while (len > maxwidth)
{
++wrapped;
len -= maxwidth;
wp->w_width = maxwidth;
}
}
else if (len > maxwidth
&& allow_adjust_left
&& (wp->w_popup_pos == POPPOS_TOPLEFT
|| wp->w_popup_pos == POPPOS_BOTLEFT))
{
// adjust leftwise to fit text on screen
int shift_by = len - maxwidth;
if (shift_by > wp->w_wincol)
{
int truncate_shift = shift_by - wp->w_wincol;
len -= truncate_shift;
shift_by -= truncate_shift;
}
wp->w_wincol -= shift_by;
maxwidth += shift_by;
wp->w_width = maxwidth;
}
if (wp->w_width < len)
wp->w_width = len;
// do not use the width of lines we're not going to show
if (wp->w_maxheight > 0 && wp->w_buffer->b_ml.ml_line_count
- wp->w_topline + 1 + wrapped > wp->w_maxheight)
break;
}
minwidth = wp->w_minwidth;
if (wp->w_popup_title != NULL && *wp->w_popup_title != NUL)
{
int title_len = vim_strsize(wp->w_popup_title) + 2 - extra_width;
if (minwidth < title_len)
minwidth = title_len;
}
if (minwidth > 0 && wp->w_width < minwidth)
wp->w_width = minwidth;
if (wp->w_width > maxwidth)
wp->w_width = maxwidth;
if (center_hor)
{
wp->w_wincol = (Columns - wp->w_width - extra_width) / 2;
if (wp->w_wincol < 0)
wp->w_wincol = 0;
}
else if (wp->w_popup_pos == POPPOS_BOTRIGHT
|| wp->w_popup_pos == POPPOS_TOPRIGHT)
{
// Right aligned: move to the right if needed.
// No truncation, because that would change the height.
if (wp->w_width + extra_width < wp->w_wantcol)
wp->w_wincol = wp->w_wantcol - (wp->w_width + extra_width);
}
wp->w_height = wp->w_buffer->b_ml.ml_line_count - wp->w_topline
+ 1 + wrapped;
if (wp->w_minheight > 0 && wp->w_height < wp->w_minheight)
wp->w_height = wp->w_minheight;
if (wp->w_maxheight > 0 && wp->w_height > wp->w_maxheight)
wp->w_height = wp->w_maxheight;
if (wp->w_height > Rows - wp->w_winrow)
wp->w_height = Rows - wp->w_winrow;
if (center_vert)
{
wp->w_winrow = (Rows - wp->w_height - extra_height) / 2;
if (wp->w_winrow < 0)
wp->w_winrow = 0;
}
else if (wp->w_popup_pos == POPPOS_BOTRIGHT
|| wp->w_popup_pos == POPPOS_BOTLEFT)
{
if ((wp->w_height + extra_height) <= wp->w_wantline)
// bottom aligned: may move down
wp->w_winrow = wp->w_wantline - (wp->w_height + extra_height);
else
// not enough space, make top aligned
wp->w_winrow = wp->w_wantline + 1;
}
wp->w_popup_last_changedtick = CHANGEDTICK(wp->w_buffer);
// Need to update popup_mask if the position or size changed.
// And redraw windows that were behind the popup.
if (org_winrow != wp->w_winrow
|| org_wincol != wp->w_wincol
|| org_width != wp->w_width
|| org_height != wp->w_height)
{
redraw_all_later(VALID);
popup_mask_refresh = TRUE;
}
}
typedef enum
{
TYPE_NORMAL,
TYPE_ATCURSOR,
TYPE_NOTIFICATION,
TYPE_DIALOG,
TYPE_MENU
} create_type_T;
/*
* Make "buf" empty and set the contents to "text".
* Used by popup_create() and popup_settext().
*/
static void
popup_set_buffer_text(buf_T *buf, typval_T text)
{
int lnum;
// Clear the buffer, then replace the lines.
curbuf = buf;
for (lnum = buf->b_ml.ml_line_count; lnum > 0; --lnum)
ml_delete(lnum, FALSE);
curbuf = curwin->w_buffer;
// Add text to the buffer.
if (text.v_type == VAR_STRING)
{
// just a string
ml_append_buf(buf, 0, text.vval.v_string, (colnr_T)0, TRUE);
}
else
{
list_T *l = text.vval.v_list;
if (l->lv_len > 0)
{
if (l->lv_first->li_tv.v_type == VAR_STRING)
// list of strings
add_popup_strings(buf, l);
else
// list of dictionaries
add_popup_dicts(buf, l);
}
}
// delete the line that was in the empty buffer
curbuf = buf;
ml_delete(buf->b_ml.ml_line_count, FALSE);
curbuf = curwin->w_buffer;
}
/*
* popup_create({text}, {options})
* popup_atcursor({text}, {options})
*/
static win_T *
popup_create(typval_T *argvars, typval_T *rettv, create_type_T type)
{
win_T *wp;
tabpage_T *tp = NULL;
int tabnr;
buf_T *buf;
dict_T *d;
int nr;
int i;
// Check arguments look OK.
if (!(argvars[0].v_type == VAR_STRING && argvars[0].vval.v_string != NULL)
&& !(argvars[0].v_type == VAR_LIST && argvars[0].vval.v_list != NULL))
{
emsg(_(e_listreq));
return NULL;
}
if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
{
emsg(_(e_dictreq));
return NULL;
}
d = argvars[1].vval.v_dict;
if (dict_find(d, (char_u *)"tabpage", -1) != NULL)
tabnr = (int)dict_get_number(d, (char_u *)"tabpage");
else if (type == TYPE_NOTIFICATION)
tabnr = -1; // notifications are global by default
else
tabnr = 0;
if (tabnr > 0)
{
tp = find_tabpage(tabnr);
if (tp == NULL)
{
semsg(_("E996: Tabpage not found: %d"), tabnr);
return NULL;
}
}
// Create the window and buffer.
wp = win_alloc_popup_win();
if (wp == NULL)
return NULL;
rettv->vval.v_number = wp->w_id;
wp->w_popup_pos = POPPOS_TOPLEFT;
buf = buflist_new(NULL, NULL, (linenr_T)0, BLN_NEW|BLN_LISTED|BLN_DUMMY);
if (buf == NULL)
return NULL;
ml_open(buf);
win_init_popup_win(wp, buf);
set_local_options_default(wp);
set_string_option_direct_in_buf(buf, (char_u *)"buftype", -1,
(char_u *)"popup", OPT_FREE|OPT_LOCAL, 0);
set_string_option_direct_in_buf(buf, (char_u *)"bufhidden", -1,
(char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
buf->b_p_ul = -1; // no undo
buf->b_p_swf = FALSE; // no swap file
buf->b_p_bl = FALSE; // unlisted buffer
buf->b_locked = TRUE;
wp->w_p_wrap = TRUE; // 'wrap' is default on
// Avoid that 'buftype' is reset when this buffer is entered.
buf->b_p_initialized = TRUE;
if (tp != NULL)
{
// popup on specified tab page
wp->w_next = tp->tp_first_popupwin;
tp->tp_first_popupwin = wp;
}
else if (tabnr == 0)
{
// popup on current tab page
wp->w_next = curtab->tp_first_popupwin;
curtab->tp_first_popupwin = wp;
}
else // (tabnr < 0)
{
win_T *prev = first_popupwin;
// Global popup: add at the end, so that it gets displayed on top of
// older ones with the same zindex. Matters for notifications.
if (first_popupwin == NULL)
first_popupwin = wp;
else
{
while (prev->w_next != NULL)
prev = prev->w_next;
prev->w_next = wp;
}
}
popup_set_buffer_text(buf, argvars[0]);
if (type == TYPE_ATCURSOR)
{
wp->w_popup_pos = POPPOS_BOTLEFT;
setcursor_mayforce(TRUE);
wp->w_wantline = screen_screenrow();
if (wp->w_wantline == 0) // cursor in first line
{
wp->w_wantline = 2;
wp->w_popup_pos = POPPOS_TOPLEFT;
}
wp->w_wantcol = screen_screencol() + 1;
set_moved_values(wp);
set_moved_columns(wp, FIND_STRING);
}
// set default values
wp->w_zindex = POPUPWIN_DEFAULT_ZINDEX;
if (type == TYPE_NOTIFICATION)
{
win_T *twp, *nextwin;
int height = buf->b_ml.ml_line_count + 3;
// Try to not overlap with another global popup. Guess we need 3
// more screen lines than buffer lines.
wp->w_wantline = 1;
for (twp = first_popupwin; twp != NULL; twp = nextwin)
{
nextwin = twp->w_next;
if (twp != wp
&& twp->w_zindex == POPUPWIN_NOTIFICATION_ZINDEX
&& twp->w_winrow <= wp->w_wantline - 1 + height
&& twp->w_winrow + popup_height(twp) > wp->w_wantline - 1)
{
// move to below this popup and restart the loop to check for
// overlap with other popups
wp->w_wantline = twp->w_winrow + popup_height(twp) + 1;
nextwin = first_popupwin;
}
}
if (wp->w_wantline + height > Rows)
{
// can't avoid overlap, put on top in the hope that message goes
// away soon.
wp->w_wantline = 1;
}
wp->w_wantcol = 10;
wp->w_zindex = POPUPWIN_NOTIFICATION_ZINDEX;
wp->w_minwidth = 20;
wp->w_popup_drag = 1;
for (i = 0; i < 4; ++i)
wp->w_popup_border[i] = 1;
wp->w_popup_padding[1] = 1;
wp->w_popup_padding[3] = 1;
nr = syn_name2id((char_u *)"PopupNotification");
set_string_option_direct_in_win(wp, (char_u *)"wincolor", -1,
(char_u *)(nr == 0 ? "WarningMsg" : "PopupNotification"),
OPT_FREE|OPT_LOCAL, 0);
}
if (type == TYPE_DIALOG || type == TYPE_MENU)
{
wp->w_popup_pos = POPPOS_CENTER;
wp->w_zindex = POPUPWIN_DIALOG_ZINDEX;
wp->w_popup_drag = 1;
for (i = 0; i < 4; ++i)
{
wp->w_popup_border[i] = 1;
wp->w_popup_padding[i] = 1;
}
}
if (type == TYPE_MENU)
{
typval_T tv;
callback_T callback;
tv.v_type = VAR_STRING;
tv.vval.v_string = (char_u *)"popup_filter_menu";
callback = get_callback(&tv);
if (callback.cb_name != NULL)
set_callback(&wp->w_filter_cb, &callback);
wp->w_p_wrap = 0;
}
for (i = 0; i < 4; ++i)
VIM_CLEAR(wp->w_border_highlight[i]);
for (i = 0; i < 8; ++i)
wp->w_border_char[i] = 0;
// Deal with options.
apply_options(wp, argvars[1].vval.v_dict);
#ifdef FEAT_TIMERS
if (type == TYPE_NOTIFICATION && wp->w_popup_timer == NULL)
popup_add_timeout(wp, 3000);
#endif
popup_adjust_position(wp);
wp->w_vsep_width = 0;
redraw_all_later(NOT_VALID);
popup_mask_refresh = TRUE;
return wp;
}
/*
* popup_clear()
*/
void
f_popup_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
{
close_all_popups();
}
/*
* popup_create({text}, {options})
*/
void
f_popup_create(typval_T *argvars, typval_T *rettv)
{
popup_create(argvars, rettv, TYPE_NORMAL);
}
/*
* popup_atcursor({text}, {options})
*/
void
f_popup_atcursor(typval_T *argvars, typval_T *rettv)
{
popup_create(argvars, rettv, TYPE_ATCURSOR);
}
/*
* Invoke the close callback for window "wp" with value "result".
* Careful: The callback may make "wp" invalid!
*/
static void
invoke_popup_callback(win_T *wp, typval_T *result)
{
typval_T rettv;
int dummy;
typval_T argv[3];
argv[0].v_type = VAR_NUMBER;
argv[0].vval.v_number = (varnumber_T)wp->w_id;
if (result != NULL && result->v_type != VAR_UNKNOWN)
copy_tv(result, &argv[1]);
else
{
argv[1].v_type = VAR_NUMBER;
argv[1].vval.v_number = 0;
}
argv[2].v_type = VAR_UNKNOWN;
call_callback(&wp->w_close_cb, -1,
&rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
if (result != NULL)
clear_tv(&argv[1]);
clear_tv(&rettv);
}
/*
* Close popup "wp" and invoke any close callback for it.
*/
static void
popup_close_and_callback(win_T *wp, typval_T *arg)
{
int id = wp->w_id;
if (wp->w_close_cb.cb_name != NULL)
// Careful: This may make "wp" invalid.
invoke_popup_callback(wp, arg);
popup_close(id);
}
/*
* In a filter: check if the typed key is a mouse event that is used for
* dragging the popup.
*/
static void
filter_handle_drag(win_T *wp, int c, typval_T *rettv)
{
int row = mouse_row;
int col = mouse_col;
if (wp->w_popup_drag
&& is_mouse_key(c)
&& (wp == popup_dragwin
|| wp == mouse_find_win(&row, &col, FIND_POPUP)))
// do not consume the key, allow for dragging the popup
rettv->vval.v_number = 0;
}
static void
popup_highlight_curline(win_T *wp)
{
int id;
char buf[100];
match_delete(wp, 1, FALSE);
id = syn_name2id((char_u *)"PopupSelected");
vim_snprintf(buf, sizeof(buf), "\\%%%dl.*", (int)wp->w_cursor.lnum);
match_add(wp, (char_u *)(id == 0 ? "PmenuSel" : "PopupSelected"),
(char_u *)buf, 10, 1, NULL, NULL);
}
/*
* popup_filter_menu({text}, {options})
*/
void
f_popup_filter_menu(typval_T *argvars, typval_T *rettv)
{
int id = tv_get_number(&argvars[0]);
win_T *wp = win_id2wp(id);
char_u *key = tv_get_string(&argvars[1]);
typval_T res;
int c;
linenr_T old_lnum;
// If the popup has been closed do not consume the key.
if (wp == NULL)
return;
c = *key;
if (c == K_SPECIAL && key[1] != NUL)
c = TO_SPECIAL(key[1], key[2]);
// consume all keys until done
rettv->vval.v_number = 1;
res.v_type = VAR_NUMBER;
old_lnum = wp->w_cursor.lnum;
if ((c == 'k' || c == 'K' || c == K_UP) && wp->w_cursor.lnum > 1)
--wp->w_cursor.lnum;
if ((c == 'j' || c == 'J' || c == K_DOWN)
&& wp->w_cursor.lnum < wp->w_buffer->b_ml.ml_line_count)
++wp->w_cursor.lnum;
if (old_lnum != wp->w_cursor.lnum)
{
popup_highlight_curline(wp);
return;
}
if (c == 'x' || c == 'X' || c == ESC || c == Ctrl_C)
{
// Cancelled, invoke callback with -1
res.vval.v_number = -1;
popup_close_and_callback(wp, &res);
return;
}
if (c == ' ' || c == K_KENTER || c == CAR || c == NL)
{
// Invoke callback with current index.
res.vval.v_number = wp->w_cursor.lnum;
popup_close_and_callback(wp, &res);
return;
}
filter_handle_drag(wp, c, rettv);
}
/*
* popup_filter_yesno({text}, {options})
*/
void
f_popup_filter_yesno(typval_T *argvars, typval_T *rettv)
{
int id = tv_get_number(&argvars[0]);
win_T *wp = win_id2wp(id);
char_u *key = tv_get_string(&argvars[1]);
typval_T res;
int c;
// If the popup has been closed don't consume the key.
if (wp == NULL)
return;
c = *key;
if (c == K_SPECIAL && key[1] != NUL)
c = TO_SPECIAL(key[1], key[2]);
// consume all keys until done
rettv->vval.v_number = 1;
if (c == 'y' || c == 'Y')
res.vval.v_number = 1;
else if (c == 'n' || c == 'N' || c == 'x' || c == 'X' || c == ESC)
res.vval.v_number = 0;
else
{
filter_handle_drag(wp, c, rettv);
return;
}
// Invoke callback
res.v_type = VAR_NUMBER;
popup_close_and_callback(wp, &res);
}
/*
* popup_dialog({text}, {options})
*/
void
f_popup_dialog(typval_T *argvars, typval_T *rettv)
{
popup_create(argvars, rettv, TYPE_DIALOG);
}
/*
* popup_menu({text}, {options})
*/
void
f_popup_menu(typval_T *argvars, typval_T *rettv)
{
win_T *wp = popup_create(argvars, rettv, TYPE_MENU);
if (wp != NULL)
popup_highlight_curline(wp);
}
/*
* popup_notification({text}, {options})
*/
void
f_popup_notification(typval_T *argvars, typval_T *rettv)
{
popup_create(argvars, rettv, TYPE_NOTIFICATION);
}
/*
* Find the popup window with window-ID "id".
* If the popup window does not exist NULL is returned.
* If the window is not a popup window, and error message is given.
*/
static win_T *
find_popup_win(int id)
{
win_T *wp = win_id2wp(id);
if (wp != NULL && !bt_popup(wp->w_buffer))
{
semsg(_("E993: window %d is not a popup window"), id);
return NULL;
}
return wp;
}
/*
* popup_close({id})
*/
void
f_popup_close(typval_T *argvars, typval_T *rettv UNUSED)
{
int id = (int)tv_get_number(argvars);
win_T *wp = find_popup_win(id);
if (wp != NULL)
popup_close_and_callback(wp, &argvars[1]);
}
/*
* popup_hide({id})
*/
void
f_popup_hide(typval_T *argvars, typval_T *rettv UNUSED)
{
int id = (int)tv_get_number(argvars);
win_T *wp = find_popup_win(id);
if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) == 0)
{
wp->w_popup_flags |= POPF_HIDDEN;
--wp->w_buffer->b_nwindows;
redraw_all_later(NOT_VALID);
popup_mask_refresh = TRUE;
}
}
/*
* popup_show({id})
*/
void
f_popup_show(typval_T *argvars, typval_T *rettv UNUSED)
{
int id = (int)tv_get_number(argvars);
win_T *wp = find_popup_win(id);
if (wp != NULL && (wp->w_popup_flags & POPF_HIDDEN) != 0)
{
wp->w_popup_flags &= ~POPF_HIDDEN;
++wp->w_buffer->b_nwindows;
redraw_all_later(NOT_VALID);
popup_mask_refresh = TRUE;
}
}
/*
* popup_settext({id}, {text})
*/
void
f_popup_settext(typval_T *argvars, typval_T *rettv UNUSED)
{
int id = (int)tv_get_number(&argvars[0]);
win_T *wp = find_popup_win(id);
if (wp != NULL)
{
popup_set_buffer_text(wp->w_buffer, argvars[1]);
popup_adjust_position(wp);
}
}
static void
popup_free(win_T *wp)
{
wp->w_buffer->b_locked = FALSE;
if (wp->w_winrow + wp->w_height >= cmdline_row)
clear_cmdline = TRUE;
win_free_popup(wp);
redraw_all_later(NOT_VALID);
popup_mask_refresh = TRUE;
}
/*
* Close a popup window by Window-id.
* Does not invoke the callback.
*/
void
popup_close(int id)
{
win_T *wp;
tabpage_T *tp;
win_T *prev = NULL;
// go through global popups
for (wp = first_popupwin; wp != NULL; prev = wp, wp = wp->w_next)
if (wp->w_id == id)
{
if (prev == NULL)
first_popupwin = wp->w_next;
else
prev->w_next = wp->w_next;
popup_free(wp);
return;
}
// go through tab-local popups
FOR_ALL_TABPAGES(tp)
popup_close_tabpage(tp, id);
}
/*
* Close a popup window with Window-id "id" in tabpage "tp".
*/
void
popup_close_tabpage(tabpage_T *tp, int id)
{
win_T *wp;
win_T **root = &tp->tp_first_popupwin;
win_T *prev = NULL;
for (wp = *root; wp != NULL; prev = wp, wp = wp->w_next)
if (wp->w_id == id)
{
if (prev == NULL)
*root = wp->w_next;
else
prev->w_next = wp->w_next;
popup_free(wp);
return;
}
}
void
close_all_popups(void)
{
while (first_popupwin != NULL)
popup_close(first_popupwin->w_id);
while (curtab->tp_first_popupwin != NULL)
popup_close(curtab->tp_first_popupwin->w_id);
}
/*
* popup_move({id}, {options})
*/
void
f_popup_move(typval_T *argvars, typval_T *rettv UNUSED)
{
dict_T *dict;
int id = (int)tv_get_number(argvars);
win_T *wp = find_popup_win(id);
if (wp == NULL)
return; // invalid {id}
if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
{
emsg(_(e_dictreq));
return;
}
dict = argvars[1].vval.v_dict;
apply_move_options(wp, dict);
if (wp->w_winrow + wp->w_height >= cmdline_row)
clear_cmdline = TRUE;
popup_adjust_position(wp);
}
/*
* popup_setoptions({id}, {options})
*/
void
f_popup_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
{
dict_T *dict;
int id = (int)tv_get_number(argvars);
win_T *wp = find_popup_win(id);
if (wp == NULL)
return; // invalid {id}
if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
{
emsg(_(e_dictreq));
return;
}
dict = argvars[1].vval.v_dict;
apply_move_options(wp, dict);
apply_general_options(wp, dict);
popup_mask_refresh = TRUE;
popup_adjust_position(wp);
}
/*
* popup_getpos({id})
*/
void
f_popup_getpos(typval_T *argvars, typval_T *rettv)
{
dict_T *dict;
int id = (int)tv_get_number(argvars);
win_T *wp = find_popup_win(id);
int top_extra;
int left_extra;
if (rettv_dict_alloc(rettv) == OK)
{
if (wp == NULL)
return; // invalid {id}
top_extra = popup_top_extra(wp);
left_extra = wp->w_popup_border[3] + wp->w_popup_padding[3];
dict = rettv->vval.v_dict;
dict_add_number(dict, "line", wp->w_winrow + 1);
dict_add_number(dict, "col", wp->w_wincol + 1);
dict_add_number(dict, "width", wp->w_width + left_extra
+ wp->w_popup_border[1] + wp->w_popup_padding[1]);
dict_add_number(dict, "height", wp->w_height + top_extra
+ wp->w_popup_border[2] + wp->w_popup_padding[2]);
dict_add_number(dict, "core_line", wp->w_winrow + 1 + top_extra);
dict_add_number(dict, "core_col", wp->w_wincol + 1 + left_extra);
dict_add_number(dict, "core_width", wp->w_width);
dict_add_number(dict, "core_height", wp->w_height);
dict_add_number(dict, "visible",
win_valid(wp) && (wp->w_popup_flags & POPF_HIDDEN) == 0);
}
}
/*
* For popup_getoptions(): add a "border" or "padding" entry to "dict".
*/
static void
get_padding_border(dict_T *dict, int *array, char *name)
{
list_T *list;
int i;
if (array[0] == 0 && array[1] == 0 && array[2] == 0 && array[3] == 0)
return;
list = list_alloc();
if (list != NULL)
{
dict_add_list(dict, name, list);
if (array[0] != 1 || array[1] != 1 || array[2] != 1 || array[3] != 1)
for (i = 0; i < 4; ++i)
list_append_number(list, array[i]);
}
}
/*
* For popup_getoptions(): add a "borderhighlight" entry to "dict".
*/
static void
get_borderhighlight(dict_T *dict, win_T *wp)
{
list_T *list;
int i;
for (i = 0; i < 4; ++i)
if (wp->w_border_highlight[i] != NULL)
break;
if (i == 4)
return;
list = list_alloc();
if (list != NULL)
{
dict_add_list(dict, "borderhighlight", list);
for (i = 0; i < 4; ++i)
list_append_string(list, wp->w_border_highlight[i], -1);
}
}
/*
* For popup_getoptions(): add a "borderchars" entry to "dict".
*/
static void
get_borderchars(dict_T *dict, win_T *wp)
{
list_T *list;
int i;
char_u buf[NUMBUFLEN];
int len;
for (i = 0; i < 8; ++i)
if (wp->w_border_char[i] != 0)
break;
if (i == 8)
return;
list = list_alloc();
if (list != NULL)
{
dict_add_list(dict, "borderchars", list);
for (i = 0; i < 8; ++i)
{
len = mb_char2bytes(wp->w_border_char[i], buf);
list_append_string(list, buf, len);
}
}
}
/*
* For popup_getoptions(): add a "moved" entry to "dict".
*/
static void
get_moved_list(dict_T *dict, win_T *wp)
{
list_T *list;
list = list_alloc();
if (list != NULL)
{
dict_add_list(dict, "moved", list);
list_append_number(list, wp->w_popup_mincol);
list_append_number(list, wp->w_popup_maxcol);
}
}
/*
* popup_getoptions({id})
*/
void
f_popup_getoptions(typval_T *argvars, typval_T *rettv)
{
dict_T *dict;
int id = (int)tv_get_number(argvars);
win_T *wp = find_popup_win(id);
tabpage_T *tp;
int i;
if (rettv_dict_alloc(rettv) == OK)
{
if (wp == NULL)
return;
dict = rettv->vval.v_dict;
dict_add_number(dict, "line", wp->w_wantline);
dict_add_number(dict, "col", wp->w_wantcol);
dict_add_number(dict, "minwidth", wp->w_minwidth);
dict_add_number(dict, "minheight", wp->w_minheight);
dict_add_number(dict, "maxheight", wp->w_maxheight);
dict_add_number(dict, "maxwidth", wp->w_maxwidth);
dict_add_number(dict, "firstline", wp->w_firstline);
dict_add_number(dict, "zindex", wp->w_zindex);
dict_add_number(dict, "fixed", wp->w_popup_fixed);
dict_add_string(dict, "title", wp->w_popup_title);
dict_add_number(dict, "wrap", wp->w_p_wrap);
dict_add_number(dict, "drag", wp->w_popup_drag);
dict_add_string(dict, "highlight", wp->w_p_wcr);
// find the tabpage that holds this popup
i = 1;
FOR_ALL_TABPAGES(tp)
{
win_T *p;
for (p = tp->tp_first_popupwin; p != NULL; p = wp->w_next)
if (p->w_id == id)
break;
if (p != NULL)
break;
++i;
}
if (tp == NULL)
i = -1; // must be global
else if (tp == curtab)
i = 0;
dict_add_number(dict, "tabpage", i);
get_padding_border(dict, wp->w_popup_padding, "padding");
get_padding_border(dict, wp->w_popup_border, "border");
get_borderhighlight(dict, wp);
get_borderchars(dict, wp);
get_moved_list(dict, wp);
if (wp->w_filter_cb.cb_name != NULL)
dict_add_callback(dict, "filter", &wp->w_filter_cb);
if (wp->w_close_cb.cb_name != NULL)
dict_add_callback(dict, "callback", &wp->w_close_cb);
for (i = 0; i < (int)(sizeof(poppos_entries) / sizeof(poppos_entry_T));
++i)
if (wp->w_popup_pos == poppos_entries[i].pp_val)
{
dict_add_string(dict, "pos",
(char_u *)poppos_entries[i].pp_name);
break;
}
# if defined(FEAT_TIMERS)
dict_add_number(dict, "time", wp->w_popup_timer != NULL
? (long)wp->w_popup_timer->tr_interval : 0L);
# endif
}
}
int
error_if_popup_window()
{
if (bt_popup(curwin->w_buffer))
{
emsg(_("E994: Not allowed in a popup window"));
return TRUE;
}
return FALSE;
}
/*
* Reset all the POPF_HANDLED flags in global popup windows and popup windows
* in the current tab page.
*/
void
popup_reset_handled()
{
win_T *wp;
for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
wp->w_popup_flags &= ~POPF_HANDLED;
for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
wp->w_popup_flags &= ~POPF_HANDLED;
}
/*
* Find the next visible popup where POPF_HANDLED is not set.
* Must have called popup_reset_handled() first.
* When "lowest" is TRUE find the popup with the lowest zindex, otherwise the
* popup with the highest zindex.
*/
win_T *
find_next_popup(int lowest)
{
win_T *wp;
win_T *found_wp;
int found_zindex;
found_zindex = lowest ? INT_MAX : 0;
found_wp = NULL;
for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
&& (lowest ? wp->w_zindex < found_zindex
: wp->w_zindex > found_zindex))
{
found_zindex = wp->w_zindex;
found_wp = wp;
}
for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
if ((wp->w_popup_flags & (POPF_HANDLED|POPF_HIDDEN)) == 0
&& (lowest ? wp->w_zindex < found_zindex
: wp->w_zindex > found_zindex))
{
found_zindex = wp->w_zindex;
found_wp = wp;
}
if (found_wp != NULL)
found_wp->w_popup_flags |= POPF_HANDLED;
return found_wp;
}
/*
* Invoke the filter callback for window "wp" with typed character "c".
* Uses the global "mod_mask" for modifiers.
* Returns the return value of the filter.
* Careful: The filter may make "wp" invalid!
*/
static int
invoke_popup_filter(win_T *wp, int c)
{
int res;
typval_T rettv;
int dummy;
typval_T argv[3];
char_u buf[NUMBUFLEN];
// Emergency exit: CTRL-C closes the popup.
if (c == Ctrl_C)
{
rettv.v_type = VAR_NUMBER;
rettv.vval.v_number = -1;
popup_close_and_callback(wp, &rettv);
return 1;
}
argv[0].v_type = VAR_NUMBER;
argv[0].vval.v_number = (varnumber_T)wp->w_id;
// Convert the number to a string, so that the function can use:
// if a:c == "\<F2>"
buf[special_to_buf(c, mod_mask, TRUE, buf)] = NUL;
argv[1].v_type = VAR_STRING;
argv[1].vval.v_string = vim_strsave(buf);
argv[2].v_type = VAR_UNKNOWN;
// NOTE: The callback might close the popup, thus make "wp" invalid.
call_callback(&wp->w_filter_cb, -1,
&rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
res = tv_get_number(&rettv);
vim_free(argv[1].vval.v_string);
clear_tv(&rettv);
return res;
}
/*
* Called when "c" was typed: invoke popup filter callbacks.
* Returns TRUE when the character was consumed,
*/
int
popup_do_filter(int c)
{
int res = FALSE;
win_T *wp;
popup_reset_handled();
while (!res && (wp = find_next_popup(FALSE)) != NULL)
if (wp->w_filter_cb.cb_name != NULL)
res = invoke_popup_filter(wp, c);
return res;
}
/*
* Called when the cursor moved: check if any popup needs to be closed if the
* cursor moved far enough.
*/
void
popup_check_cursor_pos()
{
win_T *wp;
typval_T tv;
popup_reset_handled();
while ((wp = find_next_popup(TRUE)) != NULL)
if (wp->w_popup_curwin != NULL
&& (curwin != wp->w_popup_curwin
|| curwin->w_cursor.lnum != wp->w_popup_lnum
|| curwin->w_cursor.col < wp->w_popup_mincol
|| curwin->w_cursor.col > wp->w_popup_maxcol))
{
tv.v_type = VAR_NUMBER;
tv.vval.v_number = -1;
popup_close_and_callback(wp, &tv);
}
}
/*
* Return TRUE if "col" / "line" matches with an entry in w_popup_mask.
* "col" and "line" are screen coordinates.
*/
static int
popup_masked(win_T *wp, int screencol, int screenline)
{
int col = screencol - wp->w_wincol + 1;
int line = screenline - wp->w_winrow + 1;
listitem_T *lio, *li;
int width, height;
if (wp->w_popup_mask == NULL)
return FALSE;
width = popup_width(wp);
height = popup_height(wp);
for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
{
int cols, cole;
int lines, linee;
li = lio->li_tv.vval.v_list->lv_first;
cols = tv_get_number(&li->li_tv);
if (cols < 0)
cols = width + cols + 1;
if (col < cols)
continue;
li = li->li_next;
cole = tv_get_number(&li->li_tv);
if (cole < 0)
cole = width + cole + 1;
if (col > cole)
continue;
li = li->li_next;
lines = tv_get_number(&li->li_tv);
if (lines < 0)
lines = height + lines + 1;
if (line < lines)
continue;
li = li->li_next;
linee = tv_get_number(&li->li_tv);
if (linee < 0)
linee = height + linee + 1;
if (line > linee)
continue;
return TRUE;
}
return FALSE;
}
/*
* Set flags in popup_transparent[] for window "wp" to "val".
*/
static void
update_popup_transparent(win_T *wp, int val)
{
if (wp->w_popup_mask != NULL)
{
int width = popup_width(wp);
int height = popup_height(wp);
listitem_T *lio, *li;
int cols, cole;
int lines, linee;
int col, line;
for (lio = wp->w_popup_mask->lv_first; lio != NULL; lio = lio->li_next)
{
li = lio->li_tv.vval.v_list->lv_first;
cols = tv_get_number(&li->li_tv);
if (cols < 0)
cols = width + cols + 1;
li = li->li_next;
cole = tv_get_number(&li->li_tv);
if (cole < 0)
cole = width + cole + 1;
li = li->li_next;
lines = tv_get_number(&li->li_tv);
if (lines < 0)
lines = height + lines + 1;
li = li->li_next;
linee = tv_get_number(&li->li_tv);
if (linee < 0)
linee = height + linee + 1;
--cols;
--lines;
for (line = lines; line < linee && line < screen_Rows; ++line)
for (col = cols; col < cole && col < screen_Columns; ++col)
popup_transparent[(line + wp->w_winrow) * screen_Columns
+ col + wp->w_wincol] = val;
}
}
}
/*
* Update "popup_mask" if needed.
* Also recomputes the popup size and positions.
* Also updates "popup_visible".
* Also marks window lines for redrawing.
*/
void
may_update_popup_mask(int type)
{
win_T *wp;
short *mask;
int line, col;
int redraw_all = FALSE;
// Need to recompute when switching tabs.
// Also recompute when the type is CLEAR or NOT_VALID, something basic
// (such as the screen size) must have changed.
if (popup_mask_tab != curtab || type >= NOT_VALID)
{
popup_mask_refresh = TRUE;
redraw_all = TRUE;
}
if (!popup_mask_refresh)
{
// Check if any buffer has changed.
for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
popup_mask_refresh = TRUE;
for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
popup_mask_refresh = TRUE;
if (!popup_mask_refresh)
return;
}
// Need to update the mask, something has changed.
popup_mask_refresh = FALSE;
popup_mask_tab = curtab;
popup_visible = FALSE;
// If redrawing everything, just update "popup_mask".
// If redrawing only what is needed, update "popup_mask_next" and then
// compare with "popup_mask" to see what changed.
if (type >= SOME_VALID)
mask = popup_mask;
else
mask = popup_mask_next;
vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
// Find the window with the lowest zindex that hasn't been handled yet,
// so that the window with a higher zindex overwrites the value in
// popup_mask.
popup_reset_handled();
while ((wp = find_next_popup(TRUE)) != NULL)
{
int height = popup_height(wp);
int width = popup_width(wp);
popup_visible = TRUE;
// Recompute the position if the text changed.
if (redraw_all
|| wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
popup_adjust_position(wp);
for (line = wp->w_winrow;
line < wp->w_winrow + height && line < screen_Rows; ++line)
for (col = wp->w_wincol;
col < wp->w_wincol + width && col < screen_Columns; ++col)
if (!popup_masked(wp, col, line))
mask[line * screen_Columns + col] = wp->w_zindex;
}
// Only check which lines are to be updated if not already
// updating all lines.
if (mask == popup_mask_next)
for (line = 0; line < screen_Rows; ++line)
{
int col_done = 0;
for (col = 0; col < screen_Columns; ++col)
{
int off = line * screen_Columns + col;
if (popup_mask[off] != popup_mask_next[off])
{
popup_mask[off] = popup_mask_next[off];
if (line >= cmdline_row)
{
// the command line needs to be cleared if text below
// the popup is now visible.
if (!msg_scrolled && popup_mask_next[off] == 0)
clear_cmdline = TRUE;
}
else if (col >= col_done)
{
linenr_T lnum;
int line_cp = line;
int col_cp = col;
// The screen position "line" / "col" needs to be
// redrawn. Figure out what window that is and update
// w_redraw_top and w_redr_bot. Only needs to be done
// once for each window line.
wp = mouse_find_win(&line_cp, &col_cp, IGNORE_POPUP);
if (wp != NULL)
{
if (line_cp >= wp->w_height)
// In (or below) status line
wp->w_redr_status = TRUE;
// compute the position in the buffer line from the
// position on the screen
else if (mouse_comp_pos(wp, &line_cp, &col_cp,
&lnum))
// past bottom
wp->w_redr_status = TRUE;
else
redrawWinline(wp, lnum);
// This line is going to be redrawn, no need to
// check until the right side of the window.
col_done = wp->w_wincol + wp->w_width - 1;
}
}
}
}
}
}
/*
* Return a string of "len" spaces in IObuff.
*/
static char_u *
get_spaces(int len)
{
vim_memset(IObuff, ' ', (size_t)len);
IObuff[len] = NUL;
return IObuff;
}
/*
* Update popup windows. They are drawn on top of normal windows.
* "win_update" is called for each popup window, lowest zindex first.
*/
void
update_popups(void (*win_update)(win_T *wp))
{
win_T *wp;
int top_off;
int left_off;
int total_width;
int total_height;
int top_padding;
int popup_attr;
int border_attr[4];
int border_char[8];
char_u buf[MB_MAXBYTES];
int row;
int i;
// Find the window with the lowest zindex that hasn't been updated yet,
// so that the window with a higher zindex is drawn later, thus goes on
// top.
popup_reset_handled();
while ((wp = find_next_popup(TRUE)) != NULL)
{
// This drawing uses the zindex of the popup window, so that it's on
// top of the text but doesn't draw when another popup with higher
// zindex is on top of the character.
screen_zindex = wp->w_zindex;
// Set flags in popup_transparent[] for masked cells.
update_popup_transparent(wp, 1);
// adjust w_winrow and w_wincol for border and padding, since
// win_update() doesn't handle them.
top_off = popup_top_extra(wp);
left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
wp->w_winrow += top_off;
wp->w_wincol += left_off;
// Draw the popup text.
win_update(wp);
wp->w_winrow -= top_off;
wp->w_wincol -= left_off;
total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
+ wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
total_height = popup_top_extra(wp)
+ wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
popup_attr = get_wcr_attr(wp);
// We can only use these line drawing characters when 'encoding' is
// "utf-8" and 'ambiwidth' is "single".
if (enc_utf8 && *p_ambw == 's')
{
border_char[0] = border_char[2] = 0x2550;
border_char[1] = border_char[3] = 0x2551;
border_char[4] = 0x2554;
border_char[5] = 0x2557;
border_char[6] = 0x255d;
border_char[7] = 0x255a;
}
else
{
border_char[0] = border_char[2] = '-';
border_char[1] = border_char[3] = '|';
for (i = 4; i < 8; ++i)
border_char[i] = '+';
}
for (i = 0; i < 8; ++i)
if (wp->w_border_char[i] != 0)
border_char[i] = wp->w_border_char[i];
for (i = 0; i < 4; ++i)
{
border_attr[i] = popup_attr;
if (wp->w_border_highlight[i] != NULL)
border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
}
top_padding = wp->w_popup_padding[0];
if (wp->w_popup_border[0] > 0)
{
// top border
screen_fill(wp->w_winrow, wp->w_winrow + 1,
wp->w_wincol,
wp->w_wincol + total_width,
wp->w_popup_border[3] != 0
? border_char[4] : border_char[0],
border_char[0], border_attr[0]);
if (wp->w_popup_border[1] > 0)
{
buf[mb_char2bytes(border_char[5], buf)] = NUL;
screen_puts(buf, wp->w_winrow,
wp->w_wincol + total_width - 1, border_attr[1]);
}
}
else if (wp->w_popup_padding[0] == 0 && popup_top_extra(wp) > 0)
top_padding = 1;
if (top_padding > 0)
{
// top padding
row = wp->w_winrow + wp->w_popup_border[0];
screen_fill(row, row + top_padding,
wp->w_wincol + wp->w_popup_border[3],
wp->w_wincol + total_width - wp->w_popup_border[1],
' ', ' ', popup_attr);
}
// Title goes on top of border or padding.
if (wp->w_popup_title != NULL)
screen_puts(wp->w_popup_title, wp->w_winrow, wp->w_wincol + 1,
wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
for (row = wp->w_winrow + wp->w_popup_border[0];
row < wp->w_winrow + total_height - wp->w_popup_border[2];
++row)
{
// left border
if (wp->w_popup_border[3] > 0)
{
buf[mb_char2bytes(border_char[3], buf)] = NUL;
screen_puts(buf, row, wp->w_wincol, border_attr[3]);
}
// left padding
if (wp->w_popup_padding[3] > 0)
screen_puts(get_spaces(wp->w_popup_padding[3]), row,
wp->w_wincol + wp->w_popup_border[3], popup_attr);
// right border
if (wp->w_popup_border[1] > 0)
{
buf[mb_char2bytes(border_char[1], buf)] = NUL;
screen_puts(buf, row,
wp->w_wincol + total_width - 1, border_attr[1]);
}
// right padding
if (wp->w_popup_padding[1] > 0)
screen_puts(get_spaces(wp->w_popup_padding[1]), row,
wp->w_wincol + wp->w_popup_border[3]
+ wp->w_popup_padding[3] + wp->w_width, popup_attr);
}
if (wp->w_popup_padding[2] > 0)
{
// bottom padding
row = wp->w_winrow + wp->w_popup_border[0]
+ wp->w_popup_padding[0] + wp->w_height;
screen_fill(row, row + wp->w_popup_padding[2],
wp->w_wincol + wp->w_popup_border[3],
wp->w_wincol + total_width - wp->w_popup_border[1],
' ', ' ', popup_attr);
}
if (wp->w_popup_border[2] > 0)
{
// bottom border
row = wp->w_winrow + total_height - 1;
screen_fill(row , row + 1,
wp->w_wincol,
wp->w_wincol + total_width,
wp->w_popup_border[3] != 0
? border_char[7] : border_char[2],
border_char[2], border_attr[2]);
if (wp->w_popup_border[1] > 0)
{
buf[mb_char2bytes(border_char[6], buf)] = NUL;
screen_puts(buf, row,
wp->w_wincol + total_width - 1, border_attr[2]);
}
}
update_popup_transparent(wp, 0);
// Back to the normal zindex.
screen_zindex = 0;
}
}
/*
* Mark references in callbacks of one popup window.
*/
static int
set_ref_in_one_popup(win_T *wp, int copyID)
{
int abort = FALSE;
typval_T tv;
if (wp->w_close_cb.cb_partial != NULL)
{
tv.v_type = VAR_PARTIAL;
tv.vval.v_partial = wp->w_close_cb.cb_partial;
abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
}
if (wp->w_filter_cb.cb_partial != NULL)
{
tv.v_type = VAR_PARTIAL;
tv.vval.v_partial = wp->w_filter_cb.cb_partial;
abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
}
abort = abort || set_ref_in_list(wp->w_popup_mask, copyID);
return abort;
}
/*
* Set reference in callbacks of popup windows.
*/
int
set_ref_in_popups(int copyID)
{
int abort = FALSE;
win_T *wp;
tabpage_T *tp;
for (wp = first_popupwin; !abort && wp != NULL; wp = wp->w_next)
abort = abort || set_ref_in_one_popup(wp, copyID);
FOR_ALL_TABPAGES(tp)
{
for (wp = tp->tp_first_popupwin; !abort && wp != NULL; wp = wp->w_next)
abort = abort || set_ref_in_one_popup(wp, copyID);
if (abort)
break;
}
return abort;
}
#endif // FEAT_TEXT_PROP
|