Skip to content

Commit ab0d013

Browse files
phanenibhagwan
authored andcommitted
feat(actions): ctrl-x to delete command/search history
render_crlf=true to avoid mess up the index. A history entry can contain multi-line.
1 parent a80e78f commit ab0d013

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

lua/fzf-lua/actions.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,31 @@ M.search_cr = function(selected, opts)
588588
utils.feed_keys_termcodes("<CR>")
589589
end
590590

591+
---@param kind ":"|"/"
592+
---@param selected string[]
593+
---@param opts fzf-lua.config.CommandHistory|{}
594+
local hist_del = function(kind, selected, opts)
595+
if not vim.iter then return end
596+
local iter = opts.reverse_list and vim.iter(selected) or vim.iter(selected):rev()
597+
iter:each(function(e)
598+
local idx = assert(utils.tointeger(opts.reverse_list and e or -e - 1))
599+
local entry = vim.fn.histget(":", idx) -- get before deleted
600+
local res = vim.fn.histdel(kind, idx)
601+
local info = res == 1 and "deleted" or "fail to delete"
602+
local notify = res == 1 and utils.info or utils.warn
603+
notify("%s: %s", info, entry)
604+
end)
605+
vim.cmd("wshada!")
606+
end
607+
608+
M.ex_del = function(...)
609+
hist_del(":", ...)
610+
end
611+
612+
M.search_del = function(...)
613+
hist_del("/", ...)
614+
end
615+
591616
M.goto_mark = function(selected)
592617
if not selected[1] then return end
593618
local mark = selected[1]

lua/fzf-lua/core.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ M.ACTION_DEFINITIONS = {
8686
[actions.git_worktree_del] = { "delete worktree" },
8787
[actions.ex_run] = { "edit" },
8888
[actions.ex_run_cr] = { "execute" },
89+
[actions.ex_del] = { "delete" },
8990
[actions.search] = { "edit" },
9091
[actions.search_cr] = { "search" },
92+
[actions.search_del] = { "delete" },
9193
}
9294

9395
-- converts contents array sent to `fzf_exec` into a single contents

lua/fzf-lua/defaults.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,25 +1544,31 @@ M.defaults.undotree = {
15441544
}
15451545

15461546
---@class fzf-lua.config.CommandHistory: fzf-lua.config.Base
1547+
---@field reverse_list? boolean
15471548
M.defaults.command_history = {
15481549
fzf_opts = { ["--tiebreak"] = "index", ["--no-multi"] = true },
1550+
render_crlf = true,
15491551
_treesitter = function(line) return "foo.vim", nil, line end,
15501552
fzf_colors = { ["hl"] = "-1:reverse", ["hl+"] = "-1:reverse" },
15511553
actions = {
15521554
["enter"] = actions.ex_run_cr,
15531555
["ctrl-e"] = actions.ex_run,
1556+
["ctrl-x"] = { fn = actions.ex_del, field_index = "{+n}", reload = true }
15541557
},
15551558
_headers = { "actions" },
15561559
}
15571560

15581561
---@class fzf-lua.config.SearchHistory : fzf-lua.config.CommandHistory
1562+
---@field reverse_search? boolean
15591563
M.defaults.search_history = {
15601564
fzf_opts = { ["--tiebreak"] = "index", ["--no-multi"] = true },
1565+
render_crlf = true,
15611566
_treesitter = function(line) return "", nil, line, "regex" end,
15621567
fzf_colors = { ["hl"] = "-1:reverse", ["hl+"] = "-1:reverse" },
15631568
actions = {
15641569
["enter"] = actions.search_cr,
15651570
["ctrl-e"] = actions.search,
1571+
["ctrl-x"] = { fn = actions.search_del, field_index = "{+n}", reload = true }
15661572
},
15671573
_headers = { "actions" },
15681574
}

lua/fzf-lua/providers/nvim.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ M.commands = function(opts)
109109
return core.fzf_exec(entries, opts)
110110
end
111111

112-
---@param opts table
112+
---@param opts fzf-lua.config.CommandHistory
113113
---@param str ":"|"/"
114114
local history = function(opts, str)
115115
local histnr = vim.fn.histnr(str)

lua/fzf-lua/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ local FzfLua = require("fzf-lua")
146146
---@field cmd? string
147147
---@field debug? boolean|integer|'v'|'verbose'
148148
---@field preview_offset? string
149+
---@field render_crlf? boolean
149150
---@field _fzf_cli_args? string[]
150151
---@field __INFO fzf-lua.Info
151152
---@field __CTX fzf-lua.Ctx

0 commit comments

Comments
 (0)