|
|
@ -1,6 +1,5 @@
|
|
|
|
local core = require "fzf-lua.core"
|
|
|
|
local core = require "fzf-lua.core"
|
|
|
|
local path = require "fzf-lua.path"
|
|
|
|
local path = require "fzf-lua.path"
|
|
|
|
local shell = require "fzf-lua.shell"
|
|
|
|
|
|
|
|
local utils = require "fzf-lua.utils"
|
|
|
|
local utils = require "fzf-lua.utils"
|
|
|
|
local config = require "fzf-lua.config"
|
|
|
|
local config = require "fzf-lua.config"
|
|
|
|
local actions = require "fzf-lua.actions"
|
|
|
|
local actions = require "fzf-lua.actions"
|
|
|
@ -8,21 +7,26 @@ local fn, api = vim.fn, vim.api
|
|
|
|
|
|
|
|
|
|
|
|
local M = {}
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
|
|
|
|
local function getbufnumber(line)
|
|
|
|
-- will hold current/previous buffer/tab
|
|
|
|
return tonumber(string.match(line, "%[(%d+)"))
|
|
|
|
local __STATE = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function getfilename(line)
|
|
|
|
local UPDATE_STATE = function()
|
|
|
|
-- return string.match(line, "%[.*%] (.+)")
|
|
|
|
__STATE = {
|
|
|
|
-- greedy match anything after last nbsp
|
|
|
|
curtab = vim.api.nvim_win_get_tabpage(0),
|
|
|
|
return line:match("[^" .. utils.nbsp .. "]*$")
|
|
|
|
curbuf = vim.api.nvim_get_current_buf(),
|
|
|
|
|
|
|
|
prevbuf = vim.fn.bufnr('#'),
|
|
|
|
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local filter_buffers = function(opts, unfiltered)
|
|
|
|
local filter_buffers = function(opts, unfiltered)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if type(unfiltered) == 'function' then
|
|
|
|
|
|
|
|
unfiltered = unfiltered()
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local curtab_bufnrs = {}
|
|
|
|
local curtab_bufnrs = {}
|
|
|
|
if opts.current_tab_only then
|
|
|
|
if opts.current_tab_only then
|
|
|
|
local curtab = vim.api.nvim_win_get_tabpage(0)
|
|
|
|
for _, w in ipairs(vim.api.nvim_tabpage_list_wins(__STATE.curtab)) do
|
|
|
|
for _, w in ipairs(vim.api.nvim_tabpage_list_wins(curtab)) do
|
|
|
|
|
|
|
|
local b = vim.api.nvim_win_get_buf(w)
|
|
|
|
local b = vim.api.nvim_win_get_buf(w)
|
|
|
|
curtab_bufnrs[b] = true
|
|
|
|
curtab_bufnrs[b] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
@ -37,7 +41,7 @@ local filter_buffers = function(opts, unfiltered)
|
|
|
|
if opts.show_all_buffers == false and not vim.api.nvim_buf_is_loaded(b) then
|
|
|
|
if opts.show_all_buffers == false and not vim.api.nvim_buf_is_loaded(b) then
|
|
|
|
excluded[b] = true
|
|
|
|
excluded[b] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if opts.ignore_current_buffer and b == vim.api.nvim_get_current_buf() then
|
|
|
|
if opts.ignore_current_buffer and b == __STATE.curbuf then
|
|
|
|
excluded[b] = true
|
|
|
|
excluded[b] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if opts.current_tab_only and not curtab_bufnrs[b] then
|
|
|
|
if opts.current_tab_only and not curtab_bufnrs[b] then
|
|
|
@ -55,12 +59,11 @@ local filter_buffers = function(opts, unfiltered)
|
|
|
|
return bufnrs, excluded
|
|
|
|
return bufnrs, excluded
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local make_buffer_entries = function(opts, bufnrs, tabnr, curbuf)
|
|
|
|
local populate_buffer_entries = function(opts, bufnrs, tabnr)
|
|
|
|
local header_line = false
|
|
|
|
|
|
|
|
local buffers = {}
|
|
|
|
local buffers = {}
|
|
|
|
curbuf = curbuf or vim.fn.bufnr('')
|
|
|
|
|
|
|
|
for _, bufnr in ipairs(bufnrs) do
|
|
|
|
for _, bufnr in ipairs(bufnrs) do
|
|
|
|
local flag = bufnr == curbuf and '%' or (bufnr == vim.fn.bufnr('#') and '#' or ' ')
|
|
|
|
local flag = (bufnr == __STATE.curbuf and '%') or
|
|
|
|
|
|
|
|
(bufnr == __STATE.prevbuf and '#') or ' '
|
|
|
|
|
|
|
|
|
|
|
|
local element = {
|
|
|
|
local element = {
|
|
|
|
bufnr = bufnr,
|
|
|
|
bufnr = bufnr,
|
|
|
@ -76,10 +79,6 @@ local make_buffer_entries = function(opts, bufnrs, tabnr, curbuf)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
if opts.sort_lastused and flag == "%" then
|
|
|
|
|
|
|
|
header_line = true
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
table.insert(buffers, element)
|
|
|
|
table.insert(buffers, element)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if opts.sort_lastused then
|
|
|
|
if opts.sort_lastused then
|
|
|
@ -87,11 +86,11 @@ local make_buffer_entries = function(opts, bufnrs, tabnr, curbuf)
|
|
|
|
return a.info.lastused > b.info.lastused
|
|
|
|
return a.info.lastused > b.info.lastused
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return buffers, header_line
|
|
|
|
return buffers
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function add_buffer_entry(opts, buf, items, header_line)
|
|
|
|
local function gen_buffer_entry(opts, buf, hl_curbuf)
|
|
|
|
-- local hidden = buf.info.hidden == 1 and 'h' or 'a'
|
|
|
|
-- local hidden = buf.info.hidden == 1 and 'h' or 'a'
|
|
|
|
local hidden = ''
|
|
|
|
local hidden = ''
|
|
|
|
local readonly = vim.api.nvim_buf_get_option(buf.bufnr, 'readonly') and '=' or ' '
|
|
|
|
local readonly = vim.api.nvim_buf_get_option(buf.bufnr, 'readonly') and '=' or ' '
|
|
|
@ -104,7 +103,8 @@ local function add_buffer_entry(opts, buf, items, header_line)
|
|
|
|
utils._if(buf.info.lnum>0, buf.info.lnum, ""))
|
|
|
|
utils._if(buf.info.lnum>0, buf.info.lnum, ""))
|
|
|
|
if buf.flag == '%' then
|
|
|
|
if buf.flag == '%' then
|
|
|
|
flags = utils.ansi_codes.red(buf.flag) .. flags
|
|
|
|
flags = utils.ansi_codes.red(buf.flag) .. flags
|
|
|
|
if not header_line then
|
|
|
|
if hl_curbuf then
|
|
|
|
|
|
|
|
-- no header line, highlight current buffer
|
|
|
|
leftbr = utils.ansi_codes.green('[')
|
|
|
|
leftbr = utils.ansi_codes.green('[')
|
|
|
|
rightbr = utils.ansi_codes.green(']')
|
|
|
|
rightbr = utils.ansi_codes.green(']')
|
|
|
|
bufname = utils.ansi_codes.green(bufname)
|
|
|
|
bufname = utils.ansi_codes.green(bufname)
|
|
|
@ -140,48 +140,36 @@ local function add_buffer_entry(opts, buf, items, header_line)
|
|
|
|
buficon,
|
|
|
|
buficon,
|
|
|
|
utils.nbsp,
|
|
|
|
utils.nbsp,
|
|
|
|
bufname)
|
|
|
|
bufname)
|
|
|
|
table.insert(items, item_str)
|
|
|
|
return item_str
|
|
|
|
return items
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
M.buffers = function(opts)
|
|
|
|
M.buffers = function(opts)
|
|
|
|
|
|
|
|
|
|
|
|
opts = config.normalize_opts(opts, config.globals.buffers)
|
|
|
|
opts = config.normalize_opts(opts, config.globals.buffers)
|
|
|
|
if not opts then return end
|
|
|
|
if not opts then return end
|
|
|
|
|
|
|
|
|
|
|
|
local act = shell.action(function (items, fzf_lines, _)
|
|
|
|
-- get current tab/buffer/previos buffer
|
|
|
|
-- only preview first item
|
|
|
|
-- save as a func ref for resume to reuse
|
|
|
|
local item = items[1]
|
|
|
|
opts.fn_pre_fzf = UPDATE_STATE
|
|
|
|
local buf = getbufnumber(item)
|
|
|
|
|
|
|
|
if api.nvim_buf_is_loaded(buf) then
|
|
|
|
|
|
|
|
return api.nvim_buf_get_lines(buf, 0, fzf_lines, false)
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
local name = getfilename(item)
|
|
|
|
|
|
|
|
if fn.filereadable(name) ~= 0 then
|
|
|
|
|
|
|
|
return fn.readfile(name, "", fzf_lines)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
return "UNLOADED: " .. name
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local filtered = filter_buffers(opts,
|
|
|
|
|
|
|
|
opts._list_bufs and opts._list_bufs() or vim.api.nvim_list_bufs())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not next(filtered) then return end
|
|
|
|
local contents = function(cb)
|
|
|
|
|
|
|
|
|
|
|
|
local items = {}
|
|
|
|
local filtered = filter_buffers(opts, vim.api.nvim_list_bufs)
|
|
|
|
|
|
|
|
|
|
|
|
local buffers, header_line = make_buffer_entries(opts, filtered, nil, opts.curbuf)
|
|
|
|
if next(filtered) then
|
|
|
|
for _, buf in pairs(buffers) do
|
|
|
|
local buffers = populate_buffer_entries(opts, filtered)
|
|
|
|
items = add_buffer_entry(opts, buf, items, header_line)
|
|
|
|
for _, bufinfo in pairs(buffers) do
|
|
|
|
|
|
|
|
cb(gen_buffer_entry(opts, bufinfo, not opts.sort_lastused))
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
cb(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
opts.fzf_opts['--preview'] = act
|
|
|
|
opts.fzf_opts['--header-lines'] =
|
|
|
|
if header_line and not opts.ignore_current_buffer then
|
|
|
|
(not opts.ignore_current_buffer and opts.sort_lastused) and '1'
|
|
|
|
opts.fzf_opts['--header-lines'] = '1'
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
core.fzf_wrap(opts, items, function(selected)
|
|
|
|
core.fzf_wrap(opts, contents, function(selected)
|
|
|
|
|
|
|
|
|
|
|
|
if not selected then return end
|
|
|
|
if not selected then return end
|
|
|
|
|
|
|
|
|
|
|
@ -205,9 +193,12 @@ end
|
|
|
|
M.buffer_lines = function(opts)
|
|
|
|
M.buffer_lines = function(opts)
|
|
|
|
if not opts then return end
|
|
|
|
if not opts then return end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
opts.fn_pre_fzf = UPDATE_STATE
|
|
|
|
|
|
|
|
opts.fn_pre_fzf()
|
|
|
|
|
|
|
|
|
|
|
|
local buffers = filter_buffers(opts,
|
|
|
|
local buffers = filter_buffers(opts,
|
|
|
|
opts.current_buffer_only and { vim.api.nvim_get_current_buf() } or
|
|
|
|
opts.current_buffer_only and { __STATE.curbuf } or
|
|
|
|
vim.api.nvim_list_bufs())
|
|
|
|
vim.api.nvim_list_bufs)
|
|
|
|
|
|
|
|
|
|
|
|
local items = {}
|
|
|
|
local items = {}
|
|
|
|
|
|
|
|
|
|
|
@ -273,7 +264,7 @@ M.tabs = function(opts)
|
|
|
|
opts = config.normalize_opts(opts, config.globals.tabs)
|
|
|
|
opts = config.normalize_opts(opts, config.globals.tabs)
|
|
|
|
if not opts then return end
|
|
|
|
if not opts then return end
|
|
|
|
|
|
|
|
|
|
|
|
local curtab = vim.api.nvim_win_get_tabpage(0)
|
|
|
|
opts.fn_pre_fzf = UPDATE_STATE
|
|
|
|
|
|
|
|
|
|
|
|
opts._tab_to_buf = {}
|
|
|
|
opts._tab_to_buf = {}
|
|
|
|
opts._list_bufs = function()
|
|
|
|
opts._list_bufs = function()
|
|
|
@ -289,36 +280,37 @@ M.tabs = function(opts)
|
|
|
|
return res
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local contents = function(cb)
|
|
|
|
|
|
|
|
local filtered, excluded = filter_buffers(opts, opts._list_bufs)
|
|
|
|
|
|
|
|
if not next(filtered) then return end
|
|
|
|
|
|
|
|
|
|
|
|
local filtered, excluded = filter_buffers(opts, opts._list_bufs())
|
|
|
|
-- remove the filtered-out buffers
|
|
|
|
if not next(filtered) then return end
|
|
|
|
for b, _ in pairs(excluded) do
|
|
|
|
|
|
|
|
for _, bufnrs in pairs(opts._tab_to_buf) do
|
|
|
|
-- remove the filtered-out buffers
|
|
|
|
bufnrs[b] = nil
|
|
|
|
for b, _ in pairs(excluded) do
|
|
|
|
end
|
|
|
|
for _, bufnrs in pairs(opts._tab_to_buf) do
|
|
|
|
|
|
|
|
bufnrs[b] = nil
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local items = {}
|
|
|
|
for t, bufnrs in pairs(opts._tab_to_buf) do
|
|
|
|
|
|
|
|
|
|
|
|
for t, bufnrs in pairs(opts._tab_to_buf) do
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
table.insert(items, ("%d)%s%s\t%s"):format(t, utils.nbsp,
|
|
|
|
cb(("%d)%s%s\t%s"):format(t, utils.nbsp,
|
|
|
|
utils.ansi_codes.blue("%s%s#%d"):format(opts.tab_title, utils.nbsp, t),
|
|
|
|
utils.ansi_codes.blue("%s%s#%d"):format(opts.tab_title, utils.nbsp, t),
|
|
|
|
(t==curtab) and utils.ansi_codes.blue(utils.ansi_codes.bold(opts.tab_marker)) or ''))
|
|
|
|
(t==__STATE.curtab) and
|
|
|
|
|
|
|
|
utils.ansi_codes.blue(utils.ansi_codes.bold(opts.tab_marker)) or ''))
|
|
|
|
|
|
|
|
|
|
|
|
local bufnrs_flat = {}
|
|
|
|
local bufnrs_flat = {}
|
|
|
|
for b, _ in pairs(bufnrs) do
|
|
|
|
for b, _ in pairs(bufnrs) do
|
|
|
|
table.insert(bufnrs_flat, b)
|
|
|
|
table.insert(bufnrs_flat, b)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
opts.sort_lastused = false
|
|
|
|
opts.sort_lastused = false
|
|
|
|
opts._prefix = ("%d)%s%s%s"):format(t, utils.nbsp, utils.nbsp, utils.nbsp)
|
|
|
|
opts._prefix = ("%d)%s%s%s"):format(t, utils.nbsp, utils.nbsp, utils.nbsp)
|
|
|
|
local buffers = make_buffer_entries(opts, bufnrs_flat, t)
|
|
|
|
local buffers = populate_buffer_entries(opts, bufnrs_flat, t)
|
|
|
|
for _, buf in pairs(buffers) do
|
|
|
|
for _, bufinfo in pairs(buffers) do
|
|
|
|
items = add_buffer_entry(opts, buf, items, true)
|
|
|
|
cb(gen_buffer_entry(opts, bufinfo, false))
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
cb(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
opts.fzf_opts["--no-multi"] = ''
|
|
|
|
opts.fzf_opts["--no-multi"] = ''
|
|
|
@ -326,7 +318,7 @@ M.tabs = function(opts)
|
|
|
|
opts.fzf_opts["--delimiter"] = vim.fn.shellescape('[\\)]')
|
|
|
|
opts.fzf_opts["--delimiter"] = vim.fn.shellescape('[\\)]')
|
|
|
|
opts.fzf_opts["--with-nth"] = '2'
|
|
|
|
opts.fzf_opts["--with-nth"] = '2'
|
|
|
|
|
|
|
|
|
|
|
|
core.fzf_wrap(opts, items, function(selected)
|
|
|
|
core.fzf_wrap(opts, contents, function(selected)
|
|
|
|
|
|
|
|
|
|
|
|
if not selected then return end
|
|
|
|
if not selected then return end
|
|
|
|
|
|
|
|
|
|
|
|