buffers|tabs|lines: exclude quickfix buffers (closes #337)

main
bhagwan 2 years ago
parent e1f13f882e
commit 330742a4c0

@ -275,7 +275,7 @@ function Previewer.buffer_or_file:populate_preview_buf(entry_str)
end)
self:preview_buf_post(entry)
else
if entry.bufnr then
if entry.bufnr and vim.api.nvim_buf_is_valid(entry.bufnr) then
-- buffer was unloaded, can happen when calling `lines`
-- with `set nohidden`, fix entry.path since it contains
-- filename only

@ -14,6 +14,14 @@ local UPDATE_STATE = function()
curtab = vim.api.nvim_win_get_tabpage(0),
curbuf = vim.api.nvim_get_current_buf(),
prevbuf = vim.fn.bufnr('#'),
buflist = vim.api.nvim_list_bufs(),
bufmap = (function()
local map = {}
for _, b in ipairs(vim.api.nvim_list_bufs()) do
map[b] = true
end
return map
end)()
}
end
@ -40,6 +48,9 @@ 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
end
if opts.ignore_current_buffer and b == __STATE.curbuf then
excluded[b] = true
end
@ -100,6 +111,10 @@ local function gen_buffer_entry(opts, buf, hl_curbuf)
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
if buf.flag == '%' then
flags = utils.ansi_codes.red(buf.flag) .. flags
if hl_curbuf then
@ -154,7 +169,7 @@ M.buffers = function(opts)
local contents = function(cb)
local filtered = filter_buffers(opts, vim.api.nvim_list_bufs)
local filtered = filter_buffers(opts, __STATE.buflist)
if next(filtered) then
local buffers = populate_buffer_entries(opts, filtered)
@ -196,8 +211,7 @@ M.buffer_lines = function(opts)
opts.fn_pre_fzf()
local buffers = filter_buffers(opts,
opts.current_buffer_only and { __STATE.curbuf } or
vim.api.nvim_list_bufs)
opts.current_buffer_only and { __STATE.curbuf } or __STATE.buflist)
local items = {}
@ -219,13 +233,21 @@ M.buffer_lines = function(opts)
buficon = utils.ansi_codes[hl](buficon)
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
end
for l, text in ipairs(data) do
table.insert(items, ("[%s]%s%s%s%s:%s: %s"):format(
utils.ansi_codes.yellow(tostring(bufnr)),
utils.nbsp,
buficon or '',
buficon and utils.nbsp or '',
utils.ansi_codes.magenta(#bufname>0 and bufname or "[No Name]"),
utils.ansi_codes.magenta(bufname),
utils.ansi_codes.green(tostring(l)),
text))
end
@ -271,9 +293,13 @@ M.tabs = function(opts)
for _, t in ipairs(vim.api.nvim_list_tabpages()) do
for _, w in ipairs(vim.api.nvim_tabpage_list_wins(t)) do
local b = vim.api.nvim_win_get_buf(w)
opts._tab_to_buf[t] = opts._tab_to_buf[t] or {}
opts._tab_to_buf[t][b] = t
table.insert(res, b)
-- since this function is called after fzf window
-- is created, exclude the scratch fzf buffers
if __STATE.bufmap[b] then
opts._tab_to_buf[t] = opts._tab_to_buf[t] or {}
opts._tab_to_buf[t][b] = t
table.insert(res, b)
end
end
end
return res

@ -358,7 +358,8 @@ function M.is_term_bufname(bufname)
end
function M.is_term_buffer(bufnr)
local bufname = vim.api.nvim_buf_get_name(tonumber(bufnr) or 0)
bufnr = tonumber(bufnr) or 0
local bufname = vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_get_name(bufnr)
return M.is_term_bufname(bufname)
end
@ -374,6 +375,30 @@ function M.buffer_is_dirty(bufnr, warn)
return false
end
-- returns:
-- 1 for qf list
-- 2 for loc list
function M.win_is_qf(winid, wininfo)
wininfo = wininfo or
(vim.api.nvim_win_is_valid(winid) and vim.fn.getwininfo(winid)[1])
if wininfo and wininfo.quickfix == 1 then
return wininfo.loclist == 1 and 2 or 1
end
return false
end
function M.buf_is_qf(bufnr, bufinfo)
bufinfo = bufinfo or
(vim.api.nvim_buf_is_valid(bufnr) and vim.fn.getbufinfo(bufnr)[1])
if bufinfo and bufinfo.variables and
bufinfo.variables.current_syntax == 'qf' and
not vim.tbl_isempty(bufinfo.windows) then
return M.win_is_qf(bufinfo.windows[1])
end
return false
end
function M.winid_from_tab_buf(tabnr, bufnr)
for _, w in ipairs(vim.api.nvim_tabpage_list_wins(tabnr)) do
if bufnr == vim.api.nvim_win_get_buf(w) then

Loading…
Cancel
Save