@@ -513,6 +513,7 @@ util.toggle_checkbox = function(opts, line_num)
513513 local checkboxes = opts or { " " , " x" }
514514
515515 if not string.match (line , checkbox_pattern ) then
516+ -- Add an empty checkbox if one is not found.
516517 local unordered_list_pattern = " ^(%s*)[-*+] (.*)"
517518 if string.match (line , unordered_list_pattern ) then
518519 line = string.gsub (line , unordered_list_pattern , " %1- [ ] %2" )
@@ -534,6 +535,22 @@ util.toggle_checkbox = function(opts, line_num)
534535 vim .api .nvim_buf_set_lines (0 , line_num - 1 , line_num , true , { line })
535536end
536537
538+ --- Set the value of a checkbox to a given check_char
539+ util .set_checkbox = function (check_char , line_num )
540+ -- Allow line_num to be optional, defaulting to the current line if not provided
541+ line_num = line_num or unpack (vim .api .nvim_win_get_cursor (0 ))
542+ local line = vim .api .nvim_buf_get_lines (0 , line_num - 1 , line_num , false )[1 ]
543+
544+ local checkbox_pattern = " ^%s*- %[.] "
545+
546+ if string.match (line , checkbox_pattern ) then
547+ -- Only set the check_char if the line contains the checkbox pattern.
548+ line = util .string_replace (line , " - [" .. check_char .. " ]" , " - [" .. check_char .. " ]" , 1 )
549+ end
550+ -- 0-indexed
551+ vim .api .nvim_buf_set_lines (0 , line_num - 1 , line_num , true , { line })
552+ end
553+
537554--- Determines if the given date is a working day (not weekend)
538555---
539556--- @param time integer
0 commit comments