@@ -890,19 +890,34 @@ function Headline:get_cookie()
890890 return self :_parse_title_part (' %[%d?%d?%d?%%%]' )
891891end
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