Skip to content

Commit 57c3899

Browse files
fix(calendar): Rerender time when switching to time picker
Also bind 'd' and 'T' all the time but ignore them when needed
1 parent c294fd4 commit 57c3899

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

lua/orgmode/objects/calendar.lua

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,12 @@ function Calendar:open()
164164
vim.keymap.set('n', 't', function()
165165
self:set_time()
166166
end, map_opts)
167-
if self:has_time() then
168-
vim.keymap.set('n', 'T', function()
169-
self:clear_time()
170-
end, map_opts)
171-
end
167+
vim.keymap.set('n', 'T', function()
168+
self:clear_time()
169+
end, map_opts)
170+
vim.keymap.set('n', 'd', function()
171+
self:set_day()
172+
end, map_opts)
172173
self:jump_day()
173174
return Promise.new(function(resolve)
174175
self.callback = resolve
@@ -235,8 +236,8 @@ function Calendar:render()
235236
table.insert(content, ' [i] - enter date')
236237
end
237238

238-
if self:has_time() or self.select_state ~= SelState.DAY then
239-
if self.select_state == SelState.DAY then
239+
if self:has_time() or self:_time_picker_active() then
240+
if not self:_time_picker_active() then
240241
table.insert(content, ' [t] - enter time [T] - clear time')
241242
else
242243
table.insert(content, ' [d] - select day [T] - clear time')
@@ -345,17 +346,10 @@ function Calendar:rerender_time()
345346
vim.api.nvim_set_option_value('modifiable', true, { buf = self.buf })
346347
vim.api.nvim_buf_set_lines(self.buf, 8, 9, true, { self:render_time() })
347348
if self:has_time() then
348-
local map_opts = { buffer = self.buf, silent = true, nowait = true }
349-
vim.keymap.set('n', 'T', function()
350-
self:clear_time()
351-
end, map_opts)
352-
vim.keymap.set('n', 'd', function()
353-
self:set_day()
354-
end, map_opts)
355-
if self.select_state == SelState.DAY then
356-
vim.api.nvim_buf_set_lines(self.buf, 13, 14, true, { ' [t] - select day [T] - clear time' })
357-
else
349+
if self:_time_picker_active() then
358350
vim.api.nvim_buf_set_lines(self.buf, 13, 14, true, { ' [d] - select day [T] - clear time' })
351+
else
352+
vim.api.nvim_buf_set_lines(self.buf, 13, 14, true, { ' [t] - enter time [T] - clear time' })
359353
end
360354
self:_apply_hl('Normal', 8, 0, -1)
361355
self:_apply_hl('Comment', 13, 0, -1)
@@ -373,7 +367,7 @@ end
373367

374368
---@private
375369
function Calendar:_ensure_day()
376-
if self.select_state ~= SelState.DAY then
370+
if self:_time_picker_active() then
377371
self:set_day()
378372
end
379373
end
@@ -397,7 +391,7 @@ function Calendar:backward()
397391
end
398392

399393
function Calendar:cursor_right()
400-
if self.select_state ~= SelState.DAY then
394+
if self:_time_picker_active() then
401395
if self.select_state == SelState.HOUR then
402396
self:set_min_big()
403397
elseif self.select_state == SelState.MIN_BIG then
@@ -420,7 +414,7 @@ function Calendar:cursor_right()
420414
end
421415

422416
function Calendar:cursor_left()
423-
if self.select_state ~= SelState.DAY then
417+
if self:_time_picker_active() then
424418
if self.select_state == SelState.HOUR then
425419
self:set_min_small()
426420
elseif self.select_state == SelState.MIN_BIG then
@@ -443,7 +437,7 @@ function Calendar:cursor_left()
443437
end
444438

445439
function Calendar:beginning()
446-
if self.select_state ~= SelState.DAY then
440+
if self:_time_picker_active() then
447441
return
448442
end
449443
local line = vim.fn.line('.')
@@ -453,7 +447,7 @@ function Calendar:beginning()
453447
end
454448

455449
function Calendar:ending()
456-
if self.select_state ~= SelState.DAY then
450+
if self:_time_picker_active() then
457451
return
458452
end
459453
local line = vim.fn.line('.')
@@ -495,7 +489,7 @@ local function step_hour(direction, current, count)
495489
end
496490

497491
function Calendar:cursor_up()
498-
if self.select_state ~= SelState.DAY then
492+
if self:_time_picker_active() then
499493
-- to avoid unexpectedly changing the day we cache it ...
500494
local day = self.date.day
501495
if self.select_state == SelState.HOUR then
@@ -537,7 +531,7 @@ function Calendar:cursor_up()
537531
end
538532

539533
function Calendar:cursor_down()
540-
if self.select_state ~= SelState.DAY then
534+
if self:_time_picker_active() then
541535
local day = self.date.day
542536
if self.select_state == SelState.HOUR then
543537
self.date = self.date:subtract(step_hour('down', self.date, vim.v.count1))
@@ -586,7 +580,7 @@ end
586580

587581
---@return OrgDate?
588582
function Calendar:get_selected_date()
589-
if self.select_state ~= SelState.DAY then
583+
if self:_time_picker_active() then
590584
return self.date
591585
end
592586
local col = vim.fn.col('.')
@@ -608,7 +602,7 @@ end
608602

609603
function Calendar:select()
610604
local selected_date
611-
if self.select_state == SelState.DAY then
605+
if not self:_time_picker_active() then
612606
selected_date = self:get_selected_date()
613607
else
614608
selected_date = self.date:set({
@@ -670,17 +664,22 @@ function Calendar:set_time()
670664
if self.date.date_only then
671665
self.date = self.date:set({ date_only = false }):set_current_time()
672666
end
673-
--self:rerender_time()
674667
self:set_sel_hour()
675668
self:render() -- because we want to highlight the currently selected date, we have to render everything
676669
end
677670

678671
function Calendar:set_day()
672+
if not self:_time_picker_active() then
673+
return
674+
end
679675
self:set_sel_day()
680676
self:rerender_time()
681677
end
682678

683679
function Calendar:clear_time()
680+
if not self:has_time() then
681+
return
682+
end
684683
self.date = self.date:set({ hour = 0, min = 0, date_only = true })
685684
self:set_sel_day()
686685
self:rerender_time()
@@ -712,4 +711,8 @@ function Calendar:jump_day()
712711
vim.fn.search(search_day, 'W')
713712
end
714713

714+
function Calendar:_time_picker_active()
715+
return self.select_state ~= SelState.DAY
716+
end
717+
715718
return Calendar

0 commit comments

Comments
 (0)