commit 3212b021319006dae92c71990e91e7ffe1207319
parent 0f8fdbe1fda1579ec0debaba9d2f5f13e1ef9efa
Author: Chris Bracken <chris@bracken.jp>
Date: Mon, 8 Jun 2026 06:40:40 +0900
nvim: improve utils.jump_daily logic
When called with nil/0 jump unconditionally to today. Otherwise try to
offset relative to filename (if the filename is in ISO date format).
Otherwise offset relative to today.
Diffstat:
1 file changed, 21 insertions(+), 15 deletions(-)
diff --git a/.config/nvim/lua/custom/utils.lua b/.config/nvim/lua/custom/utils.lua
@@ -22,35 +22,41 @@ end
function M.jump_daily(offset)
local notes_dir = vim.g.notes_dir
assert(notes_dir, "vim.g.notes_dir is not set")
+ offset = tonumber(offset) or 0
local current_file = vim.api.nvim_buf_get_name(0)
local year, month, day = current_file:match("(%d%d%d%d)%-(%d%d)%-(%d%d)%.md$")
- local target_date
- if year and month and day then
- local current_time = os.time({
+ local target_time
+ if offset == 0 then
+ -- Jump to today.
+ target_time = os.time()
+ elseif year and month and day then
+ -- Offset relative to note date.
+ target_time = os.time({
year = tonumber(year),
month = tonumber(month),
- day = tonumber(day),
- hour = 12
+ day = tonumber(day) + offset,
})
- local target_time = current_time + (offset * 86400)
- target_date = os.date("*t", target_time)
else
- local target_time = os.time() + (offset * 86400)
- target_date = os.date("*t", target_time)
+ -- Unable to resolve note date; offset relative to today.
+ local today = os.date("*t")
+ target_time = os.time({
+ year = today.year,
+ month = today.month,
+ day = today.day + offset,
+ })
end
+ local target_date = os.date("*t", target_time)
local formatted_date = string.format("%04d/%02d/%04d-%02d-%02d",
target_date.year, target_date.month, target_date.year, target_date.month, target_date.day)
- local path = vim.fn.expand(notes_dir .. "/" .. formatted_date .. ".md")
+ local expanded_dir = vim.fn.expand(notes_dir)
+ local path = vim.fs.joinpath(expanded_dir, formatted_date .. ".md")
- -- Create parent directories if they don't exist
- local dir = vim.fn.fnamemodify(path, ":h")
- if vim.fn.isdirectory(dir) == 0 then
- vim.fn.mkdir(dir, "p")
- end
+ -- Create parent directories if they don't exist.
+ vim.fn.mkdir(vim.fn.fnamemodify(path, ":h"), "p")
vim.cmd("edit " .. vim.fn.fnameescape(path))
end