j-morano / buffer_manager.nvim

A simple plugin to easily manage Neovim buffers.
MIT License
241 stars 12 forks source link

navigation only for open buffers? #38

Open daUnknownCoder opened 4 months ago

daUnknownCoder commented 4 months ago

hi, i was trying to setup the <leader>%s keys in 1-10 like this:

  {
    "j-morano/buffer_manager.nvim",
    lazy = true,
    event = { "BufAdd", "BufDelete", "BufEnter" },
    keys = {
      { ",,", "<cmd>lua require('buffer_manager.ui').toggle_quick_menu()<CR>", desc = "Buffer Manager" },
    },
    config = function()
      local bm_status_ok, bm = pcall(require, "buffer_manager")
      if not bm_status_ok then
        print("Buffer Manager not found!")
      end
      bm.setup({
        select_menu_item_commands = {
          edit = { key = "<CR>", command = "edit" },
          v = { key = "<C-v>", command = "vsplit" },
          h = { key = "<C-h>", command = "split" },
        },
        short_file_names = true,
        short_term_names = true,
      })
      local keys = "1234567890"
      local bmui = require("buffer_manager.ui")
      for i = 1, #keys do
        local key = keys:sub(i, i)
        local desc = "Switch Buffer: " .. (i == 10 and "10" or key)
        vim.keymap.set({ "t", "n" }, string.format(",%s", key), function()
          bmui.nav_file(i)
        end, { desc = desc, noremap = true })
      end
      vim.keymap.set({ "t", "n" }, ",s", function()
        bmui.toggle_quick_menu()
        vim.defer_fn(function()
          vim.fn.feedkeys("/")
        end, 50)
      end, { desc = "Search Buffer", noremap = true })
      vim.api.nvim_set_hl(0, "BufferManagerModified", { fg = "#50fa7b" })
    end,
  },

but it registers 10 keys even though i only have 3 buffers which is not feasible, so i got chatgpt to work and it got me something like this:

local function count_buffers()
  local count = 1
  for _, buffer in ipairs(vim.api.nvim_list_bufs()) do
    if vim.api.nvim_buf_is_loaded(buffer) then
      count = count + 1
    end
  end
  return count
end

local buffer_count = count_buffers()

for i = 1, buffer_count do
  local key = keys:sub(i, i)
  vim.keymap.set({ "t", "n" }, string.format(",%s", key), function()
    bmui.nav_file(i)
  end, { desc = "Switch Buffer: " .. key, noremap = true })
end

so it does work but i use which-key.nvim which doesnt update so can i get something to update that?

chatgpt didnt work this time sadly 😢