Skip to content

Commit 3485e30

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

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,13 +512,15 @@ 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 cmdline.
516516
Notes:
517517
- Has no preview.
518518
- Choosing action depends on scope:
519519
- For "cmd" / ":" scopes, the command is executed.
520520
- For "search" / "/" / "?" scopes, search is redone.
521521
- For other scopes nothing is done (but chosen item is still returned).
522+
- `<C-e>` only works for "cmd" / ":" / "search" / "/" / "?" scopes. It does
523+
nothing for all other scopes.
522524

523525
Examples ~
524526

lua/mini/extra.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,13 +921,15 @@ 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 cmdline.
925925
--- Notes:
926926
--- - Has no preview.
927927
--- - Choosing action depends on scope:
928928
--- - For "cmd" / ":" scopes, the command is executed.
929929
--- - For "search" / "/" / "?" scopes, search is redone.
930930
--- - For other scopes nothing is done (but chosen item is still returned).
931+
--- - `<C-e>` only works for "cmd" / ":" / "search" / "/" / "?" scopes. It does
932+
--- nothing for all other scopes.
931933
---
932934
--- Examples ~
933935
---
@@ -978,6 +980,17 @@ MiniExtra.pickers.history = function(local_opts, opts)
978980
end
979981
end
980982

983+
local edit_command = function()
984+
local cur_match = MiniPick.get_picker_matches().current
985+
local scope
986+
scope, cur_match = cur_match:match('^([:/?]) (.*)')
987+
if scope ~= ':' and scope ~= '/' and scope ~= '?' then return end
988+
vim.schedule(function() vim.api.nvim_input(scope .. cur_match) 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
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--|---------|---------|---------|---------|---------|---------|---------|
2+
01|
3+
02|~
4+
03|~
5+
04|~
6+
05|~
7+
06|~
8+
07|┌> @▏───────────────────────────────────────┐
9+
08|│@ input 2 │
10+
09|│@ input 1 │
11+
10|│ │
12+
11|│ │
13+
12|│ │
14+
13|│ │
15+
14|│ │
16+
15|│ │
17+
16|│ │
18+
17|│ │
19+
18|│ │
20+
19|└ History (all) ───────────────────── 1|2|8 ┘
21+
20|
22+
23+
--|---------|---------|---------|---------|---------|---------|---------|
24+
01|0000000000000000000000000000000000000000000000000000000000000000000000
25+
02|1111111111111111111111111111111111111111111111111111111111111111111111
26+
03|1111111111111111111111111111111111111111111111111111111111111111111111
27+
04|1111111111111111111111111111111111111111111111111111111111111111111111
28+
05|1111111111111111111111111111111111111111111111111111111111111111111111
29+
06|1111111111111111111111111111111111111111111111111111111111111111111111
30+
07|2334522222222222222222222222222222222222222221111111111111111111111111
31+
08|2677777777777777777777777777777777777777777721111111111111111111111111
32+
09|2899999999999999999999999999999999999999999921111111111111111111111111
33+
10|2:::::::::::::::::::::::::::::::::::::::::::21111111111111111111111111
34+
11|2:::::::::::::::::::::::::::::::::::::::::::21111111111111111111111111
35+
12|2:::::::::::::::::::::::::::::::::::::::::::21111111111111111111111111
36+
13|2:::::::::::::::::::::::::::::::::::::::::::21111111111111111111111111
37+
14|2:::::::::::::::::::::::::::::::::::::::::::21111111111111111111111111
38+
15|2:::::::::::::::::::::::::::::::::::::::::::21111111111111111111111111
39+
16|2:::::::::::::::::::::::::::::::::::::::::::21111111111111111111111111
40+
17|2:::::::::::::::::::::::::::::::::::::::::::21111111111111111111111111
41+
18|2:::::::::::::::::::::::::::::::::::::::::::21111111111111111111111111
42+
19|2;;;;;;;;;;;;;;;222222222222222222222;;;;;;;21111111111111111111111111
43+
20|0000000000000000000000000000000000000000000000000000000000000000000000

tests/test_extra.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,6 +2463,28 @@ 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+
child.set_size(20, 70)
2468+
2469+
pick_history()
2470+
eq(child.lua_get('MiniPick.get_picker_opts().mappings.edit_command.char'), '<C-e>')
2471+
2472+
type_keys(':', '<C-e>')
2473+
eq(child.fn.getcmdtype(), ':')
2474+
eq(child.fn.getcmdline(), 'lua _G.n = _G.n + 2')
2475+
2476+
type_keys('<C-c>')
2477+
pick_history()
2478+
type_keys('/', '<C-e>')
2479+
eq(child.fn.getcmdtype(), '/')
2480+
eq(child.fn.getcmdline(), 'bbb')
2481+
2482+
type_keys('<C-c>')
2483+
pick_history()
2484+
type_keys('@', '<C-e>')
2485+
child.expect_screenshot({})
2486+
end
2487+
24662488
T['pickers']['hl_groups()'] = new_set()
24672489

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

0 commit comments

Comments
 (0)