Agenda: Fix problems with follow mode.
[org-mode.git] / contrib / lisp / org-checklist.el
blob9661b53385f0b8a250c2d7812dc626246040f156
1 ;;; org-checklist.el --- org functions for checklist handling
3 ;; Copyright (C) 2008 James TD Smith
5 ;; Author: James TD Smith (@ ahktenzero (. mohorovi cc))
6 ;; Version: 1.0
7 ;; Keywords: org, checklists
8 ;;
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
12 ;; any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program; if not, write to the Free Software
21 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 ;;; Commentary:
25 ;; This file provides some functions for handing repeated tasks which involve
26 ;; checking off a list of items. By setting the RESET_CHECK_BOXES property in an
27 ;; item, when the TODO state is set to done all checkboxes under that item are
28 ;; cleared. If the LIST_EXPORT_BASENAME property is set, a file will be created
29 ;; using the value of that property plus a timestamp, containing all the items
30 ;; in the list which are not checked. Additionally the user will be prompted to
31 ;; print the list.
33 ;; I use this for to keep track of stores of various things (food stores,
34 ;; components etc) which I check periodically and use the exported list of items
35 ;; which are not present as a shopping list.
37 ;;; Usage:
38 ;; (require 'org-checklist)
40 ;; Set the RESET_CHECK_BOXES and LIST_EXPORT_BASENAME properties in items as
41 ;; needed.
43 ;;; Code:
44 (require 'org)
46 (defvar export-time-format "%Y%m%d%H%M"
47 "format of timestamp appended to export file")
48 (defvar export-function 'org-export-as-ascii
49 "function used to prepare the export file for printing")
51 (defun org-reset-checkbox-state-maybe ()
52 "Reset all checkboxes in an entry if the `RESET_CHECK_BOXES' property is set"
53 (interactive "*")
54 (if (org-entry-get (point) "RESET_CHECK_BOXES")
55 (save-restriction
56 (save-excursion
57 (org-narrow-to-subtree)
58 (org-show-subtree)
59 (goto-char (point-min))
60 (let ((end (point-max)))
61 (while (< (point) end)
62 (when (org-at-item-checkbox-p)
63 (replace-match "[ ]" t t))
64 (beginning-of-line 2))))
65 (org-update-checkbox-count-maybe))))
67 (defun org-make-checklist-export ()
68 "Produce a checklist containing all unchecked items from a list
69 of checkbox items"
70 (interactive "*")
71 (if (org-entry-get (point) "LIST_EXPORT_BASENAME")
72 (let* ((export-file (concat (org-entry-get (point) "LIST_EXPORT_BASENAME")
73 "-" (format-time-string export-time-format)
74 ".org"))
75 exported-lines
76 title)
77 (save-restriction
78 (save-excursion
79 (org-narrow-to-subtree)
80 (org-show-subtree)
81 (goto-char (point-min))
82 (if (looking-at org-complex-heading-regexp)
83 (setq title (match-string 4)))
84 (goto-char (point-min))
85 (let ((end (point-max)))
86 (while (< (point) end)
87 (when (and (org-at-item-checkbox-p)
88 (or (string= (match-string 0) "[ ]")
89 (string= (match-string 0) "[-]")))
90 (add-to-list 'exported-lines (thing-at-point 'line) t))
91 (beginning-of-line 2)))
92 (set-buffer (get-buffer-create export-file))
93 (org-insert-heading)
94 (insert (or title export-file) "\n")
95 (dolist (entry exported-lines) (insert entry))
96 (org-update-checkbox-count-maybe)
97 (write-file export-file)
98 (if (y-or-n-p "Print list? ")
99 ((funcall export-function)
100 (a2ps-buffer))))))))
102 (defun org-checklist ()
103 (if (member state org-done-keywords)
104 (org-make-checklist-export))
105 (org-reset-checkbox-state-maybe))
107 (add-hook 'org-after-todo-state-change-hook 'org-checklist)
109 (provide 'org-checklist)
111 ;;; org-checklist.el ends here