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

Code before:
end

function M.msg_history_clear() end

--- Adjust dimensions of the message windows after certain events.
---
---@param type? 'box'|'cmd'|'more'|'prompt' Type of to be positioned window (nil for all).
function M.set_pos(type)
  local function win_set_pos(win)
    local texth = type and api.nvim_win_text_height(win, {}) or 0
    local height = type and math.min(texth.all, math.ceil(o.lines * 0.5))
    api.nvim_win_set_config(win, {
      hide = false,
      relative = 'laststatus',
      height = height,
      row = win == ext.wins[ext.tab].box and 0 or 1,
      col = 10000,
    })
    if type == 'box' then
      -- Ensure last line is visible and first line is at top of window.
      local row = (texth.all > height and texth.end_row or 0) + 1
      api.nvim_win_set_cursor(ext.wins[ext.tab].box, { row, 0 })
    elseif type == 'more' and api.nvim_get_current_win() ~= win then
      -- Cannot leave the cmdwin to enter the "more" window, so close it.
      if fn.getcmdwintype() ~= '' then
        api.nvim_win_close(0, true)
      end
      -- It's actually closed one event iteration later so schedule in case it was open.
      vim.schedule(function()
        api.nvim_create_autocmd('WinEnter', {
          once = true,
          callback = function()
            if api.nvim_win_is_valid(win) then
              api.nvim_win_set_config(win, { hide = true })
            end
            append_more = 0
          end,
          desc = 'Hide inactive "more" window.',
        })
        api.nvim_set_current_win(win)
      end)
    end
  end

  for t, win in pairs(ext.wins[ext.tab] or {}) do
    local cfg = (t == type or (type == nil and t ~= 'cmd'))
      and api.nvim_win_is_valid(win)
      and api.nvim_win_get_config(win)
    if cfg and (type or not cfg.hide) then
      win_set_pos(win)
    end

Review comment (suggestion):
A _slightly_ pedantic nitpick (sorry! :grin:), but it's possible for the cmdwin to not be curwin (but only temporarily).

E.g: (run this one manually)

```vim
lua require'vim._extui'.enable{}
let s:w = win_getid()
call feedkeys('q:', 'nt')
au CmdwinEnter * ++once call win_execute(s:w, 'messages')
```

_Should_ be able to get around that by just using a plain `:quit` instead (it prioritizes the cmdwin regardless of curwin), but looping through the list of windows to `nvim_win_close` the one with `win_gettype() == "command"` may be more robust.

Yeah, the cmdwin is a bit jank...

Code after:
end

function M.msg_history_clear() end

--- Adjust dimensions of the message windows after certain events.
---
---@param type? 'box'|'cmd'|'more'|'prompt' Type of to be positioned window (nil for all).
function M.set_pos(type)
  local function win_set_pos(win)
    local texth = type and api.nvim_win_text_height(win, {}) or 0
    local height = type and math.min(texth.all, math.ceil(o.lines * 0.5))
    api.nvim_win_set_config(win, {
      hide = false,
      relative = 'laststatus',
      height = height,
      row = win == ext.wins[ext.tab].box and 0 or 1,
      col = 10000,
    })
    if type == 'box' then
      -- Ensure last line is visible and first line is at top of window.
      local row = (texth.all > height and texth.end_row or 0) + 1
      api.nvim_win_set_cursor(ext.wins[ext.tab].box, { row, 0 })
    elseif type == 'more' and api.nvim_get_current_win() ~= win then
      -- Cannot leave the cmdwin to enter the "more" window, so close it.
      -- NOTE: regression w.r.t. the message grid, which allowed this. Resolving
      -- that would require somehow bypassing textlock for the "more" window.
      if fn.getcmdwintype() ~= '' then
        api.nvim_command('quit')
      end
      -- It's actually closed one event iteration later so schedule in case it was open.
      vim.schedule(function()
        api.nvim_create_autocmd('WinEnter', {
          once = true,
          callback = function()
            if api.nvim_win_is_valid(win) then
              api.nvim_win_set_config(win, { hide = true })
            end
            append_more = 0
          end,
          desc = 'Hide inactive "more" window.',
        })
        api.nvim_set_current_win(win)
      end)
    end
  end

  for t, win in pairs(ext.wins[ext.tab] or {}) do
    local cfg = (t == type or (type == nil and t ~= 'cmd'))
      and api.nvim_win_is_valid(win)
      and api.nvim_win_get_config(win)
    if cfg and (type or not cfg.hide) then