Skip to content

Commit 8c69d03

Browse files
committed
update parent cookie from todo
1 parent 675257c commit 8c69d03

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

lua/orgmode/files/headline.lua

+45-25
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,14 @@ end
357357
function Headline:set_todo(keyword)
358358
local todo, node = self:get_todo()
359359
if todo then
360-
return self:_set_node_text(node, keyword)
360+
self:_set_node_text(node, keyword)
361+
return self:update_parent_cookie_from_todo()
361362
end
362363

363364
local stars = self:_get_child_node('stars')
364365
local _, level = stars:end_()
365-
return self:_set_node_text(stars, ('%s %s'):format(('*'):rep(level), keyword))
366+
self:_set_node_text(stars, ('%s %s'):format(('*'):rep(level), keyword))
367+
return self:update_parent_cookie_from_todo()
366368
end
367369

368370
memoize('get_todo')
@@ -882,6 +884,14 @@ function Headline:remove_scheduled_date()
882884
return self:_remove_date('SCHEDULED')
883885
end
884886

887+
function Headline:update_parent_cookie_from_todo()
888+
local parent = self:get_parent_headline()
889+
if parent and parent.headline then
890+
parent:update_cookie()
891+
end
892+
return self
893+
end
894+
885895
function Headline:get_cookie()
886896
local cookie = self:_parse_title_part('%[%d*/%d*%]')
887897
if cookie then
@@ -891,37 +901,47 @@ function Headline:get_cookie()
891901
end
892902

893903
function Headline:update_cookie()
894-
local section = self:node():parent()
895-
if not section then
904+
-- Return early if the headline doesn't have a cookie
905+
local cookie = self:get_cookie()
906+
if not cookie then
896907
return self
897908
end
898909

899-
-- Go through all the lists in this headline and gather checked_boxes
900-
local num_boxes, num_checked_boxes = 0, 0
901-
local body = section:field('body')[1]
902-
for node in body:iter_children() do
903-
if node:type() == 'list' then
904-
local boxes = self:child_checkboxes(node)
905-
num_boxes = num_boxes + #boxes
906-
local checked_boxes = vim.tbl_filter(function(box)
907-
return box:match('%[%w%]')
908-
end, boxes)
909-
num_checked_boxes = num_checked_boxes + #checked_boxes
910+
local num, denum = 0, 0
911+
-- Count checked boxes from all lists
912+
local section = self:node():parent()
913+
if section then
914+
local body = section:field('body')[1]
915+
if body then
916+
for node in body:iter_children() do
917+
if node:type() == 'list' then
918+
local boxes = self:child_checkboxes(node)
919+
denum = denum + #boxes
920+
local checked_boxes = vim.tbl_filter(function(box)
921+
return box:match('%[%w%]')
922+
end, boxes)
923+
num = num + #checked_boxes
924+
end
925+
end
910926
end
911927
end
912928

929+
-- Count done children headlines
930+
local children = self:get_child_headlines()
931+
local dones = vim.tbl_filter(function(h)
932+
return h:is_done()
933+
end, children)
934+
num = num + #dones
935+
denum = denum + #children
936+
913937
-- Update the cookie
914-
local cookie = self:get_cookie()
915-
if cookie then
916-
local new_cookie_val
917-
if self.file:get_node_text(cookie):find('%%') then
918-
new_cookie_val = ('[%d%%]'):format((num_checked_boxes / num_boxes) * 100)
919-
else
920-
new_cookie_val = ('[%d/%d]'):format(num_checked_boxes, num_boxes)
921-
end
922-
return self:_set_node_text(cookie, new_cookie_val)
938+
local new_cookie_val
939+
if self.file:get_node_text(cookie):find('%%') then
940+
new_cookie_val = ('[%d%%]'):format((num / denum) * 100)
941+
else
942+
new_cookie_val = ('[%d/%d]'):format(num, denum)
923943
end
924-
return self
944+
return self:_set_node_text(cookie, new_cookie_val)
925945
end
926946

927947
function Headline:child_checkboxes(list_node)

0 commit comments

Comments
 (0)