highlight.lua 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. local api = vim.api
  2. local highlight = {}
  3. ---@private
  4. function highlight.create(higroup, hi_info, default)
  5. local options = {}
  6. -- TODO: Add validation
  7. for k, v in pairs(hi_info) do
  8. table.insert(options, string.format("%s=%s", k, v))
  9. end
  10. vim.cmd(string.format([[highlight %s %s %s]], default and "default" or "", higroup, table.concat(options, " ")))
  11. end
  12. ---@private
  13. function highlight.link(higroup, link_to, force)
  14. vim.cmd(string.format([[highlight%s link %s %s]], force and "!" or " default", higroup, link_to))
  15. end
  16. --- Highlight range between two positions
  17. ---
  18. ---@param bufnr number of buffer to apply highlighting to
  19. ---@param ns namespace to add highlight to
  20. ---@param higroup highlight group to use for highlighting
  21. ---@param rtype type of range (:help setreg, default charwise)
  22. ---@param inclusive boolean indicating whether the range is end-inclusive (default false)
  23. function highlight.range(bufnr, ns, higroup, start, finish, rtype, inclusive)
  24. rtype = rtype or 'v'
  25. inclusive = inclusive or false
  26. -- sanity check
  27. if start[2] < 0 or finish[1] < start[1] then return end
  28. local region = vim.region(bufnr, start, finish, rtype, inclusive)
  29. for linenr, cols in pairs(region) do
  30. api.nvim_buf_add_highlight(bufnr, ns, higroup, linenr, cols[1], cols[2])
  31. end
  32. end
  33. local yank_ns = api.nvim_create_namespace('hlyank')
  34. --- Highlight the yanked region
  35. ---
  36. --- use from init.vim via
  37. --- au TextYankPost * lua vim.highlight.on_yank()
  38. --- customize highlight group and timeout via
  39. --- au TextYankPost * lua vim.highlight.on_yank {higroup="IncSearch", timeout=150}
  40. --- customize conditions (here: do not highlight a visual selection) via
  41. --- au TextYankPost * lua vim.highlight.on_yank {on_visual=false}
  42. ---
  43. -- @param opts dictionary with options controlling the highlight:
  44. -- - higroup highlight group for yanked region (default "IncSearch")
  45. -- - timeout time in ms before highlight is cleared (default 150)
  46. -- - on_macro highlight when executing macro (default false)
  47. -- - on_visual highlight when yanking visual selection (default true)
  48. -- - event event structure (default vim.v.event)
  49. function highlight.on_yank(opts)
  50. vim.validate {
  51. opts = { opts,
  52. function(t) if t == nil then return true else return type(t) == 'table' end end,
  53. 'a table or nil to configure options (see `:h highlight.on_yank`)',
  54. }}
  55. opts = opts or {}
  56. local event = opts.event or vim.v.event
  57. local on_macro = opts.on_macro or false
  58. local on_visual = (opts.on_visual ~= false)
  59. if (not on_macro) and vim.fn.reg_executing() ~= '' then return end
  60. if event.operator ~= 'y' or event.regtype == '' then return end
  61. if (not on_visual) and event.visual then return end
  62. local higroup = opts.higroup or "IncSearch"
  63. local timeout = opts.timeout or 150
  64. local bufnr = api.nvim_get_current_buf()
  65. api.nvim_buf_clear_namespace(bufnr, yank_ns, 0, -1)
  66. local pos1 = vim.fn.getpos("'[")
  67. local pos2 = vim.fn.getpos("']")
  68. pos1 = {pos1[2] - 1, pos1[3] - 1 + pos1[4]}
  69. pos2 = {pos2[2] - 1, pos2[3] - 1 + pos2[4]}
  70. highlight.range(bufnr, yank_ns, higroup, pos1, pos2, event.regtype, event.inclusive)
  71. vim.defer_fn(
  72. function()
  73. if api.nvim_buf_is_valid(bufnr) then
  74. api.nvim_buf_clear_namespace(bufnr, yank_ns, 0, -1)
  75. end
  76. end,
  77. timeout
  78. )
  79. end
  80. return highlight