-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathevents.lua
More file actions
148 lines (122 loc) · 3.3 KB
/
Copy pathevents.lua
File metadata and controls
148 lines (122 loc) · 3.3 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
local api = vim.api
local nvmark_state = require "volt.state"
local redraw = require("volt").redraw
local cycle_clickables = require("volt.utils").cycle_clickables
local MouseMove = vim.keycode "<MouseMove>"
local LeftMouse = vim.keycode "<LeftMouse>"
local map = vim.keymap.set
local function get_item_from_col(tb, n)
for _, val in ipairs(tb) do
if val.col_start <= n and val.col_end >= n then
return val
end
end
end
--- @param foo function|string
local function run_func(foo)
---@cast foo function
if type(foo) == "function" then
foo()
---@cast foo string
elseif type(foo) == "string" then
vim.cmd(foo)
end
end
---@param buf integer
---@param row integer
---@param col integer
---@param win integer
local function handle_click(buf, by, row, col, win)
local v = nvmark_state[buf]
if not row then
local cursor_pos = api.nvim_win_get_cursor(0)
row, col = cursor_pos[1], cursor_pos[2]
end
if v.clickables[row] then
local virt = get_item_from_col(v.clickables[row], col)
if virt and (by ~= "keyb" or virt.ui_type == "slider") then
local actions = virt.actions
run_func(type(actions) == "table" and actions.click or actions)
end
if win and api.nvim_win_is_valid(win) then
vim.schedule(function()
api.nvim_win_set_cursor(win, { 1, 1 })
end)
end
end
end
local function set_cursormoved_autocmd(buf)
api.nvim_create_autocmd("CursorMoved", {
buffer = buf,
callback = function()
handle_click(buf, "keyb")
end,
})
end
--- @param buf integer
--- @param row integer
--- @param col integer
local function handle_hover(buf_state, buf, row, col)
-- clear old hovers!
if buf_state.hovered_extmarks then
vim.g.nvmark_hovered = nil
redraw(buf, buf_state.hovered_extmarks)
buf_state.hovered_extmarks = nil
end
if buf_state.hoverables[row] then
local virt = get_item_from_col(buf_state.hoverables[row], col)
if virt and virt.hover then
local hover = virt.hover
if hover.callback then
hover.callback()
end
vim.g.nvmark_hovered = hover.id or nil
redraw(buf, hover.redraw)
buf_state.hovered_extmarks = hover.redraw
end
end
end
--- @param buf integer
local function buf_mappings(buf)
set_cursormoved_autocmd(buf)
map("n", "<CR>", function()
handle_click(buf)
end, { buffer = buf })
map("n", "<Tab>", function()
cycle_clickables(buf, 1)
end, { buffer = buf })
map("n", "<S-Tab>", function()
cycle_clickables(buf, -1)
end, { buffer = buf })
end
local M = {}
M.bufs = {}
function M.add(val)
if type(val) == "table" then
for _, buf in ipairs(val) do
table.insert(M.bufs, buf)
buf_mappings(buf)
end
else
table.insert(M.bufs, val)
buf_mappings(val)
end
end
function M.enable()
vim.g.extmarks_events = true
vim.o.mousemev = true
vim.on_key(function(key)
local mousepos = vim.fn.getmousepos()
local cur_win = mousepos.winid
local cur_buf = api.nvim_win_get_buf(cur_win)
if vim.tbl_contains(M.bufs, cur_buf) then
local row, col = mousepos.line, mousepos.column - 1
if key == MouseMove then
handle_hover(nvmark_state[cur_buf], cur_buf, row, col)
elseif key == LeftMouse then
handle_click(cur_buf, "mouse", row, col, cur_win)
end
end
end)
end
return M