utils.lua (2302B)
1 local M = {} 2 3 function M.change_google3_subdir(subdir) 4 return function() 5 local cwd = vim.fn.getcwd() 6 local google3_pattern = "/google3" 7 local google3_start, google3_end = string.find(cwd, google3_pattern, 1, true) 8 9 if google3_start then 10 local root = string.sub(cwd, 1, google3_end) 11 local new_dir = root .. "/" .. subdir 12 if vim.fn.isdirectory(new_dir) == 1 then 13 vim.cmd("cd " .. new_dir) 14 vim.notify("Changed directory to: " .. new_dir, vim.log.levels.INFO) 15 else 16 vim.notify("Directory not found: " .. new_dir, vim.log.levels.WARN) 17 end 18 end 19 end 20 end 21 22 function M.jump_daily(offset) 23 local notes_dir = vim.g.notes_dir 24 assert(notes_dir, "vim.g.notes_dir is not set") 25 offset = tonumber(offset) or 0 26 27 local current_file = vim.api.nvim_buf_get_name(0) 28 local year, month, day = current_file:match("(%d%d%d%d)%-(%d%d)%-(%d%d)%.md$") 29 30 local target_time 31 if offset == 0 then 32 -- Jump to today. 33 target_time = os.time() 34 elseif year and month and day then 35 -- Offset relative to note date. 36 target_time = os.time({ 37 year = tonumber(year), 38 month = tonumber(month), 39 day = tonumber(day) + offset, 40 }) 41 else 42 -- Unable to resolve note date; offset relative to today. 43 local today = os.date("*t") 44 target_time = os.time({ 45 year = today.year, 46 month = today.month, 47 day = today.day + offset, 48 }) 49 end 50 51 local target_date = os.date("*t", target_time) 52 local formatted_date = string.format("%04d/%02d/%04d-%02d-%02d", 53 target_date.year, target_date.month, target_date.year, target_date.month, target_date.day) 54 55 local expanded_dir = vim.fn.expand(notes_dir) 56 local path = vim.fs.joinpath(expanded_dir, formatted_date .. ".md") 57 58 -- Create parent directories if they don't exist. 59 vim.fn.mkdir(vim.fn.fnamemodify(path, ":h"), "p") 60 61 vim.cmd("edit " .. vim.fn.fnameescape(path)) 62 end 63 64 function M.jump_tasks(offset) 65 local notes_dir = vim.g.notes_dir 66 assert(notes_dir, "vim.g.notes_dir is not set") 67 68 local expanded_dir = vim.fn.expand(notes_dir) 69 local path = vim.fs.joinpath(expanded_dir, "Tasks.md") 70 71 -- Create parent directories if they don't exist. 72 local dir = vim.fn.fnamemodify(path, ":h") 73 74 vim.cmd("edit " .. vim.fn.fnameescape(path)) 75 end 76 77 return M