buffers|tabs|lines: normalize unnamed buffers (more below):

- prioritize 'show_quickfix' over 'show_unlisted'
2 new actions:
  - buf_switch_or_edit
  - file_switch_or_edit
main
bhagwan 2 years ago
parent 330742a4c0
commit 34281e4d88

@ -91,7 +91,8 @@ M.vimcmd_file = function(vimcmd, selected, opts)
if not path.starts_with_separator(fullpath) then
fullpath = path.join({opts.cwd or vim.loop.cwd(), fullpath})
end
if vimcmd == 'e' and curbuf ~= fullpath
if vimcmd == 'e'
and curbuf ~= fullpath
and not vim.o.hidden and
utils.buffer_is_dirty(nil, true) then
-- warn the user when trying to switch from a dirty buffer
@ -194,22 +195,31 @@ M.file_switch = function(selected, opts)
return true
end
M.file_switch_or_edit = function(...)
M.file_switch(...)
M.file_edit(...)
end
-- buffer actions
M.vimcmd_buf = function(vimcmd, selected, _)
local curbuf = vim.api.nvim_get_current_buf()
for i = 1, #selected do
local bufnr = string.match(selected[i], "%[(%d+)")
if bufnr then
if vimcmd == 'b'
and curbuf ~= tonumber(bufnr)
and not vim.o.hidden and
utils.buffer_is_dirty(nil, true) then
-- warn the user when trying to switch from a dirty buffer
-- when `:set nohidden`
return
end
local cmd = vimcmd .. " " .. bufnr
local ok, res = pcall(vim.cmd, cmd)
if not ok then
utils.warn(("':%s' failed: %s"):format(cmd, res))
if vimcmd ~= "b" or curbuf ~= tonumber(bufnr) then
local cmd = vimcmd .. " " .. bufnr
local ok, res = pcall(vim.cmd, cmd)
if not ok then
utils.warn(("':%s' failed: %s"):format(cmd, res))
end
end
end
end
@ -258,6 +268,11 @@ M.buf_switch = function(selected, _)
end
end
M.buf_switch_or_edit = function(...)
M.buf_switch(...)
M.buf_edit(...)
end
M.colorscheme = function(selected)
local colorscheme = selected[1]
vim.cmd("colorscheme " .. colorscheme)

@ -48,8 +48,13 @@ local filter_buffers = function(opts, unfiltered)
if opts.show_all_buffers == false and not vim.api.nvim_buf_is_loaded(b) then
excluded[b] = true
end
if not opts.show_quickfix and utils.buf_is_qf(b) then
excluded[b] = true
if utils.buf_is_qf(b) then
if opts.show_quickfix then
-- show_quickfix trumps show_unlisted
excluded[b] = nil
else
excluded[b] = true
end
end
if opts.ignore_current_buffer and b == __STATE.curbuf then
excluded[b] = true
@ -109,12 +114,10 @@ local function gen_buffer_entry(opts, buf, hl_curbuf)
local leftbr = utils.ansi_codes.clear('[')
local rightbr = utils.ansi_codes.clear(']')
local bufname = string.format("%s:%s",
utils._if(#buf.info.name>0, path.relative(buf.info.name, vim.loop.cwd()), "[No Name]"),
utils._if(buf.info.lnum>0, buf.info.lnum, ""))
local is_qf = utils.buf_is_qf(buf.bufnr, buf.info)
if is_qf then
bufname = string.format("%s", is_qf==1 and "[Quickfix List]" or "[Location List]")
end
#buf.info.name>0 and
path.relative(buf.info.name, vim.loop.cwd()) or
utils.nvim_buf_get_name(buf.bufnr, buf.info),
buf.info.lnum>0 and buf.info.lnum or "")
if buf.flag == '%' then
flags = utils.ansi_codes.red(buf.flag) .. flags
if hl_curbuf then
@ -234,12 +237,7 @@ M.buffer_lines = function(opts)
end
end
if not bufname or #bufname==0 then
local is_qf = utils.buf_is_qf(bufnr)
if is_qf then
bufname = is_qf==1 and "[Quickfix List]" or "[Location List]"
else
bufname = "[No Name]"
end
bufname = utils.nvim_buf_get_name(bufnr)
end
for l, text in ipairs(data) do
table.insert(items, ("[%s]%s%s%s%s:%s: %s"):format(

@ -408,6 +408,24 @@ function M.winid_from_tab_buf(tabnr, bufnr)
return nil
end
function M.nvim_buf_get_name(bufnr, bufinfo)
if not vim.api.nvim_buf_is_valid(bufnr) then return end
if bufinfo and bufinfo.name and #bufinfo.name>0 then
return bufinfo.name
end
local bufname = vim.api.nvim_buf_get_name(bufnr)
if not bufname or #bufname==0 then
local is_qf = M.buf_is_qf(bufnr, bufinfo)
if is_qf then
bufname = is_qf==1 and "[Quickfix List]" or "[Location List]"
else
bufname = "[No Name]"
end
end
assert(#bufname>0)
return bufname
end
function M.zz()
-- skip for terminal buffers
if M.is_term_buffer() then return end

Loading…
Cancel
Save