forked from ya2s/nvim-cursorline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvim-cursorline.lua
105 lines (96 loc) · 2.36 KB
/
nvim-cursorline.lua
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
local M = {}
local w = vim.w
local a = vim.api
local wo = vim.wo
local fn = vim.fn
local hl = a.nvim_set_hl
local au = a.nvim_create_autocmd
local timer = vim.loop.new_timer()
local DEFAULT_OPTIONS = {
cursorline = {
enable = true,
timeout = 1000,
number = false,
},
cursorword = {
enable = true,
min_length = 3,
hl = { underline = true },
},
}
local function matchadd()
local column = a.nvim_win_get_cursor(0)[2]
local line = a.nvim_get_current_line()
local cursorword = fn.matchstr(line:sub(1, column + 1), [[\k*$]])
.. fn.matchstr(line:sub(column + 1), [[^\k*]]):sub(2)
if cursorword == w.cursorword then
return
end
w.cursorword = cursorword
if w.cursorword_id then
vim.call("matchdelete", w.cursorword_id)
w.cursorword_id = nil
end
if
cursorword == ""
or #cursorword > 100
or #cursorword < M.options.cursorword.min_length
or string.find(cursorword, "[\192-\255]+") ~= nil
then
return
end
local pattern = [[\<]] .. cursorword .. [[\>]]
w.cursorword_id = fn.matchadd("CursorWord", pattern, -1)
end
function M.setup(options)
M.options = vim.tbl_deep_extend("force", DEFAULT_OPTIONS, options or {})
if M.options.cursorline.enable then
wo.cursorline = true
au("WinEnter", {
callback = function()
wo.cursorline = false
end,
})
au("WinLeave", {
callback = function()
wo.cursorline = false
end,
})
au({ "CursorMoved", "CursorMovedI" }, {
callback = function()
wo.cursorline = true -- immediately show cursorline on move
if M.options.cursorline.number then
wo.cursorline = true
else
wo.cursorlineopt = "number"
end
timer:start(
M.options.cursorline.timeout,
0,
vim.schedule_wrap(function()
if M.options.cursorline.number then
wo.cursorline = false -- hide the cursorline after timeout
else
wo.cursorlineopt = "both"
end
end)
)
end,
})
end
if M.options.cursorword.enable then
au("VimEnter", {
callback = function()
hl(0, "CursorWord", M.options.cursorword.hl)
matchadd()
end,
})
au({ "CursorMoved", "CursorMovedI" }, {
callback = function()
matchadd()
end,
})
end
end
M.options = nil
return M