You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
221 lines
5.2 KiB
Lua
221 lines
5.2 KiB
Lua
3 years ago
|
if not pcall(require, "fzf") then
|
||
|
return
|
||
|
end
|
||
|
|
||
|
local action = require("fzf.actions").action
|
||
|
local core = require "fzf-lua.core"
|
||
|
local utils = require "fzf-lua.utils"
|
||
|
local config = require "fzf-lua.config"
|
||
|
local actions = require "fzf-lua.actions"
|
||
|
|
||
|
local M = {}
|
||
|
|
||
|
M.commands = function(opts)
|
||
|
|
||
|
opts = config.normalize_opts(opts, config.globals.nvim.commands)
|
||
|
|
||
|
coroutine.wrap(function ()
|
||
|
|
||
|
local commands = vim.api.nvim_get_commands {}
|
||
|
|
||
|
local prev_act = action(function (args)
|
||
|
local cmd = args[1]
|
||
|
if commands[cmd] then
|
||
|
cmd = vim.inspect(commands[cmd])
|
||
|
end
|
||
|
return cmd
|
||
|
end)
|
||
|
|
||
|
local entries = {}
|
||
|
for k, _ in pairs(commands) do
|
||
|
table.insert(entries, utils.ansi_codes.magenta(k))
|
||
|
end
|
||
|
|
||
|
table.sort(entries, function(a, b) return a<b end)
|
||
|
|
||
|
opts.nomulti = true
|
||
|
opts.preview = prev_act
|
||
|
|
||
|
local selected = core.fzf(opts, entries,
|
||
|
core.build_fzf_cli(opts),
|
||
|
config.winopts(opts))
|
||
|
|
||
|
if not selected then return end
|
||
|
actions.act(opts.actions, selected)
|
||
|
|
||
|
end)()
|
||
|
|
||
|
end
|
||
|
|
||
|
local history = function(opts, str)
|
||
|
coroutine.wrap(function ()
|
||
|
|
||
|
local history = vim.fn.execute("history " .. str)
|
||
|
history = vim.split(history, "\n")
|
||
|
|
||
|
local entries = {}
|
||
|
for i = #history, 3, -1 do
|
||
|
local item = history[i]
|
||
|
local _, finish = string.find(item, "%d+ +")
|
||
|
table.insert(entries, string.sub(item, finish + 1))
|
||
|
end
|
||
|
|
||
|
opts.nomulti = true
|
||
|
opts.preview = nil
|
||
|
opts.preview_window = 'hidden:down:0'
|
||
|
|
||
|
local selected = core.fzf(opts, entries,
|
||
|
core.build_fzf_cli(opts),
|
||
|
config.winopts(opts))
|
||
|
|
||
|
if not selected then return end
|
||
|
actions.act(opts.actions, selected)
|
||
|
|
||
|
end)()
|
||
|
end
|
||
|
|
||
|
M.command_history = function(opts)
|
||
|
opts = config.normalize_opts(opts, config.globals.nvim.command_history)
|
||
|
history(opts, "cmd")
|
||
|
end
|
||
|
|
||
|
M.search_history = function(opts)
|
||
|
opts = config.normalize_opts(opts, config.globals.nvim.search_history)
|
||
|
history(opts, "search")
|
||
|
end
|
||
|
|
||
|
M.registers = function(opts)
|
||
|
|
||
|
opts = config.normalize_opts(opts, config.globals.nvim.registers)
|
||
|
|
||
|
coroutine.wrap(function ()
|
||
|
|
||
|
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 = action(function (args)
|
||
|
local r = args[1]:match("%[(.*)%] ")
|
||
|
local contents = vim.fn.getreg(r)
|
||
|
return contents or r
|
||
|
end)
|
||
|
|
||
|
local entries = {}
|
||
|
for _, r in ipairs(registers) do
|
||
|
local contents = 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.nomulti = true
|
||
|
opts.preview = prev_act
|
||
|
|
||
|
local selected = core.fzf(opts, entries,
|
||
|
core.build_fzf_cli(opts),
|
||
|
config.winopts(opts))
|
||
|
|
||
|
if not selected then return end
|
||
|
actions.act(opts.actions, selected)
|
||
|
|
||
|
end)()
|
||
|
end
|
||
|
|
||
|
M.keymaps = function(opts)
|
||
|
|
||
|
opts = config.normalize_opts(opts, config.globals.nvim.keymaps)
|
||
|
|
||
|
coroutine.wrap(function ()
|
||
|
|
||
|
local modes = { "n", "i", "c" }
|
||
|
local keymaps = {}
|
||
|
|
||
|
local add_keymap = function(keymap)
|
||
|
-- hijack field
|
||
|
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))
|
||
|
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
|
||
|
local buf_local = vim.api.nvim_buf_get_keymap(0, mode)
|
||
|
for _, keymap in pairs(buf_local) do
|
||
|
add_keymap(keymap)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
local prev_act = 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.nomulti = true
|
||
|
opts.preview = prev_act
|
||
|
|
||
|
local selected = core.fzf(opts, entries,
|
||
|
core.build_fzf_cli(opts),
|
||
|
config.winopts(opts))
|
||
|
|
||
|
if not selected then return end
|
||
|
actions.act(opts.actions, selected)
|
||
|
|
||
|
end)()
|
||
|
end
|
||
|
|
||
|
M.spell_suggest = function(opts)
|
||
|
|
||
|
-- if not vim.wo.spell then return false end
|
||
|
opts = config.normalize_opts(opts, config.globals.nvim.spell_suggest)
|
||
|
|
||
|
coroutine.wrap(function ()
|
||
|
|
||
|
local cursor_word = vim.fn.expand "<cword>"
|
||
|
local entries = vim.fn.spellsuggest(cursor_word)
|
||
|
|
||
|
if vim.tbl_isempty(entries) then return end
|
||
|
|
||
|
opts.nomulti = true
|
||
|
opts.preview = nil
|
||
|
opts.preview_window = 'hidden:down:0'
|
||
|
|
||
|
local selected = core.fzf(opts, entries,
|
||
|
core.build_fzf_cli(opts),
|
||
|
config.winopts(opts))
|
||
|
|
||
|
if not selected then return end
|
||
|
actions.act(opts.actions, selected)
|
||
|
|
||
|
end)()
|
||
|
|
||
|
end
|
||
|
|
||
|
return M
|