diff --git a/lua/orgmode/clock/init.lua b/lua/orgmode/clock/init.lua index 20020eb09..19d5d6001 100644 --- a/lua/orgmode/clock/init.lua +++ b/lua/orgmode/clock/init.lua @@ -26,11 +26,20 @@ function Clock:init() end end +function Clock:update_clocked_headline() + local last_clocked_headline = self.files:get_clocked_headline() + if last_clocked_headline and last_clocked_headline:is_clocked_in() then + self.clocked_headline = last_clocked_headline + end +end + function Clock:has_clocked_headline() + self:update_clocked_headline() return self.clocked_headline ~= nil end function Clock:org_clock_in() + self:update_clocked_headline() local item = self.files:get_closest_headline() if item:is_clocked_in() then return utils.echo_info(string.format('Clock continues in "%s"', item:get_title())) @@ -53,6 +62,7 @@ function Clock:org_clock_in() end function Clock:org_clock_out() + self:update_clocked_headline() if not self.clocked_headline or not self.clocked_headline:is_clocked_in() then return end @@ -62,6 +72,7 @@ function Clock:org_clock_out() end function Clock:org_clock_cancel() + self:update_clocked_headline() if not self.clocked_headline or not self.clocked_headline:is_clocked_in() then return utils.echo_info('No active clock') end @@ -72,6 +83,7 @@ function Clock:org_clock_cancel() end function Clock:org_clock_goto() + self:update_clocked_headline() if not self.clocked_headline then return utils.echo_info('No active or recent clock task') end diff --git a/tests/plenary/ui/clock_spec.lua b/tests/plenary/ui/clock_spec.lua index 39f2d7fed..6ee39ba92 100644 --- a/tests/plenary/ui/clock_spec.lua +++ b/tests/plenary/ui/clock_spec.lua @@ -1,5 +1,6 @@ local helpers = require('tests.plenary.helpers') local Date = require('orgmode.objects.date') +local orgmode = require('orgmode') describe('Clock', function() local files = {} @@ -149,4 +150,39 @@ describe('Clock', function() assert.are.same('', vim.fn.getline(6)) assert.are.same('', vim.fn.getline(7)) end) + + it('should properly clock in an entry if unsaved edits were made to the buffer', function() + local file = helpers.create_agenda_file({ + '* TODO Test 1', + ' :LOGBOOK:', + ' CLOCK: [2024-05-22 Wed 05:15]', + ' :END:', + '* TODO Test 2', + }) + + vim.cmd('edit ' .. file.filename) + + -- Establish baseline: Test 1 is clocked in + local clock = orgmode.clock + assert.are.same('Test 1', clock.clocked_headline:get_title()) + assert.is_true(clock.clocked_headline:is_clocked_in()) + + -- Move the test 2 header above test 1 and then clock test 2 in + vim.fn.cursor({ 5, 1 }) + vim.cmd([[norm dd]]) + vim.fn.cursor({ 1, 1 }) + vim.cmd([[norm P]]) + vim.fn.cursor({ 1, 1 }) + clock:org_clock_in():wait() + file:reload():wait() + + -- Test 2 is properly clocked in + assert.are.same('Test 2', clock.clocked_headline:get_title()) + assert.are.same('Test 2', file:get_headlines()[1]:get_title()) + assert.is_true(file:get_headlines()[1]:is_clocked_in()) + + -- Test 1 is properly clocked out + assert.are.same('Test 1', file:get_headlines()[2]:get_title()) + assert.is_false(file:get_headlines()[2]:is_clocked_in()) + end) end)