mirror of
https://git.savannah.gnu.org/git/emacs/org-mode.git
synced 2024-08-24 03:48:51 +00:00
org-list: checkbox count code cleanup
This commit is contained in:
parent
2c79244687
commit
e5293ba347
|
@ -2132,12 +2132,13 @@ With optional prefix argument ALL, do this for the whole buffer."
|
||||||
(save-excursion (outline-next-heading) (point)))))
|
(save-excursion (outline-next-heading) (point)))))
|
||||||
(count-boxes
|
(count-boxes
|
||||||
(function
|
(function
|
||||||
;; add checked boxes and boxes of all types in all
|
;; Return number of checked boxes and boxes of all types
|
||||||
;; structures in STRUCTS to c-on and c-all, respectively.
|
;; in all structures in STRUCTS. If RECURSIVEP is non-nil,
|
||||||
;; This looks at RECURSIVEP value. If ITEM is nil, count
|
;; also count boxes in sub-lists. If ITEM is nil, count
|
||||||
;; across the whole structure, else count only across
|
;; across the whole structure, else count only across
|
||||||
;; subtree whose ancestor is ITEM.
|
;; subtree whose ancestor is ITEM.
|
||||||
(lambda (item structs)
|
(lambda (item structs recursivep)
|
||||||
|
(let ((c-on 0) (c-all 0))
|
||||||
(mapc
|
(mapc
|
||||||
(lambda (s)
|
(lambda (s)
|
||||||
(let* ((pre (org-list-struct-prev-alist s))
|
(let* ((pre (org-list-struct-prev-alist s))
|
||||||
|
@ -2155,22 +2156,25 @@ With optional prefix argument ALL, do this for the whole buffer."
|
||||||
items))))
|
items))))
|
||||||
(setq c-all (+ (length cookies) c-all)
|
(setq c-all (+ (length cookies) c-all)
|
||||||
c-on (+ (org-count "[X]" cookies) c-on))))
|
c-on (+ (org-count "[X]" cookies) c-on))))
|
||||||
structs))))
|
structs)
|
||||||
|
(cons c-on c-all)))))
|
||||||
(backup-end 1)
|
(backup-end 1)
|
||||||
cookies-list structs-backup)
|
cookies-list structs-bak box-num)
|
||||||
(goto-char (car bounds))
|
(goto-char (car bounds))
|
||||||
;; 1. Build an alist for each cookie found within BOUNDS. The
|
;; 1. Build an alist for each cookie found within BOUNDS. The
|
||||||
;; key will be position at beginning of cookie and values
|
;; key will be position at beginning of cookie and values
|
||||||
;; ending position, format of cookie, number of checked boxes
|
;; ending position, format of cookie, and a cell whose car is
|
||||||
;; to report, and total number of boxes.
|
;; number of checked boxes to report, and cdr total number of
|
||||||
|
;; boxes.
|
||||||
(while (re-search-forward cookie-re (cdr bounds) t)
|
(while (re-search-forward cookie-re (cdr bounds) t)
|
||||||
(catch 'skip
|
(catch 'skip
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(let ((c-on 0) (c-all 0))
|
(push
|
||||||
(save-match-data
|
(list
|
||||||
;; There are two types of cookies: those at headings and those
|
(match-beginning 1) ; cookie start
|
||||||
;; at list items.
|
(match-end 1) ; cookie end
|
||||||
(cond
|
(match-string 2) ; percent?
|
||||||
|
(cond ; boxes count
|
||||||
;; Cookie is at an heading, but specifically for todo,
|
;; Cookie is at an heading, but specifically for todo,
|
||||||
;; not for checkboxes: skip it.
|
;; not for checkboxes: skip it.
|
||||||
((and (org-on-heading-p)
|
((and (org-on-heading-p)
|
||||||
|
@ -2180,53 +2184,47 @@ With optional prefix argument ALL, do this for the whole buffer."
|
||||||
(throw 'skip nil))
|
(throw 'skip nil))
|
||||||
;; Cookie is at an heading, but all lists before next
|
;; Cookie is at an heading, but all lists before next
|
||||||
;; heading already have been read. Use data collected
|
;; heading already have been read. Use data collected
|
||||||
;; in STRUCTS-BACKUP. This should only happen when
|
;; in STRUCTS-BAK. This should only happen when heading
|
||||||
;; heading has more than one cookie on it.
|
;; has more than one cookie on it.
|
||||||
((and (org-on-heading-p)
|
((and (org-on-heading-p)
|
||||||
(<= (save-excursion (outline-next-heading) (point))
|
(<= (save-excursion (outline-next-heading) (point))
|
||||||
backup-end))
|
backup-end))
|
||||||
(funcall count-boxes nil structs-backup))
|
(funcall count-boxes nil structs-bak recursivep))
|
||||||
;; Cookie is at a fresh heading. Grab structure of
|
;; Cookie is at a fresh heading. Grab structure of
|
||||||
;; every list containing a checkbox between point and
|
;; every list containing a checkbox between point and
|
||||||
;; next headline, and save them in STRUCTS-BACKUP.
|
;; next headline, and save them in STRUCTS-BAK.
|
||||||
((org-on-heading-p)
|
((org-on-heading-p)
|
||||||
(setq backup-end (save-excursion
|
(setq backup-end (save-excursion
|
||||||
(outline-next-heading) (point)))
|
(outline-next-heading) (point)))
|
||||||
(while (org-list-search-forward box-re backup-end 'move)
|
(while (org-list-search-forward box-re backup-end 'move)
|
||||||
(let* ((struct (org-list-struct))
|
(let* ((struct (org-list-struct))
|
||||||
(bottom (org-list-get-bottom-point struct)))
|
(bottom (org-list-get-bottom-point struct)))
|
||||||
(push struct structs-backup)
|
(push struct structs-bak)
|
||||||
(goto-char bottom)))
|
(goto-char bottom)))
|
||||||
(funcall count-boxes nil structs-backup))
|
(funcall count-boxes nil structs-bak recursivep))
|
||||||
;; Cookie is at an item, and we already list structure
|
;; Cookie is at an item, and we already have list
|
||||||
;; stored in STRUCTS-BACKUP.
|
;; structure stored in STRUCTS-BAK.
|
||||||
((and (org-at-item-p)
|
((and (org-at-item-p)
|
||||||
(< (point-at-bol) backup-end))
|
(< (point-at-bol) backup-end))
|
||||||
(funcall count-boxes (point-at-bol) structs-backup))
|
(funcall count-boxes (point-at-bol) structs-bak recursivep))
|
||||||
;; Cookie is at an item, but we need to compute list
|
;; Cookie is at an item, but we need to compute list
|
||||||
;; structure.
|
;; structure.
|
||||||
((org-at-item-p)
|
((org-at-item-p)
|
||||||
(let ((struct (org-list-struct)))
|
(let ((struct (org-list-struct)))
|
||||||
(setq backup-end (org-list-get-bottom-point struct)
|
(setq backup-end (org-list-get-bottom-point struct)
|
||||||
structs-backup (list struct)))
|
structs-bak (list struct)))
|
||||||
(funcall count-boxes (point-at-bol) structs-backup))
|
(funcall count-boxes (point-at-bol) structs-bak recursivep))
|
||||||
;; Else, cookie found is at a wrong place. Skip it.
|
;; Else, cookie found is at a wrong place. Skip it.
|
||||||
(t (throw 'skip nil))))
|
(t (throw 'skip nil))))
|
||||||
;; Build the cookies list, with appropriate information
|
cookies-list))))
|
||||||
(push (list (match-beginning 1) ; cookie start
|
|
||||||
(match-end 1) ; cookie end
|
|
||||||
(match-string 2) ; percent?
|
|
||||||
c-on ; checked boxes
|
|
||||||
c-all) ; total boxes
|
|
||||||
cookies-list)))))
|
|
||||||
;; 2. Apply alist to buffer, in reverse order so positions stay
|
;; 2. Apply alist to buffer, in reverse order so positions stay
|
||||||
;; unchanged after cookie modifications.
|
;; unchanged after cookie modifications.
|
||||||
(mapc (lambda (cookie)
|
(mapc (lambda (cookie)
|
||||||
(let* ((beg (car cookie))
|
(let* ((beg (car cookie))
|
||||||
(end (nth 1 cookie))
|
(end (nth 1 cookie))
|
||||||
(percentp (nth 2 cookie))
|
(percentp (nth 2 cookie))
|
||||||
(checked (nth 3 cookie))
|
(checked (car (nth 3 cookie)))
|
||||||
(total (nth 4 cookie))
|
(total (cdr (nth 3 cookie)))
|
||||||
(new (if percentp
|
(new (if percentp
|
||||||
(format "[%d%%]" (/ (* 100 checked)
|
(format "[%d%%]" (/ (* 100 checked)
|
||||||
(max 1 total)))
|
(max 1 total)))
|
||||||
|
|
Loading…
Reference in a new issue