aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/highlight.lua
diff options
context:
space:
mode:
authorChristian Clason <christian.clason@uni-due.de>2020-05-31 20:56:00 +0200
committerGitHub <noreply@github.com>2020-05-31 14:56:00 -0400
commit91e41c857622b61adcf84f6a6af87a3f5a4d65f5 (patch)
tree7c4e30a9354be6cad2864447d6dd526a0befcbe9 /runtime/lua/vim/highlight.lua
parented815c61fdc3fa5a0873a3f9005fdf3c5c49e8df (diff)
downloadrneovim-91e41c857622b61adcf84f6a6af87a3f5a4d65f5.tar.gz
rneovim-91e41c857622b61adcf84f6a6af87a3f5a4d65f5.tar.bz2
rneovim-91e41c857622b61adcf84f6a6af87a3f5a4d65f5.zip
lua: add vim.highlight.range (#12401)
Diffstat (limited to 'runtime/lua/vim/highlight.lua')
-rw-r--r--runtime/lua/vim/highlight.lua29
1 files changed, 24 insertions, 5 deletions
diff --git a/runtime/lua/vim/highlight.lua b/runtime/lua/vim/highlight.lua
index 5c98c626a4..69c3c8a4dc 100644
--- a/runtime/lua/vim/highlight.lua
+++ b/runtime/lua/vim/highlight.lua
@@ -2,12 +2,34 @@ local api = vim.api
local highlight = {}
+--- Highlight range between two positions
+---
+--@param bufnr number of buffer to apply highlighting to
+--@param ns namespace to add highlight to
+--@param higroup highlight group to use for highlighting
+--@param rtype type of range (:help setreg, default charwise)
+--@param inclusive boolean indicating whether the range is end-inclusive (default false)
+function highlight.range(bufnr, ns, higroup, start, finish, rtype, inclusive)
+ rtype = rtype or 'v'
+ inclusive = inclusive or false
+
+ -- sanity check
+ if start[2] < 0 or finish[2] < start[2] then return end
+
+ local region = vim.region(bufnr, start, finish, rtype, inclusive)
+ for linenr, cols in pairs(region) do
+ api.nvim_buf_add_highlight(bufnr, ns, higroup, linenr, cols[1], cols[2])
+ end
+
+end
+
--- Highlight the yanked region
---
+---
--- use from init.vim via
--- au TextYankPost * lua require'vim.highlight'.on_yank()
--- customize highlight group and timeout via
--- au TextYankPost * lua require'vim.highlight'.on_yank("IncSearch", 500)
+---
-- @param higroup highlight group for yanked region
-- @param timeout time in ms before highlight is cleared
-- @param event event structure
@@ -27,10 +49,7 @@ function highlight.on_yank(higroup, timeout, event)
pos1 = {pos1[2] - 1, pos1[3] - 1 + pos1[4]}
pos2 = {pos2[2] - 1, pos2[3] - 1 + pos2[4]}
- local region = vim.region(bufnr, pos1, pos2, event.regtype, event.inclusive)
- for linenr, cols in pairs(region) do
- api.nvim_buf_add_highlight(bufnr, yank_ns, higroup, linenr, cols[1], cols[2])
- end
+ highlight.range(bufnr, yank_ns, higroup, pos1, pos2, event.regtype, event.inclusive)
vim.defer_fn(
function() api.nvim_buf_clear_namespace(bufnr, yank_ns, 0, -1) end,