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

terminal :enew buffer should set buftype=terminal before BufWinEnter autocmd #29419

Open
stringTrimmer opened this issue Jun 19, 2024 · 2 comments
Labels
enhancement feature request terminal built-in :terminal or :shell
Milestone

Comments

@stringTrimmer
Copy link

Problem

I have some code I'd like to run when most buffers enter a window, so I create an autocmd for BufWinEnter. But there are some buffers on which I do not want to run this code. I can exclude most of these cases by checking the filetype or buftype. One case that I'd like to exclude is terminal buffers. However when a terminal buffer is created via :terminal and my BufWinEnter autocmd is executed the first time, the buftype is still empty. I've also checked other terminal buffer related variables: b:term_title, the channel local option and getwininfo(buf).[1].terminal, but these are also empty during the initial BufWinEnter. Note: they are present in subsequent BufWinEnters.

This might be fine if I wanted to avoid this code running for any buffer that has empty buftype, filetype, event.file, event.match (as terminal buffers do during initial BufWinEnter), but I do want this code to run on normal new buffers like with :enew (which have the same state).

My current hack is to run the code with vim.defer_fn() after x ms, after which buftype has usually then been set to 'terminal'.

Steps to reproduce:

  1. Write the below code to a file 'test.lua'
  2. nvim --clean test.lua
  3. :source %
  4. check the messages to see if buftype et al. have values initially and after the delay.
vim.api.nvim_create_autocmd({ 'BufWinEnter' }, {
  group = vim.api.nvim_create_augroup('DetectTerminalBufWin', { clear = true }),
  desc = 'Find a way to detect if this bufer is (or is going to be) a terminal buffer',
  pattern = '*',
  callback = function(evt)
    vim.print(
      evt.event,
      ('filetype:%s'):format(vim.bo[evt.buf].filetype),
      ('buftype:%s'):format(vim.bo[evt.buf].buftype),
      ('b:term_title:%s'):format(vim.b.term_title),
      ('channel:%s'):format(vim.bo.channel),
      ('getwininfo.terminal:%s'):format(vim.fn.getwininfo(vim.api.nvim_get_current_win())[1].terminal)
    )

    if evt.file == '' and evt.match == '' and vim.bo[evt.buf].filetype == '' and vim.bo[evt.buf].buftype == '' then
      vim.defer_fn(
        function()
          vim.print(
            ' ',
            'defered',
            ('filetype:%s'):format(vim.bo[evt.buf].filetype),
            ('buftype:%s'):format(vim.bo[evt.buf].buftype),
            ('b:term_title:%s'):format(vim.b.term_title),
            ('channel:%s'):format(vim.bo.channel),
            ('getwininfo.terminal:%s'):format(vim.fn.getwininfo(vim.api.nvim_get_current_win())[1].terminal)
          )
        end,
        200
      )
    end
  end,
})
vim.cmd.terminal()

System: Windows 11
nvim: v0.11.0

Expected behavior

Either buftype is set to 'terminal' prior to initial BufWinEnter autocmds or there is some other variable/mechanism to detect a terminal buffer at this time.

@stringTrimmer stringTrimmer added the enhancement feature request label Jun 19, 2024
@stringTrimmer stringTrimmer changed the title A way to detect weather a buffer is (or is going to be) a terminal buffer during a BufWinEnter autocmd A way to detect whether a buffer is (or is going to be) a terminal buffer during a BufWinEnter autocmd Jun 19, 2024
@zeertzjq zeertzjq added the terminal built-in :terminal or :shell label Jun 20, 2024
@justinmk justinmk changed the title A way to detect whether a buffer is (or is going to be) a terminal buffer during a BufWinEnter autocmd terminal buffer should set buftype=terminal before BufWinEnter autocmd Jun 20, 2024
@justinmk justinmk added this to the backlog milestone Jun 20, 2024
@justinmk
Copy link
Member

justinmk commented Jun 20, 2024

when a terminal buffer is created via :terminal and my BufWinEnter autocmd is executed the first time, the buftype is still empty. I've also checked other terminal buffer related variables: b:term_title, the channel local option and getwininfo(buf).[1].terminal, but these are also empty during the initial BufWinEnter

Surely the name is set? Terminal buffer name always starts with term:// (though it can be renamed later).

@zeertzjq
Copy link
Member

BufWinEnter is triggered by the :enew, not the termopen().

@justinmk justinmk changed the title terminal buffer should set buftype=terminal before BufWinEnter autocmd terminal :enew buffer should set buftype=terminal before BufWinEnter autocmd Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement feature request terminal built-in :terminal or :shell
Projects
None yet
Development

No branches or pull requests

3 participants