org-checklist.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ;;; org-checklist.el --- org functions for checklist handling
  2. ;;
  3. ;; Copyright (C) 2008 James TD Smith
  4. ;;
  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.
  13. ;;
  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.
  18. ;;
  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.
  22. ;;
  23. ;;; Commentary:
  24. ;; This file provides some functions for handing repeated tasks which involve
  25. ;; checking off a list of items. By setting the RESET_CHECK_BOXES property in an
  26. ;; item, when the TODO state is set to done all checkboxes under that item are
  27. ;; cleared. If the LIST_EXPORT_BASENAME property is set, a file will be created
  28. ;; using the value of that property plus a timestamp, containing all the items
  29. ;; in the list which are not checked. Additionally the user will be prompted to
  30. ;; print the list.
  31. ;;
  32. ;; I use this for to keep track of stores of various things (food stores,
  33. ;; components etc) which I check periodically and use the exported list of items
  34. ;; which are not present as a shopping list.
  35. ;;
  36. ;;; Usage:
  37. ;; (require 'org-checklist)
  38. ;;
  39. ;; Set the RESET_CHECK_BOXES and LIST_EXPORT_BASENAME properties in items as
  40. ;; needed.
  41. ;;
  42. ;;; Code:
  43. (require 'org)
  44. (defvar export-time-format "%Y%m%d%H%M"
  45. "format of timestamp appended to export file")
  46. (defvar export-function 'org-export-as-ascii
  47. "function used to prepare the export file for printing")
  48. (defun org-reset-checkbox-state-maybe ()
  49. "Reset all checkboxes in an entry if the `RESET_CHECK_BOXES' property is set"
  50. (interactive "*")
  51. (if (org-entry-get (point) "RESET_CHECK_BOXES")
  52. (save-restriction
  53. (save-excursion
  54. (org-narrow-to-subtree)
  55. (org-show-subtree)
  56. (goto-char (point-min))
  57. (let ((end (point-max)))
  58. (while (< (point) end)
  59. (when (org-at-item-checkbox-p)
  60. (replace-match "[ ]" t t))
  61. (beginning-of-line 2))))
  62. (org-update-checkbox-count-maybe))))
  63. (defun org-make-checklist-export ()
  64. "Produce a checklist containing all unchecked items from a list
  65. of checkbox items"
  66. (interactive "*")
  67. (if (org-entry-get (point) "LIST_EXPORT_BASENAME")
  68. (let* ((export-file (concat (org-entry-get (point) "LIST_EXPORT_BASENAME")
  69. "-" (format-time-string export-time-format)
  70. ".org"))
  71. exported-lines
  72. title)
  73. (save-restriction
  74. (save-excursion
  75. (org-narrow-to-subtree)
  76. (org-show-subtree)
  77. (goto-char (point-min))
  78. (if (looking-at org-complex-heading-regexp)
  79. (setq title (match-string 4)))
  80. (goto-char (point-min))
  81. (let ((end (point-max)))
  82. (while (< (point) end)
  83. (when (and (org-at-item-checkbox-p)
  84. (or (string= (match-string 0) "[ ]")
  85. (string= (match-string 0) "[-]")))
  86. (add-to-list 'exported-lines (thing-at-point 'line) t))
  87. (beginning-of-line 2)))
  88. (set-buffer (get-buffer-create export-file))
  89. (org-insert-heading)
  90. (insert (or title export-file) "\n")
  91. (dolist (entry exported-lines) (insert entry))
  92. (org-update-checkbox-count-maybe)
  93. (write-file export-file)
  94. (if (y-or-n-p "Print list? ")
  95. ((funcall export-function)
  96. (a2ps-buffer))))))))
  97. (defun org-checklist ()
  98. (if (member state org-done-keywords)
  99. (org-make-checklist-export))
  100. (org-reset-checkbox-state-maybe))
  101. (add-hook 'org-after-todo-state-change-hook 'org-checklist)
  102. (provide 'org-checklist)
  103. ;;; org-checklist.el ends here