Skip to content

Commit 84ff220

Browse files
Add shortcut to calendar to clear the currently active date. Closes #397
1 parent 5b667f4 commit 84ff220

File tree

3 files changed

+66
-27
lines changed

3 files changed

+66
-27
lines changed

lua/orgmode/api/headline.lua

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,17 @@ function OrgHeadline:set_deadline(date)
132132
local headline = ts_org.closest_headline()
133133
local deadline_date = headline:deadline()
134134
if not date then
135-
return Calendar.new({ date = deadline_date or Date.today() }).open():next(function(new_date)
136-
if not new_date then
137-
return
138-
end
139-
return headline:set_deadline_date(new_date)
140-
end)
135+
return Calendar.new({ date = deadline_date or Date.today(), clearable = true })
136+
.open()
137+
:next(function(new_date, cleared)
138+
if cleared then
139+
return headline:remove_deadline_date()
140+
end
141+
if not new_date then
142+
return
143+
end
144+
return headline:set_deadline_date(new_date)
145+
end)
141146
end
142147

143148
if type(date) == 'string' then
@@ -167,12 +172,17 @@ function OrgHeadline:set_scheduled(date)
167172
local headline = ts_org.closest_headline()
168173
local scheduled_date = headline:scheduled()
169174
if not date then
170-
return Calendar.new({ date = scheduled_date or Date.today() }).open():next(function(new_date)
171-
if not new_date then
172-
return
173-
end
174-
return headline:set_scheduled_date(new_date)
175-
end)
175+
return Calendar.new({ date = scheduled_date or Date.today(), clearable = true })
176+
.open()
177+
:next(function(new_date, cleared)
178+
if cleared then
179+
return headline:remove_scheduled_date()
180+
end
181+
if not new_date then
182+
return
183+
end
184+
return headline:set_scheduled_date(new_date)
185+
end)
176186
end
177187

178188
if type(date) == 'string' then

lua/orgmode/objects/calendar.lua

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ local Calendar = {
1616
namespace = vim.api.nvim_create_namespace('org_calendar'),
1717
date = nil,
1818
month = Date.today():start_of('month'),
19+
clearable = false,
1920
}
2021

2122
vim.cmd([[hi OrgCalendarToday gui=reverse cterm=reverse]])
@@ -27,14 +28,15 @@ function Calendar.new(data)
2728
Calendar.date = data.date
2829
Calendar.month = data.date:set({ day = 1 })
2930
end
31+
Calendar.clearable = data.clearable
3032
return Calendar
3133
end
3234

3335
function Calendar.open()
3436
local opts = {
3537
relative = 'editor',
3638
width = 36,
37-
height = 10,
39+
height = Calendar.clearable and 11 or 10,
3840
style = 'minimal',
3941
border = 'single',
4042
row = vim.o.lines / 2 - 4,
@@ -76,6 +78,9 @@ function Calendar.open()
7678
vim.keymap.set('n', '.', '<cmd>lua require("orgmode.objects.calendar").reset()<CR>', map_opts)
7779
vim.keymap.set('n', 'q', ':call nvim_win_close(win_getid(), v:true)<CR>', map_opts)
7880
vim.keymap.set('n', '<Esc>', ':call nvim_win_close(win_getid(), v:true)<CR>', map_opts)
81+
if Calendar.clearable then
82+
vim.keymap.set('n', 'r', '<cmd>lua require("orgmode.objects.calendar").clear_date()<CR>', map_opts)
83+
end
7984
local search_day = Date.today():format('%d')
8085
if Calendar.date then
8186
search_day = Calendar.date:format('%d')
@@ -124,9 +129,15 @@ function Calendar.render()
124129
table.insert(value, 1, month)
125130
table.insert(value, ' [<] - prev month [>] - next month')
126131
table.insert(value, ' [.] - today [Enter] - select day')
132+
if Calendar.clearable then
133+
table.insert(value, ' [r] Clear date')
134+
end
127135

128136
vim.api.nvim_buf_set_lines(Calendar.buf, 0, -1, true, value)
129137
vim.api.nvim_buf_clear_namespace(Calendar.buf, Calendar.namespace, 0, -1)
138+
if Calendar.clearable then
139+
vim.api.nvim_buf_add_highlight(Calendar.buf, Calendar.namespace, 'Comment', #value - 3, 0, -1)
140+
end
130141
vim.api.nvim_buf_add_highlight(Calendar.buf, Calendar.namespace, 'Comment', #value - 2, 0, -1)
131142
vim.api.nvim_buf_add_highlight(Calendar.buf, Calendar.namespace, 'Comment', #value - 1, 0, -1)
132143
if is_today_month then
@@ -262,4 +273,12 @@ function Calendar.dispose()
262273
end
263274
end
264275

276+
function Calendar.clear_date()
277+
local cb = Calendar.callback
278+
Calendar.callback = nil
279+
vim.cmd([[echon]])
280+
vim.api.nvim_win_close(0, true)
281+
cb(nil, true)
282+
end
283+
265284
return Calendar

lua/orgmode/org/mappings.lua

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -763,25 +763,35 @@ end
763763
function OrgMappings:org_deadline()
764764
local headline = ts_org.closest_headline()
765765
local deadline_date = headline:deadline()
766-
return Calendar.new({ date = deadline_date or Date.today() }).open():next(function(new_date)
767-
if not new_date then
768-
return
769-
end
770-
headline:remove_closed_date()
771-
headline:set_deadline_date(new_date)
772-
end)
766+
return Calendar.new({ date = deadline_date or Date.today(), clearable = true })
767+
.open()
768+
:next(function(new_date, cleared)
769+
if cleared then
770+
return headline:remove_deadline_date()
771+
end
772+
if not new_date then
773+
return
774+
end
775+
headline:remove_closed_date()
776+
headline:set_deadline_date(new_date)
777+
end)
773778
end
774779

775780
function OrgMappings:org_schedule()
776781
local headline = ts_org.closest_headline()
777782
local scheduled_date = headline:scheduled()
778-
return Calendar.new({ date = scheduled_date or Date.today() }).open():next(function(new_date)
779-
if not new_date then
780-
return
781-
end
782-
headline:remove_closed_date()
783-
headline:set_scheduled_date(new_date)
784-
end)
783+
return Calendar.new({ date = scheduled_date or Date.today(), clearable = true })
784+
.open()
785+
:next(function(new_date, cleared)
786+
if cleared then
787+
return headline:remove_scheduled_date()
788+
end
789+
if not new_date then
790+
return
791+
end
792+
headline:remove_closed_date()
793+
headline:set_scheduled_date(new_date)
794+
end)
785795
end
786796

787797
---@param inactive boolean

0 commit comments

Comments
 (0)