Skip to content
12 changes: 12 additions & 0 deletions lua/orgmode/clock/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
36 changes: 36 additions & 0 deletions tests/plenary/ui/clock_spec.lua
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand Down Expand Up @@ -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)
Loading