dotfiles

Personal dotfiles
git clone https://git.bracken.jp/dotfiles.git
Log | Files | Refs | LICENSE

utils.lua (2896B)


      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 local function get_monday_time(time_table)
     23   local d = os.date("*t", os.time(time_table))
     24   local days_to_monday = (d.wday - 2 + 7) % 7
     25   return os.time({
     26     year = d.year,
     27     month = d.month,
     28     day = d.day - days_to_monday,
     29   })
     30 end
     31 
     32 function M.jump_daily(offset)
     33   local notes_dir = vim.g.notes_dir
     34   assert(notes_dir, "vim.g.notes_dir is not set")
     35   offset = tonumber(offset) or 0
     36 
     37   local current_file = vim.api.nvim_buf_get_name(0)
     38   local year, month, day = current_file:match("(%d%d%d%d)%-(%d%d)%-(%d%d)%.md$")
     39 
     40   local target_time
     41   if offset == 0 then
     42     -- Jump to current week's Monday (based on today).
     43     target_time = get_monday_time(os.date("*t"))
     44   elseif year and month and day then
     45     -- Offset relative to current note's Monday.
     46     local monday_time = get_monday_time({
     47       year = tonumber(year),
     48       month = tonumber(month),
     49       day = tonumber(day),
     50     })
     51     local monday_date = os.date("*t", monday_time)
     52     target_time = os.time({
     53       year = monday_date.year,
     54       month = monday_date.month,
     55       day = monday_date.day + (offset * 7),
     56     })
     57   else
     58     -- Unable to resolve note date; offset relative to current week's Monday.
     59     local monday_time = get_monday_time(os.date("*t"))
     60     local monday_date = os.date("*t", monday_time)
     61     target_time = os.time({
     62       year = monday_date.year,
     63       month = monday_date.month,
     64       day = monday_date.day + (offset * 7),
     65     })
     66   end
     67 
     68   local target_date = os.date("*t", target_time)
     69   local formatted_date = string.format("%04d/%04d-%02d-%02d",
     70     target_date.year, target_date.year, target_date.month, target_date.day)
     71 
     72   local expanded_dir = vim.fn.expand(notes_dir)
     73   local path = vim.fs.joinpath(expanded_dir, formatted_date .. ".md")
     74 
     75   -- Create parent directories if they don't exist.
     76   vim.fn.mkdir(vim.fn.fnamemodify(path, ":h"), "p")
     77 
     78   vim.cmd("edit " .. vim.fn.fnameescape(path))
     79 end
     80 
     81 function M.jump_tasks(offset)
     82   local notes_dir = vim.g.notes_dir
     83   assert(notes_dir, "vim.g.notes_dir is not set")
     84 
     85   local expanded_dir = vim.fn.expand(notes_dir)
     86   local path = vim.fs.joinpath(expanded_dir, "Tasks.md")
     87 
     88   -- Create parent directories if they don't exist.
     89   local dir = vim.fn.fnamemodify(path, ":h")
     90 
     91   vim.cmd("edit " .. vim.fn.fnameescape(path))
     92 end
     93 
     94 return M