diff --git a/CHANGELOG.md b/CHANGELOG.md index b585ed0e2..fa5d1bf0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added `opts.follow_img_func` option for customizing how to handle image paths. - Added better handling for undefined template fields, which will now be prompted for. +- Added `ObsidianSetCheckbox` to set the value of a checkbox to the passed in value. Defaults to `x`. ### Changed diff --git a/lua/obsidian/commands/init.lua b/lua/obsidian/commands/init.lua index f31eaf8ea..9d70a141e 100644 --- a/lua/obsidian/commands/init.lua +++ b/lua/obsidian/commands/init.lua @@ -3,6 +3,7 @@ local iter = require("obsidian.itertools").iter local command_lookups = { ObsidianCheck = "obsidian.commands.check", + ObsidianSetCheckbox = "obsidian.commands.set_checkbox", ObsidianToggleCheckbox = "obsidian.commands.toggle_checkbox", ObsidianToday = "obsidian.commands.today", ObsidianYesterday = "obsidian.commands.yesterday", @@ -168,6 +169,8 @@ M.register("ObsidianLinks", { opts = { nargs = 0, desc = "Collect all links with M.register("ObsidianFollowLink", { opts = { nargs = "?", desc = "Follow reference or link under cursor" } }) +M.register("ObsidianSetCheckbox", { opts = { nargs = 1, desc = "Set the value of a checkbox" } }) + M.register("ObsidianToggleCheckbox", { opts = { nargs = 0, desc = "Toggle checkbox" } }) M.register("ObsidianWorkspace", { opts = { nargs = "?", desc = "Check or switch workspace" } }) diff --git a/lua/obsidian/commands/set_checkbox.lua b/lua/obsidian/commands/set_checkbox.lua new file mode 100644 index 000000000..2d29206b7 --- /dev/null +++ b/lua/obsidian/commands/set_checkbox.lua @@ -0,0 +1,10 @@ +local set_checkbox = require("obsidian.util").set_checkbox + +---@param client obsidian.Client +return function(client, data) + -- If no check_char is provided, default to "x" + if string.len(data.args) == 0 then + data.args = "x" + end + set_checkbox(data.args) +end diff --git a/lua/obsidian/util.lua b/lua/obsidian/util.lua index 1db3d044e..709463bf2 100644 --- a/lua/obsidian/util.lua +++ b/lua/obsidian/util.lua @@ -513,6 +513,7 @@ util.toggle_checkbox = function(opts, line_num) local checkboxes = opts or { " ", "x" } if not string.match(line, checkbox_pattern) then + -- Add an empty checkbox if one is not found. local unordered_list_pattern = "^(%s*)[-*+] (.*)" if string.match(line, unordered_list_pattern) then line = string.gsub(line, unordered_list_pattern, "%1- [ ] %2") @@ -534,6 +535,22 @@ util.toggle_checkbox = function(opts, line_num) vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, true, { line }) end +---Set the value of a checkbox to a given check_char +util.set_checkbox = function(check_char, line_num) + -- Allow line_num to be optional, defaulting to the current line if not provided + line_num = line_num or unpack(vim.api.nvim_win_get_cursor(0)) + local line = vim.api.nvim_buf_get_lines(0, line_num - 1, line_num, false)[1] + + local checkbox_pattern = "^%s*- %[.] " + + if string.match(line, checkbox_pattern) then + -- Only set the check_char if the line contains the checkbox pattern. + line = util.string_replace(line, "- [" .. check_char .. "]", "- [" .. check_char .. "]", 1) + end + -- 0-indexed + vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, true, { line }) +end + ---Determines if the given date is a working day (not weekend) --- ---@param time integer