org-checklist.el 4.9 KB

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