Repository: neovim/neovim
File: runtime/lua/vim/_extui/shared.lua (Lua)

Code before:
        hide = type ~= 'cmd' or M.cmdheight == 0 or nil,
        title = type == 'pager' and 'Pager' or nil,
        border = type == 'msg' and 'single' or border or 'none',
        -- kZIndexMessages < zindex < kZIndexCmdlinePopupMenu (grid_defs.h), pager below others.
        zindex = 200 - (type == 'pager' and 1 or 0),
        _cmdline_offset = type == 'cmd' and 0 or nil,
      })
      if tab ~= curtab and api.nvim_win_is_valid(M.wins[type]) then
        cfg = api.nvim_win_get_config(M.wins[type])
        api.nvim_win_close(M.wins[type], true)
      end
      M.wins[type] = api.nvim_open_win(M.bufs[type], false, cfg)
      if type == 'cmd' then
        api.nvim_win_set_hl_ns(M.wins[type], M.ns)
      end
      setopt = true
    elseif api.nvim_win_get_buf(M.wins[type]) ~= M.bufs[type] then
      api.nvim_win_set_buf(M.wins[type], M.bufs[type])
      setopt = true
    end

    if setopt then
      local name = { cmd = 'Cmd', dialog = 'Dialog', msg = 'Msg', pager = 'Pager' }
      api.nvim_buf_set_name(M.bufs[type], ('[%s]'):format(name[type]))
      if type == 'pager' then
        -- Close pager with `q`, same as `checkhealth`
        api.nvim_buf_set_keymap(M.bufs.pager, 'n', 'q', '<Cmd>wincmd c<CR>', {})
      end

      -- Fire a FileType autocommand with window context to let the user reconfigure local options.
      api.nvim_win_call(M.wins[type], function()
        api.nvim_set_option_value('wrap', true, { scope = 'local' })
        api.nvim_set_option_value('linebreak', false, { scope = 'local' })
        api.nvim_set_option_value('smoothscroll', true, { scope = 'local' })
        local ft = name[type]:sub(1, 1):lower() .. name[type]:sub(2)
        api.nvim_set_option_value('filetype', ft, { scope = 'local' })
        local ignore = 'all' .. (type == 'pager' and ',-TextYankPost' or '')
        api.nvim_set_option_value('eventignorewin', ignore, { scope = 'local' })
      end)
    end
  end
  tab = curtab
end

return M


Review comment (suggestion):
curious why this is necessary given we use `nvim_create_buf(false, true)` to create a "scratch" buffer ? https://github.com/neovim/neovim/blob/927dc3c2c49faeb8be94097d5faa0322928059b1/runtime/lua/vim/_extui/shared.lua#L37

Code after:
        hide = type ~= 'cmd' or M.cmdheight == 0 or nil,
        title = type == 'pager' and 'Pager' or nil,
        border = type == 'msg' and 'single' or border or 'none',
        -- kZIndexMessages < zindex < kZIndexCmdlinePopupMenu (grid_defs.h), pager below others.
        zindex = 200 - (type == 'pager' and 1 or 0),
        _cmdline_offset = type == 'cmd' and 0 or nil,
      })
      if tab ~= curtab and api.nvim_win_is_valid(M.wins[type]) then
        cfg = api.nvim_win_get_config(M.wins[type])
        api.nvim_win_close(M.wins[type], true)
      end
      M.wins[type] = api.nvim_open_win(M.bufs[type], false, cfg)
      if type == 'cmd' then
        api.nvim_win_set_hl_ns(M.wins[type], M.ns)
      end
      setopt = true
    elseif api.nvim_win_get_buf(M.wins[type]) ~= M.bufs[type] then
      api.nvim_win_set_buf(M.wins[type], M.bufs[type])
      setopt = true
    end

    if setopt then
      local name = { cmd = 'Cmd', dialog = 'Dialog', msg = 'Msg', pager = 'Pager' }
      api.nvim_buf_set_name(M.bufs[type], ('[%s]'):format(name[type]))
      api.nvim_set_option_value('swapfile', false, { buf = M.bufs[type] })
      api.nvim_set_option_value('modifiable', true, { buf = M.bufs[type] })
      if type == 'pager' then
        -- Close pager with `q`, same as `checkhealth`
        api.nvim_buf_set_keymap(M.bufs.pager, 'n', 'q', '<Cmd>wincmd c<CR>', {})
      end

      -- Fire a FileType autocommand with window context to let the user reconfigure local options.
      api.nvim_win_call(M.wins[type], function()
        api.nvim_set_option_value('wrap', true, { scope = 'local' })
        api.nvim_set_option_value('linebreak', false, { scope = 'local' })
        api.nvim_set_option_value('smoothscroll', true, { scope = 'local' })
        local ft = name[type]:sub(1, 1):lower() .. name[type]:sub(2)
        api.nvim_set_option_value('filetype', ft, { scope = 'local' })
        local ignore = 'all' .. (type == 'pager' and ',-TextYankPost' or '')
        api.nvim_set_option_value('eventignorewin', ignore, { scope = 'local' })
      end)
    end
  end
  tab = curtab
end

return M