What can be wrong whit this code?
local function is_in_num_list()
local current_line = vim.api.nvim_get_current_line()
return current_line:match("^%s*%d+%s-%s") ~= nil
end
vim.keymap.set(
'i',
'<cr>',
function()
if is_in_num_list() then
local line = vim.api.nvim_get_current_line()
local modified_line = line:gsub("^(%d+)(%s-.*)$",
function(numb, rest)
numb = tonumber(numb) + 1
return tostring(numb) .. rest
end)
return '<cr>' .. modified_line
else
return '<cr>'
end
end,
{
expr = true,
desc = 'increment numbered lists'
}
)
Here it is an improvedversion that works:
local function is_in_list()
local current_line = vim.api.nvim_get_current_line()
return current_line:match('^%s*[%*-+]%s') ~= nil
end
local function has_checkbox()
local current_line = vim.api.nvim_get_current_line()
return current_line:match('%s*[%*-+]%s%[[ x]%]') ~= nil
end
local function list_prefix()
local line = vim.api.nvim_get_current_line()
local list_char = line:gsub("^%s*([-%*+] )(.*)",
function(prefix, rest)
return prefix
end)
return list_char
end
local function is_in_num_list()
local current_line = vim.api.nvim_get_current_line()
return current_line:match('^%s*%d+%.%s') ~= nil
end
vim.keymap.set('i', '<cr>', function()
if is_in_list() then
local prefix = list_prefix()
return has_checkbox() and '<cr>' .. prefix .. '[ ] ' or '<cr>' .. prefix
elseif is_in_num_list() then
local line = vim.api.nvim_get_current_line()
local modified_line = line:gsub("^%s*(%d+)%.%s.*$",
function(numb)
numb = tonumber(numb) + 1
return tostring(numb)
end)
return '<cr>' .. modified_line .. '. '
else
return '<cr>'
end
end, {
buffer = true,
expr = true,
})
vim.keymap.set(
'n',
'o',
function()
if is_in_list() then
local prefix = list_prefix()
return has_checkbox() and 'o' .. prefix .. '[ ] ' or 'o' .. prefix
elseif is_in_num_list() then
local line = vim.api.nvim_get_current_line()
local modified_line = line:gsub("^%s*(%d+)%.%s.*$",
function(numb)
numb = tonumber(numb) + 1
return tostring(numb)
end)
return 'o' .. modified_line .. '. '
else
return 'o'
end
end, {
buffer = true,
expr = true,
})
vim.keymap.set('n', 'O', function()
if is_in_list() then
local prefix = list_prefix()
return has_checkbox() and 'O' .. prefix .. '[ ] ' or 'O' .. prefix
elseif is_in_num_list() then
local line = vim.api.nvim_get_current_line()
local modified_line = line:gsub("^%s*(%d+)%.%s.*$",
function(numb)
numb = tonumber(numb) + 1
return tostring(numb)
end)
return 'O' .. modified_line .. '. '
else
return 'O'
end
end, {
buffer = true,
expr = true,
})