summaryrefslogtreecommitdiff
path: root/mut/neovim/pack/plugins/start/quicker.nvim/lua/quicker/cursor.lua
blob: 58ee587b18b27f81e14b619f7a519c2623bc9e4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
local M = {}

local function constrain_cursor()
  local display = require("quicker.display")
  local cur = vim.api.nvim_win_get_cursor(0)
  local line = vim.api.nvim_buf_get_lines(0, cur[1] - 1, cur[1], true)[1]
  local idx = line:find(display.EM_QUAD, 1, true)
  if not idx then
    return
  end
  local min_col = idx + display.EM_QUAD_LEN - 1
  if cur[2] < min_col then
    vim.api.nvim_win_set_cursor(0, { cur[1], min_col })
  end
end

---@param bufnr number
function M.constrain_cursor(bufnr)
  -- HACK: we have to defer this call because sometimes the autocmds don't take effect.
  vim.schedule(function()
    if not vim.api.nvim_buf_is_valid(bufnr) then
      return
    end
    local aug = vim.api.nvim_create_augroup("quicker", { clear = false })
    vim.api.nvim_create_autocmd("InsertEnter", {
      desc = "Constrain quickfix cursor position",
      group = aug,
      nested = true,
      buffer = bufnr,
      -- For some reason the cursor bounces back to its original position,
      -- so we have to defer the call
      callback = vim.schedule_wrap(constrain_cursor),
    })
    vim.api.nvim_create_autocmd({ "CursorMoved", "ModeChanged" }, {
      desc = "Constrain quickfix cursor position",
      nested = true,
      group = aug,
      buffer = bufnr,
      callback = constrain_cursor,
    })
  end)
end

return M