Skip to content

Commit 062ca65

Browse files
committedMar 8, 2025
Update cookies from multiple list
1 parent dafb6aa commit 062ca65

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
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_cookies()
7575
end
7676
end
7777
end

‎lua/orgmode/files/headline.lua

+33
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,39 @@ function Headline:update_cookie(list_node)
908908
end
909909
end
910910

911+
function Headline:update_cookies()
912+
local section = self:node():parent()
913+
if not section then
914+
return nil
915+
end
916+
917+
-- go through all the lists in this headline and gather checked_boxes
918+
local num_boxes, num_checked_boxes = 0, 0
919+
local body = section:field('body')[1]
920+
for node in body:iter_children() do
921+
if node:type() == 'list' then
922+
local boxes = self:child_checkboxes(node)
923+
num_boxes = num_boxes + #boxes
924+
local checked_boxes = vim.tbl_filter(function(box)
925+
return box:match('%[%w%]')
926+
end, boxes)
927+
num_checked_boxes = num_checked_boxes + #checked_boxes
928+
end
929+
end
930+
931+
-- update the cookie
932+
local cookie = self:get_cookie()
933+
if cookie then
934+
local new_cookie_val
935+
if self.file:get_node_text(cookie):find('%%') then
936+
new_cookie_val = ('[%d%%]'):format((num_checked_boxes / num_boxes) * 100)
937+
else
938+
new_cookie_val = ('[%d/%d]'):format(num_checked_boxes, num_boxes)
939+
end
940+
return self:_set_node_text(cookie, new_cookie_val)
941+
end
942+
end
943+
911944
function Headline:child_checkboxes(list_node)
912945
return vim.tbl_map(function(node)
913946
local text = self.file:get_node_text(node)

0 commit comments

Comments
 (0)