1 -------------------------------------------------------------------------------
2 -- Tools to deal with notes
3 -------------------------------------------------------------------------------
6 local Util = require('nota.util')
7 local Configuration = require('nota.configuration')
9 -------------------------------------------------------------------------------
11 -------------------------------------------------------------------------------
20 daily = '^(%d+)-(%d+)-(%d+).md$',
21 weekly = '^(%d+)-w(%d+).md$',
22 monthly = '^(%d+)-(%d+).md$',
23 seasonal = '^(%d+)-s(%d+).md$',
27 local function open_or_create_from_template(type, file_path)
29 local journal_file = io.open(file_path, 'r')
30 if not journal_file then
31 local template_contents = Configuration.load_template(type)
33 journal_file = io.open(file_path, 'w')
34 journal_file:write(template_contents)
37 vim.cmd('edit ' .. file_path)
40 local function open_periodic_note(type, filename)
41 local file_directory_path = Configuration.path_for(Configuration.configuration.periodic_locations[type])
43 Util.ensure_directory_exists(file_directory_path)
44 local file_path = Util.join(file_directory_path, filename)
46 open_or_create_from_template(type, file_path)
49 local function find_next(type, filename, go_back_in_time)
51 if go_back_in_time then
55 local pattern = patterns[type]
56 local year, second_match, day = filename:match(pattern)
59 -- Depending on context, second_match is either month, week, or season.
61 second_match = tonumber(second_match)
70 if type == 'daily' and year and second_match and day then
71 next = os.date(templates.daily, os.time({
75 }) + multiplier * 24 * 60 * 60)
76 elseif type == 'weekly' and year and second_match then
77 -- According to ISO, the first week is the one that contains january 4
78 local first_day_of_the_week = os.time({year = year, month = 1, day = 4})
79 if multiplier < 0 then
80 second_match = second_match - 2
82 next = os.date(templates.weekly, first_day_of_the_week + second_match * 7 * 24 * 60 * 60)
83 elseif type == 'monthly' and year and second_match then
84 next = os.date(templates.monthly, os.time({
86 month = second_match + multiplier,
89 elseif type == 'seasonal' and year and second_match then
90 if multiplier > 0 and second_match == 4 then
93 if multiplier < 0 and second_match == 1 then
96 if multiplier > 0 then
97 second_match = (second_match % 4) + 1
99 second_match = (second_match + 2) % 4 + 1
101 next = year .. '-s' .. second_match
102 elseif type == 'yearly' and year then
103 next = os.date(templates.yearly, os.time({
104 year = year + multiplier,
111 Notes['open_' .. type](next)
115 -------------------------------------------------------------------------------
117 -------------------------------------------------------------------------------
119 --- Opens the daily note
120 function Notes.open_daily(date)
121 if not date or date == '' then
122 date = os.date(templates.daily)
124 local filename = date .. '.md'
125 open_periodic_note('daily', filename)
128 --- Opens the weekly note
129 function Notes.open_weekly(date)
130 if not date or date == '' then
131 date = os.date(templates.weekly)
133 local filename = date .. '.md'
134 open_periodic_note('weekly', filename)
137 --- Opens the monthly note
138 function Notes.open_monthly(date)
139 if not date or date == '' then
140 date = os.date(templates.monthly)
142 local filename = date .. '.md'
143 open_periodic_note('monthly', filename)
146 --- Opens the seasonal note
147 function Notes.open_seasonal(date)
148 if not date or date == '' then
149 local year = os.date('%Y')
150 local month = tonumber(os.date('%m'))
151 local season = math.ceil(month / 3)
152 date = year .. '-s' .. season
155 local filename = date .. '.md'
156 open_periodic_note('seasonal', filename)
159 --- Opens the yearly note
160 function Notes.open_yearly(date)
161 if not date or date == '' then
162 date = os.date(templates.yearly)
164 local filename = date .. '.md'
165 open_periodic_note('yearly', filename)
168 --- Opens the next note
169 function Notes.open_next()
170 local filename = vim.fn.expand('%:t')
171 for type, pattern in pairs(patterns) do
172 if filename:match(pattern) ~= nil then
173 return find_next(type, filename)
176 api.nvim_err_writeln('Opening next only works on periodic notes.')
179 --- Opens the previous note
180 function Notes.open_previous()
181 local filename = vim.fn.expand('%:t')
182 for type, pattern in pairs(patterns) do
183 if filename:match(pattern) ~= nil then
184 return find_next(type, filename, true)
187 api.nvim_err_writeln('Opening previous only works on periodic notes.')
190 --- Opens an arbitrary note
191 function Notes.open()
192 local success, module = pcall(require, 'fzf-lua')
194 local notes_path = Configuration.path_for()
195 Util.ensure_directory_exists(notes_path)
196 module.files({ cwd = notes_path })
198 api.nvim_err_writeln('This feature requires optional dependency fzf-lua')