1 +local auto
= require 'std.auto' 16 +local SETTLE_MS
= 1000 17 +local MAX_HEIGHT
= 100 19 +local PATH_START_OFFSET
= 3 21 +local function get_icon(buf_path)
22 + local ok, icon_data
= pcall(
require,
'mini.icons')
23 + if not ok
then return ' ',
'' end 24 + local icon, hl
= icon_data.get(
'file', buf_path)
25 + return icon
or ' ', hl
or '' 28 +local function truncate(str, max)
30 + return str:sub(
1, max
- 1)
.. '…' 35 +local function shorten_path(full_path, max_len)
36 + local basename
= vim.fs.basename(full_path)
37 + local abs_cwd
= vim.fs.normalize(vim.fn.getcwd())
38 + local abs_path
= vim.fs.normalize(full_path)
40 + if abs_path:find(abs_cwd,
1,
true)
~= 1 then 41 + return #basename
<= max_len
and basename
or truncate(basename, max_len)
44 + local rel_path
= abs_path:sub(
#abs_cwd
+ 1)
45 + if rel_path:sub(
1,
1)
== '/' then 46 + rel_path
= rel_path:sub(
2)
48 + rel_path
= './' .. rel_path
50 + if #rel_path
<= max_len
then 54 + if #basename
>= max_len
then 55 + return truncate(basename, max_len)
58 + local available
= max_len
- #basename
- 1 59 + if available
<= 0 then 63 + if #rel_path
- #basename
> available
then 64 + local dirname_len
= available
- 1 65 + if dirname_len
> 0 then 66 + local dirname_start
= #rel_path
- #basename
- dirname_len
67 + rel_path
= './…/' .. rel_path:sub(dirname_start)
75 +local function render_item(b, idx, selecting_idx, width)
76 + local prefix
= idx
== selecting_idx
and '› ' or ' ' 77 + local full_path
= b.name
78 + local icon, icon_hl
= get_icon(full_path)
79 + local icon_len
= #icon
80 + local rel_path
= shorten_path(full_path, width
- #prefix
- icon_len
- 1)
81 + local line
= prefix
.. icon
.. ' ' .. rel_path
83 + local basename
= vim.fs.basename(full_path)
84 + local basename_in_line
= icon
.. ' ' .. rel_path
85 + local name_start_byte
= basename_in_line:find(basename,
1,
true)
86 + local name_start
= #prefix
+ (name_start_byte
or 1)
87 + local name_end_exclusive
= name_start
+ #basename
90 + icon_hl = icon_hl
~= '' and icon_hl
or nil,
91 + name_start = name_start,
92 + name_end_exclusive = name_end_exclusive,
97 +local function render_list(buffers, selecting_idx, width)
99 + local highlights
= {}
100 + for i, b
in ipairs(buffers)
do 101 + local line, info
= render_item(b, i, selecting_idx, width)
103 + highlights[i]
= info
105 + return lines, highlights
108 +local function get_listed_buffers()
109 + local cur
= vim.api.nvim_get_current_buf()
111 + for _, bufnr
in ipairs(history)
do 112 + if vim.api.nvim_buf_is_valid(bufnr)
and vim.bo[bufnr].buflisted
then 113 + local name
= vim.api.nvim_buf_get_name(bufnr)
115 + table.
insert(result, {
bufnr = bufnr,
name = name })
119 + for _, info
in ipairs(vim.fn.getbufinfo {
buflisted =
1 })
do 120 + local bufnr
= info.bufnr
121 + if vim.api.nvim_buf_is_valid(bufnr)
and vim.bo[bufnr].buflisted
and info.name
~= '' then 122 + local found
= false 123 + for _, b
in ipairs(result)
do 124 + if b.bufnr
== bufnr
then found
= true break end 127 + table.
insert(result, {
bufnr = bufnr,
name = info.name })
134 +local function close_popup()
135 + if state.popup_win
and type(state.popup_win)
== 'number' then 136 + local ok, valid
= pcall(vim.api.nvim_win_is_valid, state.popup_win)
137 + if ok
and valid
then 138 + pcall(vim.api.nvim_win_close, state.popup_win,
true)
141 + state.popup_win
= nil 142 + state.popup_buf
= nil 143 + state.selecting_idx
= 1 148 +local function cancel()
149 + if state.timer
then 151 + state.timer:close()
155 + if state.source_buf
then 156 + local ok, valid
= pcall(vim.api.nvim_buf_is_valid, state.source_buf)
157 + if ok
and valid
then 158 + pcall(vim.api.nvim_set_current_buf, state.source_buf)
163 +local function commit_switch()
164 + local target
= state.buffers[state.selecting_idx]
167 + vim.api.nvim_set_current_buf(target.bufnr)
168 + for i
= #history,
1,
-1 do 169 + if history[i]
== target.bufnr
then table.
remove(history, i)
end 171 + table.
insert(history, target.bufnr)
172 + if #history
> 50 then table.
remove(history,
1)
end 178 +local function apply_highlights(bufnr, hl_ns, highlights)
179 + for _, h
in ipairs(highlights)
do 182 + vim.api.nvim_buf_add_highlight(bufnr, hl_ns, h.icon_hl, h.line_idx,
0,
2)
184 + vim.api.nvim_buf_add_highlight(bufnr, hl_ns,
'CycleBufferFileName', h.line_idx, h.name_start
- 1, h.name_end_exclusive
- 1)
188 +local function clear_highlights()
189 + if state.hl_ns
and state.popup_buf
and vim.api.nvim_buf_is_valid(state.popup_buf)
then 190 + vim.api.nvim_buf_clear_highlight(state.popup_buf, state.hl_ns,
0,
-1)
194 +local function open_popup()
197 + state.buffers
= get_listed_buffers()
198 + if #state.buffers
== 0 then return end 200 + if state.selecting_idx
> #state.buffers
then 201 + state.selecting_idx
= 1 204 + local width
= math.
min(
55, vim.o.columns
- 4)
205 + local height
= math.
min(
#state.buffers, MAX_HEIGHT)
206 + local row
= math.
floor((vim.o.lines
- height)
/ 2)
- 1 207 + local col
= math.
floor((vim.o.columns
- width)
/ 2)
209 + state.popup_buf
= vim.api.nvim_create_buf(
false,
true)
210 + state.hl_ns
= vim.api.nvim_create_namespace(
'cycle_buffer')
212 + local lines, highlights
= render_list(state.buffers, state.selecting_idx, width)
213 + vim.api.nvim_buf_set_lines(state.popup_buf,
0,
-1,
false, lines)
214 + vim.bo[state.popup_buf].modifiable
= false 215 + vim.bo[state.popup_buf].filetype
= 'CycleBuffer' 218 + relative =
'editor',
224 + border = vim.g.border_style,
227 + state.popup_win
= vim.api.nvim_open_win(state.popup_buf,
true, opts)
228 + vim.wo[state.popup_win].cursorline
= true 230 + apply_highlights(state.popup_buf, state.hl_ns, highlights)
232 + local mod
= require 'bugabinga.cycle' 233 + vim.keymap.set(
'n',
'<C-Tab>',
function() mod.cycle(
1)
end, {
buffer = state.popup_buf,
silent =
true })
234 + vim.keymap.set(
'n',
'<C-S-Tab>',
function() mod.cycle(
-1)
end, {
buffer = state.popup_buf,
silent =
true })
235 + vim.keymap.set(
'n',
'<Tab>',
function() mod.cycle(
1)
end, {
buffer = state.popup_buf,
silent =
true })
236 + vim.keymap.set(
'n',
'<S-Tab>',
function() mod.cycle(
-1)
end, {
buffer = state.popup_buf,
silent =
true })
237 + vim.keymap.set(
'n',
'<Esc>', cancel, {
buffer = state.popup_buf,
silent =
true })
238 + vim.keymap.set(
'n',
'q', cancel, {
buffer = state.popup_buf,
silent =
true })
241 +local function cycle(direction)
242 + if state.timer
then 244 + state.timer:close()
248 + if not state.popup_win
then 249 + state.source_win
= vim.api.nvim_get_current_win()
250 + state.source_buf
= vim.api.nvim_get_current_buf()
251 + state.selecting_idx
= 1 253 + if not state.popup_win
then 254 + vim.notify(
'buffer cycle: No other buffers', vim.log.levels.INFO)
258 + state.selecting_idx
= state.selecting_idx
+ direction
259 + if state.selecting_idx
< 1 then state.selecting_idx
= #state.buffers
260 + elseif state.selecting_idx
> #state.buffers
then state.selecting_idx
= 1 end 261 + if state.popup_buf
and vim.api.nvim_buf_is_valid(state.popup_buf)
then 262 + local width
= math.
min(
55, vim.o.columns
- 4)
263 + local lines, highlights
= render_list(state.buffers, state.selecting_idx, width)
265 + local was_modifiable
= vim.bo[state.popup_buf].modifiable
266 + vim.bo[state.popup_buf].modifiable
= true 267 + vim.api.nvim_buf_set_lines(state.popup_buf,
0,
-1,
false, lines)
268 + vim.bo[state.popup_buf].modifiable
= was_modifiable
269 + apply_highlights(state.popup_buf, state.hl_ns, highlights)
270 + vim.api.nvim_win_set_cursor(state.popup_win, { state.selecting_idx,
0 })
274 + local timer
= vim.uv.new_timer()
275 + timer:start(SETTLE_MS,
0, vim.schedule_wrap(
function()
281 + state.timer
= timer
285 + events =
'BufEnter',
286 + command =
function()
287 + if state.popup_win
then return end 288 + local bufnr
= vim.api.nvim_get_current_buf()
289 + if not vim.bo[bufnr].buflisted
then return end 290 + local name
= vim.api.nvim_buf_get_name(bufnr)
291 + if name
== '' then return end 292 + for i
= #history,
1,
-1 do 293 + if history[i]
== bufnr
then table.
remove(history, i)
end 295 + table.
insert(history, bufnr)
296 + if #history
> 50 then table.
remove(history,
1)
end 300 +local map
= require 'std.map' 303 + description =
'Cycle to next MRU buffer.',
304 + category =
'cycle',
306 + command =
function() cycle(
1)
end,
310 + description =
'Cycle to previous MRU buffer.',
311 + category =
'cycle',
312 + keys =
'<C-S-Tab>',
313 + command =
function() cycle(
-1)
end,
316 +vim.api.nvim_create_autocmd(
'BufWipeout', {
317 + callback =
function(args)
318 + for i
= #history,
1,
-1 do 319 + if history[i]
== args.buf
then table.
remove(history, i)
end 324 +vim.api.nvim_create_autocmd(
'VimLeave', {
326 + callback =
function()
335 + get_icon = get_icon,
336 + shorten_path = shorten_path,