diff --git a/plugin/todoer.lua b/plugin/todoer.lua index a1795cb..acc6dbf 100644 --- a/plugin/todoer.lua +++ b/plugin/todoer.lua @@ -1 +1,141 @@ -print("hello borja") +vim.keymap.set("n", "t", function() end, { desc = "+Todos" }) + +-- function that checks if the current line starts with the string "- [ ]" or "- [x]" and toggles the x +local function toggle_todo() + local line = vim.api.nvim_get_current_line() + if string.sub(line, 1, 5) == "- [ ]" then + line = string.sub(line, 6, -1) + line = "- [x]" .. line + vim.api.nvim_set_current_line(line) + elseif string.sub(line, 1, 5) == "- [x]" then + line = string.sub(line, 6, -1) + line = "- [ ]" .. line + vim.api.nvim_set_current_line(line) + elseif string.sub(line, 1, 5) == "# [ ]" then + line = string.sub(line, 6, -1) + line = "# [x]" .. line + vim.api.nvim_set_current_line(line) + elseif string.sub(line, 1, 5) == "# [x]" then + line = string.sub(line, 6, -1) + line = "# [ ]" .. line + vim.api.nvim_set_current_line(line) + end +end +vim.keymap.set("n", "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 line = vim.api.nvim_get_current_line() + if string.sub(line, 1, 5) ~= "- [ ] " then + line = "- [ ] " .. line + vim.api.nvim_set_current_line(line) + vim.api.nvim_feedkeys("A", "n", true) + end +end +vim.keymap.set("n", "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 line = vim.api.nvim_get_current_line() + if string.sub(line, 1, 6) == "- [ ] " or string.sub(line, 1, 6) == "- [x] " then + line = string.sub(line, 7, -1) + vim.api.nvim_set_current_line(line) + end +end +vim.keymap.set("n", "td", remove_todo, { desc = "Remove todo" }) + +-- add new todo line when previous is already a todo + +local function on_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('"\\"'), "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", "", on_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 line = vim.api.nvim_get_current_line() + if string.sub(line, 1, 5) == "- [ ]" then + line = string.sub(line, 6, -1) + line = "- [x]" .. line + vim.api.nvim_set_current_line(line) + elseif string.sub(line, 1, 5) == "- [x]" then + line = string.sub(line, 6, -1) + line = "- [ ]" .. line + vim.api.nvim_set_current_line(line) + elseif string.sub(line, 1, 5) == "# [ ]" then + line = string.sub(line, 6, -1) + line = "# [x]" .. line + vim.api.nvim_set_current_line(line) + elseif string.sub(line, 1, 5) == "# [x]" then + line = string.sub(line, 6, -1) + line = "# [ ]" .. line + vim.api.nvim_set_current_line(line) + end +end +vim.keymap.set("n", "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 line = vim.api.nvim_get_current_line() + if string.sub(line, 1, 5) ~= "- [ ] " then + line = "- [ ] " .. line + vim.api.nvim_set_current_line(line) + vim.api.nvim_feedkeys("A", "n", true) + end +end +vim.keymap.set("n", "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 line = vim.api.nvim_get_current_line() + if string.sub(line, 1, 6) == "- [ ] " or string.sub(line, 1, 6) == "- [x] " then + line = string.sub(line, 7, -1) + vim.api.nvim_set_current_line(line) + end +end +vim.keymap.set("n", "td", remove_todo, { desc = "Remove todo" }) + +-- add new todo line when previous is already a todo + +local function on_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('"\\"'), "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", "", on_enter, { desc = "On enter", noremap = true, expr = true })