From 019c7093b2aa1cd3a518c0c57679bff7e22e4739 Mon Sep 17 00:00:00 2001 From: Deepak Mallubhotla Date: Wed, 2 Apr 2025 12:08:33 -0500 Subject: [PATCH 1/4] feat: add toggle checkbox --- home/deepak/neovim/init-vim.nix | 1 + home/deepak/neovim/toggle-checkbox.lua | 71 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 home/deepak/neovim/toggle-checkbox.lua diff --git a/home/deepak/neovim/init-vim.nix b/home/deepak/neovim/init-vim.nix index d4198ea..fb265ed 100644 --- a/home/deepak/neovim/init-vim.nix +++ b/home/deepak/neovim/init-vim.nix @@ -270,5 +270,6 @@ ${builtins.readFile ./lualine.lua} + ${builtins.readFile ./toggle-checkboxes.lua} EOF '' diff --git a/home/deepak/neovim/toggle-checkbox.lua b/home/deepak/neovim/toggle-checkbox.lua new file mode 100644 index 0000000..be4ce19 --- /dev/null +++ b/home/deepak/neovim/toggle-checkbox.lua @@ -0,0 +1,71 @@ +-- stolen from https://github.com/opdavies/toggle-checkbox.nvim/blob/main/lua/toggle-checkbox.lua +-- Just copying the code because I don't want to import it as a dependency + +local checked_character = "x" + +local checked_checkbox = "%[" .. checked_character .. "%]" +local unchecked_checkbox = "%[ %]" + +local line_contains_unchecked = function(line) + return line:find(unchecked_checkbox) +end + +local line_contains_checked = function(line) + return line:find(checked_checkbox) +end + +local line_with_checkbox = function(line) + -- return not line_contains_a_checked_checkbox(line) and not line_contains_an_unchecked_checkbox(line) + return line:find("^%s*- " .. checked_checkbox) + or line:find("^%s*- " .. unchecked_checkbox) + or line:find("^%s*%d%. " .. checked_checkbox) + or line:find("^%s*%d%. " .. unchecked_checkbox) +end + +local checkbox = { + check = function(line) + return line:gsub(unchecked_checkbox, checked_checkbox, 1) + end, + + uncheck = function(line) + return line:gsub(checked_checkbox, unchecked_checkbox, 1) + end, + + make_checkbox = function(line) + if not line:match("^%s*-%s.*$") and not line:match("^%s*%d%s.*$") then + -- "xxx" -> "- [ ] xxx" + return line:gsub("(%S+)", "- [ ] %1", 1) + else + -- "- xxx" -> "- [ ] xxx", "3. xxx" -> "3. [ ] xxx" + return line:gsub("(%s*- )(.*)", "%1[ ] %2", 1):gsub("(%s*%d%. )(.*)", "%1[ ] %2", 1) + end + end, +} + +local M = {} + +M.toggle = function() + local bufnr = vim.api.nvim_buf_get_number(0) + local cursor = vim.api.nvim_win_get_cursor(0) + local start_line = cursor[1] - 1 + local current_line = vim.api.nvim_buf_get_lines(bufnr, start_line, start_line + 1, false)[1] or "" + + -- If the line contains a checked checkbox then uncheck it. + -- Otherwise, if it contains an unchecked checkbox, check it. + local new_line = "" + + if not line_with_checkbox(current_line) then + new_line = checkbox.make_checkbox(current_line) + elseif line_contains_unchecked(current_line) then + new_line = checkbox.check(current_line) + elseif line_contains_checked(current_line) then + new_line = checkbox.uncheck(current_line) + end + + vim.api.nvim_buf_set_lines(bufnr, start_line, start_line + 1, false, { new_line }) + vim.api.nvim_win_set_cursor(0, cursor) +end + +vim.api.nvim_create_user_command("ToggleCheckbox", M.toggle, {}) + +return M From 83b99fb833c7d21993dcd84e852bdc4cc1694763 Mon Sep 17 00:00:00 2001 From: Deepak Mallubhotla Date: Wed, 2 Apr 2025 12:16:33 -0500 Subject: [PATCH 2/4] feat: adds toggle checkbox --- home/deepak/neovim/init-vim.nix | 2 +- home/deepak/neovim/toggle-checkbox.lua | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/home/deepak/neovim/init-vim.nix b/home/deepak/neovim/init-vim.nix index fb265ed..13e3c2b 100644 --- a/home/deepak/neovim/init-vim.nix +++ b/home/deepak/neovim/init-vim.nix @@ -270,6 +270,6 @@ ${builtins.readFile ./lualine.lua} - ${builtins.readFile ./toggle-checkboxes.lua} + ${builtins.readFile ./toggle-checkbox.lua} EOF '' diff --git a/home/deepak/neovim/toggle-checkbox.lua b/home/deepak/neovim/toggle-checkbox.lua index be4ce19..3e6a4e7 100644 --- a/home/deepak/neovim/toggle-checkbox.lua +++ b/home/deepak/neovim/toggle-checkbox.lua @@ -67,5 +67,4 @@ M.toggle = function() end vim.api.nvim_create_user_command("ToggleCheckbox", M.toggle, {}) - -return M +vim.api.nvim_set_keymap("n", "x", "ToggleCheckbox", { noremap = true}) From f59aa8711cb400cd6963db1284443d7fd55a4c7e Mon Sep 17 00:00:00 2001 From: Deepak Mallubhotla Date: Wed, 2 Apr 2025 12:19:05 -0500 Subject: [PATCH 3/4] doc: add todo about toggle --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 661f5e7..0330b2d 100644 --- a/README.md +++ b/README.md @@ -22,3 +22,8 @@ Quite incomplete. If `sops-nix` is having trouble in WSL it could be because the user systemd set up doesn't get correctly started. Using something like `systemctl restart user@1000` can be helpful (cf. [this link]{https://github.com/microsoft/WSL/issues/8842#issuecomment-2346387618}) + + +## Todo + +- [ ] Toggle checkbox needs to only be enabled for markdown, use autocmd or something From be052e45bdae35cea6e01dbb4dbe1acdacbf8850 Mon Sep 17 00:00:00 2001 From: Deepak Mallubhotla Date: Wed, 2 Apr 2025 12:42:23 -0500 Subject: [PATCH 4/4] fmt: spacing fix --- home/deepak/neovim/toggle-checkbox.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/home/deepak/neovim/toggle-checkbox.lua b/home/deepak/neovim/toggle-checkbox.lua index 3e6a4e7..419fb83 100644 --- a/home/deepak/neovim/toggle-checkbox.lua +++ b/home/deepak/neovim/toggle-checkbox.lua @@ -67,4 +67,4 @@ M.toggle = function() end vim.api.nvim_create_user_command("ToggleCheckbox", M.toggle, {}) -vim.api.nvim_set_keymap("n", "x", "ToggleCheckbox", { noremap = true}) +vim.api.nvim_set_keymap("n", "x", "ToggleCheckbox", { noremap = true })