Skip to content

Commit 9f9c4ad

Browse files
committed
Update cookies from multiple list
1 parent dafb6aa commit 9f9c4ad

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lua/orgmode/files/elements/listitem.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function Listitem:update_checkbox(action)
7171
else
7272
local parent_headline = self.file:get_closest_headline_or_nil()
7373
if parent_headline then
74-
parent_headline:update_cookie(parent_list)
74+
parent_headline:update_cookie()
7575
end
7676
end
7777
end

lua/orgmode/files/headline.lua

+22-7
Original file line numberDiff line numberDiff line change
@@ -890,19 +890,34 @@ function Headline:get_cookie()
890890
return self:_parse_title_part('%[%d?%d?%d?%%%]')
891891
end
892892

893-
function Headline:update_cookie(list_node)
894-
local total_boxes = self:child_checkboxes(list_node)
895-
local checked_boxes = vim.tbl_filter(function(box)
896-
return box:match('%[%w%]')
897-
end, total_boxes)
893+
function Headline:update_cookie()
894+
local section = self:node():parent()
895+
if not section then
896+
return nil
897+
end
898+
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+
end
911+
end
898912

913+
-- update the cookie
899914
local cookie = self:get_cookie()
900915
if cookie then
901916
local new_cookie_val
902917
if self.file:get_node_text(cookie):find('%%') then
903-
new_cookie_val = ('[%d%%]'):format((#checked_boxes / #total_boxes) * 100)
918+
new_cookie_val = ('[%d%%]'):format((num_checked_boxes / num_boxes) * 100)
904919
else
905-
new_cookie_val = ('[%d/%d]'):format(#checked_boxes, #total_boxes)
920+
new_cookie_val = ('[%d/%d]'):format(num_checked_boxes, num_boxes)
906921
end
907922
return self:_set_node_text(cookie, new_cookie_val)
908923
end

0 commit comments

Comments
 (0)