3

If I have outline structure like this:

* Tasks
** First
** Second
** Third
** etc

is there a simple way to turn all tasks into TODO items or checkboxes? I can of course use replace-string but maybe there's already a keybinding, since maybe it's seems to be done quite often?

iLemming
  • 1,223
  • 9
  • 14

2 Answers2

3

I don't think there's a built-in function for that, but you can create one and bind it yourself. I wrote this one, that will replace all outlines in region with - [ ]. If no region is active, replace the outline in current line.

(defun org-outline-to-checkbox ()
  (interactive)
  (if (region-active-p)
      (replace-regexp "^*+" "- [ ]" nil (region-beginning) (region-end))
    (replace-regexp "^*+" "- [ ]" nil
                    (line-beginning-position)
                    (line-end-position))))
Jesse
  • 1,984
  • 11
  • 19
2

Let's discuss low-level command that you need. org-todo cycles the headline your cursor is on through TODO states. org-toggle-item turns headlines into lists, org-toggle-heading turns lists into headlines. In standard Emacs they are mapped to C-c C-t, C-c -, C-c * respectively.

org-todo doesn't work on regions, but it would be cool if we could put a TODO state on every heading that doesn't have a TODO (and doesn't have other states, including DONE). This is how we would do that.

(defun outline-to-checkboxes (from to)
  (interactive "r")
  (save-excursion
    (save-restriction
      (with-undo-collapse
        (narrow-to-region from to)
        (end-of-buffer)
        (while (outline-previous-heading)
          (when (null (org-get-todo-state))
            (org-todo "TODO")))))))

The reason I go to the end of buffer and apply outline-previous-heading instead of going from top forward is described in a StackOverflow answer: while always calls the first argument at least once, and if you started at the beginning of your buffer starts with an Org heading, you'll skip it.

with-undo-collapse is a custom macro that allows you to undo the whole change in one action (follow the link to get the code).

Unfortunately, due to a bug in org-toggle-list, changing headings to a list with checkboxes programmatically is difficult, so just go with the regex approach in Jesse's answer.

Mirzhan Irkegulov
  • 1,088
  • 1
  • 9
  • 16