Skip to content

Add ObsidianSetCheckbox (Attempt) #802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions lua/obsidian/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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" } })
Expand Down
6 changes: 6 additions & 0 deletions lua/obsidian/commands/set_checkbox.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local set_checkbox = require("obsidian.util").set_checkbox

---@param client obsidian.Client
return function(client, data)
set_checkbox(data.args)
end
20 changes: 20 additions & 0 deletions lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -534,6 +535,25 @@ 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*)- %[.%] "

local indent, rest = string.match(line, checkbox_pattern .. "(.*)")

if indent and rest then
-- Rebuild the line with the new check_char
line = indent .. "- [" .. check_char .. "] " .. rest

-- Update the line in the buffer (0-indexed)
vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, true, { line })
end
end

---Determines if the given date is a working day (not weekend)
---
---@param time integer
Expand Down
Loading