]> git.r.bdr.sh - rbdr/dotfiles/blob - config/nvim/lua/brain.lua
Add agenda
[rbdr/dotfiles] / config / nvim / lua / brain.lua
1 local brain_home = os.getenv('HOME') .. '/brain'
2 local templates_path = brain_home .. '/1 periodic/99 templates'
3
4 local function load_template(template_name)
5 local template_path = brain_home .. templates_path .. '/' .. template_name
6
7 local template_file = io.open(template_path, 'r')
8 if not template_file then
9 print('Template file not found')
10 return
11 end
12 local content = template_file:read('*a')
13 template_file:close()
14 end
15
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)
20
21 journal_file = io.open(file, 'w')
22 journal_file:write(template_contents)
23 journal_file:close()
24 end
25 vim.cmd('edit ' .. file)
26 end
27
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
32 local filenames = {}
33
34 for i = 0, 6 do
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))
37 end
38
39 return filenames
40 end
41
42 -------------------------------------------------------------------------------
43 -- Periodic Notes
44 -------------------------------------------------------------------------------
45
46 function open_daily_note()
47 local filename_format = os.date('%Y-%m-%d')
48 local template_name = '001 daily.md'
49
50 local daily_note = brain_home .. '/1 periodic/01 journal/' .. filename_format .. '.md'
51 open_or_create_from_template(template_name, daily_note)
52 end
53
54 function open_weekly_note()
55 local filename_format = os.date('%Y-w%V')
56 local template_name = '002 weekly.md'
57
58 local weekly_note = brain_home .. '/1 periodic/02 weeks/' .. filename_format .. '.md'
59 open_or_create_from_template(template_name, weekly_note)
60 end
61
62 -------------------------------------------------------------------------------
63 -- Task Utilities
64 -------------------------------------------------------------------------------
65
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()
69
70 local unchecked_pattern = '^%s*%- %[ %]'
71 local checked_pattern = '^%s*%- %[x%]'
72
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)
77 end
78
79 vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, false, {line})
80 end
81
82 -------------------------------------------------------------------------------
83 -- Agenda View
84 -------------------------------------------------------------------------------
85
86 function open_agenda()
87 local uv = vim.loop
88 local week_filenames = get_this_weeks_files()
89 local items = {}
90 local pattern = '^%s*%- %[[ ]?x?%]'
91 print(week_filenames[1])
92
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)
96
97 if stat then -- File exists
98 local file, err = io.open(daily_note, 'r')
99 if file then
100 local set_header = 0
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})
108 set_header = 1
109 end
110 table.insert(items, {filename = daily_note, text = line, lnum = line_number})
111 end
112 end
113 file:close()
114 end
115 end
116 end
117 vim.fn.setloclist(0, {}, ' ', {title = 'Weekly Tasks', items = items})
118 vim.cmd('lopen')
119 end
120
121 -------------------------------------------------------------------------------
122 -- Key Bindings
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 })