Mercurial > vim
comparison src/testdir/test_visual.vim @ 34387:e6defaa1e46a v9.1.0120
patch 9.1.0120: hard to get visual region using Vim script
Commit: https://github.com/vim/vim/commit/3f905ab3c4f66562f4a224bf00f49d98a0b0da91
Author: Shougo Matsushita <Shougo.Matsu@gmail.com>
Date: Wed Feb 21 00:02:45 2024 +0100
patch 9.1.0120: hard to get visual region using Vim script
Problem: hard to get visual region using Vim script
Solution: Add getregion() Vim script function
(Shougo Matsushita, Jakub ?uczy?ski)
closes: #13998
closes: #11579
Co-authored-by: =?UTF-8?q?Jakub=20=C5=81uczy=C5=84ski?= <doubleloop@o2.pl>
Co-authored-by: Shougo Matsushita <Shougo.Matsu@gmail.com>
Signed-off-by: Shougo Matsushita <Shougo.Matsu@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Wed, 21 Feb 2024 00:15:02 +0100 |
parents | be4389b04043 |
children | 940c794b6ba7 |
comparison
equal
deleted
inserted
replaced
34386:d9a589d77d3e | 34387:e6defaa1e46a |
---|---|
1628 call feedkeys("Gk\<C-V>j$:s/\\%V\\_.*\\%V/foobar\<CR>", 'tx') | 1628 call feedkeys("Gk\<C-V>j$:s/\\%V\\_.*\\%V/foobar\<CR>", 'tx') |
1629 call assert_equal(['one', 'foobar'], getline(1, '$')) | 1629 call assert_equal(['one', 'foobar'], getline(1, '$')) |
1630 bwipe! | 1630 bwipe! |
1631 endfunc | 1631 endfunc |
1632 | 1632 |
1633 func Test_visual_getregion() | |
1634 new | |
1635 | |
1636 call setline(1, ['one', 'two', 'three']) | |
1637 | |
1638 " Visual mode | |
1639 call cursor(1, 1) | |
1640 call feedkeys("\<ESC>vjl", 'tx') | |
1641 call assert_equal(['one', 'tw'], 'v'->getregion('.', 'v')) | |
1642 call assert_equal(['one', 'tw'], '.'->getregion('v', 'v')) | |
1643 call assert_equal(['o'], 'v'->getregion('v', 'v')) | |
1644 call assert_equal(['w'], '.'->getregion('.', 'v')) | |
1645 call assert_equal(['one', 'two'], '.'->getregion('v', 'V')) | |
1646 call assert_equal(['on', 'tw'], '.'->getregion('v', "\<C-v>")) | |
1647 | |
1648 " Line visual mode | |
1649 call cursor(1, 1) | |
1650 call feedkeys("\<ESC>Vl", 'tx') | |
1651 call assert_equal(['one'], getregion('v', '.', 'V')) | |
1652 call assert_equal(['one'], getregion('.', 'v', 'V')) | |
1653 call assert_equal(['one'], getregion('v', 'v', 'V')) | |
1654 call assert_equal(['one'], getregion('.', '.', 'V')) | |
1655 call assert_equal(['on'], '.'->getregion('v', 'v')) | |
1656 call assert_equal(['on'], '.'->getregion('v', "\<C-v>")) | |
1657 | |
1658 " Block visual mode | |
1659 call cursor(1, 1) | |
1660 call feedkeys("\<ESC>\<C-v>ll", 'tx') | |
1661 call assert_equal(['one'], getregion('v', '.', "\<C-v>")) | |
1662 call assert_equal(['one'], getregion('.', 'v', "\<C-v>")) | |
1663 call assert_equal(['o'], getregion('v', 'v', "\<C-v>")) | |
1664 call assert_equal(['e'], getregion('.', '.', "\<C-v>")) | |
1665 call assert_equal(['one'], '.'->getregion('v', 'V')) | |
1666 call assert_equal(['one'], '.'->getregion('v', 'v')) | |
1667 | |
1668 " Using Marks | |
1669 call setpos("'a", [0, 2, 3, 0]) | |
1670 call cursor(1, 1) | |
1671 call assert_equal(['one', 'two'], "'a"->getregion('.', 'v')) | |
1672 call assert_equal(['one', 'two'], "."->getregion("'a", 'v')) | |
1673 call assert_equal(['one', 'two'], "."->getregion("'a", 'V')) | |
1674 call assert_equal(['two'], "'a"->getregion("'a", 'V')) | |
1675 call assert_equal(['one', 'two'], "."->getregion("'a", "\<c-v>")) | |
1676 | |
1677 " Multiline with line visual mode | |
1678 call cursor(1, 1) | |
1679 call feedkeys("\<ESC>Vjj", 'tx') | |
1680 call assert_equal(['one', 'two', 'three'], getregion('v', '.', 'V')) | |
1681 | |
1682 " Multiline with block visual mode | |
1683 call cursor(1, 1) | |
1684 call feedkeys("\<ESC>\<C-v>jj", 'tx') | |
1685 call assert_equal(['o', 't', 't'], getregion('v', '.', "\<C-v>")) | |
1686 | |
1687 call cursor(1, 1) | |
1688 call feedkeys("\<ESC>\<C-v>jj$", 'tx') | |
1689 call assert_equal(['one', 'two', 'three'], getregion('v', '.', "\<C-v>")) | |
1690 | |
1691 " 'virtualedit' | |
1692 set virtualedit=all | |
1693 call cursor(1, 1) | |
1694 call feedkeys("\<ESC>\<C-v>10ljj$", 'tx') | |
1695 call assert_equal(['one ', 'two ', 'three '], | |
1696 \ getregion('v', '.', "\<C-v>")) | |
1697 set virtualedit& | |
1698 | |
1699 " Invalid position | |
1700 call cursor(1, 1) | |
1701 call feedkeys("\<ESC>vjj$", 'tx') | |
1702 call assert_fails("call getregion(1, 2, 'v')", 'E1174:') | |
1703 call assert_fails("call getregion('.', {}, 'v')", 'E1174:') | |
1704 call assert_equal([], getregion('', '.', 'v')) | |
1705 call assert_equal([], getregion('.', '.', '')) | |
1706 call feedkeys("\<ESC>", 'tx') | |
1707 call assert_equal([], getregion('v', '.', 'v')) | |
1708 | |
1709 " using an unset mark | |
1710 call assert_equal([], "'z"->getregion(".", 'V')) | |
1711 " using the wrong type | |
1712 call assert_fails(':echo "."->getregion([],"V")', 'E1174:') | |
1713 call assert_fails(':echo "."->getregion("$", {})', 'E1174:') | |
1714 call assert_fails(':echo [0, 1, 1, 0]->getregion("$", "v")', 'E1174:') | |
1715 | |
1716 | |
1717 bwipe! | |
1718 " Selection in starts or ends in the middle of a multibyte character | |
1719 new | |
1720 call setline(1, [ | |
1721 \ "abcdefghijk\u00ab", | |
1722 \ "\U0001f1e6\u00ab\U0001f1e7\u00ab\U0001f1e8\u00ab\U0001f1e9", | |
1723 \ "1234567890" | |
1724 \ ]) | |
1725 call cursor(1, 3) | |
1726 call feedkeys("\<Esc>\<C-v>ljj", 'xt') | |
1727 call assert_equal(['cd', "\u00ab ", '34'], | |
1728 \ getregion('v', '.', "\<C-v>")) | |
1729 call cursor(1, 4) | |
1730 call feedkeys("\<Esc>\<C-v>ljj", 'xt') | |
1731 call assert_equal(['de', "\U0001f1e7", '45'], | |
1732 \ getregion('v', '.', "\<C-v>")) | |
1733 call cursor(1, 5) | |
1734 call feedkeys("\<Esc>\<C-v>jj", 'xt') | |
1735 call assert_equal(['e', ' ', '5'], getregion('v', '.', "\<C-v>")) | |
1736 call cursor(1, 1) | |
1737 call feedkeys("\<Esc>vj", 'xt') | |
1738 call assert_equal(['abcdefghijk«', "\U0001f1e6"], getregion('v', '.', "v")) | |
1739 " marks on multibyte chars | |
1740 set selection=exclusive | |
1741 call setpos("'a", [0, 1, 11, 0]) | |
1742 call setpos("'b", [0, 2, 16, 0]) | |
1743 call setpos("'c", [0, 2, 0, 0]) | |
1744 call cursor(1, 1) | |
1745 call assert_equal(['ghijk', '🇨«🇩'], getregion("'a", "'b", "\<c-v>")) | |
1746 call assert_equal(['k«', '🇦«🇧«🇨'], getregion("'a", "'b", "v")) | |
1747 call assert_equal(['k«'], getregion("'a", "'c", "v")) | |
1748 | |
1749 bwipe! | |
1750 | |
1751 " Exclusive selection | |
1752 new | |
1753 set selection=exclusive | |
1754 call setline(1, ["a\tc", "x\tz", '', '']) | |
1755 call cursor(1, 1) | |
1756 call feedkeys("\<Esc>v2l", 'xt') | |
1757 call assert_equal(["a\t"], getregion('v', '.', 'v')) | |
1758 call cursor(1, 1) | |
1759 call feedkeys("\<Esc>v$G", 'xt') | |
1760 call assert_equal(["a\tc", "x\tz", ''], getregion('v', '.', 'v')) | |
1761 call cursor(1, 1) | |
1762 call feedkeys("\<Esc>v$j", 'xt') | |
1763 call assert_equal(["a\tc", "x\tz"], getregion('v', '.', 'v')) | |
1764 call cursor(1, 1) | |
1765 call feedkeys("\<Esc>\<C-v>$j", 'xt') | |
1766 call assert_equal(["a\tc", "x\tz"], getregion('v', '.', "\<C-v>")) | |
1767 call cursor(1, 1) | |
1768 call feedkeys("\<Esc>\<C-v>$G", 'xt') | |
1769 call assert_equal(["a", "x", '', ''], getregion('v', '.', "\<C-v>")) | |
1770 call cursor(1, 1) | |
1771 call feedkeys("\<Esc>wv2j", 'xt') | |
1772 call assert_equal(["c", "x\tz"], getregion('v', '.', 'v')) | |
1773 | |
1774 " virtualedit | |
1775 set virtualedit=all | |
1776 call cursor(1, 1) | |
1777 call feedkeys("\<Esc>2lv2lj", 'xt') | |
1778 call assert_equal([' c', 'x '], getregion('v', '.', 'v')) | |
1779 call cursor(1, 1) | |
1780 call feedkeys("\<Esc>2l\<C-v>2l2j", 'xt') | |
1781 call assert_equal([' ', ' ', ' '], getregion('v', '.', "\<C-v>")) | |
1782 set virtualedit& | |
1783 set selection& | |
1784 | |
1785 bwipe! | |
1786 endfunc | |
1787 | |
1633 " vim: shiftwidth=2 sts=2 expandtab | 1788 " vim: shiftwidth=2 sts=2 expandtab |