1
0

Allow definition of checkmark characters

This commit is contained in:
Stefan Rakel
2024-08-09 14:59:03 +02:00
parent 1d7f44983c
commit de332271d3

View File

@@ -23,6 +23,7 @@
-- Initialize patterns -- Initialize patterns
local prefixPatterns = {"%-", "%*", "#+", "%->", "=>", "%d+%."} local prefixPatterns = {"%-", "%*", "#+", "%->", "=>", "%d+%."}
local skipPatterns = {"%-", "%*", "%->", "=>", "%d+%."} local skipPatterns = {"%-", "%*", "%->", "=>", "%d+%."}
local checkmarks = {"X", "x"} --UTF8 characters currently not supported
if vim.g.checkbox_prefixPatterns then if vim.g.checkbox_prefixPatterns then
prefixPatterns = vim.g.checkbox_prefixPatterns prefixPatterns = vim.g.checkbox_prefixPatterns
@@ -36,12 +37,15 @@ end
local function getCheckboxFromLine(lineNum) local function getCheckboxFromLine(lineNum)
local line = vim.fn.getline(lineNum) local line = vim.fn.getline(lineNum)
for _, pat in ipairs(prefixPatterns) do for _, pat in ipairs(prefixPatterns) do
local pattern = "(%s*)(" .. pat .. "%s*)%[([ xX]?)%](.*)" local pattern = "(%s*)(" .. pat .. "%s*)%[([ " .. vim.fn.join(checkmarks, "") .."]?)%](.*)"
local whitespace, before, checked, after = string.match(line, pattern) local whitespace, before, checked, after = string.match(line, pattern)
if whitespace then if whitespace then
if checked == "x" or checked == "X" then for _, c in ipairs(checkmarks) do
if checked == c then
checked = true checked = true
else end
end
if checked ~= true then
checked = false checked = false
end end
whitespace = string.gsub(whitespace, "\t", " ") whitespace = string.gsub(whitespace, "\t", " ")
@@ -55,7 +59,7 @@ end
local function writeCheckbox(checkbox) local function writeCheckbox(checkbox)
local checkedString = " " local checkedString = " "
if checkbox.checked then if checkbox.checked then
checkedString = "X" checkedString = checkmarks[1]
end end
local newLine = string.rep(" ", checkbox.indentation) .. checkbox.before .. "[" .. checkedString .. "]" .. checkbox.after local newLine = string.rep(" ", checkbox.indentation) .. checkbox.before .. "[" .. checkedString .. "]" .. checkbox.after
vim.fn.setline(checkbox.lineNum, newLine) vim.fn.setline(checkbox.lineNum, newLine)