Skip to content

Commit cb88941

Browse files
committed
Add ObsidianSetCheckbox
1 parent 14e0427 commit cb88941

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

lua/obsidian/commands/init.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local iter = require("obsidian.itertools").iter
33

44
local command_lookups = {
55
ObsidianCheck = "obsidian.commands.check",
6+
ObsidianSetCheckbox = "obsidian.commands.set_checkbox",
67
ObsidianToggleCheckbox = "obsidian.commands.toggle_checkbox",
78
ObsidianToday = "obsidian.commands.today",
89
ObsidianYesterday = "obsidian.commands.yesterday",
@@ -168,6 +169,8 @@ M.register("ObsidianLinks", { opts = { nargs = 0, desc = "Collect all links with
168169

169170
M.register("ObsidianFollowLink", { opts = { nargs = "?", desc = "Follow reference or link under cursor" } })
170171

172+
M.register("ObsidianSetCheckbox", { opts = { nargs = 1, desc = "Set the value of a checkbox" } })
173+
171174
M.register("ObsidianToggleCheckbox", { opts = { nargs = 0, desc = "Toggle checkbox" } })
172175

173176
M.register("ObsidianWorkspace", { opts = { nargs = "?", desc = "Check or switch workspace" } })
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local set_checkbox = require("obsidian.util").set_checkbox
2+
3+
---@param client obsidian.Client
4+
return function(client, data)
5+
-- If no check_char is provided, default to "x"
6+
if string.len(data.args) == 0 then
7+
data.args = "x"
8+
end
9+
set_checkbox(data.args)
10+
end

lua/obsidian/util.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 })
535536
end
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

Comments
 (0)