@@ -904,52 +904,56 @@ function Headline:set_cookie(cookie, num, denum)
904
904
end
905
905
906
906
function Headline :update_cookie ()
907
+ -- Update cookie state from a check box state change
908
+
907
909
-- Return early if the headline doesn't have a cookie
908
910
local cookie = self :get_cookie ()
909
911
if not cookie then
910
912
return self
911
913
end
912
914
913
- local num , denum = 0 , 0
914
- -- Count checked boxes from all lists
915
915
local section = self :node ():parent ()
916
- if section then
917
- local body = section :field (' body' )[1 ]
918
- if body then
919
- for node in body :iter_children () do
920
- if node :type () == ' list' then
921
- local boxes = self :child_checkboxes (node )
922
- denum = denum + # boxes
923
- local checked_boxes = vim .tbl_filter (function (box )
924
- return box :match (' %[%w%]' )
925
- end , boxes )
926
- num = num + # checked_boxes
927
- end
916
+ if not section then
917
+ return self
918
+ end
919
+
920
+ -- Count checked boxes from all lists
921
+ local num_checked_boxes , num_boxes = 0 , 0
922
+ local body = section :field (' body' )[1 ]
923
+ if body then
924
+ for node in body :iter_children () do
925
+ if node :type () == ' list' then
926
+ local boxes = self :child_checkboxes (node )
927
+ num_boxes = num_boxes + # boxes
928
+ local checked_boxes = vim .tbl_filter (function (box )
929
+ return box :match (' %[%w%]' )
930
+ end , boxes )
931
+ num_checked_boxes = num_checked_boxes + # checked_boxes
928
932
end
929
933
end
930
934
end
931
935
932
936
-- Update the cookie
933
- return self :set_cookie (cookie , num , denum )
937
+ return self :set_cookie (cookie , num_checked_boxes , num_boxes )
934
938
end
935
939
936
940
function Headline :update_todo_cookie ()
937
- -- Count done children headlines
941
+ -- Update cookie state from a TODO state change
942
+
938
943
-- Return early if the headline doesn't have a cookie
939
944
local cookie = self :get_cookie ()
940
945
if not cookie then
941
946
return self
942
947
end
943
- local num , denum = 0 , 0
948
+
949
+ -- Count done children headlines
944
950
local children = self :get_child_headlines ()
945
951
local dones = vim .tbl_filter (function (h )
946
952
return h :is_done ()
947
953
end , children )
948
- num = num + # dones
949
- denum = denum + # children
950
954
951
955
-- Update the cookie
952
- return self :set_cookie (cookie , num , denum )
956
+ return self :set_cookie (cookie , # dones , # children )
953
957
end
954
958
955
959
function Headline :update_parent_cookie ()
0 commit comments