From 20e4001eeedc80b1f2857fcaca81f7a211a09b40 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 22 Feb 2024 20:32:52 +0800 Subject: vim-patch:9.1.0120: hard to get visual region using Vim script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: hard to get visual region using Vim script Solution: Add getregion() Vim script function (Shougo Matsushita, Jakub Łuczyński) closes: vim/vim#13998 closes: vim/vim#11579 https://github.com/vim/vim/commit/3f905ab3c4f66562f4a224bf00f49d98a0b0da91 Cherry-pick changes from patch 9.1.0122, with :echom instead of :echow. Co-authored-by: Shougo Matsushita Co-authored-by: Jakub Łuczyński --- runtime/lua/vim/_meta/vimfn.lua | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua index 224cecf144..527113c016 100644 --- a/runtime/lua/vim/_meta/vimfn.lua +++ b/runtime/lua/vim/_meta/vimfn.lua @@ -3525,6 +3525,46 @@ function vim.fn.getreg(regname, list) end --- @return table function vim.fn.getreginfo(regname) end +--- Returns the list of strings from {pos1} to {pos2} as if it's +--- selected in visual mode of {type}. +--- For possible values of {pos1} and {pos2} see |line()|. +--- {type} is the selection type: +--- "v" for |charwise| mode +--- "V" for |linewise| mode +--- "" for |blockwise-visual| mode +--- You can get the last selection type by |visualmode()|. +--- If Visual mode is active, use |mode()| to get the Visual mode +--- (e.g., in a |:vmap|). +--- This function uses the line and column number from the +--- specified position. +--- It is useful to get text starting and ending in different +--- columns, such as |charwise-visual| selection. +--- +--- Note that: +--- - Order of {pos1} and {pos2} doesn't matter, it will always +--- return content from the upper left position to the lower +--- right position. +--- - If 'virtualedit' is enabled and selection is past the end of +--- line, resulting lines are filled with blanks. +--- - If the selection starts or ends in the middle of a multibyte +--- character, it is not included but its selected part is +--- substituted with spaces. +--- - If {pos1} or {pos2} equals "v" (see |line()|) and it is not in +--- |visual-mode|, an empty list is returned. +--- - If {pos1}, {pos2} or {type} is an invalid string, an empty +--- list is returned. +--- +--- Examples: > +--- :xnoremap +--- \ echom getregion('v', '.', mode()) +--- < +--- +--- @param pos1 string +--- @param pos2 string +--- @param type string +--- @return string[] +function vim.fn.getregion(pos1, pos2, type) end + --- The result is a String, which is type of register {regname}. --- The value will be one of: --- "v" for |charwise| text -- cgit