sindrets / diffview.nvim

Single tabpage interface for easily cycling through diffs for all modified files for any git rev.
Other
3.93k stars 107 forks source link

Custom `cursorline` in Diffview buffers #388

Closed gegoune closed 1 year ago

gegoune commented 1 year ago

Hey Sindre,

I understand that this isn't an issue with Diffview (no discussions are enabled for that repository hence abusing issue here) but I would greatly appreciate your help, feel free to close it without even reading any further though.

I have a functions tied to some auto commands that make cursorline appear in window with cursor only. I have noticed that it doesn't work for Diffview buffers though. I have tried to catch Diffview buffers in multiple ways but didn't manage to make it work properly. Here is the entirety of the code:

vim.opt.cursorline = true

vim.api.nvim_create_autocmd({ 'InsertLeave', 'WinEnter' }, {
  group = k_wins,
  callback = function()
    local ok, cl = pcall(vim.api.nvim_win_get_var, 0, 'auto-cursorline')
    if ok and cl then
      vim.wo.cursorline = true
      vim.api.nvim_win_del_var(0, 'auto-cursorline')
    end
  end,
})

vim.api.nvim_create_autocmd({ 'InsertEnter', 'WinLeave' }, {
  group = k_wins,
  callback = function(event)
    local cl = vim.wo.cursorline
    if cl and not event.file:match 'NvimTree' then
      vim.api.nvim_win_set_var(0, 'auto-cursorline', cl)
      vim.wo.cursorline = false
    end
  end,
})

I would like cursorline to be always visible in all Diffview buffers, as they are nicely synced between both sides of diff.

Thank you in advance and have a good weekend!

sindrets commented 1 year ago

We have a function used internally to get the current open view. I suppose you could use that to determine whether a window is in a diffview:

vim.api.nvim_create_autocmd({ 'InsertEnter', 'WinLeave' }, {
  callback = function(event)
    if require("diffview.lib").get_current_view() then return end
    --- ...
  end,
})

Thank you in advance and have a good weekend!

Thank you, you too 😄

sindrets commented 1 year ago

(no discussions are enabled for that repository hence abusing issue here)

This is the right place to ask questions. The "Discussions" feature isn't enabled because I just don't want to spread out discussion over multiple places. I think issues work more than fine for questions, when they're labeled as such. Issues can easily be filtered and searched. And this way I keep all discussion in one place.

gegoune commented 1 year ago

Thanks for the hint, I still wasn't able to get it to work as is would like to. No need to keep that issue opened. Thanks!

sindrets commented 1 year ago

If I correctly understood what you're trying to do, this seems to work for me:

vim.opt.cursorline = false;

vim.api.nvim_create_autocmd({ "WinLeave", "BufLeave" }, {
  callback = function(e)
    local dv_lib = require("diffview.lib");
    if vim.wo.diff or dv_lib.get_current_view() then return end
    vim.opt_local.cursorline = nil
  end,
})

vim.api.nvim_create_autocmd({ "WinEnter", "BufEnter" }, {
  callback = function(e)
    vim.opt_local.cursorline = true
  end,
})
gegoune commented 1 year ago

That is incredibly kind of you! Thank you for taking your time to solve someone else's configuration issue! You are awesome!