Browse Source

Checkboxes: New command to add and remove them easily from items

See documentation changes for details.
Carsten Dominik 16 years ago
parent
commit
bc7007b893
4 changed files with 56 additions and 18 deletions
  1. 8 0
      ORGWEBPAGE/Changes.org
  2. 3 4
      doc/org.texi
  3. 4 0
      lisp/ChangeLog
  4. 41 14
      lisp/org-list.el

+ 8 - 0
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
 key for selecting text.  Make sure that you carefully read the
 docstring of that variable first.
 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
 * Version 6.19
 
 
 ** Overview
 ** Overview

+ 3 - 4
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.
 which is considered to be an intermediate state.
 @kindex C-c C-x C-b
 @kindex C-c C-x C-b
 @item 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
 @itemize @minus
 @item
 @item
 If there is an active region, toggle the first checkbox in the region
 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
 @item
 If the cursor is in a headline, toggle checkboxes in the region between
 If the cursor is in a headline, toggle checkboxes in the region between
 this headline and the next (so @emph{not} the entire subtree).
 this headline and the next (so @emph{not} the entire subtree).

+ 4 - 0
lisp/ChangeLog

@@ -1,5 +1,9 @@
 2009-01-28  Carsten Dominik  <carsten.dominik@gmail.com>
 2009-01-28  Carsten Dominik  <carsten.dominik@gmail.com>
 
 
+	* 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
 	* org-rmail.el (org-rmail-store-link): Protect the call to
 	`rmail-narrow-to-non-pruned-header'.
 	`rmail-narrow-to-non-pruned-header'.
 
 

+ 41 - 14
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")
 	 (skip-chars-forward " \t")
 	 (looking-at "\\[[- X]\\]"))))
 	 (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")
   (interactive "P")
   (catch 'exit
   (catch 'exit
-    (let (beg end status (firstnew 'unknown))
+    (let (beg end status first-present first-status)
       (cond
       (cond
        ((org-region-active-p)
        ((org-region-active-p)
 	(setq beg (region-beginning) end (region-end)))
 	(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))))
 	(setq beg (point) end (save-excursion (outline-next-heading) (point))))
        ((org-at-item-checkbox-p)
        ((org-at-item-checkbox-p)
 	(let ((pos (point)))
 	(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))
 	  (goto-char pos))
 	(throw 'exit t))
 	(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")))
        (t (error "Not at a checkbox or heading, and no active region")))
+      (setq end (move-marker (make-marker) end))
       (save-excursion
       (save-excursion
 	(goto-char beg)
 	(goto-char beg)
+	(setq first-present (org-at-item-checkbox-p)
+	      first-status (and first-present (equal (match-string 0) "[X]")))
 	(while (< (point) end)
 	(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)))))
 	  (beginning-of-line 2)))))
   (org-update-checkbox-count-maybe))
   (org-update-checkbox-count-maybe))