org-inlinetask.el 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ;;; org-inlinetask.el --- Tasks independent of outline hierarchy
  2. ;; Copyright (C) 2009 Free Software Foundation, Inc.
  3. ;;
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 6.27
  8. ;;
  9. ;; This file is not yet part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 3, or (at your option)
  14. ;; any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  21. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. ;; Boston, MA 02110-1301, USA.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; This module implements inline tasks in Org-mode. Inline tasks are
  28. ;; tasks that have all the properties of normal outline nodes, including
  29. ;; the ability to store meta data like scheduling dates, TODO state, tags
  30. ;; and properties. However, these nodes are treated specially by the
  31. ;; visibility cycling and export commands.
  32. ;;
  33. ;; Visibility cycling exempts these nodes from cycling. So whenever their
  34. ;; parent is opened, so are these tasks. This will only work with
  35. ;; `org-cycle', so if you are also using orther commands to show/hide
  36. ;; entries, you will occasionally find these tasks to behave like
  37. ;; all other outline nodes, seemingly splitting the text of the parent
  38. ;; into children.
  39. ;;
  40. ;; Export commands do not treat these nodes as part of the sectioning
  41. ;; structure, but as a special inline text that is either removed, or
  42. ;; formatted in some special way.
  43. ;;
  44. ;; Special fontification of inline tasks, so that they can be immediately
  45. ;; recognized. From the stars of the headline, only the first and the
  46. ;; last two will be visible, the others will be hidden using the
  47. ;; `org-hide' face.
  48. ;;
  49. ;; An inline task is identified solely by a minimum outline level, given
  50. ;; by the variable `org-inlinetask-min-level', default 15.
  51. ;;
  52. ;; Inline tasks are normally assumed to contain at most a time planning
  53. ;; line (DEADLINE etc) after it, and then any number of drawers, for
  54. ;; example LOGBOOK of PROPERTIES. No empty lines are allowed.
  55. ;; If you need to have normal text as part of an inline task, you
  56. ;; can do so by adding an "END" headline with the same number of stars,
  57. ;; for example
  58. ;;
  59. ;; **************** TODO some small task
  60. ;; DEADLINE: <2009-03-30 Mon>
  61. ;; :PROPERTIES:
  62. ;; :SOMETHING: or other
  63. ;; :END:
  64. ;; And here is some extra text
  65. ;; **************** END
  66. ;;; Code
  67. (defgroup org-inlinetask nil
  68. "Options concerning inline tasks in Org mode."
  69. :tag "Org Inline Tasks"
  70. :group 'org-structure)
  71. (defcustom org-inlinetask-min-level 15
  72. "Minimum level a headline must have before it is treated as an inline task.
  73. It is strongly recommended that you set `org-cycle-max-level' not at all,
  74. or to a number smaller than this one. In fact, when `org-cycle-max-level' is
  75. not set, it will be assumed to be one less than the value of smaller than
  76. the value of this variable."
  77. :group 'org-inlinetask
  78. :type 'boolean)
  79. (defcustom org-inlinetask-export 'arrow+content
  80. "What should be done with inlinetasks upon export?
  81. Possible values:
  82. nil Remove entirely, headline and \"content\"
  83. arrow Insert heading in bold, preceeded by an arrow
  84. arrow+content Insert arrow and headline, add content below in an
  85. #+begin_example box (ugly, but works for now)
  86. The \"content\" of an inline task is the material below the planning
  87. line and any drawers, up to a lines wit the same number of stars,
  88. but containing only the word END."
  89. :group 'org-inlinetask
  90. :group 'org-export-general
  91. :type '(choice
  92. (const :tag "Remove entirely" nil)
  93. (const :tag "Headline preceeded by arrow" arrow)
  94. (const :tag "Arrow, headline, + content" arrow+content)))
  95. (defvar org-odd-levels-only)
  96. (defvar org-keyword-time-regexp)
  97. (defvar org-drawer-regexp)
  98. (defvar org-complex-heading-regexp)
  99. (defvar org-property-end-re)
  100. (defun org-inlinetask-export-handler ()
  101. "Handle headlines with level larger or equal to `org-inlinetask-min-level'.
  102. Either remove headline and meta data, or do special formatting."
  103. (goto-char (point-min))
  104. (let* ((nstars (if org-odd-levels-only
  105. (1- (* 2 (or org-inlinetask-min-level 200)))
  106. (or org-inlinetask-min-level 200)))
  107. (re1 (format "^\\(\\*\\{%d,\\}\\) .*\n" nstars))
  108. (re2 (concat "^[ \t]*" org-keyword-time-regexp))
  109. headline beg end stars content)
  110. (while (re-search-forward re1 nil t)
  111. (setq headline (match-string 0)
  112. stars (match-string 1)
  113. content nil)
  114. (replace-match "")
  115. (while (looking-at re2)
  116. (delete-region (point) (1+ (point-at-eol))))
  117. (while (looking-at org-drawer-regexp)
  118. (setq beg (point))
  119. (if (re-search-forward org-property-end-re nil t)
  120. (delete-region beg (1+ (match-end 0)))))
  121. (setq beg (point))
  122. (when (and (re-search-forward "^\\(\\*+\\) " nil t)
  123. (= (length (match-string 1)) (length stars))
  124. (progn (goto-char (match-end 0))
  125. (looking-at "END[ \t]*$")))
  126. (setq content (buffer-substring beg (1- (point-at-bol))))
  127. (delete-region beg (1+ (match-end 0))))
  128. (goto-char beg)
  129. (when (and org-inlinetask-export
  130. (string-match org-complex-heading-regexp headline))
  131. (when (memq org-inlinetask-export '(arrow+content arrow))
  132. (insert "\n\n\\Rightarrow\\Rightarrow\\Rightarrow *"
  133. (if (match-end 2) (concat (match-string 2 headline) " ") "")
  134. (match-string 4 headline) "*\n"))
  135. (when (and content (eq org-inlinetask-export 'arrow+content))
  136. (insert "#+BEGIN_EXAMPLE\n" content "\n#+END_EXAMPLE\n"))
  137. (insert "\n")))))
  138. (defun org-inlinetask-fontify (limit)
  139. "Fontify the inline tasks."
  140. (let* ((nstars (if org-odd-levels-only
  141. (1- (* 2 (or org-inlinetask-min-level 200)))
  142. (or org-inlinetask-min-level 200)))
  143. (re (concat "^\\(\\*\\)\\(\\*\\{"
  144. (format "%d" (- nstars 3))
  145. ",\\}\\)\\(\\*\\* .*\\)")))
  146. (while (re-search-forward re limit t)
  147. (add-text-properties (match-beginning 1) (match-end 1)
  148. '(face org-warning font-lock-fontified t))
  149. (add-text-properties (match-beginning 2) (match-end 2)
  150. '(face org-hide font-lock-fontified t))
  151. (add-text-properties (match-beginning 3) (match-end 3)
  152. '(face shadow font-lock-fontified t)))))
  153. (eval-after-load "org-exp"
  154. '(add-hook 'org-export-preprocess-after-tree-selection-hook
  155. 'org-inlinetask-export-handler))
  156. (eval-after-load "org"
  157. '(add-hook 'org-font-lock-hook 'org-inlinetask-fontify))
  158. (provide 'org-inlinetask)
  159. ;;; org-inlinetask.el ends here