diff --git a/lua/todoer/init.lua b/lua/todoer/init.lua new file mode 100644 index 0000000..78687f5 --- /dev/null +++ b/lua/todoer/init.lua @@ -0,0 +1,132 @@ +-- Create the module table +local M = {} + +-- add new todo line when previous is already a todo +local function press_enter() + local current_line = vim.api.nvim_get_current_line() + local index, spaces = string.find(current_line, "^%s*") + + -- 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 + +-- indent line if tab is pressed when line is a todo +local function press_tab() + local current_line = vim.api.nvim_get_current_line() + + -- Check if current line matches the patterns + local openpattern = "%- %[[ ]%]" + local closedpattern = "%- %[[x]%]" + + if string.match(current_line, openpattern) or string.match(current_line, closedpattern) then + print("tab pressed, allegedly") + vim.api.nvim_feedkeys("C-t", "i", true) + end +end + +-- indent line if shift tab is pressed when line is a todo +local function press_shift_tab() + local current_line = vim.api.nvim_get_current_line() + + -- Check if current line matches the patterns + local openpattern = "%- %[[ ]%]" + local closedpattern = "%- %[[x]%]" + + if string.match(current_line, openpattern) or string.match(current_line, closedpattern) then + print("shift tab pressed, allegedly") + vim.api.nvim_feedkeys("C-d", "i", true) + end +end + +-- function that checks if the current line starts with the string "- [ ]" or "- [x]" and toggles the x +local function toggle_todo() + local openpattern = "%- %[[ ]%]" + local closedpattern = "%- %[[x]%]" + local titleopenpattern = "# %[[ ]%]" + local titleclosedpattern = "# %[[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 + +-- 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() + local index, spaces = string.find(line, "^%s*") + + if index == 1 and not string.match(line, openpattern) and not string.match(line, closedpattern) then + if index == 1 and spaces == 0 then + line = "- [ ] " .. line + vim.api.nvim_set_current_line(line) + vim.api.nvim_feedkeys("A", "n", true) + elseif index == 1 and spaces > 0 then + local trimedline = string.sub(line, spaces + 1) + local spacestring = "" + for x = 1, spaces, 1 do + spacestring = spacestring .. " " + end + line = spacestring .. "- [ ] " .. trimedline + vim.api.nvim_set_current_line(line) + end + end +end + +-- 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 = "%- %[[ ]%]" + local closedpattern = "%- %[[x]%]" + local line = vim.api.nvim_get_current_line() + -- local index, spaces = string.find(line, "^%s*") + + if string.match(line, openpattern) or string.match(line, closedpattern) then + print("encontrado") + line = string.gsub(line, openpattern, "") + line = string.gsub(line, closedpattern, "") + vim.api.nvim_set_current_line(line) + end +end + +-- Set up the keymaps (for now, this happens immediately on require) +vim.keymap.set("n", "t", function() end, { desc = "+TODOs" }) +vim.keymap.set("i", "", press_enter, { desc = "On enter", noremap = true, expr = true }) +vim.keymap.set("i", "", press_tab, { desc = "On tab", noremap = true, expr = true }) +vim.keymap.set("i", "S-Tab", press_shift_tab, { desc = "On shift tab", noremap = true, expr = true }) +vim.keymap.set("n", "tt", toggle_todo, { desc = "Toggle TODO" }) +vim.keymap.set("n", "ta", add_todo, { desc = "Add TODO" }) +vim.keymap.set("n", "td", remove_todo, { desc = "Remove TODO" }) + +-- Return the module table +return M + diff --git a/plugin/todoer.lua b/plugin/todoer.lua index d0ee27f..3c96b27 100644 --- a/plugin/todoer.lua +++ b/plugin/todoer.lua @@ -1,125 +1,5 @@ -vim.keymap.set("n", "t", function() end, { desc = "+TODOs" }) +-- Load the main module. +-- This will execute the code inside lua/todoer/init.lua immediately, +-- including setting the keymaps. +require('todoer') --- add new todo line when previous is already a todo - -local function press_enter() - local current_line = vim.api.nvim_get_current_line() - local index, spaces = string.find(current_line, "^%s*") - - -- 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", "", press_enter, { desc = "On enter", noremap = true, expr = true }) - --- indent line if tab is pressed when line is a todo -local function press_tab() - local current_line = vim.api.nvim_get_current_line() - - -- Check if current line matches the patterns - local openpattern = "%- %[[ ]%]" - local closedpattern = "%- %[[x]%]" - - if string.match(current_line, openpattern) or string.match(current_line, closedpattern) then - print("tab pressed, allegedly") - vim.api.nvim_feedkeys("C-t", "i", true) - end -end -vim.keymap.set("i", "", press_tab, { desc = "On tab", noremap = true, expr = true }) - --- indent line if shift tab is pressed when line is a todo -local function press_shift_tab() - local current_line = vim.api.nvim_get_current_line() - - -- Check if current line matches the patterns - local openpattern = "%- %[[ ]%]" - local closedpattern = "%- %[[x]%]" - - if string.match(current_line, openpattern) or string.match(current_line, closedpattern) then - print("shift tab pressed, allegedly") - vim.api.nvim_feedkeys("C-d", "i", true) - end -end -vim.keymap.set("i", "S-Tab", press_shift_tab, { desc = "On shift tab", 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 = "%- %[[ ]%]" - local closedpattern = "%- %[[x]%]" - local titleopenpattern = "# %[[ ]%]" - local titleclosedpattern = "# %[[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", "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() - local index, spaces = string.find(line, "^%s*") - - if index == 1 and not string.match(line, openpattern) and not string.match(line, closedpattern) then - if index == 1 and spaces == 0 then - line = "- [ ] " .. line - vim.api.nvim_set_current_line(line) - vim.api.nvim_feedkeys("A", "n", true) - elseif index == 1 and spaces > 0 then - local trimedline = string.sub(line, spaces + 1) - local spacestring = "" - for x = 1, spaces, 1 do - spacestring = spacestring .. " " - end - line = spacestring .. "- [ ] " .. trimedline - vim.api.nvim_set_current_line(line) - end - 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 openpattern = "%- %[[ ]%]" - local closedpattern = "%- %[[x]%]" - local line = vim.api.nvim_get_current_line() - -- local index, spaces = string.find(line, "^%s*") - - if string.match(line, openpattern) or string.match(line, closedpattern) then - print("encontrado") - line = string.gsub(line, openpattern, "") - line = string.gsub(line, closedpattern, "") - vim.api.nvim_set_current_line(line) - end -end -vim.keymap.set("n", "td", remove_todo, { desc = "Remove TODO" })