Skip to content

Commit ba7a34b

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

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
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

+28-8
Original file line numberDiff line numberDiff line change
@@ -890,22 +890,42 @@ 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)
898-
893+
function Headline:set_cookie(num, denum)
899894
local cookie = self:get_cookie()
900895
if cookie then
901896
local new_cookie_val
902897
if self.file:get_node_text(cookie):find('%%') then
903-
new_cookie_val = ('[%d%%]'):format((#checked_boxes / #total_boxes) * 100)
898+
new_cookie_val = ('[%d%%]'):format((num / denum) * 100)
904899
else
905-
new_cookie_val = ('[%d/%d]'):format(#checked_boxes, #total_boxes)
900+
new_cookie_val = ('[%d/%d]'):format(num, denum)
906901
end
907902
return self:_set_node_text(cookie, new_cookie_val)
908903
end
904+
return self
905+
end
906+
907+
function Headline:update_cookie()
908+
local section = self:node():parent()
909+
if not section then
910+
return self
911+
end
912+
913+
-- Go through all the lists in this headline and gather checked_boxes
914+
local num_boxes, num_checked_boxes = 0, 0
915+
local body = section:field('body')[1]
916+
for node in body:iter_children() do
917+
if node:type() == 'list' then
918+
local boxes = self:child_checkboxes(node)
919+
num_boxes = num_boxes + #boxes
920+
local checked_boxes = vim.tbl_filter(function(box)
921+
return box:match('%[%w%]')
922+
end, boxes)
923+
num_checked_boxes = num_checked_boxes + #checked_boxes
924+
end
925+
end
926+
927+
-- Update the cookie
928+
return self:set_cookie(num_checked_boxes, num_boxes)
909929
end
910930

911931
function Headline:child_checkboxes(list_node)

0 commit comments

Comments
 (0)