Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keymap.set in on_attach function is always called when buffer entered #166

Closed
adoyle-h opened this issue Oct 20, 2022 · 4 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@adoyle-h
Copy link

adoyle-h commented Oct 20, 2022

Describe the bug

Set buffer keymap in on_attach function will affect all buffers.

To Reproduce

require('aerial').setup({
  on_attach = function(bufnr)
    local aerial = require('aerial')
    require('notify')(string.format('buffer: %s', bufnr)) -- If notify not installed, use `print`

    -- vim.keymap.set('n', '<C-k>', function()
    -- 	aerial.up(-1)
    -- end, { buffer = bufnr })
    --
    -- vim.keymap.set('n', '<C-j>', function()
    -- 	aerial.up(1)
    -- end, { buffer = bufnr })
  end,
})

Screenshots

CleanShot 2022-10-20 at 18 14 51@2x

What to be expected

Only call on_attach after aerial window opened.

Set buffer keymap in on_attach function will only affect aerial buffer.

@adoyle-h adoyle-h added the bug Something isn't working label Oct 20, 2022
@stevearc
Copy link
Owner

Hmmm...perhaps this is a wording issue in the documentation. The on_attach function is called only for the source buffer, not the aerial buffer. It's intended to make it easy to set up keymaps for interacting with aerial once aerial has found a backend and attached to the buffer. If you want to set keymaps for aerial specifically, I'd recommend using a ftplugin/aerial.lua file, or by simply creating a FileType autocmd.

I may make it easier to change bindings in the aerial window in the future.

@adoyle-h
Copy link
Author

I see. Thank you.

@adoyle-h
Copy link
Author

adoyle-h commented Nov 3, 2022

@stevearc I have disabled all plugins but aerial. And set below codes.

vim.api.nvim_create_autocmd('FileType', {
	callback = function(args)
		if (args.match == 'aerial') then print('====', vim.loop.now()) end
	end,
})

I find that the print time is triggered when aerial window opened and cursor moved out aerial window.
But I expected it to print time only when aerial window opened. Is it a bug?

@stevearc
Copy link
Owner

stevearc commented Nov 8, 2022

Not strictly a bug, since the work it's doing should be idempotent, but unnecessary. I've pushed up a change that should prevent the duplicate calls.

Also, you can now set aerial-buffer keymaps in the call to setup(). Check the "keymaps" option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants