Skip to content

Commit b681e55

Browse files
fix(todo_keywords): Do not set todo keyword on repeater if there was not any before
Closes #1096
1 parent 0f86532 commit b681e55

3 files changed

Lines changed: 12 additions & 21 deletions

File tree

lua/orgmode/objects/todo_state.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ function TodoState:_navigate_within_sequence(direction)
153153
end
154154

155155
---@param headline OrgHeadline|nil
156-
---@return OrgTodoKeyword
157-
function TodoState:get_reset_todo(headline)
156+
---@param initial_todo_keyword string | nil The initial TODO keyword of the headline
157+
---@return OrgTodoKeyword | ''
158+
function TodoState:get_reset_todo(headline, initial_todo_keyword)
158159
local repeat_to_state = (headline and headline:get_property('REPEAT_TO_STATE'))
159160
or config.opts.org_todo_repeat_to_state
160161
local todo_keyword = self.todos:find(repeat_to_state)
@@ -163,6 +164,11 @@ function TodoState:get_reset_todo(headline)
163164
return todo_keyword
164165
end
165166

167+
-- If there was no TODO at the beginning, make sure we don't set one
168+
if not initial_todo_keyword then
169+
return TodoKeyword:empty()
170+
end
171+
166172
-- For repeating tasks, reset to first TODO of the same sequence
167173
if headline and self.current_state and not self.current_state:is_empty() then
168174
local seq_idx = self.current_state.sequence_index

lua/orgmode/org/mappings.lua

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -476,14 +476,9 @@ function OrgMappings:_todo_change_state(direction)
476476
-- Reset to first TODO of the same sequence for repeating tasks
477477
local todos = item.file:get_todo_keywords()
478478
local todo_state = TodoState:new({ current_state = new_todo, todos = todos })
479-
local reset_keyword = todo_state:get_reset_todo(item)
479+
local reset_keyword = todo_state:get_reset_todo(item, old_state)
480480

481-
if reset_keyword then
482-
item:set_todo(reset_keyword.value)
483-
else
484-
self:_change_todo_state('reset')
485-
new_todo = item:get_todo()
486-
end
481+
item:set_todo(reset_keyword.value)
487482

488483
local prompt_repeat_note = config.org_log_repeat == 'note'
489484
local log_repeat_enabled = config.org_log_repeat ~= false
@@ -1066,14 +1061,6 @@ function OrgMappings:_change_todo_state(direction, use_fast_access)
10661061

10671062
local todos = headline.file:get_todo_keywords()
10681063

1069-
-- Store the sequence index of the original keyword, if any
1070-
local original_sequence_index = nil
1071-
local current_keyword_obj = todos:find(current_keyword)
1072-
1073-
if current_keyword_obj then
1074-
original_sequence_index = current_keyword_obj.sequence_index
1075-
end
1076-
10771064
local todo_state = TodoState:new({ current_state = current_keyword, todos = todos })
10781065
local next_state = nil
10791066

@@ -1084,8 +1071,6 @@ function OrgMappings:_change_todo_state(direction, use_fast_access)
10841071
next_state = todo_state:get_next()
10851072
elseif direction == 'prev' then
10861073
next_state = todo_state:get_prev()
1087-
elseif direction == 'reset' then
1088-
next_state = todo_state:get_reset_todo(headline)
10891074
end
10901075
end
10911076

tests/plenary/object/todo_state_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ describe('Todo state', function()
188188
current_state = headline1:get_todo(),
189189
todos = file:get_todo_keywords(),
190190
})
191-
local reset_state1 = todo_state1:get_reset_todo(headline1)
191+
local reset_state1 = todo_state1:get_reset_todo(headline1, 'TODO')
192192

193193
-- It should reset to TODO, which is the first state in the first sequence
194194
assert.are.equal('TODO', reset_state1.value)
@@ -199,7 +199,7 @@ describe('Todo state', function()
199199
current_state = headline2:get_todo(),
200200
todos = file:get_todo_keywords(),
201201
})
202-
local reset_state2 = todo_state2:get_reset_todo(headline2)
202+
local reset_state2 = todo_state2:get_reset_todo(headline2, 'MEETING')
203203

204204
-- It should reset to MEETING, which is the first state in the second sequence
205205
assert.are.equal('MEETING', reset_state2.value)

0 commit comments

Comments
 (0)