Skip to content

Commit 50f05bd

Browse files
pkazmierechasnovski
andcommitted
feat(clue): allow mode arrays for clues and triggers
Resolve #2192 Co-authored-by: Evgeni Chasnovski <[email protected]>
1 parent 94a20e7 commit 50f05bd

File tree

7 files changed

+155
-123
lines changed

7 files changed

+155
-123
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ There are following change types:
1515
- Add new plugin integrations:
1616
- 'folke/snacks.nvim'
1717

18+
## mini.clue
19+
20+
### Expand
21+
22+
- Allow mode arrays for clues and triggers for parity with `modes` parameter of `vim.keymap.set` . By @pkazmier, PR #2202.
23+
1824
## mini.hues
1925

2026
### Expand

doc/mini-clue.txt

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ this module plus all |<Leader>| mappings in Normal and Visual modes: >lua
237237
miniclue.setup({
238238
triggers = {
239239
-- Leader triggers
240-
{ mode = 'n', keys = '<Leader>' },
241-
{ mode = 'x', keys = '<Leader>' },
240+
{ mode = { 'n', 'x' }, keys = '<Leader>' },
242241

243242
-- `[` and `]` keys
244243
{ mode = 'n', keys = '[' },
@@ -248,27 +247,21 @@ this module plus all |<Leader>| mappings in Normal and Visual modes: >lua
248247
{ mode = 'i', keys = '<C-x>' },
249248

250249
-- `g` key
251-
{ mode = 'n', keys = 'g' },
252-
{ mode = 'x', keys = 'g' },
250+
{ mode = { 'n', 'x' }, keys = 'g' },
253251

254252
-- Marks
255-
{ mode = 'n', keys = "'" },
256-
{ mode = 'n', keys = '`' },
257-
{ mode = 'x', keys = "'" },
258-
{ mode = 'x', keys = '`' },
253+
{ mode = { 'n', 'x' }, keys = "'" },
254+
{ mode = { 'n', 'x' }, keys = '`' },
259255

260256
-- Registers
261-
{ mode = 'n', keys = '"' },
262-
{ mode = 'x', keys = '"' },
263-
{ mode = 'i', keys = '<C-r>' },
264-
{ mode = 'c', keys = '<C-r>' },
257+
{ mode = { 'n', 'x' }, keys = '"' },
258+
{ mode = { 'i', 'c' }, keys = '<C-r>' },
265259

266260
-- Window commands
267261
{ mode = 'n', keys = '<C-w>' },
268262

269263
-- `z` key
270-
{ mode = 'n', keys = 'z' },
271-
{ mode = 'x', keys = 'z' },
264+
{ mode = { 'n', 'x' }, keys = 'z' },
272265
},
273266

274267
clues = {
@@ -311,8 +304,7 @@ modes and add descriptions to mapping groups: >lua
311304
require('mini.clue').setup({
312305
-- Register `<Leader>` as trigger
313306
triggers = {
314-
{ mode = 'n', keys = '<Leader>' },
315-
{ mode = 'x', keys = '<Leader>' },
307+
{ mode = { 'n', 'x' }, keys = '<Leader>' },
316308
},
317309

318310
-- Add descriptions for mapping groups
@@ -398,20 +390,15 @@ In this module submode can be implemented following these steps:
398390
triggers = {
399391
-- This can also set up directly `<Leader>m` as a trigger, but make
400392
-- sure to not also use `<Leader>`, as they would "overlap"
401-
{ mode = 'n', keys = '<Leader>' },
402-
{ mode = 'x', keys = '<Leader>' },
393+
{ mode = { 'n', 'x' }, keys = '<Leader>' },
403394
},
404395
clues = {
405396
{ mode = 'n', keys = '<Leader>m', desc = '+Move' },
406397

407-
{ mode = 'n', keys = '<Leader>mh', postkeys = '<Leader>m' },
408-
{ mode = 'n', keys = '<Leader>mj', postkeys = '<Leader>m' },
409-
{ mode = 'n', keys = '<Leader>mk', postkeys = '<Leader>m' },
410-
{ mode = 'n', keys = '<Leader>ml', postkeys = '<Leader>m' },
411-
{ mode = 'x', keys = '<Leader>mh', postkeys = '<Leader>m' },
412-
{ mode = 'x', keys = '<Leader>mj', postkeys = '<Leader>m' },
413-
{ mode = 'x', keys = '<Leader>mk', postkeys = '<Leader>m' },
414-
{ mode = 'x', keys = '<Leader>ml', postkeys = '<Leader>m' },
398+
{ mode = { 'n', 'x' }, keys = '<Leader>mh', postkeys = '<Leader>m' },
399+
{ mode = { 'n', 'x' }, keys = '<Leader>mj', postkeys = '<Leader>m' },
400+
{ mode = { 'n', 'x' }, keys = '<Leader>mk', postkeys = '<Leader>m' },
401+
{ mode = { 'n', 'x' }, keys = '<Leader>ml', postkeys = '<Leader>m' },
415402
},
416403
})
417404
<
@@ -534,8 +521,9 @@ Each element can be one of:
534521
- Callable (function) returning either of the previous two.
535522

536523
A clue table is a table with the following fields:
537-
- <mode> `(string)` - single character describing **single** mode short-name of
538-
key combination as in `nvim_set_keymap()` ('n', 'x', 'i', 'o', 'c', etc.).
524+
- <mode> `(string|table)` - single character describing mode short-name of
525+
key combination as in `nvim_set_keymap()` ('n', 'x', 'i', 'o', 'c', etc.),
526+
or a array thereof.
539527
- <keys> `(string)` - key combination for which clue will be shown.
540528
"Human-readable" key names as in |key-notation| (like "<Leader>", "<Space>",
541529
"<Tab>", etc.) are allowed.
@@ -671,8 +659,7 @@ Generate clues for `g` key
671659

672660
Contains clues for the following triggers: >lua
673661

674-
{ mode = 'n', keys = 'g' }
675-
{ mode = 'x', keys = 'g' }
662+
{ mode = { 'n', 'x' }, keys = 'g' }
676663
<
677664
Return ~
678665
`(table)` Array of clues.
@@ -697,14 +684,10 @@ Generate clues for marks
697684

698685
Contains clues for the following triggers: >lua
699686

700-
{ mode = 'n', keys = "'" }
701-
{ mode = 'n', keys = "g'" }
702-
{ mode = 'n', keys = '`' }
703-
{ mode = 'n', keys = 'g`' }
704-
{ mode = 'x', keys = "'" }
705-
{ mode = 'x', keys = "g'" }
706-
{ mode = 'x', keys = '`' }
707-
{ mode = 'x', keys = 'g`' }
687+
{ mode = { 'n', 'x' }, keys = "'" }
688+
{ mode = { 'n', 'x' }, keys = "g'" }
689+
{ mode = { 'n', 'x' }, keys = '`' }
690+
{ mode = { 'n', 'x' }, keys = 'g`' }
708691
<
709692
Note: if you use "g" as trigger (like to enable |MiniClue.gen_clues.g()|),
710693
don't add "g'" and "g`" as triggers: they already will be taken into account.
@@ -722,10 +705,8 @@ Generate clues for registers
722705

723706
Contains clues for the following triggers: >lua
724707

725-
{ mode = 'n', keys = '"' }
726-
{ mode = 'x', keys = '"' }
727-
{ mode = 'i', keys = '<C-r>' }
728-
{ mode = 'c', keys = '<C-r>' }
708+
{ mode = { 'n', 'x' }, keys = '"' }
709+
{ mode = { 'i', 'c' }, keys = '<C-r>' }
729710
<
730711
Parameters ~
731712
{opts} `(table|nil)` Options. Possible keys:
@@ -769,8 +750,7 @@ Generate clues for `z` key
769750

770751
Contains clues for the following triggers: >lua
771752

772-
{ mode = 'n', keys = 'z' }
773-
{ mode = 'x', keys = 'z' }
753+
{ mode = { 'n', 'x' }, keys = 'z' }
774754
<
775755
Return ~
776756
`(table)` Array of clues.

lua/mini/clue.lua

Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@
233233
--- miniclue.setup({
234234
--- triggers = {
235235
--- -- Leader triggers
236-
--- { mode = 'n', keys = '<Leader>' },
237-
--- { mode = 'x', keys = '<Leader>' },
236+
--- { mode = { 'n', 'x' }, keys = '<Leader>' },
238237
---
239238
--- -- `[` and `]` keys
240239
--- { mode = 'n', keys = '[' },
@@ -244,27 +243,21 @@
244243
--- { mode = 'i', keys = '<C-x>' },
245244
---
246245
--- -- `g` key
247-
--- { mode = 'n', keys = 'g' },
248-
--- { mode = 'x', keys = 'g' },
246+
--- { mode = { 'n', 'x' }, keys = 'g' },
249247
---
250248
--- -- Marks
251-
--- { mode = 'n', keys = "'" },
252-
--- { mode = 'n', keys = '`' },
253-
--- { mode = 'x', keys = "'" },
254-
--- { mode = 'x', keys = '`' },
249+
--- { mode = { 'n', 'x' }, keys = "'" },
250+
--- { mode = { 'n', 'x' }, keys = '`' },
255251
---
256252
--- -- Registers
257-
--- { mode = 'n', keys = '"' },
258-
--- { mode = 'x', keys = '"' },
259-
--- { mode = 'i', keys = '<C-r>' },
260-
--- { mode = 'c', keys = '<C-r>' },
253+
--- { mode = { 'n', 'x' }, keys = '"' },
254+
--- { mode = { 'i', 'c' }, keys = '<C-r>' },
261255
---
262256
--- -- Window commands
263257
--- { mode = 'n', keys = '<C-w>' },
264258
---
265259
--- -- `z` key
266-
--- { mode = 'n', keys = 'z' },
267-
--- { mode = 'x', keys = 'z' },
260+
--- { mode = { 'n', 'x' }, keys = 'z' },
268261
--- },
269262
---
270263
--- clues = {
@@ -307,8 +300,7 @@
307300
--- require('mini.clue').setup({
308301
--- -- Register `<Leader>` as trigger
309302
--- triggers = {
310-
--- { mode = 'n', keys = '<Leader>' },
311-
--- { mode = 'x', keys = '<Leader>' },
303+
--- { mode = { 'n', 'x' }, keys = '<Leader>' },
312304
--- },
313305
---
314306
--- -- Add descriptions for mapping groups
@@ -394,20 +386,15 @@
394386
--- triggers = {
395387
--- -- This can also set up directly `<Leader>m` as a trigger, but make
396388
--- -- sure to not also use `<Leader>`, as they would "overlap"
397-
--- { mode = 'n', keys = '<Leader>' },
398-
--- { mode = 'x', keys = '<Leader>' },
389+
--- { mode = { 'n', 'x' }, keys = '<Leader>' },
399390
--- },
400391
--- clues = {
401392
--- { mode = 'n', keys = '<Leader>m', desc = '+Move' },
402393
---
403-
--- { mode = 'n', keys = '<Leader>mh', postkeys = '<Leader>m' },
404-
--- { mode = 'n', keys = '<Leader>mj', postkeys = '<Leader>m' },
405-
--- { mode = 'n', keys = '<Leader>mk', postkeys = '<Leader>m' },
406-
--- { mode = 'n', keys = '<Leader>ml', postkeys = '<Leader>m' },
407-
--- { mode = 'x', keys = '<Leader>mh', postkeys = '<Leader>m' },
408-
--- { mode = 'x', keys = '<Leader>mj', postkeys = '<Leader>m' },
409-
--- { mode = 'x', keys = '<Leader>mk', postkeys = '<Leader>m' },
410-
--- { mode = 'x', keys = '<Leader>ml', postkeys = '<Leader>m' },
394+
--- { mode = { 'n', 'x' }, keys = '<Leader>mh', postkeys = '<Leader>m' },
395+
--- { mode = { 'n', 'x' }, keys = '<Leader>mj', postkeys = '<Leader>m' },
396+
--- { mode = { 'n', 'x' }, keys = '<Leader>mk', postkeys = '<Leader>m' },
397+
--- { mode = { 'n', 'x' }, keys = '<Leader>ml', postkeys = '<Leader>m' },
411398
--- },
412399
--- })
413400
--- <
@@ -529,8 +516,9 @@ end
529516
--- - Callable (function) returning either of the previous two.
530517
---
531518
--- A clue table is a table with the following fields:
532-
--- - <mode> `(string)` - single character describing **single** mode short-name of
533-
--- key combination as in `nvim_set_keymap()` ('n', 'x', 'i', 'o', 'c', etc.).
519+
--- - <mode> `(string|table)` - single character describing mode short-name of
520+
--- key combination as in `nvim_set_keymap()` ('n', 'x', 'i', 'o', 'c', etc.),
521+
--- or a array thereof.
534522
--- - <keys> `(string)` - key combination for which clue will be shown.
535523
--- "Human-readable" key names as in |key-notation| (like "<Leader>", "<Space>",
536524
--- "<Tab>", etc.) are allowed.
@@ -721,8 +709,7 @@ end
721709
---
722710
--- Contains clues for the following triggers: >lua
723711
---
724-
--- { mode = 'n', keys = 'g' }
725-
--- { mode = 'x', keys = 'g' }
712+
--- { mode = { 'n', 'x' }, keys = 'g' }
726713
--- <
727714
---@return table Array of clues.
728715
MiniClue.gen_clues.g = function()
@@ -874,14 +861,10 @@ end
874861
---
875862
--- Contains clues for the following triggers: >lua
876863
---
877-
--- { mode = 'n', keys = "'" }
878-
--- { mode = 'n', keys = "g'" }
879-
--- { mode = 'n', keys = '`' }
880-
--- { mode = 'n', keys = 'g`' }
881-
--- { mode = 'x', keys = "'" }
882-
--- { mode = 'x', keys = "g'" }
883-
--- { mode = 'x', keys = '`' }
884-
--- { mode = 'x', keys = 'g`' }
864+
--- { mode = { 'n', 'x' }, keys = "'" }
865+
--- { mode = { 'n', 'x' }, keys = "g'" }
866+
--- { mode = { 'n', 'x' }, keys = '`' }
867+
--- { mode = { 'n', 'x' }, keys = 'g`' }
885868
--- <
886869
--- Note: if you use "g" as trigger (like to enable |MiniClue.gen_clues.g()|),
887870
--- don't add "g'" and "g`" as triggers: they already will be taken into account.
@@ -912,28 +895,20 @@ MiniClue.gen_clues.marks = function()
912895

913896
--stylua: ignore
914897
return {
915-
-- Normal mode
916-
describe_marks('n', "'"),
917-
describe_marks('n', "g'"),
918-
describe_marks('n', "`"),
919-
describe_marks('n', "g`"),
920-
921-
-- Visual mode
922-
describe_marks('x', "'"),
923-
describe_marks('x', "g'"),
924-
describe_marks('x', "`"),
925-
describe_marks('x', "g`"),
898+
-- Normal and visual mode
899+
describe_marks({ 'n', 'x' }, "'"),
900+
describe_marks({ 'n', 'x' }, "g'"),
901+
describe_marks({ 'n', 'x' }, "`"),
902+
describe_marks({ 'n', 'x' }, "g`"),
926903
}
927904
end
928905

929906
--- Generate clues for registers
930907
---
931908
--- Contains clues for the following triggers: >lua
932909
---
933-
--- { mode = 'n', keys = '"' }
934-
--- { mode = 'x', keys = '"' }
935-
--- { mode = 'i', keys = '<C-r>' }
936-
--- { mode = 'c', keys = '<C-r>' }
910+
--- { mode = { 'n', 'x' }, keys = '"' }
911+
--- { mode = { 'i', 'c' }, keys = '<C-r>' }
937912
--- <
938913
---@param opts table|nil Options. Possible keys:
939914
--- - <show_contents> `(boolean)` - whether to show contents of all possible
@@ -972,11 +947,8 @@ MiniClue.gen_clues.registers = function(opts)
972947

973948
--stylua: ignore
974949
return {
975-
-- Normal mode
976-
describe_registers('n', '"'),
977-
978-
-- Visual mode
979-
describe_registers('x', '"'),
950+
-- Normal and Visual mode
951+
describe_registers({ 'n', 'x' }, '"'),
980952

981953
-- Insert mode
982954
describe_registers('i', '<C-r>'),
@@ -1084,8 +1056,7 @@ end
10841056
---
10851057
--- Contains clues for the following triggers: >lua
10861058
---
1087-
--- { mode = 'n', keys = 'z' }
1088-
--- { mode = 'x', keys = 'z' }
1059+
--- { mode = { 'n', 'x' }, keys = 'z' }
10891060
--- <
10901061
---@return table Array of clues.
10911062
MiniClue.gen_clues.z = function()
@@ -1328,15 +1299,21 @@ H.map_buf_triggers = function(buf_id)
13281299
if not H.is_valid_buf(buf_id) or H.is_disabled(buf_id) then return end
13291300

13301301
for _, trigger in ipairs(H.get_config(nil, buf_id).triggers) do
1331-
H.map_trigger(buf_id, trigger)
1302+
local modes = type(trigger.mode) == 'table' and trigger.mode or { trigger.mode }
1303+
for _, mode in ipairs(modes) do
1304+
H.map_trigger(buf_id, { mode = mode, keys = trigger.keys })
1305+
end
13321306
end
13331307
end
13341308

13351309
H.unmap_buf_triggers = function(buf_id)
13361310
if not H.is_valid_buf(buf_id) or H.is_disabled(buf_id) then return end
13371311

13381312
for _, trigger in ipairs(H.get_config(nil, buf_id).triggers) do
1339-
H.unmap_trigger(buf_id, trigger)
1313+
local modes = type(trigger.mode) == 'table' and trigger.mode or { trigger.mode }
1314+
for _, mode in ipairs(modes) do
1315+
H.unmap_trigger(buf_id, { mode = mode, keys = trigger.keys })
1316+
end
13401317
end
13411318
end
13421319

@@ -1780,7 +1757,8 @@ H.clues_get_all = function(mode)
17801757

17811758
-- Order of clue precedence: config clues < buffer mappings < global mappings
17821759
local config_clues = H.clues_normalize(H.get_config().clues) or {}
1783-
local mode_clues = vim.tbl_filter(function(x) return x.mode == mode end, config_clues)
1760+
local mode_filter = function(x) return type(x.mode) == 'table' and vim.tbl_contains(x.mode, mode) or x.mode == mode end
1761+
local mode_clues = vim.tbl_filter(mode_filter, config_clues)
17841762
for _, clue in ipairs(mode_clues) do
17851763
local lhsraw = H.replace_termcodes(clue.keys)
17861764

@@ -1949,11 +1927,13 @@ H.make_clues_with_register_contents = function(mode, prefix)
19491927
end
19501928

19511929
-- Predicates -----------------------------------------------------------------
1952-
H.is_trigger = function(x) return type(x) == 'table' and type(x.mode) == 'string' and type(x.keys) == 'string' end
1930+
H.is_trigger = function(x)
1931+
return type(x) == 'table' and (type(x.mode) == 'string' or type(x.mode) == 'table') and type(x.keys) == 'string'
1932+
end
19531933

19541934
H.is_clue = function(x)
19551935
if type(x) ~= 'table' then return false end
1956-
local mandatory = type(x.mode) == 'string' and type(x.keys) == 'string'
1936+
local mandatory = (type(x.mode) == 'string' or type(x.mode) == 'table') and type(x.keys) == 'string'
19571937
local extra = (x.desc == nil or type(x.desc) == 'string' or vim.is_callable(x.desc))
19581938
and (x.postkeys == nil or type(x.postkeys) == 'string')
19591939
return mandatory and extra

0 commit comments

Comments
 (0)