Repository: neovim/neovim
File: runtime/lua/man.lua (Lua)

Code before:
  if fn.getline(1):match('^%s*$') then
    api.nvim_buf_set_lines(0, 0, 1, false, {})
  else
    vim.cmd('keepjumps 1')
  end
  highlight_man_page()
  -- Guess the ref from the heading (which is usually uppercase, so we cannot
  -- know the correct casing, cf. `man glDrawArraysInstanced`).
  --- @type string
  local ref = (fn.getline(1):match('^[^)]+%)') or ''):gsub(' ', '_')
  local _, sect, err = pcall(parse_ref, ref)
  vim.b.man_sect = err ~= nil and sect or ''

  local man_bufname = 'man://' .. fn.fnameescape(ref):lower()

  -- Raw manpage into (:Man!) overlooks `match('man://')` condition,
  -- so if the buffer already exists, create new with a non existing name.
  if vim.fn.bufexists(man_bufname) == 1 then
    local new_bufname = man_bufname
    local i = 1
    while vim.fn.bufexists(new_bufname) == 1 do
      new_bufname = man_bufname .. '?new=' .. tostring(i)
      i = i + 1
    end
    vim.cmd.file({ new_bufname, mods = { silent = true } })
  elseif not fn.bufname('%'):match('man://') then -- Avoid duplicate buffers, E95.
    vim.cmd.file({ man_bufname, mods = { silent = true } })
  end

  set_options()
end

--- Combine the name and sect into a manpage reference so that all
--- verification/extraction can be kept in a single function.
--- @param args string[]
--- @return string? ref
local function ref_from_args(args)
  if #args <= 1 then
    return args[1]
  elseif args[1]:match('^%d$') or args[1]:match('^%d%a') or args[1]:match('^%a$') then
    -- NB: Valid sections are not only digits, but also:
    --  - <digit><word> (see POSIX mans),
    --  - and even <letter> and <word> (see, for example, by tcl/tk)
    -- NB2: don't optimize to :match("^%d"), as it will match manpages like
    --    441toppm and others whose name starts with digit
    local sect = args[1]
    table.remove(args, 1)
    local name = table.concat(args, ' ')
    return ('%s(%s)'):format(name, sect)
  end


Review comment (bug):
nit (untested). for style, but also to avoid infinite loop.
```suggestion
    local new_bufname = man_bufname
    for i = 1, 100 do
        if vim.fn.bufexists(new_bufname) == 0 then
            break
        end
        new_bufname = ('%s?new=%s'):format(man_bufname, i)
    end
    vim.cmd.file({ new_bufname, mods = { silent = true } })
```

Code after:
  if fn.getline(1):match('^%s*$') then
    api.nvim_buf_set_lines(0, 0, 1, false, {})
  else
    vim.cmd('keepjumps 1')
  end
  highlight_man_page()
  -- Guess the ref from the heading (which is usually uppercase, so we cannot
  -- know the correct casing, cf. `man glDrawArraysInstanced`).
  --- @type string
  local ref = (fn.getline(1):match('^[^)]+%)') or ''):gsub(' ', '_')
  local _, sect, err = pcall(parse_ref, ref)
  vim.b.man_sect = err ~= nil and sect or ''

  local man_bufname = 'man://' .. fn.fnameescape(ref):lower()

  -- Raw manpage into (:Man!) overlooks `match('man://')` condition,
  -- so if the buffer already exists, create new with a non existing name.
  if vim.fn.bufexists(man_bufname) == 1 then
    local new_bufname = man_bufname
    for i = 1, 100 do
      if vim.fn.bufexists(new_bufname) == 0 then
        break
      end
      new_bufname = ('%s?new=%s'):format(man_bufname, i)
    end
    vim.cmd.file({ new_bufname, mods = { silent = true } })
  elseif not fn.bufname('%'):match('man://') then -- Avoid duplicate buffers, E95.
    vim.cmd.file({ man_bufname, mods = { silent = true } })
  end

  set_options()
end

--- Combine the name and sect into a manpage reference so that all
--- verification/extraction can be kept in a single function.
--- @param args string[]
--- @return string? ref
local function ref_from_args(args)
  if #args <= 1 then
    return args[1]
  elseif args[1]:match('^%d$') or args[1]:match('^%d%a') or args[1]:match('^%a$') then
    -- NB: Valid sections are not only digits, but also:
    --  - <digit><word> (see POSIX mans),
    --  - and even <letter> and <word> (see, for example, by tcl/tk)
    -- NB2: don't optimize to :match("^%d"), as it will match manpages like
    --    441toppm and others whose name starts with digit
    local sect = args[1]
    table.remove(args, 1)
    local name = table.concat(args, ' ')
    return ('%s(%s)'):format(name, sect)
  end