Skip to content

Commit fafb8f1

Browse files
Fix stale clocked header references when making edits to headers (#759)
Co-authored-by: Kristijan Husak <husakkristijan@gmail.com>
1 parent 8b1dfcd commit fafb8f1

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lua/orgmode/clock/init.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,20 @@ function Clock:init()
2626
end
2727
end
2828

29+
function Clock:update_clocked_headline()
30+
local last_clocked_headline = self.files:get_clocked_headline()
31+
if last_clocked_headline and last_clocked_headline:is_clocked_in() then
32+
self.clocked_headline = last_clocked_headline
33+
end
34+
end
35+
2936
function Clock:has_clocked_headline()
37+
self:update_clocked_headline()
3038
return self.clocked_headline ~= nil
3139
end
3240

3341
function Clock:org_clock_in()
42+
self:update_clocked_headline()
3443
local item = self.files:get_closest_headline()
3544
if item:is_clocked_in() then
3645
return utils.echo_info(string.format('Clock continues in "%s"', item:get_title()))
@@ -53,6 +62,7 @@ function Clock:org_clock_in()
5362
end
5463

5564
function Clock:org_clock_out()
65+
self:update_clocked_headline()
5666
if not self.clocked_headline or not self.clocked_headline:is_clocked_in() then
5767
return
5868
end
@@ -62,6 +72,7 @@ function Clock:org_clock_out()
6272
end
6373

6474
function Clock:org_clock_cancel()
75+
self:update_clocked_headline()
6576
if not self.clocked_headline or not self.clocked_headline:is_clocked_in() then
6677
return utils.echo_info('No active clock')
6778
end
@@ -72,6 +83,7 @@ function Clock:org_clock_cancel()
7283
end
7384

7485
function Clock:org_clock_goto()
86+
self:update_clocked_headline()
7587
if not self.clocked_headline then
7688
return utils.echo_info('No active or recent clock task')
7789
end

tests/plenary/ui/clock_spec.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local helpers = require('tests.plenary.helpers')
22
local Date = require('orgmode.objects.date')
3+
local orgmode = require('orgmode')
34

45
describe('Clock', function()
56
local files = {}
@@ -149,4 +150,39 @@ describe('Clock', function()
149150
assert.are.same('', vim.fn.getline(6))
150151
assert.are.same('', vim.fn.getline(7))
151152
end)
153+
154+
it('should properly clock in an entry if unsaved edits were made to the buffer', function()
155+
local file = helpers.create_agenda_file({
156+
'* TODO Test 1',
157+
' :LOGBOOK:',
158+
' CLOCK: [2024-05-22 Wed 05:15]',
159+
' :END:',
160+
'* TODO Test 2',
161+
})
162+
163+
vim.cmd('edit ' .. file.filename)
164+
165+
-- Establish baseline: Test 1 is clocked in
166+
local clock = orgmode.clock
167+
assert.are.same('Test 1', clock.clocked_headline:get_title())
168+
assert.is_true(clock.clocked_headline:is_clocked_in())
169+
170+
-- Move the test 2 header above test 1 and then clock test 2 in
171+
vim.fn.cursor({ 5, 1 })
172+
vim.cmd([[norm dd]])
173+
vim.fn.cursor({ 1, 1 })
174+
vim.cmd([[norm P]])
175+
vim.fn.cursor({ 1, 1 })
176+
clock:org_clock_in():wait()
177+
file:reload():wait()
178+
179+
-- Test 2 is properly clocked in
180+
assert.are.same('Test 2', clock.clocked_headline:get_title())
181+
assert.are.same('Test 2', file:get_headlines()[1]:get_title())
182+
assert.is_true(file:get_headlines()[1]:is_clocked_in())
183+
184+
-- Test 1 is properly clocked out
185+
assert.are.same('Test 1', file:get_headlines()[2]:get_title())
186+
assert.is_false(file:get_headlines()[2]:is_clocked_in())
187+
end)
152188
end)

0 commit comments

Comments
 (0)