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
|
/* vi:set ts=8 sts=4 sw=4 noet:
*
* MNV - MNV is not Vim by Bram Moolenaar
*
* Do ":help uganda" in MNV to read copying and usage conditions.
* Do ":help credits" in MNV to see a list of people who contributed.
* See README.txt for an overview of the MNV source code.
*/
/*
* popupmenu.c: Popup menu (PUM)
*/
#include "mnv.h"
static pumitem_T *pum_array = NULL; // items of displayed pum
static int pum_size; // nr of items in "pum_array"
static int pum_selected; // index of selected item or -1
static int pum_first = 0; // index of top item
static int call_update_screen = FALSE;
static int pum_in_cmdline = FALSE;
static int pum_height; // nr of displayed pum items
static int pum_width; // width of displayed pum items
static int pum_base_width; // width of pum items base
static int pum_kind_width; // width of pum items kind column
static int pum_extra_width; // width of extra stuff
static int pum_scrollbar; // TRUE when scrollbar present
#ifdef FEAT_RIGHTLEFT
static int pum_rl; // TRUE when pum is drawn 'rightleft'
#endif
static int pum_row; // top row of pum
static int pum_col; // left column of pum
static win_T *pum_window = NULL;
static int pum_win_row;
static int pum_win_height;
static int pum_win_col;
static int pum_win_wcol;
static int pum_win_width;
// Some parts are not updated when a popup menu is visible. Setting this flag
// makes pum_visible() return FALSE even when there is a popup menu.
static int pum_pretend_not_visible = FALSE;
// Border support
static int pum_border = 0;
static int pum_margin = 0; // margin of 1 cell on left and right
static int pum_shadow = 0;
// Border characters
static struct {
int top;
int right;
int bottom;
int left;
int top_left;
int top_right;
int bottom_right;
int bottom_left;
} pum_border_chars;
static int pum_set_selected(int n, int repeat);
static void pum_draw_border(void);
static void pum_draw_shadow(void);
static void compute_margins(int *right_margin, int *left_margin, int *left_padding);
#define PUM_DEF_HEIGHT 10
#define DEFINE_PUM_SETTER(feature) \
void \
pum_set_##feature(int enable) \
{ \
pum_##feature = enable ? 1 : 0; \
}
DEFINE_PUM_SETTER(border)
DEFINE_PUM_SETTER(shadow)
DEFINE_PUM_SETTER(margin)
static void
pum_compute_size(void)
{
int w;
// Compute the width of the widest match and the widest extra.
pum_base_width = 0;
pum_kind_width = 0;
pum_extra_width = 0;
for (int i = 0; i < pum_size; ++i)
{
if (pum_array[i].pum_text != NULL)
{
w = mnv_strsize(pum_array[i].pum_text);
if (pum_base_width < w)
pum_base_width = w;
}
if (pum_array[i].pum_kind != NULL)
{
w = mnv_strsize(pum_array[i].pum_kind) + 1;
if (pum_kind_width < w)
pum_kind_width = w;
}
if (pum_array[i].pum_extra != NULL)
{
w = mnv_strsize(pum_array[i].pum_extra) + 1;
if (pum_extra_width < w)
pum_extra_width = w;
}
}
}
/*
* Calculate vertical placement for popup menu.
* Sets pum_row and pum_height based on available space.
*/
static void
pum_compute_vertical_placement(
int size,
int above_row,
int below_row)
{
int context_lines;
int cline_visible_offset;
int extra_height = 2 * pum_border + pum_shadow;
int extra_offset = pum_border + pum_shadow;
pum_height = MIN(size, PUM_DEF_HEIGHT);
if (p_ph > 0 && pum_height > p_ph)
pum_height = p_ph;
// Put the pum below "pum_win_row" if possible. If there are few lines
// decide on where there is more room.
if (pum_win_row + 2 >= below_row - pum_height - extra_height
&& pum_win_row - above_row > (below_row - above_row) / 2)
{
// pum above "pum_win_row"
if (State & MODE_CMDLINE)
// for cmdline pum, no need for context lines
context_lines = 0;
else
// Leave two lines of context if possible
context_lines = MIN(2, curwin->w_wrow - curwin->w_cline_row);
if (pum_win_row >= size + context_lines + extra_height)
{
pum_row = pum_win_row - size - context_lines - extra_offset;
pum_height = size;
}
else
{
pum_row = pum_border;
pum_height = pum_win_row - context_lines - extra_height;
}
if (p_ph > 0 && pum_height > p_ph)
{
pum_row += pum_height - p_ph;
pum_height = p_ph;
}
}
else
{
// pum below "pum_win_row"
if (State & MODE_CMDLINE)
// for cmdline pum, no need for context lines
context_lines = 0;
else
{
// Leave three lines of context if possible
validate_cheight();
cline_visible_offset = curwin->w_cline_row +
curwin->w_cline_height - curwin->w_wrow;
context_lines = MIN(3, cline_visible_offset);
}
pum_row = pum_win_row + context_lines + pum_border;
pum_height = MIN(below_row - pum_row - extra_offset, size);
if (p_ph > 0 && pum_height > p_ph)
pum_height = p_ph;
}
#if defined(FEAT_QUICKFIX)
// If there is a preview window above avoid drawing over it.
if (above_row > 0 && pum_row < above_row && pum_height > above_row)
{
pum_row = above_row;
pum_height = pum_win_row - above_row;
}
#endif
}
/*
* Try to set 'pum_width' so that it fits within available_width.
* Returns TRUE if pum_width was successfully set, FALSE otherwise.
*/
static int
set_pum_width_aligned_with_cursor(int width, int available_width)
{
int end_padding = TRUE;
if (width < p_pw)
{
width = p_pw;
end_padding = FALSE;
}
if (p_pmw > 0 && width >= p_pmw)
{
width = p_pmw;
end_padding = FALSE;
}
pum_width = width + (end_padding && width >= p_pw ? 1 : 0);
return available_width >= pum_width;
}
/*
* Calculate horizontal placement for popup menu. Sets pum_col and pum_width
* based on cursor position and available space.
*/
static void
pum_compute_horizontal_placement(int cursor_col)
{
int desired_width = pum_base_width + pum_kind_width + pum_extra_width;
int available_width;
int col_offset;
int trailing_overhead = pum_scrollbar + pum_border
+ (pum_margin && pum_border ? 1 : 0) + (pum_shadow ? 2 : 0);
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
{
col_offset = pum_border && (cursor_col >= Columns - 2)
? (3 - Columns + cursor_col) : 0;
available_width = cursor_col + 1;
pum_col = cursor_col - col_offset;
}
else
#endif
{
col_offset = pum_border && (cursor_col <= 1) ? (2 - cursor_col) : 0;
available_width = Columns - cursor_col;
pum_col = cursor_col + col_offset;
}
available_width -= trailing_overhead + col_offset;
// Case 1: Align pum with "cursor_col"
if (set_pum_width_aligned_with_cursor(desired_width, available_width))
return;
// Show the pum truncated, provided it is at least as wide as 'pum_width'
if (available_width > p_pw)
{
pum_width = available_width;
return;
}
// Case 2: Truncated pum is no longer aligned with "cursor_col"
available_width = Columns - trailing_overhead - col_offset;
if (available_width > p_pw)
{
pum_width = p_pw; // Truncate beyond 'pum_width'
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
pum_col = pum_width + trailing_overhead;
else
#endif
pum_col = Columns - pum_width - trailing_overhead;
return;
}
// Case 3: Not enough room anywhere, use what we have
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
pum_col = Columns - 1 - col_offset;
else
#endif
pum_col = col_offset;
pum_width = Columns - trailing_overhead;
}
/*
* Show the popup menu with items "array[size]".
* "array" must remain valid until pum_undisplay() is called!
* When possible the leftmost character is aligned with cursor column.
* The menu appears above the screen line "row" or at "row" + "height" - 1.
*/
void
pum_display(
pumitem_T *array,
int size,
int selected) // index of initially selected item, -1 if
// out of range
{
int cursor_col;
int above_row;
int below_row;
int redo_count = 0;
#if defined(FEAT_QUICKFIX)
win_T *pvwin;
#endif
#ifdef FEAT_RIGHTLEFT
pum_rl = (State & MODE_CMDLINE) == 0 && curwin->w_p_rl;
#endif
do
{
above_row = 0;
below_row = cmdline_row;
// Pretend the pum is already there to avoid that must_redraw is set
// when 'cuc' is on.
pum_array = (pumitem_T *)1;
validate_cursor_col();
pum_array = NULL;
// Remember the essential parts of the window position and size, so we
// can decide when to reposition the popup menu.
pum_window = curwin;
if (State & MODE_CMDLINE)
// cmdline completion popup menu
pum_win_row = cmdline_row;
else
pum_win_row = curwin->w_wrow + W_WINROW(curwin);
pum_win_height = curwin->w_height;
pum_win_col = curwin->w_wincol;
pum_win_wcol = curwin->w_wcol;
pum_win_width = curwin->w_width;
#if defined(FEAT_QUICKFIX)
FOR_ALL_WINDOWS(pvwin)
if (pvwin->w_p_pvw)
break;
if (pvwin != NULL)
{
if (W_WINROW(pvwin) < W_WINROW(curwin))
above_row = W_WINROW(pvwin) + pvwin->w_height;
else if (W_WINROW(pvwin) > W_WINROW(curwin) + curwin->w_height)
below_row = W_WINROW(pvwin);
}
#endif
// Figure out the vertical size and position of the pum.
pum_compute_vertical_placement(size, above_row, below_row);
// don't display when we only have room for one line
if (pum_height < 1 || (pum_height == 1 && size > 1))
return;
pum_array = array;
pum_size = size;
pum_compute_size();
// Calculate cursor column
if (State & MODE_CMDLINE)
// cmdline completion popup menu
cursor_col = cmdline_compl_startcol();
else
{
// w_wcol includes virtual text "above"
int wcol = curwin->w_wcol % curwin->w_width;
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
cursor_col = curwin->w_wincol + curwin->w_width - wcol - 1;
else
#endif
cursor_col = curwin->w_wincol + wcol;
}
// if there are more items than room we need a scrollbar
pum_scrollbar = (pum_height < size) ? 1 : 0;
// Figure out the horizontal size and position of the pum.
pum_compute_horizontal_placement(cursor_col);
// Set selected item and redraw. If the window size changed need to
// redo the positioning. Limit this to two times, when there is not
// much room the window size will keep changing.
} while (pum_set_selected(selected, redo_count) && ++redo_count <= 2);
pum_redraw();
}
/*
* Set a flag that when pum_redraw() is called it first calls update_screen().
* This will avoid clearing and redrawing the popup menu, prevent flicker.
*/
void
pum_call_update_screen(void)
{
call_update_screen = TRUE;
// Update the cursor position to be able to compute the popup menu
// position. The cursor line length may have changed because of the
// inserted completion.
curwin->w_valid &= ~(VALID_CROW|VALID_CHEIGHT);
validate_cursor();
}
/*
* Return TRUE if we are going to redraw the popup menu and the screen position
* "row"/"col" is under the popup menu.
*/
int
pum_under_menu(int row, int col, int only_redrawing)
{
return (!only_redrawing || pum_will_redraw)
&& row >= pum_row
&& row < pum_row + pum_height
&& col >= pum_col - 1
&& col < pum_col + pum_width + pum_scrollbar;
}
/*
* Computes attributes of text on the popup menu.
* Returns attributes for every cell, or NULL if all attributes are the same.
*/
static int *
pum_compute_text_attrs(char_u *text, hlf_T hlf, int user_hlattr)
{
int i;
size_t leader_len;
int char_cells;
int new_attr;
char_u *ptr = text;
int cell_idx = 0;
garray_T *ga = NULL;
int *attrs = NULL;
char_u *leader = NULL;
int in_fuzzy;
int matched_len = -1;
int_u char_pos = 0;
int is_select = FALSE;
if (*text == NUL || (hlf != HLF_PSI && hlf != HLF_PNI)
|| (highlight_attr[HLF_PMSI] == highlight_attr[HLF_PSI]
&& highlight_attr[HLF_PMNI] == highlight_attr[HLF_PNI]))
return NULL;
is_select = hlf == HLF_PSI;
leader = (State & MODE_CMDLINE) ? cmdline_compl_pattern()
: ins_compl_leader();
if (leader == NULL || *leader == NUL)
return NULL;
attrs = ALLOC_MULT(int, mnv_strsize(text));
if (attrs == NULL)
return NULL;
in_fuzzy = (State & MODE_CMDLINE) ? cmdline_compl_is_fuzzy()
: (get_cot_flags() & COT_FUZZY) != 0;
leader_len = STRLEN(leader);
if (in_fuzzy)
{
ga = fuzzy_match_str_with_pos(text, leader);
if (!ga)
{
mnv_free(attrs);
return NULL;
}
}
while (*ptr != NUL)
{
new_attr = highlight_attr[hlf];
if (ga != NULL)
{
// Handle fuzzy matching
for (i = 0; i < ga->ga_len; i++)
{
if (char_pos == ((int_u *)ga->ga_data)[i])
{
new_attr = highlight_attr[is_select ? HLF_PMSI : HLF_PMNI];
new_attr = hl_combine_attr(highlight_attr[hlf], new_attr);
break;
}
}
}
else
{
if (matched_len < 0 && MB_STRNICMP(ptr, leader, leader_len) == 0)
matched_len = (int)leader_len;
if (matched_len > 0)
{
new_attr = highlight_attr[is_select ? HLF_PMSI : HLF_PMNI];
new_attr = hl_combine_attr(highlight_attr[hlf], new_attr);
matched_len--;
}
}
new_attr = hl_combine_attr(highlight_attr[HLF_PNI], new_attr);
if (user_hlattr > 0)
new_attr = hl_combine_attr(new_attr, user_hlattr);
char_cells = mb_ptr2cells(ptr);
for (i = 0; i < char_cells; i++)
attrs[cell_idx + i] = new_attr;
cell_idx += char_cells;
MB_PTR_ADV(ptr);
char_pos++;
}
if (ga != NULL)
{
ga_clear(ga);
mnv_free(ga);
}
return attrs;
}
/*
* Displays text on the popup menu with specific attributes.
*/
static void
pum_screen_puts_with_attrs(
int row,
int col,
int cells UNUSED,
char_u *text,
int textlen,
int *attrs)
{
int col_start = col;
char_u *ptr = text;
int char_len;
int attr;
// Render text with proper attributes
while (*ptr != NUL && ptr < text + textlen)
{
char_len = mb_ptr2len(ptr);
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
attr = attrs[col_start + cells - col - 1];
else
#endif
attr = attrs[col - col_start];
screen_puts_len(ptr, char_len, row, col, attr);
col += mb_ptr2cells(ptr);
ptr += char_len;
}
}
static inline char_u *
pum_get_item(int index, int type)
{
switch(type)
{
case CPT_ABBR: return pum_array[index].pum_text;
case CPT_KIND: return pum_array[index].pum_kind;
case CPT_MENU: return pum_array[index].pum_extra;
}
return NULL;
}
static inline int
pum_user_attr_combine(int idx, int type, int attr)
{
int user_attr[] = {
pum_array[idx].pum_user_abbr_hlattr,
pum_array[idx].pum_user_kind_hlattr,
};
return user_attr[type] > 0 ? hl_combine_attr(attr, user_attr[type]) : attr;
}
#ifdef FEAT_RIGHTLEFT
/*
* Display RTL text with proper attributes in the popup menu.
* Returns the adjusted column position after drawing.
*/
static int
pum_display_rtl_text(
int row,
int col,
char_u *st,
int attr,
int *attrs,
int width,
int width_limit,
int totwidth,
int next_isempty,
int selected)
{
char_u *rt = NULL;
int cells = 0;
int over_cell = 0;
int truncated = FALSE;
int pad = next_isempty ? 0 : 2;
int remaining = 0;
int trunc_attr = highlight_attr[selected ? HLF_PSI : HLF_PNI];
int truncrl = curwin->w_fill_chars.truncrl != NUL
? curwin->w_fill_chars.truncrl : '<';
if (st == NULL)
return col;
rt = reverse_text(st);
if (rt == NULL)
{
MNV_CLEAR(st);
return col;
}
char_u *rt_start = rt;
cells = mb_string2cells(rt, -1);
truncated = width_limit - totwidth < cells + pad;
// only draw the text that fits
if (cells > width_limit)
{
do
{
cells -= has_mbyte ? (*mb_ptr2cells)(rt) : 1;
MB_PTR_ADV(rt);
} while (cells > width_limit);
if (cells < width_limit)
{
// Most left character requires 2-cells
// but only 1 cell is available on screen.
// Put a '<' on the left of the pum item.
*(--rt) = '<';
cells++;
}
}
if (truncated)
{
char_u *orig_rt = rt;
int size = 0;
remaining = width_limit - totwidth - 1;
cells = mb_string2cells(rt, -1);
if (cells > remaining)
{
while (cells > remaining)
{
MB_PTR_ADV(orig_rt);
cells -= has_mbyte ? (*mb_ptr2cells)(orig_rt) : 1;
}
}
size = (int)STRLEN(orig_rt);
if (cells < remaining)
over_cell = remaining - cells;
cells = mb_string2cells(orig_rt, size);
width = cells + over_cell + 1;
rt = orig_rt;
screen_putchar(truncrl, row, col - width + 1, trunc_attr);
if (over_cell > 0)
screen_fill(row, row + 1, col - width + 2,
col - width + 2 + over_cell, ' ', ' ', attr);
}
if (attrs == NULL)
screen_puts_len(rt, (int)STRLEN(rt), row, col - cells + 1, attr);
else
pum_screen_puts_with_attrs(row, col - cells + 1, cells, rt,
(int)STRLEN(rt), attrs);
mnv_free(rt_start);
MNV_CLEAR(st);
return col - width;
}
#endif
/*
* Display LTR text with proper attributes in the popup menu.
* Returns the adjusted column position after drawing.
*/
static int
pum_display_ltr_text(
int row,
int col,
char_u *st,
int attr,
int *attrs,
int width, // width already calculated in outer loop
int width_limit,
int totwidth,
int next_isempty,
int selected)
{
int size = 0;
int cells = 0;
char_u *st_end = NULL;
int over_cell = 0;
int pad = next_isempty ? 0 : 2;
int truncated = FALSE;
int remaining = 0;
int trunc_attr = highlight_attr[selected ? HLF_PSI : HLF_PNI];
int trunc = curwin->w_fill_chars.trunc != NUL
? curwin->w_fill_chars.trunc : '>';
if (st == NULL)
return col;
size = (int)STRLEN(st);
cells = (*mb_string2cells)(st, size);
truncated = width_limit - totwidth < cells + pad;
// only draw the text that fits
while (size > 0 && col + cells > width_limit + pum_col)
{
--size;
if (has_mbyte)
{
size -= (*mb_head_off)(st, st + size);
cells -= (*mb_ptr2cells)(st + size);
}
else
--cells;
}
// truncated
if (truncated)
{
remaining = width_limit - totwidth - 1;
if (cells > remaining)
{
st_end = st + size;
while (st_end > st && cells > remaining)
{
MB_PTR_BACK(st, st_end);
cells -= has_mbyte ? (*mb_ptr2cells)(st_end) : 1;
}
size = st_end - st;
}
if (cells < remaining)
over_cell = remaining - cells;
cells = mb_string2cells(st, size);
width = cells + over_cell + 1;
}
if (attrs == NULL)
screen_puts_len(st, size, row, col, attr);
else
pum_screen_puts_with_attrs(row, col, cells, st, size, attrs);
if (truncated)
{
if (over_cell > 0)
screen_fill(row, row + 1, col + cells,
col + cells + over_cell, ' ', ' ', attr);
screen_putchar(trunc, row, col + cells + over_cell, trunc_attr);
}
MNV_CLEAR(st);
return col + width;
}
/*
* Process and display a single popup menu item (text/kind/extra).
* Returns the new column position after drawing.
*/
static int
pum_process_item(
int row,
int col,
int idx,
int j, // Current position in order array
int *order, // Order array
hlf_T hlf,
int attr,
int *totwidth_ptr,
int next_isempty)
{
int item_type = order[j];
char_u *s = NULL;
char_u *p = pum_get_item(idx, item_type);
int width = 0; // item width
int w; // char width
int selected = idx == pum_selected;
for ( ; ; MB_PTR_ADV(p))
{
if (s == NULL)
s = p;
w = ptr2cells(p);
if (*p != NUL && *p != TAB && *totwidth_ptr + w <= pum_width)
{
width += w;
continue;
}
// Display the text that fits or comes before a Tab.
// First convert it to printable characters.
char_u *st;
int *attrs = NULL;
int saved = *p;
if (saved != NUL)
*p = NUL;
st = transstr(s);
if (saved != NUL)
*p = saved;
if (item_type == CPT_ABBR)
attrs = pum_compute_text_attrs(st, hlf,
pum_array[idx].pum_user_abbr_hlattr);
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
col = pum_display_rtl_text(row, col, st, attr, attrs,
width, pum_width, *totwidth_ptr, next_isempty, selected);
else
#endif
col = pum_display_ltr_text(row, col, st, attr, attrs,
width, pum_width, *totwidth_ptr, next_isempty, selected);
if (attrs != NULL)
MNV_CLEAR(attrs);
if (*p != TAB)
break;
// Display two spaces for a Tab.
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
{
screen_puts_len((char_u *)" ", 2, row, col - 1, attr);
col -= 2;
}
else
#endif
{
screen_puts_len((char_u *)" ", 2, row, col, attr);
col += 2;
}
*totwidth_ptr += 2;
s = NULL; // start text at next char
width = 0;
}
return col;
}
/*
* Draw the scrollbar for the popup menu.
*/
static void
pum_draw_scrollbar(
int row,
int i,
int thumb_pos,
int thumb_height)
{
if (pum_scrollbar <= 0)
return;
int attr = (i >= thumb_pos && i < thumb_pos + thumb_height) ?
highlight_attr[HLF_PST] : highlight_attr[HLF_PSB];
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
screen_putchar(' ', row, pum_col - pum_width, attr);
else
#endif
screen_putchar(' ', row, pum_col + pum_width, attr);
}
static inline void
pum_align_order(int *order)
{
int is_default = cia_flags == 0;
order[0] = is_default ? CPT_ABBR : cia_flags / 100;
order[1] = is_default ? CPT_KIND : (cia_flags / 10) % 10;
order[2] = is_default ? CPT_MENU : cia_flags % 10;
}
/*
* Redraw the popup menu, using "pum_first" and "pum_selected".
*/
void
pum_redraw(void)
{
int row = pum_row;
int col;
hlf_T *hlfs; // array used for highlights
hlf_T hlf;
int attr;
int i, j;
int idx;
char_u *p = NULL;
int totwidth;
int thumb_pos = 0;
int thumb_height = 1;
int item_type;
int order[3];
int next_isempty = FALSE;
int n;
int items_width_array[3] = { pum_base_width, pum_kind_width,
pum_extra_width };
int basic_width; // first item width
int last_isabbr = FALSE;
int orig_attr = -1;
int scroll_range = pum_size - pum_height;
bool override_success;
// Use current window for highlight overrides when using 'winhighlight'
override_success = push_highlight_overrides(curwin->w_hl, curwin->w_hl_len);
hlf_T hlfsNorm[3];
hlf_T hlfsSel[3];
// "word"/"abbr"
hlfsNorm[0] = HLF_PNI;
hlfsSel[0] = HLF_PSI;
// "kind"
hlfsNorm[1] = HLF_PNK;
hlfsSel[1] = HLF_PSK;
// "extra text"
hlfsNorm[2] = HLF_PNX;
hlfsSel[2] = HLF_PSX;
if (call_update_screen)
{
call_update_screen = FALSE;
// Do not redraw in pum_may_redraw() and don't draw in the area where
// the popup menu will be.
pum_will_redraw = TRUE;
update_screen(0);
pum_will_redraw = FALSE;
}
// never display more than we have
pum_first = MIN(pum_first, scroll_range);
if (pum_scrollbar)
{
thumb_height = pum_height * pum_height / pum_size;
if (thumb_height == 0)
thumb_height = 1;
thumb_pos = (pum_first * (pum_height - thumb_height)
+ scroll_range / 2) / scroll_range;
}
#ifdef FEAT_PROP_POPUP
// The popup menu is drawn over popup menus with zindex under
// POPUPMENU_ZINDEX.
screen_zindex = POPUPMENU_ZINDEX;
#endif
// Draw border and shadow first if enabled
if (pum_border)
pum_draw_border();
if (pum_shadow)
pum_draw_shadow();
for (i = 0; i < pum_height; ++i)
{
idx = i + pum_first;
hlfs = (idx == pum_selected) ? hlfsSel : hlfsNorm;
hlf = hlfs[0]; // start with "word" highlight
attr = highlight_attr[hlf];
// prepend a space if there is room
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
{
if (pum_col < curwin->w_wincol + curwin->w_width - 1 - pum_border)
screen_putchar(' ', row, pum_col + 1, attr);
}
else
#endif
if (pum_col > pum_border)
screen_putchar(' ', row, pum_col - 1, attr);
// Display each entry, use two spaces for a Tab.
// Do this 3 times and order from p_cia
col = pum_col;
totwidth = 0;
pum_align_order(order);
basic_width = items_width_array[order[0]];
last_isabbr = order[2] == CPT_ABBR;
for (j = 0; j < 3; ++j)
{
item_type = order[j];
hlf = hlfs[item_type];
attr = highlight_attr[hlf];
orig_attr = attr;
if (item_type < 2) // try combine attr with user custom
attr = pum_user_attr_combine(idx, item_type, attr);
p = pum_get_item(idx, item_type);
if (j + 1 < 3)
next_isempty = pum_get_item(idx, order[j + 1]) == NULL;
else
next_isempty = TRUE;
if (p != NULL)
// Process and display the item
col = pum_process_item(row, col, idx, j, order, hlf, attr,
&totwidth, next_isempty);
if (j > 0)
n = items_width_array[order[1]] + (last_isabbr ? 0 : 1);
else
n = order[j] == CPT_ABBR ? 1 : 0;
// Stop when there is nothing more to display.
if (j == 2
|| (next_isempty && (j == 1 || (j == 0
&& pum_get_item(idx, order[j + 2]) == NULL)))
|| basic_width + n >= pum_width)
break;
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
{
screen_fill(row, row + 1, pum_col - basic_width - n + 1,
col + 1, ' ', ' ', orig_attr);
col = pum_col - basic_width - n;
}
else
#endif
{
screen_fill(row, row + 1, col, pum_col + basic_width + n,
' ', ' ', orig_attr);
col = pum_col + basic_width + n;
}
totwidth = basic_width + n;
}
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
screen_fill(row, row + 1, pum_col - pum_width + 1, col + 1, ' ',
' ', orig_attr);
else
#endif
screen_fill(row, row + 1, col, pum_col + pum_width, ' ', ' ',
orig_attr);
pum_draw_scrollbar(row, i, thumb_pos, thumb_height);
++row;
}
#ifdef FEAT_PROP_POPUP
screen_zindex = 0;
#endif
if (override_success)
pop_highlight_overrides();
}
#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
/*
* Position the info popup relative to the popup menu item.
*/
void
pum_position_info_popup(win_T *wp)
{
int col = pum_col + pum_width + pum_scrollbar + 1;
int row = pum_row;
int botpos = POPPOS_BOTLEFT;
int used_maxwidth_opt = FALSE;
int right_margin, left_margin, dummy;
int left_extra_width, right_extra_width;
(void)compute_margins(&right_margin, &left_margin, &dummy);
left_extra_width = pum_border + left_margin;
right_extra_width = pum_border + right_margin + (pum_shadow ? 2 : 0);
col += right_extra_width;
wp->w_popup_pos = POPPOS_TOPLEFT;
if (Columns - col < 20 && Columns - col < pum_col)
{
col = pum_col - 1 - left_extra_width;
wp->w_popup_pos = POPPOS_TOPRIGHT;
botpos = POPPOS_BOTRIGHT;
wp->w_maxwidth = pum_col - 1 - left_extra_width;
}
else
wp->w_maxwidth = Columns - col + 1;
wp->w_maxwidth -= popup_extra_width(wp);
if (wp->w_maxwidth_opt > 0 && wp->w_maxwidth > wp->w_maxwidth_opt)
{
// option value overrules computed value
wp->w_maxwidth = wp->w_maxwidth_opt;
used_maxwidth_opt = TRUE;
}
row -= popup_top_extra(wp);
if (wp->w_popup_flags & POPF_INFO_MENU)
{
if (pum_row < pum_win_row)
{
// menu above cursor line, align with bottom
row += pum_height;
wp->w_popup_pos = botpos;
}
else
// menu below cursor line, align with top
row += 1;
}
else
// align with the selected item
row += pum_selected - pum_first + 1;
wp->w_popup_flags &= ~POPF_HIDDEN;
if (wp->w_maxwidth < 10 && !used_maxwidth_opt)
// The popup is not going to fit or will overlap with the cursor
// position, hide the popup.
wp->w_popup_flags |= POPF_HIDDEN;
else
popup_set_wantpos_rowcol(wp, row, col);
}
#endif
/*
* Set the index of the currently selected item. The menu will scroll when
* necessary. When "n" is out of range don't scroll.
* This may be repeated when the preview window is used:
* "repeat" == 0: open preview window normally
* "repeat" == 1: open preview window but don't set the size
* "repeat" == 2: don't open preview window
* Returns TRUE when the window was resized and the location of the popup menu
* must be recomputed.
*/
static int
pum_set_selected(int n, int repeat UNUSED)
{
int resized = FALSE;
int context = pum_height / 2;
int scroll_offset;
#ifdef FEAT_QUICKFIX
int prev_selected = pum_selected;
unsigned cur_cot_flags = get_cot_flags();
#endif
#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
int has_info = FALSE;
#endif
pum_selected = n;
scroll_offset = pum_selected - pum_height;
if (pum_selected >= 0 && pum_selected < pum_size)
{
if (pum_first > pum_selected - 4)
{
// scroll down; when we did a jump it's probably a PageUp then
// scroll a whole page
if (pum_first > pum_selected - 2)
{
pum_first -= pum_height - 2;
if (pum_first < 0)
pum_first = 0;
else if (pum_first > pum_selected)
pum_first = pum_selected;
}
else
pum_first = pum_selected;
}
else if (pum_first < scroll_offset + 5)
{
// scroll up; when we did a jump it's probably a PageDown then
// scroll a whole page
if (pum_first < scroll_offset + 3)
pum_first = MAX(pum_first + pum_height - 2, scroll_offset + 1);
else
pum_first = scroll_offset + 1;
}
// Give a few lines of context when possible.
context = MIN(context, 3);
if (pum_height > 2)
{
if (pum_first > pum_selected - context)
pum_first = MAX(pum_selected - context, 0); // scroll down
else if (pum_first < pum_selected + context - pum_height + 1)
pum_first = pum_selected + context - pum_height + 1; // up
}
// adjust for the number of lines displayed
pum_first = MIN(pum_first, pum_size - pum_height);
#if defined(FEAT_QUICKFIX)
/*
* Show extra info in the preview window if there is something and
* 'completeopt' contains "preview" or "popup" or "popuphidden".
* Skip this when tried twice already.
* Skip this also when there is not much room.
* Skip this for command-window when 'completeopt' contains "preview".
* NOTE: Be very careful not to sync undo!
*/
if (pum_array[pum_selected].pum_info != NULL
&& Rows > 10
&& repeat <= 1
&& (cur_cot_flags & COT_ANY_PREVIEW)
&& !((cur_cot_flags & COT_PREVIEW) && cmdwin_type != 0))
{
win_T *curwin_save = curwin;
tabpage_T *curtab_save = curtab;
# ifdef FEAT_PROP_POPUP
use_popup_T use_popup;
# else
# define use_popup USEPOPUP_NONE
# endif
# ifdef FEAT_PROP_POPUP
has_info = TRUE;
if (cur_cot_flags & COT_POPUPHIDDEN)
use_popup = USEPOPUP_HIDDEN;
else if (cur_cot_flags & COT_POPUP)
use_popup = USEPOPUP_NORMAL;
else
use_popup = USEPOPUP_NONE;
if (use_popup != USEPOPUP_NONE)
// don't use WinEnter or WinLeave autocommands for the info
// popup
block_autocmds();
# endif
// Open a preview window and set "curwin" to it.
// 3 lines by default, prefer 'previewheight' if set and smaller.
g_do_tagpreview = 3;
if (p_pvh > 0 && p_pvh < g_do_tagpreview)
g_do_tagpreview = p_pvh;
++RedrawingDisabled;
// Prevent undo sync here, if an autocommand syncs undo weird
// things can happen to the undo tree.
++no_u_sync;
resized = prepare_tagpreview(FALSE, FALSE, use_popup);
--no_u_sync;
if (RedrawingDisabled > 0)
--RedrawingDisabled;
g_do_tagpreview = 0;
if (curwin->w_p_pvw
# ifdef FEAT_PROP_POPUP
|| (curwin->w_popup_flags & POPF_INFO)
# endif
)
{
int res = OK;
if (!resized
&& curbuf->b_nwindows == 1
&& curbuf->b_fname == NULL
&& bt_nofile(curbuf)
&& curbuf->b_p_bh[0] == 'w')
{
// Already a "wipeout" buffer, make it empty.
while (!BUFEMPTY())
ml_delete((linenr_T)1);
}
else
{
// Don't want to sync undo in the current buffer.
++no_u_sync;
res = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 0, NULL);
--no_u_sync;
if (res == OK)
{
// Edit a new, empty buffer. Set options for a "wipeout"
// buffer.
set_option_value_give_err((char_u *)"swf",
0L, NULL, OPT_LOCAL);
set_option_value_give_err((char_u *)"bl",
0L, NULL, OPT_LOCAL);
set_option_value_give_err((char_u *)"bt",
0L, (char_u *)"nofile", OPT_LOCAL);
set_option_value_give_err((char_u *)"bh",
0L, (char_u *)"wipe", OPT_LOCAL);
set_option_value_give_err((char_u *)"diff",
0L, NULL, OPT_LOCAL);
}
}
if (res == OK)
{
char_u *p, *e;
linenr_T lnum = 0;
for (p = pum_array[pum_selected].pum_info; *p != NUL; )
{
e = mnv_strchr(p, '\n');
if (e == NULL)
{
ml_append(lnum++, p, 0, FALSE);
break;
}
*e = NUL;
ml_append(lnum++, p, (int)(e - p + 1), FALSE);
*e = '\n';
p = e + 1;
}
// delete the empty last line
ml_delete(curbuf->b_ml.ml_line_count);
// Increase the height of the preview window to show the
// text, but no more than 'previewheight' lines.
if (repeat == 0 && use_popup == USEPOPUP_NONE)
{
lnum = MIN(lnum, p_pvh);
if (curwin->w_height < lnum)
{
win_setheight((int)lnum);
resized = TRUE;
}
}
curbuf->b_changed = 0;
curbuf->b_p_ma = FALSE;
if (pum_selected != prev_selected)
{
# ifdef FEAT_PROP_POPUP
curwin->w_firstline = 0;
# endif
curwin->w_topline = 1;
}
else if (curwin->w_topline > curbuf->b_ml.ml_line_count)
curwin->w_topline = curbuf->b_ml.ml_line_count;
curwin->w_cursor.lnum = curwin->w_topline;
curwin->w_cursor.col = 0;
# ifdef FEAT_PROP_POPUP
if (use_popup != USEPOPUP_NONE)
{
pum_position_info_popup(curwin);
if (win_valid(curwin_save))
redraw_win_later(curwin_save, UPD_SOME_VALID);
}
# endif
if ((curwin != curwin_save && win_valid(curwin_save))
|| (curtab != curtab_save
&& valid_tabpage(curtab_save)))
{
int save_redr_status;
if (curtab != curtab_save && valid_tabpage(curtab_save))
goto_tabpage_tp(curtab_save, FALSE, FALSE);
// When the first completion is done and the preview
// window is not resized, skip the preview window's
// status line redrawing.
if (ins_compl_active() && !resized)
curwin->w_redr_status = FALSE;
// Return cursor to where we were
validate_cursor();
redraw_later(UPD_SOME_VALID);
// When the preview window was resized we need to
// update the view on the buffer. Only go back to
// the window when needed, otherwise it will always be
// redrawn.
if (resized && win_valid(curwin_save))
{
++no_u_sync;
win_enter(curwin_save, TRUE);
--no_u_sync;
update_topline();
}
// Update the screen before drawing the popup menu.
// Enable updating the status lines.
pum_pretend_not_visible = TRUE;
// But don't draw text at the new popup menu position,
// it causes flicker. When resizing we need to draw
// anyway, the position may change later.
// Also do not redraw the status line of the original
// current window here, to avoid it gets drawn with
// StatusLineNC for a moment and cause flicker.
pum_will_redraw = !resized;
save_redr_status = curwin_save->w_redr_status;
curwin_save->w_redr_status = FALSE;
update_screen(0);
pum_pretend_not_visible = FALSE;
pum_will_redraw = FALSE;
curwin_save->w_redr_status = save_redr_status;
if (!resized && win_valid(curwin_save))
{
# ifdef FEAT_PROP_POPUP
win_T *wp = curwin;
# endif
++no_u_sync;
win_enter(curwin_save, TRUE);
--no_u_sync;
# ifdef FEAT_PROP_POPUP
if (use_popup == USEPOPUP_HIDDEN && win_valid(wp))
popup_hide(wp);
# endif
}
// May need to update the screen again when there are
// autocommands involved.
pum_pretend_not_visible = TRUE;
pum_will_redraw = !resized;
update_screen(0);
pum_pretend_not_visible = FALSE;
pum_will_redraw = FALSE;
call_update_screen = FALSE;
}
}
}
# if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
if (WIN_IS_POPUP(curwin))
// can't keep focus in a popup window
win_enter(firstwin, TRUE);
# endif
# ifdef FEAT_PROP_POPUP
if (use_popup != USEPOPUP_NONE)
unblock_autocmds();
# endif
}
#endif
}
#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
if (!has_info)
// hide any popup info window
popup_hide_info();
#endif
return resized;
}
/*
* Undisplay the popup menu (later).
*/
void
pum_undisplay(void)
{
pum_array = NULL;
redraw_all_later(UPD_NOT_VALID);
redraw_tabline = TRUE;
#if defined(FEAT_TABPANEL)
redraw_tabpanel = TRUE;
#endif
if (pum_in_cmdline)
{
clear_cmdline = TRUE;
pum_in_cmdline = FALSE;
}
status_redraw_all();
#if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)
// hide any popup info window
popup_hide_info();
#endif
}
/*
* Clear the popup menu. Currently only resets the offset to the first
* displayed item.
*/
void
pum_clear(void)
{
pum_first = 0;
}
/*
* Return TRUE if the popup menu is displayed. Used to avoid some redrawing
* that could overwrite it. Overruled when "pum_pretend_not_visible" is set,
* used to redraw the status lines.
*/
int
pum_visible(void)
{
return !pum_pretend_not_visible && pum_array != NULL;
}
/*
* Return TRUE if the popup can be redrawn in the same position.
*/
static int
pum_in_same_position(void)
{
return pum_window != curwin
|| (pum_win_row == curwin->w_wrow + W_WINROW(curwin)
&& pum_win_height == curwin->w_height
&& pum_win_col == curwin->w_wincol
&& pum_win_width == curwin->w_width);
}
/*
* Return TRUE when pum_may_redraw() will call pum_redraw().
* This means that the pum area should not be overwritten to avoid flicker.
*/
int
pum_redraw_in_same_position(void)
{
if (!pum_visible() || pum_will_redraw)
return FALSE; // nothing to do
return pum_in_same_position();
}
/*
* Reposition the popup menu to adjust for window layout changes.
*/
void
pum_may_redraw(void)
{
pumitem_T *array = pum_array;
int len = pum_size;
int selected = pum_selected;
if (!pum_visible() || pum_will_redraw)
return; // nothing to do
if (pum_in_same_position())
{
pum_redraw(); // Redraw window in same position
}
else
{
int wcol = curwin->w_wcol;
// Window layout changed, recompute the position.
// Use the remembered w_wcol value, the cursor may have moved when a
// completion was inserted, but we want the menu in the same position.
pum_undisplay();
curwin->w_wcol = pum_win_wcol;
curwin->w_valid |= VALID_WCOL;
pum_display(array, len, selected);
curwin->w_wcol = wcol;
}
}
/*
* Return the height of the popup menu, the number of entries visible.
* Only valid when pum_visible() returns TRUE!
*/
int
pum_get_height(void)
{
return pum_height;
}
#if defined(FEAT_EVAL)
/*
* Add size information about the pum to "dict".
*/
void
pum_set_event_info(dict_T *dict)
{
if (!pum_visible())
return;
(void)dict_add_number(dict, "height", pum_height);
(void)dict_add_number(dict, "width", pum_width);
(void)dict_add_number(dict, "row", pum_row);
(void)dict_add_number(dict, "col", pum_col);
(void)dict_add_number(dict, "size", pum_size);
(void)dict_add_bool(dict, "scrollbar",
pum_scrollbar ? VVAL_TRUE : VVAL_FALSE);
}
#endif
#if defined(FEAT_BEVAL_TERM) || defined(FEAT_TERM_POPUP_MENU)
static void
pum_position_at_mouse(int min_width)
{
int extra_height = pum_border * 2 + pum_shadow;
int extra_offset = pum_border + pum_shadow;
int trailing_overhead = 0;
int col_offset = 0;
int available_width;
if (Rows - mouse_row - extra_height > pum_size
|| Rows - mouse_row > mouse_row)
{
// Enough space below the mouse row,
// or there is more space below the mouse row than above.
pum_row = mouse_row + 1 + pum_border;
if (pum_height > Rows - pum_row - extra_height)
pum_height = Rows - pum_row - extra_height;
if (pum_row + pum_height + extra_offset >= cmdline_row)
pum_in_cmdline = TRUE;
}
else
{
// Show above the mouse row, reduce height if it does not fit.
pum_row = mouse_row - pum_size - extra_offset;
if (pum_row < pum_border)
{
pum_height += pum_row - pum_border;
pum_row = pum_border;
}
}
trailing_overhead = pum_scrollbar + pum_border
+ (pum_margin && pum_border ? 1 : 0) + (pum_shadow ? 2 : 0);
# ifdef FEAT_RIGHTLEFT
if (pum_rl)
{
col_offset = (mouse_col == Columns - 1) && pum_border;
available_width = mouse_col + 1 - trailing_overhead - col_offset;
if (available_width >= pum_base_width || available_width > min_width)
// Enough space to show at mouse column.
pum_col = mouse_col - col_offset;
else
// Not enough space, left align with window.
pum_col = MIN(pum_base_width, min_width) - 1 + trailing_overhead;
pum_width = pum_col + 1 - trailing_overhead;
}
else
# endif
{
col_offset = (mouse_col == 0) && pum_border;
available_width = Columns - mouse_col - trailing_overhead - col_offset;
if (available_width >= pum_base_width || available_width > min_width)
// Enough space to show at mouse column.
pum_col = mouse_col + col_offset;
else
// Not enough space, right align with window.
pum_col = Columns - MIN(pum_base_width, min_width)
- trailing_overhead;
pum_width = Columns - pum_col - trailing_overhead;
}
pum_width = MIN(pum_width, pum_base_width + 1);
// Do not redraw at cursor position.
pum_window = NULL;
}
#endif
#if defined(FEAT_BEVAL_TERM)
static pumitem_T *balloon_array = NULL;
static int balloon_arraysize;
# define BALLOON_MIN_WIDTH 50
# define BALLOON_MIN_HEIGHT 10
typedef struct {
char_u *start;
int bytelen;
int cells;
int indent;
} balpart_T;
/*
* Split a string into parts to display in the balloon.
* Aimed at output from gdb. Attempts to split at white space, preserve quoted
* strings and make a struct look good.
* Resulting array is stored in "array" and returns the size of the array.
*/
int
split_message(char_u *mesg, pumitem_T **array)
{
garray_T ga;
char_u *p;
balpart_T *item;
int quoted = FALSE;
int height;
int line;
int item_idx;
int indent = 0;
int max_cells = 0;
int max_height = Rows / 2 - 1;
int long_item_count = 0;
int split_long_items = FALSE;
ga_init2(&ga, sizeof(balpart_T), 20);
p = mesg;
while (*p != NUL)
{
if (ga_grow(&ga, 1) == FAIL)
goto failed;
item = ((balpart_T *)ga.ga_data) + ga.ga_len;
item->start = p;
item->indent = indent;
item->cells = indent * 2;
++ga.ga_len;
while (*p != NUL)
{
if (*p == '"')
quoted = !quoted;
else if (*p == '\n')
break;
else if (*p == '\\' && p[1] != NUL)
++p;
else if (!quoted)
{
if ((*p == ',' && p[1] == ' ') || *p == '{' || *p == '}')
{
// Looks like a good point to break.
if (*p == '{')
++indent;
else if (*p == '}' && indent > 0)
--indent;
++item->cells;
p = skipwhite(p + 1);
break;
}
}
item->cells += ptr2cells(p);
p += mb_ptr2len(p);
}
item->bytelen = p - item->start;
if (*p == '\n')
++p;
if (item->cells > max_cells)
max_cells = item->cells;
long_item_count += (item->cells - 1) / BALLOON_MIN_WIDTH;
}
height = 2 + ga.ga_len;
// If there are long items and the height is below the limit: split lines
if (long_item_count > 0 && height + long_item_count <= max_height)
{
split_long_items = TRUE;
height += long_item_count;
}
// Limit to half the window height, it has to fit above or below the mouse
// position.
if (height > max_height)
height = max_height;
*array = ALLOC_CLEAR_MULT(pumitem_T, height);
if (*array == NULL)
goto failed;
// Add an empty line above and below, looks better.
(*array)->pum_text = mnv_strsave((char_u *)"");
(*array + height - 1)->pum_text = mnv_strsave((char_u *)"");
for (line = 1, item_idx = 0; line < height - 1; ++item_idx)
{
int skip;
int thislen;
int copylen;
int ind;
int cells;
item = ((balpart_T *)ga.ga_data) + item_idx;
if (item->bytelen == 0)
(*array)[line++].pum_text = mnv_strsave((char_u *)"");
else
for (skip = 0; skip < item->bytelen; skip += thislen)
{
if (split_long_items && item->cells >= BALLOON_MIN_WIDTH)
{
cells = item->indent * 2;
for (p = item->start + skip;
p < item->start + item->bytelen;
p += mb_ptr2len(p))
if ((cells += ptr2cells(p)) > BALLOON_MIN_WIDTH)
break;
thislen = p - (item->start + skip);
}
else
thislen = item->bytelen;
// put indent at the start
p = alloc(thislen + item->indent * 2 + 1);
if (p == NULL)
{
for (line = 0; line <= height - 1; ++line)
mnv_free((*array)[line].pum_text);
mnv_free(*array);
goto failed;
}
for (ind = 0; ind < item->indent * 2; ++ind)
p[ind] = ' ';
// exclude spaces at the end of the string
for (copylen = thislen; copylen > 0; --copylen)
if (item->start[skip + copylen - 1] != ' ')
break;
mnv_strncpy(p + ind, item->start + skip, copylen);
(*array)[line].pum_text = p;
item->indent = 0; // wrapped line has no indent
++line;
}
}
ga_clear(&ga);
return height;
failed:
ga_clear(&ga);
return 0;
}
void
ui_remove_balloon(void)
{
if (balloon_array == NULL)
return;
pum_undisplay();
while (balloon_arraysize > 0)
mnv_free(balloon_array[--balloon_arraysize].pum_text);
MNV_CLEAR(balloon_array);
}
/*
* Terminal version of a balloon, uses the popup menu code.
*/
void
ui_post_balloon(char_u *mesg, list_T *list)
{
ui_remove_balloon();
if (mesg == NULL && list == NULL)
{
pum_undisplay();
return;
}
if (list != NULL)
{
listitem_T *li;
int idx;
balloon_arraysize = list->lv_len;
balloon_array = ALLOC_CLEAR_MULT(pumitem_T, list->lv_len);
if (balloon_array == NULL)
return;
CHECK_LIST_MATERIALIZE(list);
for (idx = 0, li = list->lv_first; li != NULL; li = li->li_next, ++idx)
{
char_u *text = tv_get_string_chk(&li->li_tv);
balloon_array[idx].pum_text = mnv_strsave(
text == NULL ? (char_u *)"" : text);
}
}
else
balloon_arraysize = split_message(mesg, &balloon_array);
if (balloon_arraysize <= 0)
return;
pum_array = balloon_array;
pum_size = balloon_arraysize;
pum_compute_size();
pum_scrollbar = 0;
pum_height = balloon_arraysize;
# ifdef FEAT_RIGHTLEFT
pum_rl = curwin->w_p_rl;
# endif
pum_position_at_mouse(BALLOON_MIN_WIDTH);
pum_selected = -1;
pum_first = 0;
pum_redraw();
}
/*
* Called when the mouse moved, may remove any displayed balloon.
*/
void
ui_may_remove_balloon(void)
{
// For now: remove the balloon whenever the mouse moves to another screen
// cell.
ui_remove_balloon();
}
#endif
#if defined(FEAT_TERM_POPUP_MENU)
/*
* Select the pum entry at the mouse position.
*/
static void
pum_select_mouse_pos(void)
{
int idx = mouse_row - pum_row;
if (idx < 0 || idx >= pum_height)
pum_selected = -1;
else if (*pum_array[idx].pum_text != NUL)
pum_selected = idx;
}
/*
* Execute the currently selected popup menu item.
*/
static void
pum_execute_menu(mnvmenu_T *menu, int mode)
{
mnvmenu_T *mp;
int idx = 0;
exarg_T ea;
FOR_ALL_CHILD_MENUS(menu, mp)
if ((mp->modes & mp->enabled & mode) && idx++ == pum_selected)
{
CLEAR_FIELD(ea);
execute_menu(&ea, mp, -1);
break;
}
}
/*
* Open the terminal version of the popup menu and don't return until it is
* closed.
*/
void
pum_show_popupmenu(mnvmenu_T *menu)
{
mnvmenu_T *mp;
int idx = 0;
pumitem_T *array;
# ifdef FEAT_BEVAL_TERM
int save_bevalterm = p_bevalterm;
# endif
int mode;
pum_undisplay();
pum_size = 0;
mode = get_menu_mode_flag();
FOR_ALL_CHILD_MENUS(menu, mp)
if (menu_is_separator(mp->dname)
|| (mp->modes & mp->enabled & mode))
++pum_size;
// When there are only Terminal mode menus, using "popup Edit" results in
// pum_size being zero.
if (pum_size <= 0)
{
emsg(_(e_menu_only_exists_in_another_mode));
return;
}
array = ALLOC_CLEAR_MULT(pumitem_T, pum_size);
if (array == NULL)
return;
FOR_ALL_CHILD_MENUS(menu, mp)
{
char_u *s = NULL;
// Make a copy of the text, the menu may be redefined in a callback.
if (menu_is_separator(mp->dname))
s = (char_u *)"";
else if (mp->modes & mp->enabled & mode)
s = mp->dname;
if (s != NULL)
{
s = mnv_strsave(s);
if (s != NULL)
array[idx++].pum_text = s;
}
}
pum_array = array;
pum_compute_size();
pum_scrollbar = 0;
pum_height = pum_size;
# ifdef FEAT_RIGHTLEFT
pum_rl = curwin->w_p_rl;
# endif
pum_position_at_mouse(20);
pum_selected = -1;
pum_first = 0;
# ifdef FEAT_BEVAL_TERM
p_bevalterm = TRUE; // track mouse movement
mch_setmouse(TRUE);
# endif
for (;;)
{
int c;
pum_redraw();
setcursor_mayforce(TRUE);
out_flush();
c = vgetc();
// Bail out when typing Esc, CTRL-C or some callback or <expr> mapping
// closed the popup menu.
if (c == ESC || c == Ctrl_C || pum_array == NULL)
break;
else if (c == CAR || c == NL)
{
// enter: select current item, if any, and close
pum_execute_menu(menu, mode);
break;
}
else if (c == 'k' || c == K_UP || c == K_MOUSEUP)
{
// cursor up: select previous item
while (pum_selected > 0)
{
--pum_selected;
if (*array[pum_selected].pum_text != NUL)
break;
}
}
else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN)
{
// cursor down: select next item
while (pum_selected < pum_size - 1)
{
++pum_selected;
if (*array[pum_selected].pum_text != NUL)
break;
}
}
else if (c == K_RIGHTMOUSE)
{
// Right mouse down: reposition the menu.
vungetc(c);
break;
}
else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE)
{
// mouse moved: select item in the mouse row
pum_select_mouse_pos();
}
else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE)
{
// left mouse click: select clicked item, if any, and close;
// right mouse release: select clicked item, close if any
pum_select_mouse_pos();
if (pum_selected >= 0)
{
pum_execute_menu(menu, mode);
break;
}
if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM)
break;
}
}
for (idx = 0; idx < pum_size; ++idx)
mnv_free(array[idx].pum_text);
mnv_free(array);
pum_undisplay();
# ifdef FEAT_BEVAL_TERM
p_bevalterm = save_bevalterm;
mch_setmouse(TRUE);
# endif
}
void
pum_make_popup(char_u *path_name, int use_mouse_pos)
{
mnvmenu_T *menu;
if (!use_mouse_pos)
{
// Hack: set mouse position at the cursor so that the menu pops up
// around there.
mouse_row = W_WINROW(curwin) + curwin->w_wrow;
mouse_col =
# ifdef FEAT_RIGHTLEFT
curwin->w_p_rl ? W_ENDCOL(curwin) - curwin->w_wcol - 1 :
# endif
curwin->w_wincol + curwin->w_wcol;
}
menu = gui_find_menu(path_name);
if (menu != NULL)
pum_show_popupmenu(menu);
}
#endif
static int
is_singlewidth(int c)
{
if (c >= 0 && c < 0x80)
return TRUE;
if (utf_char2cells(c) == 1)
return TRUE;
return FALSE;
}
/*
* Set border characters for popup menu.
*/
void
pum_set_border_chars(
int top,
int right,
int bottom,
int left,
int top_left,
int top_right,
int bottom_right,
int bottom_left)
{
if (!is_singlewidth(top) || !is_singlewidth(right)
|| !is_singlewidth(bottom) || !is_singlewidth(left)
|| !is_singlewidth(top_left) || !is_singlewidth(top_right)
|| !is_singlewidth(bottom_right) || !is_singlewidth(bottom_left))
return;
pum_border_chars.top = top;
pum_border_chars.right = right;
pum_border_chars.bottom = bottom;
pum_border_chars.left = left;
pum_border_chars.top_left = top_left;
pum_border_chars.top_right = top_right;
pum_border_chars.bottom_right = bottom_right;
pum_border_chars.bottom_left = bottom_left;
pum_border = 1;
}
/*
* Compute margins for the popup menu.
*/
static void
compute_margins(int *right_margin, int *left_margin, int *left_padding)
{
*right_margin = pum_margin && pum_border;
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
{
*left_padding = (pum_col < Columns - 1 - pum_border);
*left_margin = *right_margin && (pum_col < Columns - 3);
return;
}
#endif
*left_padding = (pum_col > pum_border);
*left_margin = *right_margin && (pum_col > 2);
}
/*
* Draw the border around the popup menu
*/
static void
pum_draw_border(void)
{
int top_row = pum_row - 1;
int bottom_row = pum_row + pum_height;
int col;
int attr = highlight_attr[HLF_PMB];
int right_margin, left_margin, left_padding;
int inner_width;
if (!pum_border)
return;
(void)compute_margins(&right_margin, &left_margin, &left_padding);
inner_width = pum_width + pum_scrollbar + left_padding;
// Draw border left to right or right to left (for RTL)
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
col = pum_col + left_padding + 1;
else
#endif
col = pum_col - left_padding - 1;
// left corners (right corners for RTL)
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
{
screen_putchar(pum_border_chars.top_right, top_row, col, attr);
if (left_margin)
screen_putchar(' ', top_row, col + 1, attr);
screen_putchar(pum_border_chars.bottom_right, bottom_row, col, attr);
if (left_margin)
screen_putchar(' ', bottom_row, col + 1, attr);
}
else
#endif
{
screen_putchar(pum_border_chars.top_left, top_row, col, attr);
if (left_margin)
screen_putchar(' ', top_row, col - 1, attr);
screen_putchar(pum_border_chars.bottom_left, bottom_row, col, attr);
if (left_margin)
screen_putchar(' ', bottom_row, col - 1, attr);
}
// Top horizontal lines
for (int i = 0; i < inner_width; i++)
{
int cur_col;
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
cur_col = col - 1 - i;
else
#endif
cur_col = col + 1 + i;
screen_putchar(pum_border_chars.top, top_row, cur_col, attr);
screen_putchar(pum_border_chars.bottom, bottom_row, cur_col, attr);
}
// right corners (left corners for RTL)
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
{
screen_putchar(pum_border_chars.top_left, top_row,
col - inner_width - 1, attr);
if (right_margin)
screen_putchar(' ', top_row, col - inner_width - 2, attr);
screen_putchar(pum_border_chars.bottom_left, bottom_row,
col - inner_width - 1, attr);
if (right_margin)
screen_putchar(' ', bottom_row, col - inner_width - 2, attr);
}
else
#endif
{
screen_putchar(pum_border_chars.top_right, top_row,
col + inner_width + 1, attr);
if (right_margin)
screen_putchar(' ', top_row, col + inner_width + 2, attr);
screen_putchar(pum_border_chars.bottom_right, bottom_row,
col + inner_width + 1, attr);
if (right_margin)
screen_putchar(' ', bottom_row, col + inner_width + 2, attr);
}
// Draw side borders
for (int i = 0; i < pum_height; i++)
{
int row = pum_row + i;
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
{
screen_putchar(pum_border_chars.left, row, col, attr);
if (left_margin)
screen_putchar(' ', row, col + 1, attr);
screen_putchar(pum_border_chars.right, row,
col - inner_width - 1, attr);
if (right_margin)
screen_putchar(' ', row, col - inner_width - 2, attr);
}
else
#endif
{
screen_putchar(pum_border_chars.left, row, col, attr);
if (left_margin)
screen_putchar(' ', row, col - 1, attr);
screen_putchar(pum_border_chars.right, row,
col + inner_width + 1, attr);
if (right_margin)
screen_putchar(' ', row, col + inner_width + 2, attr);
}
}
}
/*
* Get the underlying character and redraw with shadow highlight.
* Preserve bold, italic, underline, and reverse text underneath the shadow.
*/
void
put_shadow_char(int row, int col)
{
char_u buf[MB_MAXBYTES + 1];
int shadow_attr = highlight_attr[HLF_PMS];
int char_attr, new_attr;
screen_getbytes(row, col, buf, &char_attr);
new_attr = hl_combine_attr(char_attr, shadow_attr);
screen_putchar((*mb_ptr2char)(buf), row, col, new_attr);
}
/*
* Draw the shadow
*/
static void
pum_draw_shadow(void)
{
int row, col, next_col;
int width, offset;
int inner_width;
int right_margin, left_margin, left_padding;
if (!pum_shadow)
return;
(void)compute_margins(&right_margin, &left_margin, &left_padding);
inner_width = pum_width + pum_scrollbar + pum_border + right_margin;
// Draw right side shadow (left for RTL)
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
{
col = pum_col - inner_width;
next_col = col - 1;
}
else
#endif
{
col = pum_col + inner_width;
next_col = col + 1;
}
for (int i = 0; i < pum_height + pum_border; i++)
{
row = pum_row + 1 + i - pum_border;
put_shadow_char(row, col);
put_shadow_char(row, next_col);
}
// Bottom shadow
row = pum_row + pum_height + pum_border;
width = inner_width + left_padding + pum_border + left_margin;
offset = 2 - left_padding - pum_border - left_margin;
for (int i = 0; i < width; i++)
{
#ifdef FEAT_RIGHTLEFT
if (pum_rl)
col = pum_col - offset - i;
else
#endif
col = pum_col + offset + i;
put_shadow_char(row, col);
}
}
|