diff --git a/lua/mini/clue.lua b/lua/mini/clue.lua index 0cb0d1898..e1a1141ae 100644 --- a/lua/mini/clue.lua +++ b/lua/mini/clue.lua @@ -670,6 +670,26 @@ MiniClue.set_mapping_desc = function(mode, lhs, desc) if not ok_set then H.error(vim.inspect(desc) .. ' is not a valid description.') end end +MiniClue.start_query_process = function(trigger) + -- Don't act if for some reason entered another trigger is already active + local is_in_exec = type(H.exec_trigger) == 'table' + and H.exec_trigger.mode == trigger.mode + and H.exec_trigger.keys == trigger.keys + if is_in_exec then + H.exec_trigger = nil + return + end + + -- Start user query + H.state_set(trigger, { trigger.keys }) + + -- Do not advance if no other clues to query. NOTE: it is `<= 1` and not + -- `<= 0` because the "init query" mapping should match. + if vim.tbl_count(H.state.clues) <= 1 then return H.state_exec() end + + H.state_advance() +end + --- Generate pre-configured clues --- --- This is a table with function elements. Call to actually get array of clues. @@ -1246,43 +1266,23 @@ H.unmap_buf_triggers = function(buf_id) end H.map_trigger = function(buf_id, trigger) - if not H.is_valid_buf(buf_id) then return end + if not H.is_valid_buf(buf_id) or trigger.keys == '' then return end -- Compute mapping RHS trigger.keys = H.replace_termcodes(trigger.keys) local keys_trans = H.keytrans(trigger.keys) - local rhs = function() - -- Don't act if for some reason entered the same trigger during state exec - local is_in_exec = type(H.exec_trigger) == 'table' - and H.exec_trigger.mode == trigger.mode - and H.exec_trigger.keys == trigger.keys - if is_in_exec then - H.exec_trigger = nil - return - end - - -- Start user query - H.state_set(trigger, { trigger.keys }) - - -- Do not advance if no other clues to query. NOTE: it is `<= 1` and not - -- `<= 0` because the "init query" mapping should match. - if vim.tbl_count(H.state.clues) <= 1 then return H.state_exec() end - - H.state_advance() - end - -- Use buffer-local mappings and `nowait` to make it a primary source of -- keymap execution local desc = string.format('Query keys after "%s"', keys_trans) local opts = { buffer = buf_id, nowait = true, desc = desc } -- Create mapping. Use translated variant to make it work with keys. - vim.keymap.set(trigger.mode, keys_trans, rhs, opts) + vim.keymap.set(trigger.mode, keys_trans, function() MiniClue.start_query_process(trigger) end, opts) end H.unmap_trigger = function(buf_id, trigger) - if not H.is_valid_buf(buf_id) then return end + if not H.is_valid_buf(buf_id) or trigger.keys == '' then return end trigger.keys = H.replace_termcodes(trigger.keys) @@ -1438,8 +1438,11 @@ H.compute_exec_keys = function() -- Using `feedkeys()` inside Operator-pending mode leads to its cancel into -- Normal/Insert mode so extra work should be done to rebuild all keys if vim.startswith(cur_mode, 'no') then - local operator_tweak = H.operator_tweaks[vim.v.operator] or function(x) return x end - res = operator_tweak(vim.v.operator .. H.get_forced_submode() .. res) + res = H.get_forced_submode() .. res + if H.state.trigger.keys ~= '' then + local operator_tweak = H.operator_tweaks[vim.v.operator] or function(x) return x end + res = operator_tweak(vim.v.operator .. res) + end elseif not vim.startswith(cur_mode, 'i') and H.get_default_register() ~= vim.v.register then -- Force non-default register but not in Insert mode res = '"' .. vim.v.register .. res