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

Code before:
    return
  end

  if cmd_on_key then
    -- Dismiss a still expanded cmdline.
    api.nvim_feedkeys(vim.keycode('<CR>'), 'n', false)
  elseif prev_cmd then
    -- Showing output of previous command, clear in case still visible.
    M.msg_clear()
  end

  api.nvim_buf_set_lines(ui.bufs.pager, 0, -1, false, {})
  for i, entry in ipairs(entries) do
    M.show_msg('pager', entry[2], i == 1, entry[3], 0)
  end

  M.set_pos('pager')
end

--- Adjust visibility and dimensions of the message windows after certain events.
---
---@param type? 'cmd'|'dialog'|'msg'|'pager' Type of to be positioned window (nil for all).
function M.set_pos(type)
  local function win_set_pos(win)
    local cfg = { hide = false, relative = 'laststatus', col = 10000 }
    local texth = type and api.nvim_win_text_height(win, {}) or {}
    local top = { vim.opt.fcs:get().msgsep or ' ', 'MsgSeparator' }
    cfg.height = type and math.min(texth.all, math.ceil(o.lines * 0.5))
    cfg.border = win ~= ui.wins.msg and { '', top, '', '', '', '', '', '' } or nil
    cfg.focusable = type == 'cmd' or nil
    cfg.row = (win == ui.wins.msg and 0 or 1) - ui.cmd.wmnumode
    cfg.row = cfg.row - ((win == ui.wins.pager and o.laststatus == 3) and 1 or 0)
    api.nvim_win_set_config(win, cfg)

    if type == 'cmd' and not cmd_on_key then
      -- Temporarily expand the cmdline, until next key press.
      local save_spill = M.virt.msg[M.virt.idx.spill][1]
      local spill = texth.all > cfg.height and (' [+%d]'):format(texth.all - cfg.height)
      M.virt.msg[M.virt.idx.spill][1] = spill and { 0, spill } or nil
      set_virttext('msg', 'cmd')
      M.virt.msg[M.virt.idx.spill][1] = save_spill
      cmd_on_key = vim.on_key(function(_, typed)
        if not typed or fn.keytrans(typed) == '<MouseMove>' then
          return
        end
        vim.schedule(function()
          local entered = api.nvim_get_current_win() == ui.wins.cmd
          cmd_on_key = nil
          if api.nvim_win_is_valid(ui.wins.cmd) then
            api.nvim_win_close(ui.wins.cmd, true)
          end

Review comment (suggestion):
off topic, probably shouldn't shadow the `type` builtin? surprised that luals didn't flag this.

```suggestion
    local texth = type_ and api.nvim_win_text_height(win, {}) or {}
```

Code after:
    return
  end

  if cmd_on_key then
    -- Dismiss a still expanded cmdline.
    api.nvim_feedkeys(vim.keycode('<CR>'), 'n', false)
  elseif prev_cmd then
    -- Showing output of previous command, clear in case still visible.
    M.msg_clear()
  end

  api.nvim_buf_set_lines(ui.bufs.pager, 0, -1, false, {})
  for i, entry in ipairs(entries) do
    M.show_msg('pager', entry[2], i == 1, entry[3], 0)
  end

  M.set_pos('pager')
end

--- Adjust visibility and dimensions of the message windows after certain events.
---
---@param type? 'cmd'|'dialog'|'msg'|'pager' Type of to be positioned window (nil for all).
function M.set_pos(type)
  local function win_set_pos(win)
    local cfg = { hide = false, relative = 'laststatus', col = 10000 }
    local texth = type and api.nvim_win_text_height(win, {}) or {}
    local top = { vim.opt.fcs:get().msgsep or ' ', 'MsgSeparator' }
    cfg.height = type == 'pager' and texth.all
      or type and math.min(texth.all, math.ceil(o.lines * 0.5))
    cfg.border = win ~= ui.wins.msg and { '', top, '', '', '', '', '', '' } or nil
    cfg.focusable = type == 'cmd' or nil
    cfg.row = (win == ui.wins.msg and 0 or 1) - ui.cmd.wmnumode
    cfg.row = cfg.row - ((win == ui.wins.pager and o.laststatus == 3) and 1 or 0)
    api.nvim_win_set_config(win, cfg)

    if type == 'cmd' and not cmd_on_key then
      -- Temporarily expand the cmdline, until next key press.
      local save_spill = M.virt.msg[M.virt.idx.spill][1]
      local spill = texth.all > cfg.height and (' [+%d]'):format(texth.all - cfg.height)
      M.virt.msg[M.virt.idx.spill][1] = spill and { 0, spill } or nil
      set_virttext('msg', 'cmd')
      M.virt.msg[M.virt.idx.spill][1] = save_spill
      cmd_on_key = vim.on_key(function(_, typed)
        typed = typed and fn.keytrans(typed)
        if not typed or typed == '<MouseMove>' then
          return
        end

        vim.schedule(function()
          local entered = typed == '<CR>' or api.nvim_get_current_win() == ui.wins.cmd
          cmd_on_key = nil