Skip to content

Commit 3b7bea5

Browse files
committed
feat(extra): add <C-e> mapping for pickers.history
1 parent 0069a71 commit 3b7bea5

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ There are following change types:
5555

5656
- Add `pickers.colorschemes` picker. By @pkazmier, PR #1789.
5757

58+
- Add `<C-e>` mapping for `pickers.history` picker to edit commands or searches in cmdline. By @TheLeoP, PR #1960.
59+
5860
## mini.files
5961

6062
### Refine

doc/mini-extra.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,13 +512,16 @@ Return ~
512512
`MiniExtra.pickers.history`({local_opts}, {opts})
513513
Neovim history picker
514514

515-
Pick from output of |:history|.
515+
Pick from output of |:history|. Use `<C-e>` to edit current match in
516+
Command line.
517+
516518
Notes:
517519
- Has no preview.
518520
- Choosing action depends on scope:
519521
- For "cmd" / ":" scopes, the command is executed.
520522
- For "search" / "/" / "?" scopes, search is redone.
521523
- For other scopes nothing is done (but chosen item is still returned).
524+
- `<C-e>` only works for "cmd" / ":" / "search" / "/" / "?" scopes.
522525

523526
Examples ~
524527

lua/mini/extra.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,13 +921,16 @@ end
921921

922922
--- Neovim history picker
923923
---
924-
--- Pick from output of |:history|.
924+
--- Pick from output of |:history|. Use `<C-e>` to edit current match in
925+
--- Command line.
926+
---
925927
--- Notes:
926928
--- - Has no preview.
927929
--- - Choosing action depends on scope:
928930
--- - For "cmd" / ":" scopes, the command is executed.
929931
--- - For "search" / "/" / "?" scopes, search is redone.
930932
--- - For other scopes nothing is done (but chosen item is still returned).
933+
--- - `<C-e>` only works for "cmd" / ":" / "search" / "/" / "?" scopes.
931934
---
932935
--- Examples ~
933936
---
@@ -978,6 +981,16 @@ MiniExtra.pickers.history = function(local_opts, opts)
978981
end
979982
end
980983

984+
local edit_command = function()
985+
local cur_match = MiniPick.get_picker_matches().current
986+
local cur_scope, cur_item = cur_match:match('^(.) (.*)$')
987+
if not (cur_scope == ':' or cur_scope == '/' or cur_scope == '?') then return end
988+
vim.schedule(function() vim.api.nvim_input(cur_scope .. cur_item) end)
989+
return true
990+
end
991+
local mappings = { edit_command = { char = '<C-e>', func = edit_command } }
992+
993+
opts = vim.tbl_deep_extend('force', opts or {}, { mappings = mappings })
981994
local default_source = { name = string.format('History (%s)', scope), preview = preview, choose = choose }
982995
return H.pick_start(items, { source = default_source }, opts)
983996
end

tests/test_extra.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,6 +2463,36 @@ T['pickers']['history()']['validates arguments'] = function()
24632463
validate({ scope = '1' }, '`pickers%.history`.*"scope".*"1".*one of')
24642464
end
24652465

2466+
T['pickers']['history()']['has custom "edit_command" mapping'] = function()
2467+
local validate = function(type, content, active)
2468+
eq(child.fn.getcmdtype(), type)
2469+
eq(child.fn.getcmdline(), content)
2470+
eq(is_picker_active(), active)
2471+
type_keys('<C-c>')
2472+
end
2473+
2474+
pick_history()
2475+
eq(child.lua_get('MiniPick.get_picker_opts().mappings.edit_command.char'), '<C-e>')
2476+
type_keys(':', '<C-e>')
2477+
validate(':', 'lua _G.n = _G.n + 2', false)
2478+
2479+
pick_history()
2480+
type_keys('/', '<C-e>')
2481+
validate('/', 'bbb', false)
2482+
2483+
pick_history()
2484+
type_keys('@', '<C-e>')
2485+
validate('', '', true)
2486+
2487+
pick_history({ scope = ':' })
2488+
type_keys('<C-e>')
2489+
validate(':', 'lua _G.n = _G.n + 2', false)
2490+
2491+
pick_history({ scope = '/' })
2492+
type_keys('<C-e>')
2493+
validate('/', 'bbb', false)
2494+
end
2495+
24662496
T['pickers']['hl_groups()'] = new_set()
24672497

24682498
local pick_hl_groups = forward_lua_notify('MiniExtra.pickers.hl_groups')

0 commit comments

Comments
 (0)