org-checklist.el 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ;;; org-checklist.el --- org functions for checklist handling
  2. ;; Copyright (C) 2008-2014 James TD Smith
  3. ;; Author: James TD Smith (@ ahktenzero (. mohorovi cc))
  4. ;; Version: 1.0
  5. ;; Keywords: org, checklists
  6. ;;
  7. ;; This file is not part of GNU Emacs.
  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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;; Commentary:
  22. ;; This file provides some functions for handing repeated tasks which involve
  23. ;; checking off a list of items. By setting the RESET_CHECK_BOXES property in an
  24. ;; item, when the TODO state is set to done all checkboxes under that item are
  25. ;; cleared. If the LIST_EXPORT_BASENAME property is set, a file will be created
  26. ;; using the value of that property plus a timestamp, containing all the items
  27. ;; in the list which are not checked. Additionally the user will be prompted to
  28. ;; print the list.
  29. ;;
  30. ;; I use this for to keep track of stores of various things (food stores,
  31. ;; components etc) which I check periodically and use the exported list of items
  32. ;; which are not present as a shopping list.
  33. ;;
  34. ;;; Usage:
  35. ;; (require 'org-checklist)
  36. ;;
  37. ;; Set the RESET_CHECK_BOXES and LIST_EXPORT_BASENAME properties in items as
  38. ;; needed.
  39. ;;
  40. ;;; Code:
  41. (require 'org)
  42. (load "a2ps-print" 'no-error)
  43. (setq org-default-properties (cons "RESET_CHECK_BOXES" (cons "LIST_EXPORT_BASENAME" org-default-properties)))
  44. (defgroup org-checklist nil
  45. "Extended checklist handling for org"
  46. :tag "Org-checklist"
  47. :group 'org)
  48. (defcustom org-checklist-export-time-format "%Y%m%d%H%M"
  49. "The format of timestamp appended to LIST_EXPORT_BASENAME to
  50. make the name of the export file."
  51. :link '(function-link format-time-string)
  52. :group 'org-checklist
  53. :type 'string)
  54. (defcustom org-checklist-export-function 'org-export-as-ascii
  55. "function used to prepare the export file for printing"
  56. :group 'org-checklist
  57. :type '(radio (function-item :tag "ascii text" org-export-as-ascii)
  58. (function-item :tag "HTML" org-export-as-html)
  59. (function-item :tag "LaTeX" :value org-export-as-latex)
  60. (function-item :tag "XOXO" :value org-export-as-xoxo)))
  61. (defcustom org-checklist-export-params nil
  62. "options for the export function file for printing"
  63. :group 'org-checklist
  64. :type '(repeat string))
  65. (defcustom org-checklist-a2ps-params nil
  66. "options for a2ps for printing"
  67. :group 'org-checklist
  68. :type '(repeat string))
  69. (defun org-reset-checkbox-state-maybe ()
  70. "Reset all checkboxes in an entry if the `RESET_CHECK_BOXES' property is set"
  71. (interactive "*")
  72. (if (org-entry-get (point) "RESET_CHECK_BOXES")
  73. (org-reset-checkbox-state-subtree)))
  74. (defun org-make-checklist-export ()
  75. "Produce a checklist containing all unchecked items from a list
  76. of checkbox items"
  77. (interactive "*")
  78. (if (org-entry-get (point) "LIST_EXPORT_BASENAME")
  79. (let* ((export-file (concat (org-entry-get (point) "LIST_EXPORT_BASENAME" nil)
  80. "-" (format-time-string
  81. org-checklist-export-time-format)
  82. ".org"))
  83. (print (case (org-entry-get (point) "PRINT_EXPORT" nil)
  84. (("" "nil" nil) nil)
  85. (t t)
  86. (nil (y-or-n-p "Print list? "))))
  87. exported-lines
  88. (title "Checklist export"))
  89. (save-restriction
  90. (save-excursion
  91. (org-narrow-to-subtree)
  92. (org-update-checkbox-count-maybe)
  93. (org-show-subtree)
  94. (goto-char (point-min))
  95. (when (looking-at org-complex-heading-regexp)
  96. (setq title (match-string 4)))
  97. (goto-char (point-min))
  98. (let ((end (point-max)))
  99. (while (< (point) end)
  100. (when (and (org-at-item-checkbox-p)
  101. (or (string= (match-string 0) "[ ]")
  102. (string= (match-string 0) "[-]")))
  103. (add-to-list 'exported-lines (thing-at-point 'line) t))
  104. (beginning-of-line 2)))
  105. (set-buffer (get-buffer-create export-file))
  106. (org-insert-heading)
  107. (insert (or title export-file) "\n")
  108. (dolist (entry exported-lines) (insert entry))
  109. (org-update-checkbox-count-maybe)
  110. (write-file export-file)
  111. (if (print)
  112. (progn (funcall org-checklist-export-function
  113. org-checklist-export-params)
  114. (let* ((current-a2ps-switches a2ps-switches)
  115. (a2ps-switches (append current-a2ps-switches
  116. org-checklist-a2ps-params)))
  117. (a2ps-buffer)))))))))
  118. (defun org-checklist ()
  119. (when (member org-state org-done-keywords) ;; org-state dynamically bound in org.el/org-todo
  120. (org-make-checklist-export)
  121. (org-reset-checkbox-state-maybe)))
  122. (add-hook 'org-after-todo-state-change-hook 'org-checklist)
  123. (provide 'org-checklist)
  124. ;;; org-checklist.el ends here