-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
159 lines (153 loc) · 4.81 KB
/
init.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
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
149
150
151
152
153
154
155
156
157
158
159
local hlgroups = require('plugin.winbar.hlgroups')
local bar = require('plugin.winbar.bar')
local configs = require('plugin.winbar.configs')
local utils = require('plugin.winbar.utils')
_G._winbar = setmetatable({}, {
---Get winbar string for current window
---@return string
__call = function()
local buf = vim.api.nvim_get_current_buf()
local win = vim.api.nvim_get_current_win()
return _G._winbar.bars[buf][win]()
end,
})
---Store the on_click callbacks for each winbar symbol
---Make it assessable from global only because nvim's viml-lua interface
---(v:lua) only support calling global lua functions
---@type table<string, table<string, function>>
---@see winbar_t:update
_G._winbar.callbacks = setmetatable({}, {
__index = function(self, buf)
self[buf] = setmetatable({}, {
__index = function(this, win)
this[win] = {}
return this[win]
end,
})
return self[buf]
end,
})
---@type table<integer, table<integer, winbar_t>>
_G._winbar.bars = setmetatable({}, {
__index = function(self, buf)
self[buf] = setmetatable({}, {
__index = function(this, win)
this[win] = bar.winbar_t:new({
sources = configs.eval(configs.opts.bar.sources, buf, win),
})
return this[win]
end,
})
return self[buf]
end,
})
---Setup winbar
---@param opts winbar_configs_t?
local function setup(opts)
-- Don't init twice, but still allow dynamic config change
-- after first call to `setup()`
configs.set(opts)
if vim.g.loaded_winbar ~= nil then
return
end
vim.g.loaded_winbar = true
hlgroups.init()
local groupid = vim.api.nvim_create_augroup('WinBar', {})
for _, win in ipairs(vim.api.nvim_list_wins()) do
utils.bar.attach(vim.api.nvim_win_get_buf(win), win)
end
if not vim.tbl_isempty(configs.opts.bar.attach_events) then
vim.api.nvim_create_autocmd(configs.opts.bar.attach_events, {
group = groupid,
callback = function(info)
-- Try attaching winbar to all windows containing the buffer
-- Notice that we cannot simply let `win=0` here since the current
-- buffer isn't necessarily the window containing the buffer
for _, win in ipairs(vim.fn.win_findbuf(info.buf)) do
utils.bar.attach(info.buf, win)
end
end,
desc = 'Attach winbar',
})
end
vim.api.nvim_create_autocmd('BufDelete', {
group = groupid,
callback = function(info)
utils.bar.exec('del', { buf = info.buf })
_G._winbar.bars[info.buf] = nil
_G._winbar.callbacks['buf' .. info.buf] = nil
end,
desc = 'Remove winbar from cache on buffer wipeout.',
})
if not vim.tbl_isempty(configs.opts.bar.update_events.win) then
vim.api.nvim_create_autocmd(configs.opts.bar.update_events.win, {
group = groupid,
callback = function(info)
if info.event == 'WinResized' then
for _, win in ipairs(vim.v.event.windows or {}) do
utils.bar.exec('update', { win = win })
end
else
utils.bar.exec('update', {
win = info.event == 'WinScrolled' and tonumber(info.match)
or vim.api.nvim_get_current_win(),
})
end
end,
desc = 'Update a single winbar.',
})
end
if not vim.tbl_isempty(configs.opts.bar.update_events.buf) then
vim.api.nvim_create_autocmd(configs.opts.bar.update_events.buf, {
group = groupid,
callback = function(info)
utils.bar.exec('update', { buf = info.buf })
end,
desc = 'Update all winbars associated with buf.',
})
end
if not vim.tbl_isempty(configs.opts.bar.update_events.global) then
vim.api.nvim_create_autocmd(configs.opts.bar.update_events.global, {
group = groupid,
callback = function(info)
if vim.tbl_isempty(utils.bar.get({ buf = info.buf })) then
return
end
utils.bar.exec('update')
end,
desc = 'Update all winbars.',
})
end
vim.api.nvim_create_autocmd({ 'WinClosed' }, {
group = groupid,
callback = function(info)
utils.bar.exec('del', { win = tonumber(info.match) })
end,
desc = 'Remove winbar from cache on window closed.',
})
if configs.opts.bar.hover then
vim.on_key(function(key)
if key == vim.keycode('<MouseMove>') then
utils.bar.update_hover_hl(vim.fn.getmousepos())
end
end)
vim.api.nvim_create_autocmd('FocusLost', {
group = groupid,
callback = function()
utils.bar.update_hover_hl({})
end,
desc = 'Remove hover highlight on focus lost.',
})
vim.api.nvim_create_autocmd('FocusGained', {
group = groupid,
callback = function()
utils.bar.update_hover_hl(vim.fn.getmousepos())
end,
desc = 'Update hover highlight on focus gained.',
})
end
vim.g.loaded_winbar = true
end
return {
setup = setup,
}