org-inlinetask.el 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.30trans
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs 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 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;;
  23. ;; This module implements inline tasks in Org-mode. Inline tasks are
  24. ;; tasks that have all the properties of normal outline nodes, including
  25. ;; the ability to store meta data like scheduling dates, TODO state, tags
  26. ;; and properties. However, these nodes are treated specially by the
  27. ;; visibility cycling and export commands.
  28. ;;
  29. ;; Visibility cycling exempts these nodes from cycling. So whenever their
  30. ;; parent is opened, so are these tasks. This will only work with
  31. ;; `org-cycle', so if you are also using orther commands to show/hide
  32. ;; entries, you will occasionally find these tasks to behave like
  33. ;; all other outline nodes, seemingly splitting the text of the parent
  34. ;; into children.
  35. ;;
  36. ;; Export commands do not treat these nodes as part of the sectioning
  37. ;; structure, but as a special inline text that is either removed, or
  38. ;; formatted in some special way.
  39. ;;
  40. ;; Special fontification of inline tasks, so that they can be immediately
  41. ;; recognized. From the stars of the headline, only the first and the
  42. ;; last two will be visible, the others will be hidden using the
  43. ;; `org-hide' face.
  44. ;;
  45. ;; An inline task is identified solely by a minimum outline level, given
  46. ;; by the variable `org-inlinetask-min-level', default 15.
  47. ;;
  48. ;; Inline tasks are normally assumed to contain at most a time planning
  49. ;; line (DEADLINE etc) after it, and then any number of drawers, for
  50. ;; example LOGBOOK of PROPERTIES. No empty lines are allowed.
  51. ;; If you need to have normal text as part of an inline task, you
  52. ;; can do so by adding an "END" headline with the same number of stars,
  53. ;; for example
  54. ;;
  55. ;; **************** TODO some small task
  56. ;; DEADLINE: <2009-03-30 Mon>
  57. ;; :PROPERTIES:
  58. ;; :SOMETHING: or other
  59. ;; :END:
  60. ;; And here is some extra text
  61. ;; **************** END
  62. ;;
  63. ;; Also, if you want to use refiling and archiving for inline tasks,
  64. ;; The END line must be present to make things work properly.
  65. ;;
  66. ;; This package installs one new comand:
  67. ;;
  68. ;; C-c C-x t Insert a new inline task with END line
  69. ;;; Code
  70. (require 'org)
  71. (defgroup org-inlinetask nil
  72. "Options concerning inline tasks in Org mode."
  73. :tag "Org Inline Tasks"
  74. :group 'org-structure)
  75. (defcustom org-inlinetask-min-level 15
  76. "Minimum level a headline must have before it is treated as an inline task.
  77. It is strongly recommended that you set `org-cycle-max-level' not at all,
  78. or to a number smaller than this one. In fact, when `org-cycle-max-level' is
  79. not set, it will be assumed to be one less than the value of smaller than
  80. the value of this variable."
  81. :group 'org-inlinetask
  82. :type 'boolean)
  83. (defvar org-odd-levels-only)
  84. (defvar org-keyword-time-regexp)
  85. (defvar org-drawer-regexp)
  86. (defvar org-complex-heading-regexp)
  87. (defvar org-property-end-re)
  88. (defun org-inlinetask-insert-task ()
  89. "Insert an inline task."
  90. (interactive)
  91. (or (bolp) (newline))
  92. (insert (make-string org-inlinetask-min-level ?*) " \n"
  93. (make-string org-inlinetask-min-level ?*) " END\n")
  94. (end-of-line -1))
  95. (define-key org-mode-map "\C-c\C-xt" 'org-inlinetask-insert-task)
  96. (defvar htmlp) ; dynamically scoped into the next function
  97. (defvar latexp) ; dynamically scoped into the next function
  98. (defun org-inlinetask-export-handler ()
  99. "Handle headlines with level larger or equal to `org-inlinetask-min-level'.
  100. Either remove headline and meta data, or do special formatting."
  101. (goto-char (point-min))
  102. (let* ((nstars (if org-odd-levels-only
  103. (1- (* 2 (or org-inlinetask-min-level 200)))
  104. (or org-inlinetask-min-level 200)))
  105. (re1 (format "^\\(\\*\\{%d,\\}\\) .*\n" nstars))
  106. (re2 (concat "^[ \t]*" org-keyword-time-regexp))
  107. headline beg end stars content indent)
  108. (while (re-search-forward re1 nil t)
  109. (setq headline (match-string 0)
  110. stars (match-string 1)
  111. content nil)
  112. (replace-match "")
  113. (while (looking-at re2)
  114. (delete-region (point) (1+ (point-at-eol))))
  115. (while (looking-at org-drawer-regexp)
  116. (setq beg (point))
  117. (if (re-search-forward org-property-end-re nil t)
  118. (delete-region beg (1+ (match-end 0)))))
  119. (setq beg (point))
  120. (when (and (re-search-forward "^\\(\\*+\\) " nil t)
  121. (= (length (match-string 1)) (length stars))
  122. (progn (goto-char (match-end 0))
  123. (looking-at "END[ \t]*$")))
  124. (setq content (buffer-substring beg (1- (point-at-bol))))
  125. (delete-region beg (1+ (match-end 0))))
  126. (goto-char beg)
  127. (when (string-match org-complex-heading-regexp headline)
  128. (setq headline (concat
  129. (if (match-end 2)
  130. (concat (match-string 2 headline) " ") "")
  131. (match-string 4 headline)))
  132. (if (not (string-match "\\S-" content))
  133. (setq content nil)
  134. (if (string-match "[ \t\n]+\\'" content)
  135. (setq content (substring content 0 (match-beginning 0))))
  136. (setq content (org-remove-indentation content))
  137. (if latexp (setq content (concat "\\quad \\\\\n" content))))
  138. (insert "- ")
  139. (setq indent (make-string (current-column) ?\ ))
  140. (insert headline " ::")
  141. (when content
  142. (insert (if htmlp " " (concat "\n" indent))
  143. (mapconcat 'identity (org-split-string content "\n")
  144. (concat "\n" indent)) "\n"))))))
  145. (defun org-inlinetask-fontify (limit)
  146. "Fontify the inline tasks."
  147. (let* ((nstars (if org-odd-levels-only
  148. (1- (* 2 (or org-inlinetask-min-level 200)))
  149. (or org-inlinetask-min-level 200)))
  150. (re (concat "^\\(\\*\\)\\(\\*\\{"
  151. (format "%d" (- nstars 3))
  152. ",\\}\\)\\(\\*\\* .*\\)")))
  153. (while (re-search-forward re limit t)
  154. (add-text-properties (match-beginning 1) (match-end 1)
  155. '(face org-warning font-lock-fontified t))
  156. (add-text-properties (match-beginning 2) (match-end 2)
  157. '(face org-hide font-lock-fontified t))
  158. (add-text-properties (match-beginning 3) (match-end 3)
  159. '(face shadow font-lock-fontified t)))))
  160. (defun org-inlinetask-remove-END-maybe ()
  161. "Remove an END line when present."
  162. (when (looking-at (format "\\([ \t]*\n\\)*\\*\\{%d,\\}[ \t]+END[ \t]*$"
  163. org-inlinetask-min-level))
  164. (replace-match "")))
  165. (eval-after-load "org-exp"
  166. '(add-hook 'org-export-preprocess-after-tree-selection-hook
  167. 'org-inlinetask-export-handler))
  168. (eval-after-load "org"
  169. '(add-hook 'org-font-lock-hook 'org-inlinetask-fontify))
  170. (provide 'org-inlinetask)
  171. ;;; org-inlinetask.el ends here