-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabline.lua
282 lines (242 loc) · 7.22 KB
/
tabline.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
local buffer = require "unclutter.buffer"
local config = require "unclutter.config"
local has_icons, icons -- init later (when needed)
---@class unclutter.tabline
local tabline = {}
---@type table<number, boolean>
tabline.buffers_to_keep = {}
tabline.tabpage_section = ""
tabline.tablineat = vim.fn.has "tablineat"
-- We still need to allow hidden buffers even if we don't show the tabline
-- Cause we might wanna use the telescope plugin to switch unclutter buffers
vim.o.hidden = true
--- Set vim options and format tabline
function tabline.enable()
has_icons, icons = pcall(require, "nvim-web-devicons")
vim.o.showtabline = 2
tabline.create_highlight_groups()
tabline.create_clickable_tab_fn()
vim.o.tabline = [[%!luaeval('require("unclutter.tabline").get_tabline_string()')]]
end
--- Disable tabline
function tabline.disable()
vim.o.showtabline = 0
vim.o.tabline = ""
end
function tabline.create_highlight_groups()
vim.api.nvim_set_hl(0, "UnclutterCurrent", { link = "TabLineSel", default = true })
vim.api.nvim_set_hl(0, "UnclutterVisible", { link = "TabLineFill", default = true })
vim.api.nvim_set_hl(0, "UnclutterHidden", { link = "TabLine", default = true })
vim.api.nvim_set_hl(0, "UnclutterFill", { link = "Normal", default = true })
vim.api.nvim_set_hl(0, "UnclutterTabPage", { link = "Search", default = true })
vim.api.nvim_set_hl(0, "UnclutterTablineFill", { link = "Normal", default = true })
end
--- Update tabpage section string
--- Copied from https://github.com/echasnovski/mini.tabline/blob/main/lua/mini/tabline.lua
function tabline.make_tabpage_section()
local n_tabpages = vim.fn.tabpagenr "$"
if n_tabpages == 1 then
tabline.tabpage_section = ""
return
end
local cur_tabpagenr = vim.fn.tabpagenr()
tabline.tabpage_section = (" %s/%s "):format(cur_tabpagenr, n_tabpages)
end
--- Remove a buffer from the list
---@param bufnr number
function tabline.remove_buffer(bufnr)
tabline.buffers_to_keep[bufnr] = nil
end
--- Add a buffer to the list
---@param bufnr number
function tabline.keep_buffer(bufnr)
if bufnr == nil then
return
end
tabline.buffers_to_keep[bufnr] = true
end
--- Toggle a buffer in the buffer list
---@param bufnr number
function tabline.toggle_buffer(bufnr)
if tabline.is_buffer_kept(bufnr) then
tabline.remove_buffer(bufnr)
else
tabline.keep_buffer(bufnr)
end
end
--- Create the function for switching buffers with the mouse
--- Copied from https://github.com/echasnovski/mini.tabline/blob/main/lua/mini/tabline.lua#L185
function tabline.create_clickable_tab_fn()
vim.cmd(
[[function! UnclutterSwitchBuffer(buf_id, clicks, button, mod)
execute 'buffer' a:buf_id
endfunction]],
false
)
end
--- Is buffer hidden?
---@param bufnr number
---@return boolean
function tabline.is_buffer_hidden(bufnr)
return tabline.buffers_to_keep[bufnr] == nil
end
--- Is buffer shown?
---@param bufnr number
---@return boolean
function tabline.is_buffer_kept(bufnr)
return tabline.buffers_to_keep[bufnr] == true
end
--- Get the highlight group for a buffer
---@param buf number
function tabline.get_tab_highlight(buf)
local hl
if buf == buffer.current() then
hl = "Current"
elseif vim.fn.bufwinnr(buf) > 0 then
hl = "Visible"
else
hl = "Hidden"
end
return string.format("%%#Unclutter%s#", hl)
end
--- Get the tab's clickable action
---@param buf number
function tabline.get_tab_func(buf)
---@type string
local func
--- Tab's clickable action (if supported)
if tabline.tablineat > 0 then
func = string.format("%%%d@UnclutterSwitchBuffer@", buf)
else
func = ""
end
return func
end
--- Get the tab's label
---@param buf number
---@return string|nil
function tabline.get_tab_label(buf)
local file = buffer.name(buf)
local type = buffer.type(buf)
if file == "" and type ~= "nofile" then
return string.format(" %s ", type)
end
local label = vim.fn.fnamemodify(file, ":t")
if label == nil then
return nil
end
if has_icons then
local file_extension = vim.fn.fnamemodify(file, ":e")
local icon = icons.get_icon(label, file_extension, { default = true })
if label == "" and icon then
return string.format(" %s ", icon)
end
return string.format(" %s %s ", icon, label)
end
if label == "" then
return " • "
end
return string.format(" %s ", label)
end
--- Get all buffers to be displayed in the tabline
---@param hide_current boolean?
---@return table<number, number>
function tabline.list(hide_current)
hide_current = hide_current or false
local buffers = {}
local all_buffers = buffer.all()
local current_buffer = buffer.current()
for _, buf in ipairs(all_buffers) do
local is_current_buf = buf == current_buffer
local should_keep = is_current_buf -- keep current buffer
or tabline.is_buffer_kept(buf) -- keep buffers that are marked
or buffer.is_visible(buf) -- keep visible buffers
or not buffer.is_file(buf) -- keep non-file buffers
or #buffers < config.clean_after -- keep first n buffers (config)
if should_keep then
if not is_current_buf or not hide_current then
table.insert(buffers, buf)
end
end
end
return buffers
end
--- Get all tabs
---@return table<number, table>
function tabline.get_buffer_tabs()
local tabs = {}
for _, buf in ipairs(tabline.list()) do
local tab = { buf_id = buf }
tab["hl"] = tabline.get_tab_highlight(buf)
tab["func"] = tabline.get_tab_func(buf)
tab["label"] = tabline.get_tab_label(buf)
if tab["label"] ~= nil then
table.insert(tabs, tab)
end
end
return tabs
end
--- Get the tabline string
function tabline.get_tabline_string()
tabline.make_tabpage_section()
local buffer_tabs = {}
for _, tab in ipairs(tabline.get_buffer_tabs()) do
table.insert(buffer_tabs, ("%s%s%s"):format(tab.hl, tab.func, tab.label))
end
local buffer_tabline = ("%s%%X%%#UnclutterTablineFill#"):format(table.concat(buffer_tabs, ""))
local buffers_and_tabs = ("%%#UnclutterTabPage#%s%s"):format(tabline.tabpage_section, buffer_tabline)
return buffers_and_tabs
end
--- Navigate to next buffer
function tabline.next()
local buffers = tabline.list()
if buffers == nil or #buffers < 2 then
-- print "No more buffers"
return
end
local current = buffer.current()
local next_buffer = nil
for i, buf in ipairs(buffers) do
if buf == current then
if i == #buffers then
next_buffer = buffers[1]
else
next_buffer = buffers[i + 1]
end
break
end
end
if next_buffer ~= nil then
vim.cmd("buffer " .. next_buffer)
end
end
--- Navigate to previous buffer
function tabline.prev()
local buffers = tabline.list()
if buffers == nil or #buffers < 2 then
-- print "No more buffers"
return
end
local current = buffer.current()
local previous_buffer = nil
for i, buf in ipairs(buffers) do
if buf == current then
if i == 1 then
previous_buffer = buffers[#buffers]
else
previous_buffer = buffers[i - 1]
end
break
end
end
if previous_buffer ~= nil then
vim.cmd("buffer " .. previous_buffer)
end
end
--- Mark all open buffers as listed
function tabline.keep_all_buffers()
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
tabline.keep_buffer(buf)
end
end
return tabline