feat(lua): vim.keycode() gets structured parse result #38636

Problems:
1. Can't get individual parts of a key-chord separately: modifiers, key.
2. Can't separate a key-combo into individual key-chords.

Solution:
Enhance `vim.keycode()` to optionally return a structured parse result
as a list of key-chords:
- `key_raw` the key-chord (problem 2)
- `mod` the modifiers of `key_raw` (problem 1)
- `key_orig` the key part of the key-chord, only here if differing from `key`
  (this doesn't solve any of the above mentioned problems, but it may provide
  useful and it's (in terms of code) free)
- `key` a normalized version of `key_orig` (solving problem 1), example(the
  first is `key_orig` and second is `key`): `lt` and `<`, `Bar` and `|` (in
  `<C-Bar>`)
This commit is contained in:
altermo
2026-07-18 18:20:41 +02:00
committed by GitHub
parent 952e59347e
commit a2dfa195b2
14 changed files with 263 additions and 35 deletions

View File

@@ -1260,6 +1260,23 @@ function vim.print(...)
return vim._print(false, ...)
end
--- @class vim.keycode.info
--- @inlinedoc
---
--- The key chord normalized without modifiers.
--- @field key string
---
--- Non-normalized version of key chord,
--- only present when it differs from {key}.
--- Example: `lt` and `<`.
--- @field key_orig string?
---
--- The original key chord with modifiers.
--- @field key_raw string
---
--- A list of single character modifiers of the key.
--- @field mod ('M'|'T'|'C'|'S'|'2'|'3'|'4'|'D')[]
--- Translates keycodes.
---
--- Example:
@@ -1269,11 +1286,21 @@ end
--- vim.g.mapleader = k'<bs>'
--- ```
---
---
--- @param str string String to be converted.
--- @param info boolean? Also returns a normalized
--- detailed info about the key as a second return value.
--- @return string
--- @return vim.keycode.info[]? List of dicts where
--- each dict represents a key chord, and has fields:
--- @see |nvim_replace_termcodes()|
function vim.keycode(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
function vim.keycode(str, info)
if info then
local keycode = vim.keycode(str)
return keycode, vim._keyarg(keycode)
else
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
end
--- @param server_addr string