2022-08-08 17:58:47 +00:00
|
|
|
local checked_character = "x"
|
|
|
|
|
2022-08-09 04:43:18 +00:00
|
|
|
local checked_checkbox = "%[" .. checked_character .. "%]"
|
2022-08-08 23:11:15 +00:00
|
|
|
local unchecked_checkbox = "%[ %]"
|
2022-08-08 22:27:19 +00:00
|
|
|
|
2022-08-08 23:11:15 +00:00
|
|
|
local line_contains_an_unchecked_checkbox = function(line)
|
|
|
|
return string.find(line, unchecked_checkbox)
|
|
|
|
end
|
2022-08-08 18:05:45 +00:00
|
|
|
|
2022-08-09 04:43:18 +00:00
|
|
|
local checkbox = {
|
|
|
|
check = function(line)
|
|
|
|
return line:gsub(unchecked_checkbox, checked_checkbox)
|
|
|
|
end,
|
2022-08-08 22:45:22 +00:00
|
|
|
|
2022-08-09 04:43:18 +00:00
|
|
|
uncheck = function(line)
|
|
|
|
return line:gsub(checked_checkbox, unchecked_checkbox)
|
|
|
|
end,
|
|
|
|
}
|
2022-08-08 17:58:47 +00:00
|
|
|
|
2022-08-09 01:20:36 +00:00
|
|
|
local M = {}
|
|
|
|
|
2022-08-08 23:11:15 +00:00
|
|
|
M.toggle = function()
|
2022-08-09 01:20:36 +00:00
|
|
|
local bufnr = vim.api.nvim_buf_get_number(0)
|
|
|
|
local cursor = vim.api.nvim_win_get_cursor(0)
|
2022-08-09 13:48:24 +00:00
|
|
|
local start_line = cursor[1] - 1
|
|
|
|
local current_line = vim.api.nvim_buf_get_lines(bufnr, start_line, start_line + 1, false)[1] or ""
|
2022-08-09 01:20:36 +00:00
|
|
|
|
2022-08-08 23:11:15 +00:00
|
|
|
-- If the line contains a checked checkbox then uncheck it.
|
|
|
|
-- Otherwise, if it contains an unchecked checkbox, check it.
|
2022-08-09 01:20:36 +00:00
|
|
|
local new_line = ""
|
2022-08-08 23:11:15 +00:00
|
|
|
if line_contains_an_unchecked_checkbox(current_line) then
|
2022-08-09 01:20:36 +00:00
|
|
|
new_line = checkbox.check(current_line)
|
2022-08-08 23:11:15 +00:00
|
|
|
else
|
2022-08-09 01:20:36 +00:00
|
|
|
new_line = checkbox.uncheck(current_line)
|
2022-08-08 23:11:15 +00:00
|
|
|
end
|
|
|
|
|
2022-08-09 13:48:24 +00:00
|
|
|
vim.api.nvim_buf_set_lines(bufnr, start_line, start_line + 1, false, { new_line })
|
2022-08-09 01:20:36 +00:00
|
|
|
vim.api.nvim_win_set_cursor(0, cursor)
|
2022-08-08 17:58:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|