1 local brain_home = os.getenv('HOME') .. '/brain'
2 local templates_path = brain_home .. '/1 periodic/99 templates'
4 local function load_template(template_name)
5 local template_path = brain_home .. templates_path .. '/' .. template_name
7 local template_file = io.open(template_path, 'r')
8 if not template_file then
9 print('Template file not found')
12 local content = template_file:read('*a')
16 local function open_or_create_from_template(template_name, file)
17 local journal_file = io.open(file, 'r')
18 if not journal_file then
19 local template_contents = load_template(template_name)
21 journal_file = io.open(file, 'w')
22 journal_file:write(template_contents)
25 vim.cmd('edit ' .. file)
28 local function get_this_weeks_files()
29 local today = os.time()
30 local day_of_week = os.date('*t', today).wday
31 local week_start = today - (day_of_week - 2) * 86400
35 local date = os.date('*t', week_start + i * 86400)
36 table.insert(filenames, string.format('%04d-%02d-%02d.md', date.year, date.month, date.day))
42 -------------------------------------------------------------------------------
44 -------------------------------------------------------------------------------
46 function open_daily_note()
47 local filename_format = os.date('%Y-%m-%d')
48 local template_name = '001 daily.md'
50 local daily_note = brain_home .. '/1 periodic/01 journal/' .. filename_format .. '.md'
51 open_or_create_from_template(template_name, daily_note)
54 function open_weekly_note()
55 local filename_format = os.date('%Y-w%V')
56 local template_name = '002 weekly.md'
58 local weekly_note = brain_home .. '/1 periodic/02 weeks/' .. filename_format .. '.md'
59 open_or_create_from_template(template_name, weekly_note)
62 -------------------------------------------------------------------------------
64 -------------------------------------------------------------------------------
66 function toggle_task()
67 local line_num = vim.api.nvim_win_get_cursor(0)[1]
68 local line = vim.api.nvim_get_current_line()
70 local unchecked_pattern = '^%s*%- %[ %]'
71 local checked_pattern = '^%s*%- %[x%]'
73 if line:match(unchecked_pattern) then
74 line = line:gsub(unchecked_pattern, '- [x]', 1)
75 elseif line:match(checked_pattern) then
76 line = line:gsub(checked_pattern, '- [ ]', 1)
79 vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, false, {line})
82 -------------------------------------------------------------------------------
84 -------------------------------------------------------------------------------
86 function open_agenda()
88 local week_filenames = get_this_weeks_files()
90 local pattern = '^%s*%- %[[ ]?x?%]'
91 print(week_filenames[1])
93 for _, filename in ipairs(week_filenames) do
94 local daily_note = brain_home .. '/1 periodic/01 journal/' .. filename
95 local stat = uv.fs_stat(daily_note)
97 if stat then -- File exists
98 local file, err = io.open(daily_note, 'r')
101 local line_number = 0
102 for line in file:lines() do
103 line_number = line_number + 1
104 if line:match(pattern) then
105 if set_header == 0 then
106 local header = filename:match('([^/\\]+)$')
107 table.insert(items, {filename = '', lnum = 0, text = header})
110 table.insert(items, {filename = daily_note, text = line, lnum = line_number})
117 vim.fn.setloclist(0, {}, ' ', {title = 'Weekly Tasks', items = items})
121 -------------------------------------------------------------------------------
123 -------------------------------------------------------------------------------
124 vim.api.nvim_set_keymap('n', '<leader>od', '<cmd>lua open_daily_note()<CR>', { noremap = true, silent = true })
125 vim.api.nvim_set_keymap('n', '<leader>ow', '<cmd>lua open_weekly_note()<CR>', { noremap = true, silent = true })
126 vim.api.nvim_set_keymap('n', '<leader>oa', '<cmd>lua open_agenda()<CR>', { noremap = true, silent = true })
127 vim.api.nvim_set_keymap('n', '<leader>t', '<cmd>lua toggle_task()<CR>', { noremap = true, silent = true })