You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.2 KiB
Lua

-- Empty keymap for the menu
vim.keymap.set("n", "<leader>t", function() end, { desc = "+Todos" })
-- add new todo line when previous is already a todo
local function press_enter()
local current_line = vim.api.nvim_get_current_line()
-- Check if the current line matches the pattern
local pattern = "^%- %[[ x]%] .*$"
local match = string.match(current_line, pattern)
if match then
if string.len(current_line) >= 7 then
vim.api.nvim_feedkeys("\n- [ ] ", "n", true)
else
-- go to normal mode before removing the content of the line
vim.api.nvim_feedkeys(vim.api.nvim_eval('"\\<esc>"'), "n", true)
vim.api.nvim_feedkeys("_", "n", true)
vim.api.nvim_feedkeys("C", "n", true)
vim.api.nvim_feedkeys("\n", "n", true)
-- vim.api.nvim_set_current_line("")
end
else
vim.api.nvim_feedkeys("\n", "n", true)
end
end
vim.keymap.set("i", "<CR>", press_enter, { desc = "On enter", noremap = true, expr = true })
-- function that checks if the current line starts with the string "- [ ]" or "- [x]" and toggles the x
local function toggle_todo()
local openpattern = "^%s*%- %[[ ]%]"
local closedpattern = "^%s*%- %[[x]%]"
local titleopenpattern = "^%s*# %[[ ]%]"
local titleclosedpattern = "^%s*# %[[x]%]"
local line = vim.api.nvim_get_current_line()
if string.match(line, openpattern) then
line = string.gsub(line, openpattern, "- [x]")
vim.api.nvim_set_current_line(line)
elseif string.match(line, closedpattern) then
line = string.gsub(line, closedpattern, "- [ ]")
vim.api.nvim_set_current_line(line)
elseif string.match(line, titleopenpattern) then
line = string.gsub(line, titleopenpattern, "# [x]")
vim.api.nvim_set_current_line(line)
elseif string.match(line, titleclosedpattern) then
line = string.gsub(line, titleclosedpattern, "# [ ]")
vim.api.nvim_set_current_line(line)
end
end
vim.keymap.set("n", "<leader>tt", toggle_todo, { desc = "Toggle todo" })
-- function that checks if the current line doesn't start with the string "- [ ] " and, if it doesn't, adds the string at the beginning of the line
local function add_todo()
local openpattern = "^%s*%- %[[ ]%]"
local closedpattern = "^%s*%- %[[x]%]"
local line = vim.api.nvim_get_current_line()
if not string.match(line, openpattern) and not string.match(line, closedpattern) then
if not string.sub(line, 1, 1) == " " then
line = "- [ ] " .. line
vim.api.nvim_set_current_line(line)
else
line = "- [ ]" .. line
vim.api.nvim_set_current_line(line)
end
-- vim.api.nvim_feedkeys("A", "n", true) -- move cursor to the end of the line
end
end
vim.keymap.set("n", "<leader>ta", add_todo, { desc = "Add todo" })
-- function that checks if the current line starts with the string "- [ ]" or "- [x]" and, if it does, removes that string from the line
local function remove_todo()
local openpattern = "^%s*%- %[[ ]%]"
local closedpattern = "^%s*%- %[[x]%]"
local line = vim.api.nvim_get_current_line()
if string.match(line, openpattern) or string.match(line, closedpattern) then
line = string.gsub(line, openpattern, "")
line = string.gsub(line, closedpattern, "")
vim.api.nvim_set_current_line(line)
end
end
vim.keymap.set("n", "<leader>td", remove_todo, { desc = "Remove todo" })