modalkit 0.0.25

A library for building applications that use modal editing
Documentation
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
use crate::{
    actions::EditAction,
    editing::{
        application::ApplicationInfo,
        context::Resolve,
        cursor::{block_cursors, Cursor},
        rope::PrivateCursorOps,
        store::Store,
    },
    errors::{EditError, EditResult},
    prelude::*,
    util::sort2,
};

use super::{CursorGroupIdContext, CursorState, EditBuffer};

pub trait SelectionActions<C, I>
where
    I: ApplicationInfo,
{
    /// Move where the cursor is located in a selection.
    fn selection_cursor_set(
        &mut self,
        side: &SelectionCursorChange,
        ctx: &C,
        store: &mut Store<I>,
    ) -> EditResult<EditInfo, I>;

    /// Duplicate existing selections onto the lines below them.
    fn selection_duplicate(
        &mut self,
        dir: MoveDir1D,
        count: &Count,
        ctx: &C,
        store: &mut Store<I>,
    ) -> EditResult<EditInfo, I>;

    /// Expand selection to the specified boundaries if they're not already there.
    fn selection_expand(
        &mut self,
        boundary: &SelectionBoundary,
        filter: TargetShapeFilter,
        ctx: &C,
        store: &mut Store<I>,
    ) -> EditResult<EditInfo, I>;

    /// Filter selections that match the regular expression in [Register::LastSearch].
    fn selection_filter(
        &mut self,
        act: MatchAction,
        ctx: &C,
        store: &mut Store<I>,
    ) -> EditResult<EditInfo, I>;

    /// Join adjacent selections.
    fn selection_join(&mut self, ctx: &C, store: &mut Store<I>) -> EditResult<EditInfo, I>;

    /// Change the boundaries of the selection to be exactly those of the range.
    fn selection_resize(
        &mut self,
        style: &SelectionResizeStyle,
        target: &EditTarget,
        ctx: &C,
        store: &mut Store<I>,
    ) -> EditResult<EditInfo, I>;

    /// Split a multiline selection into multiple single-line selections.
    fn selection_split(
        &mut self,
        style: &SelectionSplitStyle,
        filter: TargetShapeFilter,
        ctx: &C,
        store: &mut Store<I>,
    ) -> EditResult<EditInfo, I>;

    /// Remove whitespace from the ends of matching selections.
    fn selection_trim(
        &mut self,
        boundary: &SelectionBoundary,
        filter: TargetShapeFilter,
        ctx: &C,
        store: &mut Store<I>,
    ) -> EditResult<EditInfo, I>;
}

impl<'a, I> SelectionActions<CursorGroupIdContext<'a>, I> for EditBuffer<I>
where
    I: ApplicationInfo,
{
    fn selection_cursor_set(
        &mut self,
        side: &SelectionCursorChange,
        ctx: &CursorGroupIdContext<'a>,
        _: &mut Store<I>,
    ) -> EditResult<EditInfo, I> {
        let gid = ctx.0;
        let mut group = self.get_group(gid);

        for state in group.iter_mut() {
            let mut anchor = state.anchor().clone();
            let mut cursor = state.cursor().clone();
            let shape = state.shape();

            match (shape, side) {
                (
                    TargetShape::CharWise | TargetShape::LineWise,
                    SelectionCursorChange::SwapAnchor | SelectionCursorChange::SwapSide,
                ) => {
                    state.swap();
                },
                (TargetShape::BlockWise, SelectionCursorChange::SwapAnchor) => {
                    state.swap();
                },
                (TargetShape::BlockWise, SelectionCursorChange::SwapSide) => {
                    let cctx = (&self.text, ctx.1.get_width(), true);
                    let cx = cursor.x;
                    let ax = anchor.x;

                    anchor.set_column(cx, &cctx);
                    cursor.set_column(ax, &cctx);

                    *state = CursorState::Selection(cursor, anchor, shape);
                },
                (TargetShape::CharWise, SelectionCursorChange::Beginning) => {
                    let (begin, end) = sort2(anchor, cursor);

                    *state = CursorState::Selection(begin, end, shape);
                },
                (TargetShape::CharWise, SelectionCursorChange::End) => {
                    let (begin, end) = sort2(anchor, cursor);

                    *state = CursorState::Selection(end, begin, shape);
                },
                (TargetShape::LineWise, SelectionCursorChange::Beginning) => {
                    let (mut begin, mut end) = sort2(anchor, cursor);
                    let maxcol = self.text.max_column_idx(end.y, false);

                    begin.set_x(0);
                    end.set_x(maxcol);

                    *state = CursorState::Selection(begin, end, shape);
                },
                (TargetShape::LineWise, SelectionCursorChange::End) => {
                    let (mut begin, mut end) = sort2(anchor, cursor);
                    let maxcol = self.text.max_column_idx(end.y, false);

                    begin.set_x(0);
                    end.set_x(maxcol);

                    *state = CursorState::Selection(end, begin, shape);
                },
                (TargetShape::BlockWise, SelectionCursorChange::Beginning) => {
                    let (bx, ex) = sort2(anchor.x, cursor.x);
                    let (by, ey) = sort2(anchor.y, cursor.y);

                    let begin = Cursor::new(by, bx);
                    let end = Cursor::new(ey, ex);

                    *state = CursorState::Selection(begin, end, shape);
                },
                (TargetShape::BlockWise, SelectionCursorChange::End) => {
                    let (bx, ex) = sort2(anchor.x, cursor.x);
                    let (by, ey) = sort2(anchor.y, cursor.y);

                    let begin = Cursor::new(by, bx);
                    let end = Cursor::new(ey, ex);

                    *state = CursorState::Selection(end, begin, shape);
                },
            }
        }

        self.set_group(gid, group);

        Ok(None)
    }

    fn selection_duplicate(
        &mut self,
        dir: MoveDir1D,
        count: &Count,
        ictx: &CursorGroupIdContext<'a>,
        _: &mut Store<I>,
    ) -> EditResult<EditInfo, I> {
        let gid = ictx.0;
        let count = ictx.2.resolve(count);
        let lines = self.text.get_lines();

        let mut group = self.get_group(gid);
        let mut created = vec![];

        for state in group.iter_mut() {
            let (start, end) = state.sorted();
            let shape = state.shape();

            let check = |ebuf: &EditBuffer<I>, lstart, lend| {
                let smax = ebuf.text.max_column_idx(lstart, true);
                let emax = ebuf.text.max_column_idx(lend, true);

                start.x <= smax && end.x <= emax
            };

            let mut copy = |lstart, lend| {
                let sels = Cursor::new(lstart, start.x);
                let sele = Cursor::new(lend, end.x);

                created.push(CursorState::Selection(sele, sels, shape));
            };

            match dir {
                MoveDir1D::Next => {
                    let mut lstart = end.y + 1;
                    let mut create = count;
                    let ldiff = end.y - start.y;

                    while lstart < lines && create > 0 {
                        let lend = lstart + ldiff;

                        if lend >= lines {
                            break;
                        }

                        if check(self, lstart, lend) {
                            copy(lstart, lend);
                            lstart = lend + 1;
                            create -= 1;
                            continue;
                        }

                        lstart += 1;
                    }
                },
                MoveDir1D::Previous => {
                    let ldiff = end.y - start.y;
                    let mut mstart = start.y.checked_sub(ldiff + 1);
                    let mut create = count;

                    while let Some(lstart) = mstart {
                        let lend = lstart + ldiff;

                        if create == 0 {
                            break;
                        }

                        if check(self, lstart, lend) {
                            copy(lstart, lend);
                            mstart = lstart.checked_sub(ldiff + 1);
                            create -= 1;
                            continue;
                        }

                        mstart = lstart.checked_sub(1);
                    }
                },
            }
        }

        group.members.append(&mut created);
        group.merge();
        self.set_group(gid, group);

        Ok(None)
    }

    fn selection_expand(
        &mut self,
        boundary: &SelectionBoundary,
        filter: TargetShapeFilter,
        ictx: &CursorGroupIdContext<'a>,
        _: &mut Store<I>,
    ) -> EditResult<EditInfo, I> {
        use MoveDir1D::{Next, Previous};
        use MoveTerminus::{Beginning, End};

        let ctx = &self._ctx_cgi2es(&EditAction::Motion, ictx);
        let gid = ictx.0;
        let shape = ctx.context.get_target_shape();
        let mut group = self.get_group(gid);

        // Save current positions before we expand.
        self.push_jump(gid, &group);

        for state in group.iter_mut() {
            if !filter.matches(&state.shape()) {
                continue;
            }

            if let Some(shape) = shape {
                state.set_shape(shape);
            }

            let sels = state.start();
            let sele = state.end();

            let sels = self.text.seek(sels, boundary, Beginning, Previous, 1, true, true);
            let sele = self.text.seek(sele, boundary, End, Next, 1, true, true);

            if let (Some(start), Some(end)) = (sels, sele) {
                state.set_anchor(start);
                state.set_cursor(end);
            }
        }

        group.merge();
        self.set_group(gid, group);

        Ok(None)
    }

    fn selection_filter(
        &mut self,
        act: MatchAction,
        ictx: &CursorGroupIdContext<'a>,
        store: &mut Store<I>,
    ) -> EditResult<EditInfo, I> {
        let gid = ictx.0;
        let mut group = self.get_group(gid);

        let needle = self._get_regex(store)?;
        let members = std::mem::take(&mut group.members);
        let drop = act.is_drop();

        let keep = |state: &CursorState| {
            let ms = self.text.find_matches(state.start(), state.end(), &needle);

            return ms.is_empty() == drop;
        };

        for member in members.into_iter() {
            if keep(&member) {
                group.members.push(member);
            }
        }

        if !keep(&group.leader) {
            if let Some(leader) = group.members.pop() {
                group.leader = leader;
            } else {
                let msg = "No selections remaining".to_string();
                let err = EditError::Failure(msg);

                return Err(err);
            }
        }

        self.set_group(gid, group);

        return Ok(None);
    }

    fn selection_join(
        &mut self,
        ictx: &CursorGroupIdContext<'a>,
        _: &mut Store<I>,
    ) -> EditResult<EditInfo, I> {
        let gid = ictx.0;
        let mut group = self.get_group(gid);

        group.merge();

        if group.members.is_empty() {
            // There's nothing to join.
            return Ok(None);
        }

        // Save current positions before we join them together.
        self.push_jump(gid, &group);

        let unjoined = std::mem::take(&mut group.members);
        let mut iter = unjoined.into_iter();
        let mut joined = Vec::new();
        let mut a = iter.next().unwrap();

        for b in iter {
            let ao: usize = self.text.cursor_to_offset(a.end()).into();
            let bo: usize = self.text.cursor_to_offset(b.start()).into();

            if bo.saturating_sub(ao) == 1 {
                a = a.union(&b);
            } else {
                joined.push(a);
                a = b;
            }
        }

        joined.push(a);

        for member in joined.into_iter() {
            let lo: usize = self.text.cursor_to_offset(group.leader.end()).into();
            let mo: usize = self.text.cursor_to_offset(member.start()).into();

            if mo.saturating_sub(lo) == 1 {
                group.leader = group.leader.union(&member);
            } else {
                group.members.push(member);
            }
        }

        self.set_group(gid, group);

        Ok(None)
    }

    fn selection_resize(
        &mut self,
        style: &SelectionResizeStyle,
        target: &EditTarget,
        ictx: &CursorGroupIdContext<'a>,
        store: &mut Store<I>,
    ) -> EditResult<EditInfo, I> {
        let ctx = &self._ctx_cgi2es(&EditAction::Motion, ictx);
        let gid = ictx.0;
        let shape = ctx.context.get_target_shape();

        let (reset, obj) = match style {
            SelectionResizeStyle::Extend => (false, false),
            SelectionResizeStyle::Object => (false, true),
            SelectionResizeStyle::Restart => (true, false),

            s => return Err(EditError::Unimplemented(format!("unknown resize style: {s:?}"))),
        };

        let mut group = self.get_group(gid);

        if target.is_jumping() {
            // Save current positions before we jump.
            self.push_jump(gid, &group);
        }

        for state in group.iter_mut() {
            let cursor = state.cursor().clone();

            if reset || !state.is_selection() {
                state.set_anchor(cursor.clone());
            }

            let tshape = match target {
                EditTarget::Boundary(range, inclusive, term, count) => {
                    if let Some(r) = self.text.range(&cursor, range, *inclusive, count, ctx) {
                        let nc = match term {
                            MoveTerminus::Beginning => r.start,
                            MoveTerminus::End => r.end,
                        };

                        state.set_cursor(nc);

                        r.shape
                    } else {
                        TargetShape::CharWise
                    }
                },
                EditTarget::CurrentPosition | EditTarget::Selection => {
                    // Do nothing to the cursor.
                    TargetShape::CharWise
                },
                EditTarget::CharJump(mark) => {
                    let nc = self._charjump(mark, ctx, store)?;
                    state.set_cursor(nc);

                    TargetShape::CharWise
                },
                EditTarget::LineJump(mark) => {
                    let nc = self._linejump(mark, ctx, store)?;
                    state.set_cursor(nc);

                    TargetShape::LineWise
                },
                EditTarget::Motion(mv, count) => {
                    if let Some(nc) = self.text.movement(&cursor, mv, count, ctx) {
                        state.set_cursor(nc);
                    }

                    mv.shape()
                },
                EditTarget::Range(range, inclusive, count) => {
                    if let Some(r) = self.text.range(&cursor, range, *inclusive, count, ctx) {
                        if obj {
                            state.set_anchor(r.start);
                            state.set_cursor(r.end);
                        } else {
                            let (start, end) = state.sorted();
                            let start = r.start.min(start);
                            let end = r.end.max(end);

                            state.set_anchor(start);
                            state.set_cursor(end);
                        }

                        r.shape
                    } else {
                        TargetShape::CharWise
                    }
                },
                EditTarget::Search(search, flip, count) => {
                    if let Some(r) =
                        self._search(&cursor, search, flip, count, ctx.context, store)?
                    {
                        if obj {
                            state.set_anchor(r.start);
                            state.set_cursor(r.end);
                        } else {
                            state.set_cursor(r.start);
                        }

                        r.shape
                    } else {
                        TargetShape::CharWise
                    }
                },

                t => return Err(EditError::Unimplemented(format!("unknown edit target: {t:?}"))),
            };

            state.set_shape(shape.unwrap_or(tshape));
        }

        group.merge();
        self.set_group(gid, group);

        Ok(None)
    }

    fn selection_split(
        &mut self,
        style: &SelectionSplitStyle,
        filter: TargetShapeFilter,
        ctx: &CursorGroupIdContext<'a>,
        store: &mut Store<I>,
    ) -> EditResult<EditInfo, I> {
        let gid = ctx.0;
        let mut group = self.get_group(gid);
        let members = std::mem::take(&mut group.members);

        let split = |mut state: CursorState| -> EditResult<Vec<CursorState>, I> {
            let (cursor, anchor, shape) = state.to_triple();

            if !filter.matches(&shape) {
                return Ok(vec![state]);
            }

            let split = match (style, shape) {
                (SelectionSplitStyle::Anchor, _) => {
                    if anchor.y == cursor.y && anchor.x == cursor.x {
                        // Anchor and cursor are already the same.
                        state.set_shape(TargetShape::CharWise);
                        return Ok(vec![state]);
                    }

                    // Create new selection from old anchor.
                    let anchor = CursorState::Selection(anchor.clone(), anchor.clone(), shape);

                    // Update the selection's anchor to be at the cursor position.
                    state.set_anchor(cursor.clone());

                    vec![anchor, state]
                },
                (SelectionSplitStyle::Lines, TargetShape::CharWise) => {
                    let (start, end) = state.sorted();
                    let range = start.y..=end.y;

                    range
                        .into_iter()
                        .map(|line| {
                            let lc = if line == start.y {
                                Cursor::new(line, start.x)
                            } else {
                                Cursor::new(line, 0)
                            };

                            let rc = if line == end.y {
                                Cursor::new(line, end.x)
                            } else {
                                Cursor::new(line, self.text.get_columns(line).saturating_sub(1))
                            };

                            CursorState::Selection(lc.clone(), rc.clone(), shape)
                        })
                        .collect()
                },
                (SelectionSplitStyle::Lines, TargetShape::LineWise) => {
                    let (start, end) = state.sorted();
                    let range = start.y..=end.y;

                    range
                        .into_iter()
                        .map(|line| {
                            let maxidx = self.text.get_columns(line).saturating_sub(1);
                            let lc = Cursor::new(line, 0);
                            let rc = Cursor::new(line, maxidx);

                            CursorState::Selection(lc.clone(), rc.clone(), shape)
                        })
                        .collect()
                },
                (SelectionSplitStyle::Lines, TargetShape::BlockWise) => {
                    // Determine the left and right borders of the block.
                    let (mut lc, mut rc) = block_cursors(&anchor, &cursor);

                    // Sort the cursors.
                    let (start, end) = state.sorted();
                    let range = start.y..=end.y;

                    range
                        .into_iter()
                        .map(|line| {
                            let lctx = &(&self.text, 0, true);
                            let rctx = &(&self.text, 0, false);

                            lc.set_line(line, lctx);
                            rc.set_line(line, rctx);

                            CursorState::Selection(lc.clone(), rc.clone(), shape)
                        })
                        .collect()
                },
                (SelectionSplitStyle::Regex(MatchAction::Keep), shape) => {
                    let needle = self._get_regex(store)?;
                    let ctx = &(&self.text, 0, true);

                    self._effective_cursors(state.start(), state.end(), true, shape)
                        .into_iter()
                        .flat_map(|(s, mut e, inclusive)| {
                            if !inclusive && s < e {
                                e.column(MoveDir1D::Previous, true, 1, ctx);
                            }
                            self.text.find_matches(&s, &e, &needle)
                        })
                        .map(|m| self.text.select(m))
                        .collect()
                },
                (SelectionSplitStyle::Regex(MatchAction::Drop), shape) => {
                    let needle = self._get_regex(store)?;
                    let ctx = &(&self.text, 0, true);

                    let mut split = vec![];
                    let cursors = self._effective_cursors(state.start(), state.end(), true, shape);

                    for (mut lc, mut rc, inclusive) in cursors {
                        if !inclusive && lc < rc {
                            rc.column(MoveDir1D::Previous, true, 1, ctx);
                        }

                        for m in self.text.find_matches(&lc, &rc, &needle) {
                            if lc < m.start {
                                let mut end = m.start;
                                end.column(MoveDir1D::Previous, true, 1, ctx);
                                split.push(CursorState::Selection(lc, end, TargetShape::CharWise));
                            }

                            lc = m.end;

                            if m.inclusive {
                                lc.column(MoveDir1D::Next, true, 1, ctx);
                            }
                        }

                        if lc <= rc {
                            split.push(CursorState::Selection(lc, rc, TargetShape::CharWise));
                        }
                    }

                    split
                },

                (s, _) => {
                    return Err(EditError::Unimplemented(format!("unknown split style: {s:?}")))
                },
            };

            return Ok(split);
        };

        for member in members {
            group.members.extend(split(member)?);
        }

        let mut leader_splits = split(group.leader.clone())?.into_iter();

        if let Some(leader) = leader_splits.next() {
            group.leader = leader;
            group.members.extend(leader_splits);
        } else if let Some(leader) = group.members.pop() {
            group.leader = leader;
        } else {
            let msg = "No selections remaining".to_string();
            let err = EditError::Failure(msg);

            return Err(err);
        }

        self.set_group(gid, group);

        Ok(None)
    }

    fn selection_trim(
        &mut self,
        boundary: &SelectionBoundary,
        filter: TargetShapeFilter,
        ctx: &CursorGroupIdContext<'a>,
        _: &mut Store<I>,
    ) -> EditResult<EditInfo, I> {
        use MoveDir1D::{Next, Previous};
        use MoveTerminus::{Beginning, End};

        let gid = ctx.0;
        let mut group = self.get_group(gid);

        let trim = |state: &CursorState| -> Option<CursorState> {
            if !filter.matches(&state.shape()) {
                return Some(state.clone());
            }

            let (cursor, anchor, shape) = state.to_triple();
            let shape = ctx.2.get_target_shape().unwrap_or(shape);

            let (sels, sele, before) = if cursor < anchor {
                (cursor, anchor, true)
            } else {
                (anchor, cursor, false)
            };

            let sels = self.text.seek(&sels, boundary, Beginning, Next, 1, true, true)?;
            let sele = self.text.seek(&sele, boundary, End, Previous, 1, true, true)?;

            if sels > sele {
                None
            } else if before {
                Some(CursorState::Selection(sels, sele, shape))
            } else {
                Some(CursorState::Selection(sele, sels, shape))
            }
        };

        group.members = group.members.iter().filter_map(trim).collect();

        if let Some(leader) = trim(&group.leader) {
            group.leader = leader;
        } else if let Some(leader) = group.members.pop() {
            group.leader = leader;
        } else {
            let msg = "No selections remaining".to_string();
            let err = EditError::Failure(msg);

            return Err(err);
        }

        self.set_group(gid, group);

        Ok(None)
    }
}

#[cfg(test)]
mod tests {
    use super::super::tests::*;
    use super::*;

    macro_rules! selection_cursor_set {
        ($ebuf: expr, $change: expr, $ctx: expr, $store: expr) => {
            $ebuf.selection_cursor_set($change, $ctx, &mut $store).unwrap()
        };
    }

    macro_rules! selection_split_anchor {
        ($ebuf: expr, $filter: expr, $ctx: expr, $store: expr) => {
            selection_split!($ebuf, SelectionSplitStyle::Anchor, $filter, $ctx, $store)
        };
    }

    macro_rules! selection_split_lines {
        ($ebuf: expr, $filter: expr, $ctx: expr, $store: expr) => {
            selection_split!($ebuf, SelectionSplitStyle::Lines, $filter, $ctx, $store)
        };
    }

    macro_rules! selection_split_regex {
        ($ebuf: expr, $filter: expr, $keep: expr, $ctx: expr, $store: expr) => {
            selection_split!($ebuf, SelectionSplitStyle::Regex($keep), $filter, $ctx, $store)
        };
    }

    macro_rules! selection_extend {
        ($ebuf: expr, $et: expr, $ctx: expr, $store: expr) => {
            $ebuf
                .selection_resize(&SelectionResizeStyle::Extend, &$et, $ctx, &mut $store)
                .unwrap()
        };
    }

    macro_rules! selection_restart {
        ($ebuf: expr, $et: expr, $ctx: expr, $store: expr) => {
            $ebuf
                .selection_resize(&SelectionResizeStyle::Restart, &$et, $ctx, &mut $store)
                .unwrap()
        };
    }

    macro_rules! selection_expand {
        ($ebuf: expr, $boundary: expr, $filter: expr, $ctx: expr, $store: expr) => {
            $ebuf.selection_expand(&$boundary, $filter, $ctx, &mut $store).unwrap()
        };
    }

    macro_rules! selection_trim {
        ($ebuf: expr, $boundary: expr, $filter: expr, $ctx: expr, $store: expr) => {
            $ebuf.selection_trim(&$boundary, $filter, $ctx, &mut $store).unwrap()
        };
    }

    macro_rules! selection_duplicate {
        ($ebuf: expr, $dir: expr, $count: expr, $ctx: expr, $store: expr) => {
            $ebuf.selection_duplicate($dir, &$count, $ctx, &mut $store).unwrap()
        };
    }

    macro_rules! selection_close {
        ($ebuf: expr, $target: expr, $ctx: expr, $store: expr) => {
            $ebuf.cursor_close($target, $ctx, &mut $store).unwrap()
        };
    }

    macro_rules! selection_rotate {
        ($ebuf: expr, $dir: expr, $count: expr, $ctx: expr, $store: expr) => {
            $ebuf.cursor_rotate($dir, &$count, $ctx, &mut $store).unwrap()
        };
    }

    macro_rules! selection_join {
        ($ebuf: expr, $ctx: expr, $store: expr) => {
            $ebuf.selection_join($ctx, &mut $store).unwrap()
        };
    }

    macro_rules! selection_split {
        ($ebuf: expr, $style: expr, $filter: expr, $ctx: expr, $store: expr) => {
            $ebuf.selection_split(&$style, $filter, $ctx, &mut $store).unwrap()
        };
    }

    #[test]
    fn test_selection_join() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) =
            mkfivestr("hello\nhello\nhello\nhello\nhello\n");

        // Start out at (0, 0).
        ebuf.set_leader(curid, Cursor::new(0, 0));

        vctx.persist.shape = Some(TargetShape::CharWise);

        // Extend to the newline.
        let mov = MoveType::BufferByteOffset;
        edit!(ebuf, EditAction::Motion, mv!(mov, 6), ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(0, 0), Cursor::new(0, 5), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 5));

        // Duplicate selection across the following lines.
        selection_duplicate!(ebuf, MoveDir1D::Next, 4.into(), ctx!(curid, vwctx, vctx), store);

        let fsels = vec![
            (Cursor::new(1, 0), Cursor::new(1, 5), TargetShape::CharWise),
            (Cursor::new(2, 0), Cursor::new(2, 5), TargetShape::CharWise),
            (Cursor::new(3, 0), Cursor::new(3, 5), TargetShape::CharWise),
            (Cursor::new(4, 0), Cursor::new(4, 5), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 5));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        selection_join!(ebuf, ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(0, 0), Cursor::new(4, 5), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 5));
        assert_eq!(ebuf.get_follower_selections(curid), None);
    }

    #[test]
    fn test_selection_split_anchor() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) =
            mkfivestr("a b c d\ne f g\nh i j k l\nm n o p\nq r\n");

        // Start out at (2, 6).
        ebuf.set_leader(curid, Cursor::new(2, 6));

        vctx.persist.shape = Some(TargetShape::CharWise);

        // Create a charwise selection across the three lines.
        let mov = MoveType::FirstWord(MoveDir1D::Next);
        edit!(ebuf, EditAction::Motion, mv!(mov, 2), ctx!(curid, vwctx, vctx), store);

        let selection = (Cursor::new(2, 6), Cursor::new(4, 0), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), None);
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 0));

        // Filter doesn't match, nothing happens.
        selection_split_anchor!(ebuf, TargetShapeFilter::LINE, ctx!(curid, vwctx, vctx), store);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 0));
        assert_eq!(ebuf.get_follower_selections(curid), None);

        // Filter matches, splits into two CharWise selections.
        selection_split_anchor!(ebuf, TargetShapeFilter::CHAR, ctx!(curid, vwctx, vctx), store);
        let selection1 = (Cursor::new(2, 6), Cursor::new(2, 6), TargetShape::CharWise);
        let selection2 = (Cursor::new(4, 0), Cursor::new(4, 0), TargetShape::CharWise);
        let selections = vec![selection2];
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection1.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(2, 6));
        assert_eq!(ebuf.get_follower_selections(curid), Some(selections));
        assert_eq!(ebuf.get_followers(curid), vec![Cursor::new(4, 0)]);
    }

    #[test]
    fn test_selection_split_lines_blockwise() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) =
            mkfivestr("a b c d\ne f g\nh i j k l\nm n o p\nq r\n");

        // Start out at (2, 6).
        ebuf.set_leader(curid, Cursor::new(2, 6));

        vctx.persist.shape = Some(TargetShape::BlockWise);

        // Create a blockwise selection across the three lines.
        let mov = MoveType::FirstWord(MoveDir1D::Next);
        edit!(ebuf, EditAction::Motion, mv!(mov, 2), ctx!(curid, vwctx, vctx), store);

        let selection = (Cursor::new(2, 6), Cursor::new(4, 0), TargetShape::BlockWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), None);
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 0));

        // Filter doesn't match, nothing happens.
        selection_split_lines!(ebuf, TargetShapeFilter::LINE, ctx!(curid, vwctx, vctx), store);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), None);
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 0));

        // Filter matches, splits into multiple CharWise selections.
        selection_split_lines!(ebuf, TargetShapeFilter::BLOCK, ctx!(curid, vwctx, vctx), store);

        let selection1 = (Cursor::new(2, 0), Cursor::new(2, 6), TargetShape::BlockWise);
        let selection2 = (Cursor::new(3, 0), Cursor::new(3, 6), TargetShape::BlockWise);
        let selection3 = (Cursor::new(4, 0), Cursor::new(4, 2).goal(6), TargetShape::BlockWise);
        let selections = vec![selection2, selection3];

        assert_eq!(ebuf.get_leader_selection(curid), Some(selection1.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(2, 0));
        assert_eq!(ebuf.get_follower_selections(curid), Some(selections));
        assert_eq!(ebuf.get_followers(curid), vec![Cursor::new(3, 0), Cursor::new(4, 0)]);
    }

    #[test]
    fn test_selection_split_lines_charwise() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) =
            mkfivestr("a b c d\ne f g\nh i j k l\nm n o p\nq r s t\n");

        // Start out at (1, 2).
        ebuf.set_leader(curid, Cursor::new(1, 2));

        vctx.persist.shape = Some(TargetShape::CharWise);

        // Create a charwise selection across the three lines.
        let mov = MoveType::FirstWord(MoveDir1D::Next);
        edit!(ebuf, EditAction::Motion, mv!(mov, 2), ctx!(curid, vwctx, vctx), store);

        let selection = (Cursor::new(1, 2), Cursor::new(3, 0), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), None);
        assert_eq!(ebuf.get_leader(curid), Cursor::new(3, 0));

        // Filter doesn't match, nothing happens.
        selection_split_lines!(ebuf, TargetShapeFilter::LINE, ctx!(curid, vwctx, vctx), store);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), None);
        assert_eq!(ebuf.get_leader(curid), Cursor::new(3, 0));

        // Filter matches, splits into multiple CharWise selections.
        selection_split_lines!(ebuf, TargetShapeFilter::CHAR, ctx!(curid, vwctx, vctx), store);

        let selection1 = (Cursor::new(1, 2), Cursor::new(1, 4), TargetShape::CharWise);
        let selection2 = (Cursor::new(2, 0), Cursor::new(2, 8), TargetShape::CharWise);
        let selection3 = (Cursor::new(3, 0), Cursor::new(3, 0), TargetShape::CharWise);
        let selections = vec![selection2, selection3];

        assert_eq!(ebuf.get_leader_selection(curid), Some(selection1.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 2));
        assert_eq!(ebuf.get_follower_selections(curid), Some(selections));
        assert_eq!(ebuf.get_followers(curid), vec![Cursor::new(2, 0), Cursor::new(3, 0)]);
    }

    #[test]
    fn test_selection_split_lines_linewise() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) =
            mkfivestr("a b c d\ne f g\nh i j k l\nm n o p\nq r s t\n");

        // Start out at (1, 0).
        ebuf.set_leader(curid, Cursor::new(1, 0));

        vctx.persist.shape = Some(TargetShape::LineWise);

        // Create a linewise selection across three lines.
        let mov = MoveType::FirstWord(MoveDir1D::Next);
        edit!(ebuf, EditAction::Motion, mv!(mov, 2), ctx!(curid, vwctx, vctx), store);

        let selection = (Cursor::new(1, 0), Cursor::new(3, 0), TargetShape::LineWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), None);
        assert_eq!(ebuf.get_leader(curid), Cursor::new(3, 0));

        // Filter doesn't match, nothing happens.
        selection_split_lines!(ebuf, TargetShapeFilter::CHAR, ctx!(curid, vwctx, vctx), store);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), None);
        assert_eq!(ebuf.get_leader(curid), Cursor::new(3, 0));

        // Filter matches, splits into multiple LineWise selections.
        selection_split_lines!(ebuf, TargetShapeFilter::LINE, ctx!(curid, vwctx, vctx), store);

        let selection1 = (Cursor::new(1, 0), Cursor::new(1, 4), TargetShape::LineWise);
        let selection2 = (Cursor::new(2, 0), Cursor::new(2, 8), TargetShape::LineWise);
        let selection3 = (Cursor::new(3, 0), Cursor::new(3, 6), TargetShape::LineWise);
        let selections = vec![selection2, selection3];

        assert_eq!(ebuf.get_leader_selection(curid), Some(selection1.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 0));
        assert_eq!(ebuf.get_follower_selections(curid), Some(selections));
        assert_eq!(ebuf.get_followers(curid), vec![Cursor::new(2, 0), Cursor::new(3, 0)]);
    }

    #[test]
    fn test_selection_split_regex_keep() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) =
            mkfivestr("foo,bar,baz\na,b,c\nd,,e,\nm n o p\nq r s t\n");

        // Start out at (1, 0).
        ebuf.set_leader(curid, Cursor::new(1, 0));

        vctx.persist.shape = Some(TargetShape::LineWise);

        store.registers.set_last_search(",");

        // Create a linewise selection across three lines.
        let mov = MoveType::FirstWord(MoveDir1D::Next);
        edit!(ebuf, EditAction::Motion, mv!(mov, 2), ctx!(curid, vwctx, vctx), store);

        let selection = (Cursor::new(1, 0), Cursor::new(3, 0), TargetShape::LineWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), None);
        assert_eq!(ebuf.get_leader(curid), Cursor::new(3, 0));

        // Filter doesn't match, nothing happens.
        selection_split_regex!(
            ebuf,
            TargetShapeFilter::CHAR,
            MatchAction::Keep,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), None);
        assert_eq!(ebuf.get_leader(curid), Cursor::new(3, 0));

        // Filter matches, splits into multiple LineWise selections.
        selection_split_regex!(
            ebuf,
            TargetShapeFilter::LINE,
            MatchAction::Keep,
            ctx!(curid, vwctx, vctx),
            store
        );

        let selection1 = (Cursor::new(1, 1), Cursor::new(1, 1), TargetShape::CharWise);
        let selection2 = (Cursor::new(1, 3), Cursor::new(1, 3), TargetShape::CharWise);
        let selection3 = (Cursor::new(2, 1), Cursor::new(2, 1), TargetShape::CharWise);
        let selection4 = (Cursor::new(2, 2), Cursor::new(2, 2), TargetShape::CharWise);
        let selection5 = (Cursor::new(2, 4), Cursor::new(2, 4), TargetShape::CharWise);
        let selections = vec![selection2, selection3, selection4, selection5];
        let followers = vec![
            Cursor::new(1, 3),
            Cursor::new(2, 1),
            Cursor::new(2, 2),
            Cursor::new(2, 4),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(selection1.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 1));
        assert_eq!(ebuf.get_follower_selections(curid), Some(selections));
        assert_eq!(ebuf.get_followers(curid), followers);
    }

    #[test]
    fn test_selection_split_regex_drop() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) =
            mkfivestr("foo,bar,baz\na,b,c\nd,,e,\nm n o p\nq r s t\n");

        // Start out at (1, 0).
        ebuf.set_leader(curid, Cursor::new(1, 0));

        vctx.persist.shape = Some(TargetShape::LineWise);

        store.registers.set_last_search(",");

        // Create a linewise selection across three lines.
        let mov = MoveType::FirstWord(MoveDir1D::Next);
        edit!(ebuf, EditAction::Motion, mv!(mov, 2), ctx!(curid, vwctx, vctx), store);

        let selection = (Cursor::new(1, 0), Cursor::new(3, 0), TargetShape::LineWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), None);
        assert_eq!(ebuf.get_leader(curid), Cursor::new(3, 0));

        // Filter doesn't match, nothing happens.
        selection_split_regex!(
            ebuf,
            TargetShapeFilter::CHAR,
            MatchAction::Drop,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), None);
        assert_eq!(ebuf.get_leader(curid), Cursor::new(3, 0));

        // Filter matches, splits into multiple LineWise selections.
        selection_split_regex!(
            ebuf,
            TargetShapeFilter::LINE,
            MatchAction::Drop,
            ctx!(curid, vwctx, vctx),
            store
        );

        let selection1 = (Cursor::new(1, 0), Cursor::new(1, 0), TargetShape::CharWise);
        let selection2 = (Cursor::new(1, 2), Cursor::new(1, 2), TargetShape::CharWise);
        let selection3 = (Cursor::new(1, 4), Cursor::new(2, 0), TargetShape::CharWise);
        let selection4 = (Cursor::new(2, 3), Cursor::new(2, 3), TargetShape::CharWise);
        let selection5 = (Cursor::new(2, 5), Cursor::new(3, 7), TargetShape::CharWise);
        let selections = vec![selection2, selection3, selection4, selection5];
        let followers = vec![
            Cursor::new(1, 2),
            Cursor::new(1, 4),
            Cursor::new(2, 3),
            Cursor::new(2, 5),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(selection1.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 0));
        assert_eq!(ebuf.get_follower_selections(curid), Some(selections));
        assert_eq!(ebuf.get_followers(curid), followers);
    }

    #[test]
    fn test_selection_cursor_set_charwise() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) = mkfivestr("hello world\na b c d\n");

        // Start out at (0, 6).
        ebuf.set_leader(curid, Cursor::new(0, 6));

        vctx.persist.shape = Some(TargetShape::CharWise);

        // Create a selection to resize from here to next word beginning.
        let mov = MoveType::FirstWord(MoveDir1D::Next);
        edit!(ebuf, EditAction::Motion, mv!(mov), ctx!(curid, vwctx, vctx), store);

        let selection = (Cursor::new(0, 6), Cursor::new(1, 0), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 0));

        // We're already at the end so this shouldn't move.
        selection_cursor_set!(ebuf, &SelectionCursorChange::End, ctx!(curid, vwctx, vctx), store);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 0));

        // Now swap which side is the anchor, and which side is mobile.
        selection_cursor_set!(
            ebuf,
            &SelectionCursorChange::SwapAnchor,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 6));

        // We're already at the beginning so this shouldn't move.
        selection_cursor_set!(
            ebuf,
            &SelectionCursorChange::Beginning,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 6));

        // Move to end.
        selection_cursor_set!(ebuf, &SelectionCursorChange::End, ctx!(curid, vwctx, vctx), store);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 0));
    }

    #[test]
    fn test_selection_cursor_set_linewise() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) =
            mkfivestr("foo\nhello world\na b c d\n");

        // Start out at (1, 6).
        ebuf.set_leader(curid, Cursor::new(1, 6));

        vctx.persist.shape = Some(TargetShape::LineWise);

        // Create a linewise selection across the two lines.
        let mov = MoveType::FirstWord(MoveDir1D::Next);
        edit!(ebuf, EditAction::Motion, mv!(mov), ctx!(curid, vwctx, vctx), store);

        let selection = (Cursor::new(1, 6), Cursor::new(2, 0), TargetShape::LineWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(2, 0));

        // Now swap which side is the anchor, and which side is mobile.
        selection_cursor_set!(
            ebuf,
            &SelectionCursorChange::SwapAnchor,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 6));

        // Moving to the end of the selection places cursor in last column of last line.
        let selection = (Cursor::new(1, 0), Cursor::new(2, 6), TargetShape::LineWise);
        selection_cursor_set!(ebuf, &SelectionCursorChange::End, ctx!(curid, vwctx, vctx), store);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(2, 6));

        // Moving to the beginning of the selection places cursor in first column of first line.
        selection_cursor_set!(
            ebuf,
            &SelectionCursorChange::Beginning,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 0));
    }

    #[test]
    fn test_selection_cursor_set_blockwise() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) =
            mkfivestr("foo\nhello world\na b c d\n");

        // Start out at (1, 2).
        ebuf.set_leader(curid, Cursor::new(1, 2));

        vctx.persist.shape = Some(TargetShape::BlockWise);

        // Create a block selection across two lines.
        let mov = MoveType::BufferByteOffset;
        edit!(ebuf, EditAction::Motion, mv!(mov, 21), ctx!(curid, vwctx, vctx), store);

        let selection = (Cursor::new(1, 2), Cursor::new(2, 4), TargetShape::BlockWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(2, 4));

        // Now swap which side is the anchor, and which side is mobile.
        selection_cursor_set!(
            ebuf,
            &SelectionCursorChange::SwapAnchor,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 2));

        // Now swap over to the upper-right corner.
        selection_cursor_set!(
            ebuf,
            &SelectionCursorChange::SwapSide,
            ctx!(curid, vwctx, vctx),
            store
        );

        let selection = (Cursor::new(1, 4), Cursor::new(2, 2), TargetShape::BlockWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 4));

        // Now swap to the first column of the selection.
        selection_cursor_set!(
            ebuf,
            &SelectionCursorChange::Beginning,
            ctx!(curid, vwctx, vctx),
            store
        );

        let selection = (Cursor::new(1, 2), Cursor::new(2, 4), TargetShape::BlockWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 2));

        // Now swap to the last column of the selection.
        selection_cursor_set!(ebuf, &SelectionCursorChange::End, ctx!(curid, vwctx, vctx), store);

        assert_eq!(ebuf.get_leader_selection(curid), Some(selection.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(2, 4));
    }

    #[test]
    fn test_selection_restart() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) = mkfivestr("hello world a b c d\n");

        // Start out at (0, 0).
        ebuf.set_leader(curid, Cursor::new(0, 0));

        vctx.persist.shape = Some(TargetShape::CharWise);

        // Create a selection to resize from here to next word beginning.
        let mov = MoveType::WordBegin(WordStyle::Little, MoveDir1D::Next);
        edit!(ebuf, EditAction::Motion, mv!(mov), ctx!(curid, vwctx, vctx), store);

        let selection = (Cursor::new(0, 0), Cursor::new(0, 6), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 6));

        // Now resize the selection to just be the second word.
        let mov = MoveType::WordEnd(WordStyle::Little, MoveDir1D::Next);
        selection_restart!(ebuf, mv!(mov), ctx!(curid, vwctx, vctx), store);

        let selection = (Cursor::new(0, 6), Cursor::new(0, 10), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 10));

        // Now resize the selection to just be the cursor position.
        selection_restart!(ebuf, EditTarget::CurrentPosition, ctx!(curid, vwctx, vctx), store);

        let selection = (Cursor::new(0, 10), Cursor::new(0, 10), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(selection));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 10));
    }

    #[test]
    fn test_selection_resize_range_grows() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) = mkfivestr("hello world\n");
        let cw = TargetShape::CharWise;

        // Start out at (0, 2), over "l".
        ebuf.set_leader(curid, Cursor::new(0, 2));

        vctx.persist.shape = Some(cw);

        // Create selection over "ll".
        let right = MoveType::Column(MoveDir1D::Next, false).into();
        let lsel = (Cursor::new(0, 2), Cursor::new(0, 3), cw);
        selection_extend!(ebuf, right, ctx!(curid, vwctx, vctx), store);
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 3));
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));

        // Doing a word range should also move anchor.
        let range = RangeType::Word(WordStyle::Little).into();
        let lsel = (Cursor::new(0, 0), Cursor::new(0, 4), cw);
        selection_extend!(ebuf, range, ctx!(curid, vwctx, vctx), store);
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 4));
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
    }

    #[test]
    fn test_selection_duplicate_and_rotate() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) = mkfivestr(
            "copy lines\na1\ncopy lines\n\nstart line\n\nb2\ncopy lines\nc3\ncopy lines\n",
        );

        // Start out at (4, 2), on the " " in "copy lines".
        ebuf.set_leader(curid, Cursor::new(4, 2));

        vctx.persist.shape = Some(TargetShape::CharWise);

        // Create a selection from " " to "n".
        let mov = MoveType::Column(MoveDir1D::Next, false);
        edit!(ebuf, EditAction::Motion, mv!(mov, 5), ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(4, 2), Cursor::new(4, 7), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 7));

        // Now duplicate the selection twice, onto the next available lines.
        selection_duplicate!(ebuf, MoveDir1D::Next, 2.into(), ctx!(curid, vwctx, vctx), store);

        let fsels = vec![
            (Cursor::new(7, 2), Cursor::new(7, 7), TargetShape::CharWise),
            (Cursor::new(9, 2), Cursor::new(9, 7), TargetShape::CharWise),
        ];

        // Leader stays the same, and we have new selections on the lines containing "copy line".
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 7));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Duplicating again doesn't result in overlapping selections.
        selection_duplicate!(ebuf, MoveDir1D::Next, 2.into(), ctx!(curid, vwctx, vctx), store);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 7));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Move back to word end so we have different end columns.
        let mov = MoveType::WordEnd(WordStyle::Little, MoveDir1D::Previous);
        edit!(ebuf, EditAction::Motion, mv!(mov), ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(4, 2), Cursor::new(4, 4), TargetShape::CharWise);
        let fsels = vec![
            (Cursor::new(7, 2), Cursor::new(7, 3), TargetShape::CharWise),
            (Cursor::new(9, 2), Cursor::new(9, 3), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 4));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Duplicate backwards once.
        selection_duplicate!(ebuf, MoveDir1D::Previous, 1.into(), ctx!(curid, vwctx, vctx), store);

        let fsels = vec![
            (Cursor::new(2, 2), Cursor::new(2, 4), TargetShape::CharWise),
            (Cursor::new(7, 2), Cursor::new(7, 3), TargetShape::CharWise),
            (Cursor::new(9, 2), Cursor::new(9, 3), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 4));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Duplicate 4 times backwards, getting one more selection.
        selection_duplicate!(ebuf, MoveDir1D::Previous, 1.into(), ctx!(curid, vwctx, vctx), store);

        let fsels = vec![
            (Cursor::new(0, 2), Cursor::new(0, 4), TargetShape::CharWise),
            (Cursor::new(2, 2), Cursor::new(2, 4), TargetShape::CharWise),
            (Cursor::new(7, 2), Cursor::new(7, 3), TargetShape::CharWise),
            (Cursor::new(9, 2), Cursor::new(9, 3), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 4));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Duplicate once forward, extending the second to last selection.
        selection_duplicate!(ebuf, MoveDir1D::Next, 1.into(), ctx!(curid, vwctx, vctx), store);
        let fsels = vec![
            (Cursor::new(0, 2), Cursor::new(0, 4), TargetShape::CharWise),
            (Cursor::new(2, 2), Cursor::new(2, 4), TargetShape::CharWise),
            (Cursor::new(7, 2), Cursor::new(7, 4), TargetShape::CharWise),
            (Cursor::new(9, 2), Cursor::new(9, 3), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 4));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Again.
        selection_duplicate!(ebuf, MoveDir1D::Next, 1.into(), ctx!(curid, vwctx, vctx), store);
        let fsels = vec![
            (Cursor::new(0, 2), Cursor::new(0, 4), TargetShape::CharWise),
            (Cursor::new(2, 2), Cursor::new(2, 4), TargetShape::CharWise),
            (Cursor::new(7, 2), Cursor::new(7, 4), TargetShape::CharWise),
            (Cursor::new(9, 2), Cursor::new(9, 4), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 4));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Rotate the leader three times forwards, wrapping around.
        selection_rotate!(ebuf, MoveDir1D::Next, 3.into(), ctx!(curid, vwctx, vctx), store);
        let lsel = (Cursor::new(0, 2), Cursor::new(0, 4), TargetShape::CharWise);
        let fsels = vec![
            (Cursor::new(2, 2), Cursor::new(2, 4), TargetShape::CharWise),
            (Cursor::new(4, 2), Cursor::new(4, 4), TargetShape::CharWise),
            (Cursor::new(7, 2), Cursor::new(7, 4), TargetShape::CharWise),
            (Cursor::new(9, 2), Cursor::new(9, 4), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 4));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Rotate the leader once backwards, wrapping around again.
        selection_rotate!(ebuf, MoveDir1D::Previous, 1.into(), ctx!(curid, vwctx, vctx), store);
        let lsel = (Cursor::new(9, 2), Cursor::new(9, 4), TargetShape::CharWise);
        let fsels = vec![
            (Cursor::new(0, 2), Cursor::new(0, 4), TargetShape::CharWise),
            (Cursor::new(2, 2), Cursor::new(2, 4), TargetShape::CharWise),
            (Cursor::new(4, 2), Cursor::new(4, 4), TargetShape::CharWise),
            (Cursor::new(7, 2), Cursor::new(7, 4), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(9, 4));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Rotate the leader twice backwards, no wrapping.
        selection_rotate!(ebuf, MoveDir1D::Previous, 2.into(), ctx!(curid, vwctx, vctx), store);
        let lsel = (Cursor::new(4, 2), Cursor::new(4, 4), TargetShape::CharWise);
        let fsels = vec![
            (Cursor::new(0, 2), Cursor::new(0, 4), TargetShape::CharWise),
            (Cursor::new(2, 2), Cursor::new(2, 4), TargetShape::CharWise),
            (Cursor::new(7, 2), Cursor::new(7, 4), TargetShape::CharWise),
            (Cursor::new(9, 2), Cursor::new(9, 4), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 4));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));
    }

    #[test]
    fn test_selection_duplicate_and_close() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) =
            mkfivestr("  a  \n  b  \n  c  \n  d  \n  e  \n");

        // Start out at (0, 0).
        ebuf.set_leader(curid, Cursor::new(0, 0));

        vctx.persist.shape = Some(TargetShape::CharWise);

        // Create a selection to the end of the line.
        let mov = MoveType::Column(MoveDir1D::Next, false);
        edit!(ebuf, EditAction::Motion, mv!(mov, 4), ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(0, 0), Cursor::new(0, 4), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 4));

        // Now duplicate the selection four times.
        selection_duplicate!(ebuf, MoveDir1D::Next, 4.into(), ctx!(curid, vwctx, vctx), store);

        let fsels = vec![
            (Cursor::new(1, 0), Cursor::new(1, 4), TargetShape::CharWise),
            (Cursor::new(2, 0), Cursor::new(2, 4), TargetShape::CharWise),
            (Cursor::new(3, 0), Cursor::new(3, 4), TargetShape::CharWise),
            (Cursor::new(4, 0), Cursor::new(4, 4), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 4));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Rotate the leader forward.
        selection_rotate!(ebuf, MoveDir1D::Next, 1.into(), ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(1, 0), Cursor::new(1, 4), TargetShape::CharWise);

        let fsels = vec![
            (Cursor::new(0, 0), Cursor::new(0, 4), TargetShape::CharWise),
            (Cursor::new(2, 0), Cursor::new(2, 4), TargetShape::CharWise),
            (Cursor::new(3, 0), Cursor::new(3, 4), TargetShape::CharWise),
            (Cursor::new(4, 0), Cursor::new(4, 4), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 4));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Close leader.
        selection_close!(ebuf, &CursorCloseTarget::Leader, ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(2, 0), Cursor::new(2, 4), TargetShape::CharWise);
        let fsels = vec![
            (Cursor::new(0, 0), Cursor::new(0, 4), TargetShape::CharWise),
            (Cursor::new(3, 0), Cursor::new(3, 4), TargetShape::CharWise),
            (Cursor::new(4, 0), Cursor::new(4, 4), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(2, 4));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Close followers.
        selection_close!(ebuf, &CursorCloseTarget::Followers, ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(2, 0), Cursor::new(2, 4), TargetShape::CharWise);

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(2, 4));
        assert_eq!(ebuf.get_follower_selections(curid), None);
    }

    #[test]
    fn test_selection_expand_line_charwise() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) = mkfivestr("a   \t   b   \n   c   \n");

        // Start out at (0, 1), on the space after the "a".
        ebuf.set_leader(curid, Cursor::new(0, 1));

        vctx.persist.shape = Some(TargetShape::CharWise);

        // Create a selection from the space after "a" to "c".
        let mov = MoveType::BufferByteOffset;
        edit!(ebuf, EditAction::Motion, mv!(mov, 17), ctx!(curid, vwctx, vctx), store);
        let lsel = (Cursor::new(0, 1), Cursor::new(1, 3), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 3));

        // Expand with TargetShapeFilter::LINE does nothing.
        selection_expand!(
            ebuf,
            SelectionBoundary::Line,
            TargetShapeFilter::LINE,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 3));

        // Now expand to include both lines.
        let lsel = (Cursor::new(0, 0), Cursor::new(1, 7), TargetShape::CharWise);
        selection_expand!(
            ebuf,
            SelectionBoundary::Line,
            TargetShapeFilter::CHAR,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 7));

        // Shrink selection to go from start to "b".
        let mov = MoveType::BufferByteOffset;
        edit!(ebuf, EditAction::Motion, mv!(mov, 9), ctx!(curid, vwctx, vctx), store);
        let lsel = (Cursor::new(0, 0), Cursor::new(0, 8), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 8));

        // Now expand to include just the first line.
        let lsel = (Cursor::new(0, 0), Cursor::new(0, 12), TargetShape::CharWise);
        selection_expand!(
            ebuf,
            SelectionBoundary::Line,
            TargetShapeFilter::CHAR,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 12));

        // Repeating changes nothing.
        selection_expand!(
            ebuf,
            SelectionBoundary::Line,
            TargetShapeFilter::CHAR,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 12));
    }

    #[test]
    fn test_selection_expand_line_linewise() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) = mkfivestr("a   \t   b   \n   c   \n");

        // Start out at (0, 1), on the space after the "a".
        ebuf.set_leader(curid, Cursor::new(0, 1));

        vctx.persist.shape = Some(TargetShape::LineWise);

        // Create a selection from the space after "a" to "c".
        let mov = MoveType::BufferByteOffset;
        edit!(ebuf, EditAction::Motion, mv!(mov, 17), ctx!(curid, vwctx, vctx), store);
        let lsel = (Cursor::new(0, 1), Cursor::new(1, 3), TargetShape::LineWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 3));

        // Expand with TargetShapeFilter::CHAR does nothing.
        selection_expand!(
            ebuf,
            SelectionBoundary::Line,
            TargetShapeFilter::CHAR,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 3));

        // Since this was a linewise selection, expansion should touch same content, but move
        // anchor and cursor.
        let lsel = (Cursor::new(0, 0), Cursor::new(1, 7), TargetShape::LineWise);
        selection_expand!(
            ebuf,
            SelectionBoundary::Line,
            TargetShapeFilter::LINE,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 7));

        // Shrink selection to first line, with cursor at "b".
        let mov = MoveType::BufferByteOffset;
        edit!(ebuf, EditAction::Motion, mv!(mov, 9), ctx!(curid, vwctx, vctx), store);
        let lsel = (Cursor::new(0, 0), Cursor::new(0, 8), TargetShape::LineWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 8));

        // Expansion still only selects first line, but cursor is moved.
        let lsel = (Cursor::new(0, 0), Cursor::new(0, 12), TargetShape::LineWise);
        selection_expand!(
            ebuf,
            SelectionBoundary::Line,
            TargetShapeFilter::LINE,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 12));

        // Repeating changes nothing.
        selection_expand!(
            ebuf,
            SelectionBoundary::Line,
            TargetShapeFilter::LINE,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 12));
    }

    #[test]
    fn test_selection_trim_line_charwise() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) = mkfivestr("a   \t   b   \n   c   \n");

        // Start out at (0, 1), on the space after the "a".
        ebuf.set_leader(curid, Cursor::new(0, 0));

        vctx.persist.shape = Some(TargetShape::CharWise);

        // Create a selection from the first character to "c".
        let mov = MoveType::BufferByteOffset;
        edit!(ebuf, EditAction::Motion, mv!(mov, 17), ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(0, 0), Cursor::new(1, 3), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 3));

        // Trim with TargetShapeFilter::LINE does nothing.
        selection_trim!(
            ebuf,
            SelectionBoundary::Line,
            TargetShapeFilter::LINE,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 3));

        // Now trim down to the first line.
        selection_trim!(
            ebuf,
            SelectionBoundary::Line,
            TargetShapeFilter::CHAR,
            ctx!(curid, vwctx, vctx),
            store
        );

        let lsel = (Cursor::new(0, 0), Cursor::new(0, 12), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 12));

        // Repeating changes nothing.
        selection_trim!(
            ebuf,
            SelectionBoundary::Line,
            TargetShapeFilter::CHAR,
            ctx!(curid, vwctx, vctx),
            store
        );

        let lsel = (Cursor::new(0, 0), Cursor::new(0, 12), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 12));
    }

    #[test]
    fn test_selection_trim_nws_charwise() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) = mkfivestr("a   \t   b   \n   c   \n");

        // Start out at (0, 1), on the space after the "a".
        ebuf.set_leader(curid, Cursor::new(0, 1));

        vctx.persist.shape = Some(TargetShape::CharWise);

        // Create a selection from the space after "a" to the final column.
        let mov = MoveType::BufferByteOffset;
        edit!(ebuf, EditAction::Motion, mv!(mov, 20), ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(0, 1), Cursor::new(1, 6), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 6));

        // Trim with TargetShapeFilter::LINE does nothing.
        selection_trim!(
            ebuf,
            SelectionBoundary::NonWhitespace,
            TargetShapeFilter::LINE,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 6));

        // Now trim down to the "b" and the "c" with TargetShapeFilter::CHAR.
        selection_trim!(
            ebuf,
            SelectionBoundary::NonWhitespace,
            TargetShapeFilter::CHAR,
            ctx!(curid, vwctx, vctx),
            store
        );

        let lsel = (Cursor::new(0, 8), Cursor::new(1, 3), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 3));

        // Move back 1 from the "c".
        let mov = MoveType::Column(MoveDir1D::Previous, false);
        edit!(ebuf, EditAction::Motion, mv!(mov, 1), ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(0, 8), Cursor::new(1, 2), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(1, 2));

        // Trim down to just the "b".
        selection_trim!(
            ebuf,
            SelectionBoundary::NonWhitespace,
            TargetShapeFilter::CHAR,
            ctx!(curid, vwctx, vctx),
            store
        );

        let lsel = (Cursor::new(0, 8), Cursor::new(0, 8), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 8));
    }

    #[test]
    fn test_selection_trim_nws_linewise() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) =
            mkfivestr("a\n   \n\t\nb   \n   c   \n\n\n");

        // Start out at (1, 0), a line of all spaces.
        ebuf.set_leader(curid, Cursor::new(1, 0));

        vctx.persist.shape = Some(TargetShape::LineWise);

        // Create a selection down to line 6.
        let mov = MoveType::Line(MoveDir1D::Next);
        edit!(ebuf, EditAction::Motion, mv!(mov, 5), ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(1, 0), Cursor::new(6, 0), TargetShape::LineWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(6, 0));

        // Trim with TargetShapeFilter::CHAR does nothing.
        selection_trim!(
            ebuf,
            SelectionBoundary::NonWhitespace,
            TargetShapeFilter::CHAR,
            ctx!(curid, vwctx, vctx),
            store
        );
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(6, 0));

        // Now trim down to the "b" and "c" lines with TargetShapeFilter::LINE.
        selection_trim!(
            ebuf,
            SelectionBoundary::NonWhitespace,
            TargetShapeFilter::LINE,
            ctx!(curid, vwctx, vctx),
            store
        );

        let lsel = (Cursor::new(3, 0), Cursor::new(4, 3), TargetShape::LineWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(4, 3));
    }

    #[test]
    fn test_selection_trim_line_drop_empty() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) =
            mkfivestr("  a  \n      \n  b  \n      \n  c  \n");

        // Start out at (0, 1), on the space after the "a".
        ebuf.set_leader(curid, Cursor::new(0, 0));

        vctx.persist.shape = Some(TargetShape::CharWise);

        // Select the whole first line.
        let mov = MoveType::Column(MoveDir1D::Next, true);
        edit!(ebuf, EditAction::Motion, mv!(mov, 5), ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(0, 0), Cursor::new(0, 5), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 5));

        // Duplicate selection across the following lines.
        selection_duplicate!(ebuf, MoveDir1D::Next, 4.into(), ctx!(curid, vwctx, vctx), store);

        let fsels = vec![
            (Cursor::new(1, 0), Cursor::new(1, 5), TargetShape::CharWise),
            (Cursor::new(2, 0), Cursor::new(2, 5), TargetShape::CharWise),
            (Cursor::new(3, 0), Cursor::new(3, 5), TargetShape::CharWise),
            (Cursor::new(4, 0), Cursor::new(4, 5), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 5));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Now trim, dropping two selections, and shrinking 3.
        selection_trim!(
            ebuf,
            SelectionBoundary::Line,
            TargetShapeFilter::CHAR,
            ctx!(curid, vwctx, vctx),
            store
        );

        let lsel = (Cursor::new(0, 0), Cursor::new(0, 5), TargetShape::CharWise);
        let fsels = vec![
            (Cursor::new(2, 0), Cursor::new(2, 5), TargetShape::CharWise),
            (Cursor::new(4, 0), Cursor::new(4, 5), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 5));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));
    }

    #[test]
    fn test_selection_trim_nws_drop_empty() {
        let (mut ebuf, curid, vwctx, mut vctx, mut store) =
            mkfivestr("  a  \n     \n  b  \n     \n  c  \n");

        // Start out at (0, 0).
        ebuf.set_leader(curid, Cursor::new(0, 0));

        vctx.persist.shape = Some(TargetShape::CharWise);

        // Select the whole first line.
        let mov = MoveType::Column(MoveDir1D::Next, true);
        edit!(ebuf, EditAction::Motion, mv!(mov, 4), ctx!(curid, vwctx, vctx), store);

        let lsel = (Cursor::new(0, 0), Cursor::new(0, 4), TargetShape::CharWise);
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 4));

        // Duplicate selection across the following lines.
        selection_duplicate!(ebuf, MoveDir1D::Next, 4.into(), ctx!(curid, vwctx, vctx), store);

        let fsels = vec![
            (Cursor::new(1, 0), Cursor::new(1, 4), TargetShape::CharWise),
            (Cursor::new(2, 0), Cursor::new(2, 4), TargetShape::CharWise),
            (Cursor::new(3, 0), Cursor::new(3, 4), TargetShape::CharWise),
            (Cursor::new(4, 0), Cursor::new(4, 4), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 4));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Now trim, dropping two selections, and shrinking 3.
        selection_trim!(
            ebuf,
            SelectionBoundary::NonWhitespace,
            TargetShapeFilter::CHAR,
            ctx!(curid, vwctx, vctx),
            store
        );

        let lsel = (Cursor::new(0, 2), Cursor::new(0, 2), TargetShape::CharWise);
        let fsels = vec![
            (Cursor::new(2, 2), Cursor::new(2, 2), TargetShape::CharWise),
            (Cursor::new(4, 2), Cursor::new(4, 2), TargetShape::CharWise),
        ];

        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 2));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));
    }

    #[test]
    fn test_selection_filter_keep_matches() {
        let (mut ebuf, curid, vwctx, vctx, mut store) = mkfivestr(
            "hello\nworld\nhelp\nwhisk\nhelm\naluminum\nwrithe\ncharacter\nhelium\nproduct\n",
        );

        // Start out at (0, 0).
        ebuf.set_leader(curid, Cursor::new(0, 0));

        // Duplicate selection across the following lines.
        selection_duplicate!(ebuf, MoveDir1D::Next, 9.into(), ctx!(curid, vwctx, vctx), store);

        // Extend selection to the end of the line.
        let mov = MoveType::Column(MoveDir1D::Next, false);
        selection_extend!(ebuf, mv!(mov, 10), ctx!(curid, vwctx, vctx), store);

        let fsels = vec![
            (Cursor::new(1, 0), Cursor::new(1, 4), TargetShape::CharWise), // "world"
            (Cursor::new(2, 0), Cursor::new(2, 3), TargetShape::CharWise), // "help"
            (Cursor::new(3, 0), Cursor::new(3, 4), TargetShape::CharWise), // "whisk"
            (Cursor::new(4, 0), Cursor::new(4, 3), TargetShape::CharWise), // "helm"
            (Cursor::new(5, 0), Cursor::new(5, 7), TargetShape::CharWise), // "aluminum"
            (Cursor::new(6, 0), Cursor::new(6, 5), TargetShape::CharWise), // "writhe"
            (Cursor::new(7, 0), Cursor::new(7, 8), TargetShape::CharWise), // "character"
            (Cursor::new(8, 0), Cursor::new(8, 5), TargetShape::CharWise), // "helium"
            (Cursor::new(9, 0), Cursor::new(9, 6), TargetShape::CharWise), // "product"
        ];

        let lsel = (Cursor::new(0, 0), Cursor::new(0, 4), TargetShape::CharWise); // "hello"
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 4));
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Set regex to /he/.
        store.registers.set_last_search("he");

        // Keep selections matching /he/.
        ebuf.selection_filter(MatchAction::Keep, ctx!(curid, vwctx, vctx), &mut store)
            .unwrap();

        let fsels = vec![
            (Cursor::new(2, 0), Cursor::new(2, 3), TargetShape::CharWise), // "help"
            (Cursor::new(4, 0), Cursor::new(4, 3), TargetShape::CharWise), // "helm"
            (Cursor::new(6, 0), Cursor::new(6, 5), TargetShape::CharWise), // "writhe"
            (Cursor::new(8, 0), Cursor::new(8, 5), TargetShape::CharWise), // "helium"
        ];

        let lsel = (Cursor::new(0, 0), Cursor::new(0, 4), TargetShape::CharWise); // "hello"
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 4));
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));
    }

    #[test]
    fn test_selection_filter_drop_matches() {
        let (mut ebuf, curid, vwctx, vctx, mut store) = mkfivestr(
            "hello\nworld\nhelp\nwhisk\nhelm\naluminum\nwrithe\ncharacter\nhelium\nproduct\n",
        );

        // Start out at (0, 0).
        ebuf.set_leader(curid, Cursor::new(0, 0));

        // Duplicate selection across the following lines.
        selection_duplicate!(ebuf, MoveDir1D::Next, 9.into(), ctx!(curid, vwctx, vctx), store);

        // Extend selection to the end of the line.
        let mov = MoveType::Column(MoveDir1D::Next, false);
        selection_extend!(ebuf, mv!(mov, 10), ctx!(curid, vwctx, vctx), store);

        let fsels = vec![
            (Cursor::new(1, 0), Cursor::new(1, 4), TargetShape::CharWise), // "world"
            (Cursor::new(2, 0), Cursor::new(2, 3), TargetShape::CharWise), // "help"
            (Cursor::new(3, 0), Cursor::new(3, 4), TargetShape::CharWise), // "whisk"
            (Cursor::new(4, 0), Cursor::new(4, 3), TargetShape::CharWise), // "helm"
            (Cursor::new(5, 0), Cursor::new(5, 7), TargetShape::CharWise), // "aluminum"
            (Cursor::new(6, 0), Cursor::new(6, 5), TargetShape::CharWise), // "writhe"
            (Cursor::new(7, 0), Cursor::new(7, 8), TargetShape::CharWise), // "character"
            (Cursor::new(8, 0), Cursor::new(8, 5), TargetShape::CharWise), // "helium"
            (Cursor::new(9, 0), Cursor::new(9, 6), TargetShape::CharWise), // "product"
        ];

        let lsel = (Cursor::new(0, 0), Cursor::new(0, 4), TargetShape::CharWise); // "hello"
        assert_eq!(ebuf.get_leader(curid), Cursor::new(0, 4));
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));

        // Set regex to /he/.
        store.registers.set_last_search("he");

        // Drop selections matching /he/.
        ebuf.selection_filter(MatchAction::Drop, ctx!(curid, vwctx, vctx), &mut store)
            .unwrap();

        let fsels = vec![
            (Cursor::new(1, 0), Cursor::new(1, 4), TargetShape::CharWise), // "world"
            (Cursor::new(3, 0), Cursor::new(3, 4), TargetShape::CharWise), // "whisk"
            (Cursor::new(5, 0), Cursor::new(5, 7), TargetShape::CharWise), // "aluminum"
            (Cursor::new(7, 0), Cursor::new(7, 8), TargetShape::CharWise), // "character"
        ];

        let lsel = (Cursor::new(9, 0), Cursor::new(9, 6), TargetShape::CharWise); // "product"
        assert_eq!(ebuf.get_leader(curid), Cursor::new(9, 6));
        assert_eq!(ebuf.get_leader_selection(curid), Some(lsel.clone()));
        assert_eq!(ebuf.get_follower_selections(curid), Some(fsels.clone()));
    }
}