@@ -890,22 +890,42 @@ function Headline:get_cookie()
890
890
return self :_parse_title_part (' %[%d?%d?%d?%%%]' )
891
891
end
892
892
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 )
899
894
local cookie = self :get_cookie ()
900
895
if cookie then
901
896
local new_cookie_val
902
897
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 )
904
899
else
905
- new_cookie_val = (' [%d/%d]' ):format (# checked_boxes , # total_boxes )
900
+ new_cookie_val = (' [%d/%d]' ):format (num , denum )
906
901
end
907
902
return self :_set_node_text (cookie , new_cookie_val )
908
903
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 )
909
929
end
910
930
911
931
function Headline :child_checkboxes (list_node )
0 commit comments