org-list: Change behaviour of C-c C-c at a list item with an argument

* lisp/org-list.el (org-list-write-struct): Add an optional argument
  for structure changes happening outside the function.
* lisp/org.el (org-ctrl-c-ctrl-c): Now, C-u C-c C-c on the first item
  of a sub-list should toggle check-box presence of every item in the
  same sub-list.  Also fix check-box insertion on a single item.
This commit is contained in:
Nicolas Goaziou 2011-12-31 15:36:53 +01:00 committed by Bastien Guerry
parent 4f8d383769
commit 8c19c76d2e
2 changed files with 34 additions and 11 deletions

View File

@ -1914,16 +1914,21 @@ Initial position of cursor is restored after the changes."
(goto-char origin) (goto-char origin)
(move-marker origin nil))) (move-marker origin nil)))
(defun org-list-write-struct (struct parents) (defun org-list-write-struct (struct parents &optional old-struct)
"Correct bullets, checkboxes and indentation in list at point. "Correct bullets, checkboxes and indentation in list at point.
STRUCT is the list structure. PARENTS is the alist of parents, STRUCT is the list structure. PARENTS is the alist of parents,
as returned by `org-list-parents-alist'." as returned by `org-list-parents-alist'.
When non-nil, optional argument OLD-STRUCT is the reference
structure of the list. It should be provided whenever STRUCT
doesn't correspond anymore to the real list in buffer."
;; Order of functions matters here: checkboxes and endings need ;; Order of functions matters here: checkboxes and endings need
;; correct indentation to be set, and indentation needs correct ;; correct indentation to be set, and indentation needs correct
;; bullets. ;; bullets.
;; ;;
;; 0. Save a copy of structure before modifications ;; 0. Save a copy of structure before modifications
(let ((old-struct (copy-tree struct))) (let ((old-struct (or old-struct (copy-tree struct))))
;; 1. Set a temporary, but coherent with PARENTS, indentation in ;; 1. Set a temporary, but coherent with PARENTS, indentation in
;; order to get items endings and bullets properly ;; order to get items endings and bullets properly
(org-list-struct-fix-ind struct parents 2) (org-list-struct-fix-ind struct parents 2)

View File

@ -18390,12 +18390,19 @@ This command does many different things, depending on context:
block-item) block-item)
;; Use a light version of `org-toggle-checkbox' to avoid ;; Use a light version of `org-toggle-checkbox' to avoid
;; computing list structure twice. ;; computing list structure twice.
(org-list-set-checkbox (point-at-bol) struct (let ((new-box (cond
(cond ((equal arg '(16)) "[-]")
((equal arg '(16)) "[-]") ((equal arg '(4)) nil)
((equal arg '(4)) nil) ((equal "[X]" cbox) "[ ]")
((equal "[X]" cbox) "[ ]") (t "[X]"))))
(t "[X]"))) (if firstp
;; If at first item of sub-list, remove check-box from
;; every item at the same level.
(mapc
(lambda (pos) (org-list-set-checkbox pos struct new-box))
(org-list-get-all-items
(point-at-bol) struct (org-list-prevs-alist struct)))
(org-list-set-checkbox (point-at-bol) struct new-box)))
;; Replicate `org-list-write-struct', while grabbing a return ;; Replicate `org-list-write-struct', while grabbing a return
;; value from `org-list-struct-fix-box'. ;; value from `org-list-struct-fix-box'.
(org-list-struct-fix-ind struct parents 2) (org-list-struct-fix-ind struct parents 2)
@ -18417,9 +18424,20 @@ This command does many different things, depending on context:
;; only if function was called with an argument. Send list only ;; only if function was called with an argument. Send list only
;; if at top item. ;; if at top item.
(let* ((struct (org-list-struct)) (let* ((struct (org-list-struct))
(new-struct struct)
(firstp (= (org-list-get-top-point struct) (point-at-bol)))) (firstp (= (org-list-get-top-point struct) (point-at-bol))))
(when arg (org-list-set-checkbox (point-at-bol) struct "[ ]")) (when arg
(org-list-write-struct struct (org-list-parents-alist struct)) (setq new-struct (copy-tree struct))
(if firstp
;; If at first item of sub-list, add check-box to every
;; item at the same level.
(mapc
(lambda (pos) (org-list-set-checkbox pos new-struct "[ ]"))
(org-list-get-all-items
(point-at-bol) new-struct (org-list-prevs-alist new-struct)))
(org-list-set-checkbox (point-at-bol) new-struct "[ ]")))
(org-list-write-struct
new-struct (org-list-parents-alist new-struct) struct)
(when arg (org-update-checkbox-count-maybe)) (when arg (org-update-checkbox-count-maybe))
(when firstp (org-list-send-list 'maybe)))) (when firstp (org-list-send-list 'maybe))))
((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re)) ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))