You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While there is already a CheckboxToggle function provided by obsidian sometimes you know exactly what you want instead of cycling over a list of todo items.
What I came up with are those functions which turn either the line where the cursor is or the selected lines into TODO items.
localM= {}
M._add_checkbox=function(character, line_num)
localline=vim.api.nvim_buf_get_lines(0, line_num-1, line_num, false)[1]
localcheckbox_pattern="^%s*- %[.] "localcheckbox=characteror""ifnotstring.match(line, checkbox_pattern) thenlocalunordered_list_pattern="^(%s*)[-*+] (.*)"ifstring.match(line, unordered_list_pattern) thenline=string.gsub(line, unordered_list_pattern, "%1- [ ] %2")
elseline=string.gsub(line, "^(%s*)", "%1- [ ] ")
endendlocalcapturing_checkbox_pattern="^(%s*- %[).(%] )"line=string.gsub(line, capturing_checkbox_pattern, "%1" ..checkbox.."%2")
-- 0-indexedvim.api.nvim_buf_set_lines(0, line_num-1, line_num, true, { line })
endM._remove_checkbox=function(line_num)
localline=vim.api.nvim_buf_get_lines(0, line_num-1, line_num, false)[1]
localcheckbox_pattern="^%s*- %[.]. "localcapturing_checkbox_pattern="^(%s*-) %[.%] (.*)"line=string.gsub(line, capturing_checkbox_pattern, "%1 %2")
line=string.gsub(line, checkbox_pattern, "")
-- 0-indexedvim.api.nvim_buf_set_lines(0, line_num-1, line_num, true, { line })
endM.toggle_checkbox=function(character)
-- Check if we are in visual line modelocalmode=vim.api.nvim_get_mode().modelocaltoggle_or_remove=function(character, line_num)
if (character==nil) then-- Remove checkboxM._remove_checkbox(line_num)
else-- Add checkboxM._add_checkbox(character, line_num)
endendifmode=='V' ormode=='v' then-- Get the range of selected linesvim.cmd([[execute "normal! \<ESC>"]])
localvstart=vim.fn.getcharpos("'<")
localvend=vim.fn.getcharpos("'>")
localline_start=vstart[2]
localline_end=vend[2]
-- Iterate over each line in the range and apply the transformationforline_num=line_start, line_enddotoggle_or_remove(character, line_num)
endelse-- Normal mode-- Allow line_num to be optional, defaulting to the current line if not provided (normal mode)localline_num=unpack(vim.api.nvim_win_get_cursor(0))
toggle_or_remove(character, line_num)
endend
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
While there is already a CheckboxToggle function provided by obsidian sometimes you know exactly what you want instead of cycling over a list of todo items.
What I came up with are those functions which turn either the line where the cursor is or the selected lines into TODO items.
Here's how I, for example, use them:
I can then select multiple lines and easily turn them into undone todos or remove their checkboxes
Hope that helps!
Beta Was this translation helpful? Give feedback.
All reactions