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
|
local api = {}
local CTRL_V = vim.api.nvim_replace_termcodes('<C-v>', true, true, true)
local CTRL_S = vim.api.nvim_replace_termcodes('<C-s>', true, true, true)
api.get_mode = function()
local mode = vim.api.nvim_get_mode().mode:sub(1, 1)
if mode == 'i' then
return 'i' -- insert
elseif mode == 'v' or mode == 'V' or mode == CTRL_V then
return 'x' -- visual
elseif mode == 's' or mode == 'S' or mode == CTRL_S then
return 's' -- select
elseif mode == 'c' and vim.fn.getcmdtype() ~= '=' then
return 'c' -- cmdline
end
end
api.is_insert_mode = function()
return api.get_mode() == 'i'
end
api.is_cmdline_mode = function()
return api.get_mode() == 'c'
end
api.is_select_mode = function()
return api.get_mode() == 's'
end
api.is_visual_mode = function()
return api.get_mode() == 'x'
end
api.is_suitable_mode = function()
local mode = api.get_mode()
return mode == 'i' or mode == 'c'
end
api.get_current_line = function()
if api.is_cmdline_mode() then
return vim.fn.getcmdline()
end
return vim.api.nvim_get_current_line()
end
---@return { [1]: integer, [2]: integer }
api.get_cursor = function()
if api.is_cmdline_mode() then
return { vim.o.lines - (vim.api.nvim_get_option('cmdheight') or 1) + 1, vim.fn.getcmdpos() - 1 }
end
return vim.api.nvim_win_get_cursor(0)
end
api.get_screen_cursor = function()
if api.is_cmdline_mode() then
local cursor = api.get_cursor()
return { cursor[1], cursor[2] + 1 }
end
local cursor = api.get_cursor()
local pos = vim.fn.screenpos(0, cursor[1], cursor[2] + 1)
return { pos.row, pos.col - 1 }
end
api.get_cursor_before_line = function()
local cursor = api.get_cursor()
return string.sub(api.get_current_line(), 1, cursor[2])
end
return api
|