diff --git a/README.md b/README.md index 2a4860e..dbfac2a 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,7 @@ vim.api.nvim_set_keymap('n', '', ### Misc | Command | List | | ---------------- | ------------------------------------------ | +| `resume` | resume last command/query | | `builtin` | fzf-lua builtin commands | | `help_tags` | help tags | | `man_pages` | man pages | @@ -212,6 +213,7 @@ Consult the list below for available settings: ```lua local actions = require "fzf-lua.actions" require'fzf-lua'.setup { + global_resume = true, -- enable `resume` provider? winopts = { -- split = "belowright new",-- open in a split instead? -- "belowright new" : split below diff --git a/doc/fzf-lua.txt b/doc/fzf-lua.txt index 0c49021..0881bbb 100644 --- a/doc/fzf-lua.txt +++ b/doc/fzf-lua.txt @@ -211,6 +211,7 @@ MISC *fzf-lua-misc* | Command | List | | ---------------- | ------------------------------------------ | +| `resume` | resume last command/query | | `builtin` | fzf-lua builtin commands | | `help_tags` | help tags | | `man_pages` | man pages | @@ -246,6 +247,7 @@ Consult the list below for available settings: > local actions = require "fzf-lua.actions" require'fzf-lua'.setup { + global_resume = true, -- enable `resume` provider? winopts = { -- split = "belowright new",-- open in a split instead? -- "belowright new" : split below diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index 550b269..77f59cd 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -20,7 +20,12 @@ function M._default_previewer_fn() return M.globals.default_previewer or M.globals.winopts.preview.default end +-- set this so that make_entry won't +-- get nil err when setting remotely +M.__resume_data = {} + M.globals = { + global_resume = true, winopts = { height = 0.85, width = 0.80, diff --git a/lua/fzf-lua/core.lua b/lua/fzf-lua/core.lua index d958bec..c0e4afa 100644 --- a/lua/fzf-lua/core.lua +++ b/lua/fzf-lua/core.lua @@ -1,4 +1,3 @@ --- local fzf = require "fzf" local fzf = require "fzf-lua.fzf" local path = require "fzf-lua.path" local utils = require "fzf-lua.utils" @@ -11,7 +10,55 @@ local make_entry = require "fzf-lua.make_entry" local M = {} +M.fzf_resume = function() + if not config.__resume_data or not config.__resume_data.opts then + utils.info("No resume data available, is 'global_resume' enabled?") + return + end + local opts = config.__resume_data.opts + local last_query = config.__resume_data.last_query + if last_query and #last_query>0 then + last_query = vim.fn.shellescape(last_query) + else + -- in case we continue from another resume + -- reset the previous query which was saved + -- inside "fzf_opts['--query']" argument + last_query = false + end + opts.fzf_opts['--query'] = last_query + if opts.__FNCREF__ then + -- HACK for 'live_grep' and 'lsp_live_workspace_symbols' + opts.__FNCREF__({ continue_last_search = true }) + else + M.fzf_wrap(opts, config.__resume_data.contents)() + end +end + +M.fzf_wrap = function(opts, contents, fn_post) + return coroutine.wrap(function() + opts.fn_post = opts.fn_post or fn_post + local selected = M.fzf(opts, contents) + if opts.fn_post then + opts.fn_post(selected) + end + end) +end + M.fzf = function(opts, contents) + -- support global resume? + if config.globals.global_resume and not opts.no_global_resume then + config.__resume_data = config.__resume_data or {} + config.__resume_data.opts = vim.deepcopy(opts) + config.__resume_data.contents = contents and vim.deepcopy(contents) or nil + if not opts._is_skim and not opts.no_global_resume_act then + local raw_act = shell.raw_action(function(args) + config.__resume_data.last_query = args[1] + end, "{q}") + opts._fzf_cli_args = ('--bind=change:execute-silent:%s'): + format(vim.fn.shellescape(raw_act)) + end + config.__resume_data.last_query = nil + end -- normalize with globals if not already normalized if not opts._normalized then opts = config.normalize_opts(opts, {}) @@ -334,9 +381,8 @@ M.fzf_files = function(opts, contents) if not opts then return end - coroutine.wrap(function () - local selected = M.fzf(opts, contents or opts.fzf_fn) + M.fzf_wrap(opts, contents or opts.fzf_fn, function(selected) if opts.post_select_cb then opts.post_select_cb() diff --git a/lua/fzf-lua/init.lua b/lua/fzf-lua/init.lua index 72281f2..9843184 100644 --- a/lua/fzf-lua/init.lua +++ b/lua/fzf-lua/init.lua @@ -79,6 +79,8 @@ function M.setup(opts) globals = nil end +M.resume = require'fzf-lua.core'.fzf_resume + M.files = require'fzf-lua.providers.files'.files M.files_resume = require'fzf-lua.providers.files'.files_resume M.args = require'fzf-lua.providers.files'.args @@ -155,15 +157,17 @@ end -- API shortcuts M.fzf = require'fzf-lua.core'.fzf +M.fzf_wrap = require'fzf-lua.core'.fzf_wrap M.raw_fzf = require'fzf-lua.fzf'.raw_fzf M.builtin = function(opts) if not opts then opts = {} end opts.metatable = M opts.metatable_exclude = { - ["setup"] = false, - ["fzf"] = false, - ["raw_fzf"] = false, + ["setup"] = false, + ["fzf"] = false, + ["fzf_wrap"] = false, + ["raw_fzf"] = false, } for _, m in ipairs(_modules) do opts.metatable_exclude[m] = false diff --git a/lua/fzf-lua/make_entry.lua b/lua/fzf-lua/make_entry.lua index 433c7a8..c525275 100644 --- a/lua/fzf-lua/make_entry.lua +++ b/lua/fzf-lua/make_entry.lua @@ -188,8 +188,10 @@ M.preprocess = function(opts) -- save our last search argument for resume if opts.cmd:match("{argv1}") then + local query = argv(1) set_config_section('globals.grep._last_search', - { query = argv(1), no_esc = true }) + { query = query, no_esc = true }) + set_config_section('__resume_data.last_query', query) end -- did the caller request rg with glob support? diff --git a/lua/fzf-lua/providers/buffers.lua b/lua/fzf-lua/providers/buffers.lua index 253f499..115b3ab 100644 --- a/lua/fzf-lua/providers/buffers.lua +++ b/lua/fzf-lua/providers/buffers.lua @@ -169,20 +169,20 @@ M.buffers = function(opts) if not next(filtered) then return end - coroutine.wrap(function () - local items = {} + local items = {} - local buffers, header_line = make_buffer_entries(opts, filtered, nil, opts.curbuf) - for _, buf in pairs(buffers) do - items = add_buffer_entry(opts, buf, items, header_line) - end + local buffers, header_line = make_buffer_entries(opts, filtered, nil, opts.curbuf) + for _, buf in pairs(buffers) do + items = add_buffer_entry(opts, buf, items, header_line) + end - opts.fzf_opts['--preview'] = act - if header_line and not opts.ignore_current_buffer then - opts.fzf_opts['--header-lines'] = '1' - end + opts.fzf_opts['--preview'] = act + if header_line and not opts.ignore_current_buffer then + opts.fzf_opts['--header-lines'] = '1' + end + + core.fzf_wrap(opts, items, function(selected) - local selected = core.fzf(opts, items) if not selected then return end actions.act(opts.actions, selected, opts) @@ -209,49 +209,48 @@ M.buffer_lines = function(opts) opts.current_buffer_only and { vim.api.nvim_get_current_buf() } or vim.api.nvim_list_bufs()) - coroutine.wrap(function() - local items = {} + local items = {} - for _, bufnr in ipairs(buffers) do - local data = {} - local filepath = api.nvim_buf_get_name(bufnr) - if api.nvim_buf_is_loaded(bufnr) then - data = api.nvim_buf_get_lines(bufnr, 0, -1, false) - elseif vim.fn.filereadable(filepath) ~= 0 then - data = vim.fn.readfile(filepath, "") - end - local bufname = path.basename(filepath) - local buficon, hl - if opts.file_icons then - local filename = path.tail(bufname) - local extension = path.extension(filename) - buficon, hl = core.get_devicon(filename, extension) - if opts.color_icons then - buficon = utils.ansi_codes[hl](buficon) - 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.green(tostring(l)), - text)) + for _, bufnr in ipairs(buffers) do + local data = {} + local filepath = api.nvim_buf_get_name(bufnr) + if api.nvim_buf_is_loaded(bufnr) then + data = api.nvim_buf_get_lines(bufnr, 0, -1, false) + elseif vim.fn.filereadable(filepath) ~= 0 then + data = vim.fn.readfile(filepath, "") + end + local bufname = path.basename(filepath) + local buficon, hl + if opts.file_icons then + local filename = path.tail(bufname) + local extension = path.extension(filename) + buficon, hl = core.get_devicon(filename, extension) + if opts.color_icons then + buficon = utils.ansi_codes[hl](buficon) 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.green(tostring(l)), + text)) + end + end - -- ignore bufnr when searching - -- disable multi-select - opts.fzf_opts["--no-multi"] = '' - opts.fzf_opts["--preview-window"] = 'hidden:right:0' + -- ignore bufnr when searching + -- disable multi-select + opts.fzf_opts["--no-multi"] = '' + opts.fzf_opts["--preview-window"] = 'hidden:right:0' - if opts.search and #opts.search>0 then - opts.fzf_opts['--query'] = vim.fn.shellescape(opts.search) - end + if opts.search and #opts.search>0 then + opts.fzf_opts['--query'] = vim.fn.shellescape(opts.search) + end - local selected = core.fzf(opts, items) + core.fzf_wrap(opts, items, function(selected) if not selected then return end -- get the line number @@ -300,34 +299,34 @@ M.tabs = function(opts) bufnrs[b] = nil end end - coroutine.wrap(function () - local items = {} - for t, bufnrs in pairs(opts._tab_to_buf) do + local items = {} - table.insert(items, ("%d)%s%s\t%s"):format(t, utils.nbsp, - 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 '')) + for t, bufnrs in pairs(opts._tab_to_buf) do - local bufnrs_flat = {} - for b, _ in pairs(bufnrs) do - table.insert(bufnrs_flat, b) - end + table.insert(items, ("%d)%s%s\t%s"):format(t, utils.nbsp, + 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 '')) - opts.sort_lastused = false - opts._prefix = ("%d)%s%s%s"):format(t, utils.nbsp, utils.nbsp, utils.nbsp) - local buffers = make_buffer_entries(opts, bufnrs_flat, t) - for _, buf in pairs(buffers) do - items = add_buffer_entry(opts, buf, items, true) - end + local bufnrs_flat = {} + for b, _ in pairs(bufnrs) do + table.insert(bufnrs_flat, b) end - opts.fzf_opts["--no-multi"] = '' - opts.fzf_opts["--preview-window"] = 'hidden:right:0' - opts.fzf_opts["--delimiter"] = vim.fn.shellescape('[\\)]') - opts.fzf_opts["--with-nth"] = '2' + opts.sort_lastused = false + opts._prefix = ("%d)%s%s%s"):format(t, utils.nbsp, utils.nbsp, utils.nbsp) + local buffers = make_buffer_entries(opts, bufnrs_flat, t) + for _, buf in pairs(buffers) do + items = add_buffer_entry(opts, buf, items, true) + end + end + + opts.fzf_opts["--no-multi"] = '' + opts.fzf_opts["--preview-window"] = 'hidden:right:0' + opts.fzf_opts["--delimiter"] = vim.fn.shellescape('[\\)]') + opts.fzf_opts["--with-nth"] = '2' - local selected = core.fzf(opts, items) + core.fzf_wrap(opts, items, function(selected) if not selected then return end diff --git a/lua/fzf-lua/providers/colorschemes.lua b/lua/fzf-lua/providers/colorschemes.lua index e4f9d5c..7627ca8 100644 --- a/lua/fzf-lua/providers/colorschemes.lua +++ b/lua/fzf-lua/providers/colorschemes.lua @@ -18,25 +18,24 @@ M.colorschemes = function(opts) opts = config.normalize_opts(opts, config.globals.colorschemes) if not opts then return end - coroutine.wrap(function () - local prev_act = shell.action(function (args) - if opts.live_preview and args then - local colorscheme = args[1] - vim.cmd("colorscheme " .. colorscheme) - end - end) - - local current_colorscheme = get_current_colorscheme() - local current_background = vim.o.background - local colors = vim.list_extend(opts.colors or {}, vim.fn.getcompletion('', 'color')) - - -- must add ':nohidden' or fzf ignore the preview action - -- disabling our live preview of colorschemes - opts.fzf_opts['--preview'] = prev_act - opts.fzf_opts['--no-multi'] = '' - opts.fzf_opts['--preview-window'] = 'nohidden:right:0' - - local selected = core.fzf(opts, colors) + local prev_act = shell.action(function (args) + if opts.live_preview and args then + local colorscheme = args[1] + vim.cmd("colorscheme " .. colorscheme) + end + end) + + local current_colorscheme = get_current_colorscheme() + local current_background = vim.o.background + local colors = vim.list_extend(opts.colors or {}, vim.fn.getcompletion('', 'color')) + + -- must add ':nohidden' or fzf ignore the preview action + -- disabling our live preview of colorschemes + opts.fzf_opts['--preview'] = prev_act + opts.fzf_opts['--no-multi'] = '' + opts.fzf_opts['--preview-window'] = 'nohidden:right:0' + + core.fzf_wrap(opts, colors, function(selected) -- reset color scheme if live_preview is enabled -- and nothing or non-default action was selected diff --git a/lua/fzf-lua/providers/files.lua b/lua/fzf-lua/providers/files.lua index 2292290..be0f601 100644 --- a/lua/fzf-lua/providers/files.lua +++ b/lua/fzf-lua/providers/files.lua @@ -43,26 +43,8 @@ M.files = function(opts) return core.fzf_files(opts, contents) end -local last_query = "" - -M.files_resume = function(opts) - - opts = config.normalize_opts(opts, config.globals.files) - if not opts then return end - if opts._is_skim then - utils.info("'files_resume' is not supported with 'sk'") - return - end - - local raw_act = shell.raw_action(function(args) - last_query = args[1] - end, "{q}") - - opts.fzf_opts['--query'] = vim.fn.shellescape(last_query) - opts._fzf_cli_args = ('--bind=change:execute-silent:%s'): - format(vim.fn.shellescape(raw_act)) - - return M.files(opts) +M.files_resume = function(_) + utils.warn("'files_resume' was deprecated, please use global 'resume' instead.") end M.args = function(opts) diff --git a/lua/fzf-lua/providers/git.lua b/lua/fzf-lua/providers/git.lua index c394a3c..5b61ee8 100644 --- a/lua/fzf-lua/providers/git.lua +++ b/lua/fzf-lua/providers/git.lua @@ -48,9 +48,8 @@ end local function git_cmd(opts) opts.cwd = path.git_root(opts.cwd) if not opts.cwd then return end - coroutine.wrap(function () - opts = core.set_header(opts, 2) - local selected = core.fzf(opts, opts.cmd) + opts = core.set_header(opts, 2) + core.fzf_wrap(opts, opts.cmd, function(selected) if not selected then return end actions.act(opts.actions, selected, opts) end)() diff --git a/lua/fzf-lua/providers/grep.lua b/lua/fzf-lua/providers/grep.lua index 23f4284..74a8153 100644 --- a/lua/fzf-lua/providers/grep.lua +++ b/lua/fzf-lua/providers/grep.lua @@ -13,6 +13,9 @@ local function set_last_search(query, no_esc) query = query, no_esc = no_esc } + if config.__resume_data then + config.__resume_data.last_query = query + end end local M = {} @@ -147,6 +150,10 @@ M.live_grep_st = function(opts) end end + -- disable global resume + -- conflicts with 'change:reload' event + opts.no_global_resume_act = true + opts.__FNCREF__ = opts.__FNCREF__ or utils.__FNCREF__() opts = core.set_fzf_line_args(opts) opts = core.set_fzf_interactive_cmd(opts) core.fzf_files(opts) @@ -259,6 +266,10 @@ M.live_grep_mt = function(opts) ("%s || true"):format(reload_command)))) end + -- disable global resume + -- conflicts with 'change:reload' event + opts.no_global_resume_act = true + opts.__FNCREF__ = opts.__FNCREF__ or utils.__FNCREF__() opts = core.set_fzf_line_args(opts) core.fzf_files(opts) opts.search = nil @@ -374,6 +385,7 @@ end M.lgrep_curbuf = function(opts) if not opts then opts = {} end opts.lgrep = true + opts.__FNCREF__ = utils.__FNCREF__() return M.grep_curbuf(opts) end diff --git a/lua/fzf-lua/providers/helptags.lua b/lua/fzf-lua/providers/helptags.lua index 4b65760..8ba5a3e 100644 --- a/lua/fzf-lua/providers/helptags.lua +++ b/lua/fzf-lua/providers/helptags.lua @@ -96,15 +96,13 @@ M.helptags = function(opts) opts = config.normalize_opts(opts, config.globals.helptags) if not opts then return end - coroutine.wrap(function () - - -- local prev_act = action(function (args) end) + -- local prev_act = action(function (args) end) - opts.fzf_opts['--no-multi'] = '' - opts.fzf_opts['--preview-window'] = 'hidden:right:0' - opts.fzf_opts['--nth'] = '1' + opts.fzf_opts['--no-multi'] = '' + opts.fzf_opts['--preview-window'] = 'hidden:right:0' + opts.fzf_opts['--nth'] = '1' - local selected = core.fzf(opts, fzf_function) + core.fzf_wrap(opts, fzf_function, function(selected) if not selected then return end diff --git a/lua/fzf-lua/providers/lsp.lua b/lua/fzf-lua/providers/lsp.lua index 84a8270..682f523 100644 --- a/lua/fzf-lua/providers/lsp.lua +++ b/lua/fzf-lua/providers/lsp.lua @@ -219,6 +219,9 @@ local function set_lsp_fzf_fn(opts) coroutine.wrap(function () local co = coroutine.running() + -- reset number of callbacks incase + -- we're being called from 'resume' + opts.num_callbacks = 0 -- cancel all currently running requests -- can happen when using `live_ws_symbols` @@ -436,9 +439,7 @@ M.code_actions = function(opts) -- error or no sync request no results if not opts.fzf_fn then return end - coroutine.wrap(function () - - local selected = core.fzf(opts, opts.fzf_fn) + core.fzf_wrap(opts, opts.fzf_fn, function(selected) if opts.post_select_cb then opts.post_select_cb() @@ -627,6 +628,7 @@ M.live_workspace_symbols = function(opts) if query and not (opts.save_last_search == false) then last_search = {} last_search.query = query + config.__resume_data.last_query = query end opts.sync = true opts.async = false @@ -635,6 +637,10 @@ M.live_workspace_symbols = function(opts) return opts.fzf_fn end + -- disable global resume + -- conflicts with 'change:reload' event + opts.no_global_resume_act = true + opts.__FNCREF__ = M.live_workspace_symbols opts = core.set_fzf_interactive_cb(opts) opts = core.set_fzf_line_args(opts) core.fzf_files(opts) diff --git a/lua/fzf-lua/providers/manpages.lua b/lua/fzf-lua/providers/manpages.lua index 8a9b7de..399863e 100644 --- a/lua/fzf-lua/providers/manpages.lua +++ b/lua/fzf-lua/providers/manpages.lua @@ -27,23 +27,21 @@ M.manpages = function(opts) opts = config.normalize_opts(opts, config.globals.manpages) if not opts then return end - coroutine.wrap(function () - - local fzf_fn = libuv.spawn_nvim_fzf_cmd( - { cmd = opts.cmd, cwd = opts.cwd, pid_cb = opts._pid_cb }, - function(x) - -- split by first occurence of ' - ' (spaced hyphen) - local man, desc = x:match("^(.-) %- (.*)$") - return string.format("%-45s %s", - utils.ansi_codes.magenta(man), desc) - end) - - opts.fzf_opts['--no-multi'] = '' - opts.fzf_opts['--preview-window'] = 'hidden:right:0' - opts.fzf_opts['--tiebreak'] = 'begin' - opts.fzf_opts['--nth'] = '1,2' - - local selected = core.fzf(opts, fzf_fn) + local fzf_fn = libuv.spawn_nvim_fzf_cmd( + { cmd = opts.cmd, cwd = opts.cwd, pid_cb = opts._pid_cb }, + function(x) + -- split by first occurence of ' - ' (spaced hyphen) + local man, desc = x:match("^(.-) %- (.*)$") + return string.format("%-45s %s", + utils.ansi_codes.magenta(man), desc) + end) + + opts.fzf_opts['--no-multi'] = '' + opts.fzf_opts['--preview-window'] = 'hidden:right:0' + opts.fzf_opts['--tiebreak'] = 'begin' + opts.fzf_opts['--nth'] = '1,2' + + core.fzf_wrap(opts, fzf_fn, function(selected) if not selected then return end diff --git a/lua/fzf-lua/providers/module.lua b/lua/fzf-lua/providers/module.lua index 5308252..26bb863 100644 --- a/lua/fzf-lua/providers/module.lua +++ b/lua/fzf-lua/providers/module.lua @@ -12,28 +12,26 @@ M.metatable = function(opts) if not opts.metatable then opts.metatable = getmetatable('').__index end - coroutine.wrap(function () - - local prev_act = shell.action(function (args) - -- TODO: retreive method help - local help = '' - return string.format("%s:%s", args[1], help) - end) - - local methods = {} - for k, _ in pairs(opts.metatable) do - if not opts.metatable_exclude or opts.metatable_exclude[k] == nil then - table.insert(methods, k) - end + local prev_act = shell.action(function (args) + -- TODO: retreive method help + local help = '' + return string.format("%s:%s", args[1], help) + end) + + local methods = {} + for k, _ in pairs(opts.metatable) do + if not opts.metatable_exclude or opts.metatable_exclude[k] == nil then + table.insert(methods, k) end + end - table.sort(methods, function(a, b) return a 0) or not opts.ignore_empty then - table.insert(entries, string.format("[%s] %s", - utils.ansi_codes.yellow(r), contents)) - end + local registers = { '"', "_", "#", "=", "_", "/", "*", "+", ":", ".", "%" } + -- named + for i = 0, 9 do + table.insert(registers, tostring(i)) + end + -- alphabetical + for i = 65, 90 do + table.insert(registers, string.char(i)) + end + + local prev_act = shell.action(function (args) + local r = args[1]:match("%[(.*)%] ") + local _, contents = pcall(vim.fn.getreg, r) + return contents or args[1] + end) + + local entries = {} + for _, r in ipairs(registers) do + -- pcall as this could fail with: + -- E5108: Error executing lua Vim:clipboard: + -- provider returned invalid data + local _, contents = pcall(vim.fn.getreg, r) + contents = contents:gsub("\n", utils.ansi_codes.magenta("\\n")) + if (contents and #contents > 0) or not opts.ignore_empty then + table.insert(entries, string.format("[%s] %s", + utils.ansi_codes.yellow(r), contents)) end + end - opts.fzf_opts['--no-multi'] = '' - opts.fzf_opts['--preview'] = prev_act + opts.fzf_opts['--no-multi'] = '' + opts.fzf_opts['--preview'] = prev_act - local selected = core.fzf(opts, entries) + core.fzf_wrap(opts, entries, function(selected) if not selected then return end actions.act(opts.actions, selected) @@ -215,54 +206,52 @@ M.keymaps = function(opts) opts = config.normalize_opts(opts, config.globals.nvim.keymaps) if not opts then return end - coroutine.wrap(function () - - local modes = { "n", "i", "c" } - local keymaps = {} - - local add_keymap = function(keymap) - -- hijack fields - keymap.str = string.format("[%s:%s:%s]", - utils.ansi_codes.yellow(tostring(keymap.buffer)), - utils.ansi_codes.green(keymap.mode), - utils.ansi_codes.magenta(keymap.lhs:gsub("%s", ""))) - local k = string.format("[%s:%s:%s]", - keymap.buffer, keymap.mode, keymap.lhs) - keymaps[k] = keymap + local modes = { "n", "i", "c" } + local keymaps = {} + + local add_keymap = function(keymap) + -- hijack fields + keymap.str = string.format("[%s:%s:%s]", + utils.ansi_codes.yellow(tostring(keymap.buffer)), + utils.ansi_codes.green(keymap.mode), + utils.ansi_codes.magenta(keymap.lhs:gsub("%s", ""))) + local k = string.format("[%s:%s:%s]", + keymap.buffer, keymap.mode, keymap.lhs) + keymaps[k] = keymap + end + + for _, mode in pairs(modes) do + local global = vim.api.nvim_get_keymap(mode) + for _, keymap in pairs(global) do + add_keymap(keymap) end - - for _, mode in pairs(modes) do - local global = vim.api.nvim_get_keymap(mode) - for _, keymap in pairs(global) do - add_keymap(keymap) - end - local buf_local = vim.api.nvim_buf_get_keymap(0, mode) - for _, keymap in pairs(buf_local) do - add_keymap(keymap) - end + local buf_local = vim.api.nvim_buf_get_keymap(0, mode) + for _, keymap in pairs(buf_local) do + add_keymap(keymap) end - - local prev_act = shell.action(function (args) - local k = args[1]:match("(%[.*%]) ") - local v = keymaps[k] - if v then - -- clear hijacked field - v.str = nil - k = vim.inspect(v) - end - return k - end) - - local entries = {} - for _, v in pairs(keymaps) do - table.insert(entries, string.format("%-50s %s", - v.str, v.rhs)) + end + + local prev_act = shell.action(function (args) + local k = args[1]:match("(%[.*%]) ") + local v = keymaps[k] + if v then + -- clear hijacked field + v.str = nil + k = vim.inspect(v) end + return k + end) + + local entries = {} + for _, v in pairs(keymaps) do + table.insert(entries, string.format("%-50s %s", + v.str, v.rhs)) + end - opts.fzf_opts['--no-multi'] = '' - opts.fzf_opts['--preview'] = prev_act + opts.fzf_opts['--no-multi'] = '' + opts.fzf_opts['--preview'] = prev_act - local selected = core.fzf(opts, entries) + core.fzf_wrap(opts, entries, function(selected) if not selected then return end actions.act(opts.actions, selected) @@ -276,17 +265,15 @@ M.spell_suggest = function(opts) opts = config.normalize_opts(opts, config.globals.nvim.spell_suggest) if not opts then return end - coroutine.wrap(function () + local cursor_word = vim.fn.expand "" + local entries = vim.fn.spellsuggest(cursor_word) - local cursor_word = vim.fn.expand "" - local entries = vim.fn.spellsuggest(cursor_word) + if vim.tbl_isempty(entries) then return end - if vim.tbl_isempty(entries) then return end + opts.fzf_opts['--no-multi'] = '' + opts.fzf_opts['--preview-window'] = 'hidden:right:0' - opts.fzf_opts['--no-multi'] = '' - opts.fzf_opts['--preview-window'] = 'hidden:right:0' - - local selected = core.fzf(opts, entries) + core.fzf_wrap(opts, entries, function(selected) if not selected then return end actions.act(opts.actions, selected) @@ -300,15 +287,13 @@ M.filetypes = function(opts) opts = config.normalize_opts(opts, config.globals.nvim.filetypes) if not opts then return end - coroutine.wrap(function () - - local entries = vim.fn.getcompletion('', 'filetype') - if vim.tbl_isempty(entries) then return end + local entries = vim.fn.getcompletion('', 'filetype') + if vim.tbl_isempty(entries) then return end - opts.fzf_opts['--no-multi'] = '' - opts.fzf_opts['--preview-window'] = 'hidden:right:0' + opts.fzf_opts['--no-multi'] = '' + opts.fzf_opts['--preview-window'] = 'hidden:right:0' - local selected = core.fzf(opts, entries) + core.fzf_wrap(opts, entries, function(selected) if not selected then return end actions.act(opts.actions, selected) @@ -322,16 +307,14 @@ M.packadd = function(opts) opts = config.normalize_opts(opts, config.globals.nvim.packadd) if not opts then return end - coroutine.wrap(function () - - local entries = vim.fn.getcompletion('', 'packadd') + local entries = vim.fn.getcompletion('', 'packadd') - if vim.tbl_isempty(entries) then return end + if vim.tbl_isempty(entries) then return end - opts.fzf_opts['--no-multi'] = '' - opts.fzf_opts['--preview-window'] = 'hidden:right:0' + opts.fzf_opts['--no-multi'] = '' + opts.fzf_opts['--preview-window'] = 'hidden:right:0' - local selected = core.fzf(opts, entries) + core.fzf_wrap(opts, entries, function(selected) if not selected then return end actions.act(opts.actions, selected) diff --git a/lua/fzf-lua/utils.lua b/lua/fzf-lua/utils.lua index e19c3c7..713b432 100644 --- a/lua/fzf-lua/utils.lua +++ b/lua/fzf-lua/utils.lua @@ -8,6 +8,11 @@ end local M = {} +function M.__FILE__() return debug.getinfo(2, 'S').source end +function M.__LINE__() return debug.getinfo(2, 'l').currentline end +function M.__FNC__() return debug.getinfo(2, 'n').name end +function M.__FNCREF__() return debug.getinfo(2, 'f').func end + -- sets an invisible unicode character as icon seaprator -- the below was reached after many iterations, a short summary of everything -- that was tried and why it failed: