telescope.nvim icon indicating copy to clipboard operation
telescope.nvim copied to clipboard

feat(pickers): default attach_mappings for all pickers

Open evertonse opened this issue 1 year ago • 2 comments

Description

Allows attach_mappings function to be defined in telescope.defaults that will be used as default for all pickers If you define attach_mappings on specific picker, that'll be used instead.

Use case is that it was needed to perform some operations using prompt_bufnr as soon as the picker started. It was ideal to perform that for all pickers and just override when necessary, but saw no way of doing that. Example:

-- Defaulting to turn off winbleding specifically on the prompt window, (everything else was on windblend 20)
      ...
      defaults = {

        attach_mappings = function(prompt_bufnr, map)
          local prompt_win = vim.fn.bufwinid(prompt_bufnr)
          if prompt_win ~= -1 then
            vim.schedule(function()
              vim.api.nvim_win_set_option(prompt_win, 'winblend', 0) -- Set the desired winblend for the prompt window
            end)
          end
          return true
        end,
      }
      ...
        

Testing

Should see 'We are balling' when a picker is opened.

require('telescope').setup {
  defaults = {
    attach_mappings = function(prompt_bufnr, map)
      local prompt_win = vim.fn.bufwinid(prompt_bufnr)
      if prompt_win ~= -1 then
        vim.schedule(function()
          print 'We are balling'
        end)
      end
      return true
    end,
  },
}

evertonse avatar Jul 10 '24 05:07 evertonse

attach_mappings SHOULD not be used like this! it would make more sense to consistently support user autocmds for all points where ppl want to inject custom functions OR implement a custom event system. depending on our needs. We have this on our roadmap but no deadline when this will be implemented

Conni2461 avatar Jul 10 '24 06:07 Conni2461

attach_mappings SHOULD not be used like this! it would make more sense to consistently support user autocmds for all points where ppl want to inject custom functions OR implement a custom event system. depending on our needs. We have this on our roadmap but no deadline when this will be implemented

If a default attach_mappings isn't welcome, maybe something like on_attach hook would go a long way. Allowing specification of a default hook for every picker and optionally on a specific picker to override behaviour.

evertonse avatar Jul 14 '24 03:07 evertonse