'resume' rework: better way of storing last query (closes #271)

main
bhagwan 2 years ago
parent f0927af072
commit 3717661c83

@ -217,8 +217,6 @@ require'fzf-lua'.setup {
-- can also be sent individually:
-- `<any_function>.({ gl ... })`
global_resume_query = true, -- include typed query in `resume`?
-- may cause lag when fast typing
-- disable if you're having issues
winopts = {
-- split = "belowright new",-- open in a split instead?
-- "belowright new" : split below

@ -251,8 +251,6 @@ Consult the list below for available settings:
-- can also be sent individually:
-- `<any_function>.({ gl ... })`
global_resume_query = true, -- include typed query in `resume`?
-- may cause lag when fast typing
-- disable if you're having issues
winopts = {
-- split = "belowright new",-- open in a split instead?
-- "belowright new" : split below

@ -67,22 +67,32 @@ M.fzf = function(opts, contents)
-- providers
config.__resume_data.last_query = nil
end
-- signals to the win object resume is enabled
-- so we can setup the keypress event monitoring
-- TODO: how to get keypress event in neovim?
-- InsertCharPre would be perfect here but:
-- https://github.com/neovim/neovim/issues/5018
opts.fn_save_query = function(query)
config.__resume_data.last_query = query and #query>0 and query
end
-- this is causing lag when typing too fast (#271)
-- also not possible with skim (no 'change' event)
if opts.global_resume_query and not opts._is_skim then
local raw_act = shell.raw_action(function(args)
opts.fn_save_query(args[1])
end, "{q}")
opts._fzf_cli_args = ('--bind=change:execute-silent:%s'):
format(vim.fn.shellescape(raw_act))
if opts.global_resume_query then
-- We use this option to print the query on line 1
-- later to be removed from the result by M.fzf()
-- this providers a solution for saving the query
-- when the user pressed a valid bind but not when
-- aborting with <C-c> or <Esc>, see next comment
opts.fzf_opts['--print-query'] = ''
-- Signals to the win object resume is enabled
-- so we can setup the keypress event monitoring
-- since we already have the query on valid
-- exit codes we only need to monitor <C-c>, <Esc>
opts.fn_save_query = function(query)
config.__resume_data.last_query = query and #query>0 and query or nil
end
-- 'au InsertCharPre' would be the best option here
-- but it does not work for terminals:
-- https://github.com/neovim/neovim/issues/5018
-- this is causing lag when typing too fast (#271)
-- also not possible with skim (no 'change' event)
--[[ if not opts._is_skim then
local raw_act = shell.raw_action(function(args)
opts.fn_save_query(args[1])
end, "{q}")
opts._fzf_cli_args = ('--bind=change:execute-silent:%s'):
format(vim.fn.shellescape(raw_act))
end ]]
end
end
-- setup the fzf window and preview layout
@ -127,6 +137,17 @@ M.fzf = function(opts, contents)
fzf_win:create()
local selected, exit_code = fzf.raw_fzf(contents, M.build_fzf_cli(opts),
{ fzf_binary = opts.fzf_bin, fzf_cwd = opts.cwd })
-- This was added by 'resume':
-- when '--print-query' is specified
-- we are guaranteed to have the query
-- in the first line, save&remove it
if selected and #selected>0 and
opts.fzf_opts['--print-query'] ~= nil then
if opts.fn_save_query then
opts.fn_save_query(selected[1])
end
table.remove(selected, 1)
end
if opts.fn_post_fzf then
opts.fn_post_fzf(opts, selected)
end

@ -15,6 +15,21 @@ setmetatable(FzfWin, {
end,
})
function FzfWin.save_query(key)
local self = _self
if not self then return end
local lines = vim.api.nvim_buf_get_lines(self.fzf_bufnr, 0, 1, false)
if not lines or vim.tbl_isempty(lines) then return end
local query = lines[1]:gsub("^"..self.prompt, ""):match("[^<]+")
-- trim whitespaces at the end
query = query and query:gsub("%s*$", "")
if self.fn_save_query then
self.fn_save_query(query)
end
-- feed the original key back to the term
utils.feed_keys_termcodes(utils.fzf_bind_to_neovim(key))
end
function FzfWin:setup_keybinds()
if not self:validate() then return end
if not self.keymap or not self.keymap.builtin then return end
@ -51,6 +66,19 @@ function FzfWin:setup_keybinds()
funcref_str(keymap), {nowait = true, noremap = true})
end
end
-- since '--print-query' only covers successful exists
-- we have to monitor <C-c> and <Esc> to save the query
if self.fn_save_query then
-- workaround, how to enter <Esc>/<C-c> in map as literals?
-- for now use the fzf notation and convert later, otherwise
-- the prevalance of '<Esc>' in the mapping will interrupt it
for _, key in ipairs({ "ctrl-c", "esc" }) do
api.nvim_buf_set_keymap(self.fzf_bufnr, 't',
utils.fzf_bind_to_neovim(key),
("<Cmd>lua require('fzf-lua.win').save_query('%s')<CR>"):format(key),
{nowait = true, noremap = true})
end
end
end
local generate_layout = function(winopts)
@ -569,34 +597,11 @@ function FzfWin:redraw()
end
end
function FzfWin.save_query()
local self = _self
if not self then return end
local lines = vim.api.nvim_buf_get_lines(self.fzf_bufnr, 0, 1, false)
if not lines or vim.tbl_isempty(lines) then return end
local query = lines[1]:gsub("^"..self.prompt, ""):match("[^<]+")
-- trim whitespaces at the end
query = query and query:gsub("%s*$", "")
if self.fn_save_query then
self.fn_save_query(query)
end
-- print("query:", query)
end
function FzfWin:set_winleave_autocmd()
vim.cmd("augroup FzfLua")
vim.cmd("au!")
vim.cmd(('au WinLeave <buffer> %s'):format(
[[lua require('fzf-lua.win').win_leave()]]))
--[[ if self.fn_save_query then
-- if resume is enabled fire an event for every
-- key pressed so we can retreieve the typed query
-- TODO: InsertCharPre does not work for terminals
-- find another way to trigger save_query()
-- https://github.com/neovim/neovim/issues/5018
vim.cmd(('au InsertCharPre <buffer> %s'):format(
"lua require('fzf-lua.win').save_query()"))
end ]]
vim.cmd("augroup END")
end

Loading…
Cancel
Save