termcap.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. local M = {}
  2. --- Query the host terminal emulator for terminfo capabilities.
  3. ---
  4. --- This function sends the XTGETTCAP DCS sequence to the host terminal emulator asking the terminal
  5. --- to send us its terminal capabilities. These are strings that are normally taken from a terminfo
  6. --- file, however an up to date terminfo database is not always available (particularly on remote
  7. --- machines), and many terminals continue to misidentify themselves or do not provide their own
  8. --- terminfo file, making the terminfo database unreliable.
  9. ---
  10. --- Querying the terminal guarantees that we get a truthful answer, but only if the host terminal
  11. --- emulator supports the XTGETTCAP sequence.
  12. ---
  13. --- @param caps string|table A terminal capability or list of capabilities to query
  14. --- @param cb fun(cap:string, found:boolean, seq:string?) Callback function which is called for
  15. --- each capability in {caps}. {found} is set to true if the capability was found or false
  16. --- otherwise. {seq} is the control sequence for the capability if found, or nil for
  17. --- boolean capabilities.
  18. function M.query(caps, cb)
  19. vim.validate('caps', caps, { 'string', 'table' })
  20. vim.validate('cb', cb, 'function')
  21. if type(caps) ~= 'table' then
  22. caps = { caps }
  23. end
  24. local pending = {} ---@type table<string, boolean>
  25. for _, v in ipairs(caps) do
  26. pending[v] = true
  27. end
  28. local timer = assert(vim.uv.new_timer())
  29. local id = vim.api.nvim_create_autocmd('TermResponse', {
  30. nested = true,
  31. callback = function(args)
  32. local resp = args.data ---@type string
  33. local k, rest = resp:match('^\027P1%+r(%x+)(.*)$')
  34. if k and rest then
  35. local cap = vim.text.hexdecode(k)
  36. if not cap or not pending[cap] then
  37. -- Received a response for a capability we didn't request. This can happen if there are
  38. -- multiple concurrent XTGETTCAP requests
  39. return
  40. end
  41. local seq ---@type string?
  42. if rest:match('^=%x+$') then
  43. seq = vim.text
  44. .hexdecode(rest:sub(2))
  45. :gsub('\\E', '\027')
  46. :gsub('%%p%d', '')
  47. :gsub('\\(%d+)', string.char)
  48. end
  49. cb(cap, true, seq)
  50. pending[cap] = nil
  51. if next(pending) == nil then
  52. return true
  53. end
  54. end
  55. end,
  56. })
  57. local encoded = {} ---@type string[]
  58. for i = 1, #caps do
  59. encoded[i] = vim.text.hexencode(caps[i])
  60. end
  61. local query = string.format('\027P+q%s\027\\', table.concat(encoded, ';'))
  62. -- If running in tmux, wrap with the passthrough sequence
  63. if os.getenv('TMUX') then
  64. query = string.format('\027Ptmux;%s\027\\', query:gsub('\027', '\027\027'))
  65. end
  66. io.stdout:write(query)
  67. timer:start(1000, 0, function()
  68. -- Delete the autocommand if no response was received
  69. vim.schedule(function()
  70. -- Suppress error if autocommand has already been deleted
  71. pcall(vim.api.nvim_del_autocmd, id)
  72. -- Call the callback for all capabilities that were not found
  73. for k in pairs(pending) do
  74. cb(k, false, nil)
  75. end
  76. end)
  77. if not timer:is_closing() then
  78. timer:close()
  79. end
  80. end)
  81. end
  82. return M