From bc7007b8935419ebd968ea5316f87b9077aa4521 Mon Sep 17 00:00:00 2001 From: Carsten Dominik Date: Wed, 28 Jan 2009 12:28:21 +0100 Subject: [PATCH] Checkboxes: New command to add and remove them easily from items See documentation changes for details. --- ORGWEBPAGE/Changes.org | 8 ++++++++ doc/org.texi | 7 +++---- lisp/ChangeLog | 4 ++++ lisp/org-list.el | 55 +++++++++++++++++++++++++++++++++++++------------- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/ORGWEBPAGE/Changes.org b/ORGWEBPAGE/Changes.org index db28cf8aa..909d13de5 100644 --- a/ORGWEBPAGE/Changes.org +++ b/ORGWEBPAGE/Changes.org @@ -58,6 +58,14 @@ Customize the variable =org-support-shift-select= to use S-cursor key for selecting text. Make sure that you carefully read the docstring of that variable first. +*** Adding and removing checkboxes from many lines + +The command =C-c C-x C-b= normally toggles checkbox status in the +current line, or in all lines in the region. With prefix +argument it now either adds or removes the checkbox. + +This was a requested by Daniel Clemente. + * Version 6.19 ** Overview diff --git a/doc/org.texi b/doc/org.texi index e5c2e2c43..4b6228d04 100644 --- a/doc/org.texi +++ b/doc/org.texi @@ -3601,13 +3601,12 @@ Toggle checkbox at point. With a prefix argument, set it to @samp{[-]}, which is considered to be an intermediate state. @kindex C-c C-x C-b @item C-c C-x C-b -Toggle checkbox at point. +Toggle checkbox status or (with prefix arg) checkbox presence at point. @itemize @minus @item If there is an active region, toggle the first checkbox in the region -and set all remaining boxes to the same status as the first. If you -want to toggle all boxes in the region independently, use a prefix -argument. +and set all remaining boxes to the same status as the first. With a prefix +arg, add or remove the checkbox for all items in the region. @item If the cursor is in a headline, toggle checkboxes in the region between this headline and the next (so @emph{not} the entire subtree). diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 68be8d106..ba2ba0e82 100755 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,5 +1,9 @@ 2009-01-28 Carsten Dominik + * org-list.el (org-toggle-checkbox): Implement adding or removing + checkboxes from line or region when called with a prefix + argument. + * org-rmail.el (org-rmail-store-link): Protect the call to `rmail-narrow-to-non-pruned-header'. diff --git a/lisp/org-list.el b/lisp/org-list.el index aeb172f15..ebb62c188 100644 --- a/lisp/org-list.el +++ b/lisp/org-list.el @@ -248,11 +248,15 @@ Return t when things worked, nil when we are not in an item." (skip-chars-forward " \t") (looking-at "\\[[- X]\\]")))) -(defun org-toggle-checkbox (&optional arg) - "Toggle the checkbox in the current line." +(defun org-toggle-checkbox (&optional toggle-presence) + "Toggle the checkbox in the current line. +With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. +When there is an active region, toggle status or presence of the checkbox +in the first line, and make every item in the region have the same +status or precence, respectively." (interactive "P") (catch 'exit - (let (beg end status (firstnew 'unknown)) + (let (beg end status first-present first-status) (cond ((org-region-active-p) (setq beg (region-beginning) end (region-end))) @@ -260,23 +264,46 @@ Return t when things worked, nil when we are not in an item." (setq beg (point) end (save-excursion (outline-next-heading) (point)))) ((org-at-item-checkbox-p) (let ((pos (point))) - (replace-match - (cond (arg "[-]") - ((member (match-string 0) '("[ ]" "[-]")) "[X]") - (t "[ ]")) - t t) + (if toggle-presence + (progn + (replace-match "") + (goto-char (match-beginning 0)) + (just-one-space)) + (replace-match + (cond ((member (match-string 0) '("[ ]" "[-]")) "[X]") + (t "[ ]")) + t t)) (goto-char pos)) (throw 'exit t)) + ((org-at-item-p) + ;; add a checkbox + (save-excursion + (goto-char (match-end 0)) + (insert "[ ] ")) + (throw 'exit t)) (t (error "Not at a checkbox or heading, and no active region"))) + (setq end (move-marker (make-marker) end)) (save-excursion (goto-char beg) + (setq first-present (org-at-item-checkbox-p) + first-status (and first-present (equal (match-string 0) "[X]"))) (while (< (point) end) - (when (org-at-item-checkbox-p) - (setq status (equal (match-string 0) "[X]")) - (when (eq firstnew 'unknown) - (setq firstnew (not status))) - (replace-match - (if (if arg (not status) firstnew) "[X]" "[ ]") t t)) + (if toggle-presence + (cond + ((and first-present (org-at-item-checkbox-p)) + (save-excursion + (replace-match "") + (goto-char (match-beginning 0)) + (just-one-space))) + ((and (not first-present) (not (org-at-item-checkbox-p)) + (org-at-item-p)) + (save-excursion + (goto-char (match-end 0)) + (insert "[ ] ")))) + (when (org-at-item-checkbox-p) + (setq status (equal (match-string 0) "[X]")) + (replace-match + (if first-status "[ ]" "[X]") t t))) (beginning-of-line 2))))) (org-update-checkbox-count-maybe)) -- 2.11.4.GIT