org-inlinetask.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. ;;; org-inlinetask.el --- Tasks independent of outline hierarchy
  2. ;; Copyright (C) 2009, 2010 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: 7.3
  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 other 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 command:
  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 '(choice
  83. (const :tag "Off" nil)
  84. (integer)))
  85. (defcustom org-inlinetask-export t
  86. "Non-nil means export inline tasks.
  87. When nil, they will not be exported."
  88. :group 'org-inlinetask
  89. :type 'boolean)
  90. (defvar org-odd-levels-only)
  91. (defvar org-keyword-time-regexp)
  92. (defvar org-drawer-regexp)
  93. (defvar org-complex-heading-regexp)
  94. (defvar org-property-end-re)
  95. (defcustom org-inlinetask-default-state nil
  96. "Non-nil means make inline tasks have a TODO keyword initially.
  97. This should be the state `org-inlinetask-insert-task' should use by
  98. default, or nil of no state should be assigned."
  99. :group 'org-inlinetask
  100. :type '(choice
  101. (const :tag "No state" nil)
  102. (string :tag "Specific state")))
  103. (defun org-inlinetask-insert-task (&optional no-state)
  104. "Insert an inline task.
  105. If prefix arg NO-STATE is set, ignore `org-inlinetask-default-state'."
  106. (interactive "P")
  107. (or (bolp) (newline))
  108. (let ((indent org-inlinetask-min-level))
  109. (if org-odd-levels-only
  110. (setq indent (- (* 2 indent) 1)))
  111. (insert (make-string indent ?*)
  112. (if (or no-state (not org-inlinetask-default-state))
  113. " \n"
  114. (concat " " org-inlinetask-default-state " \n"))
  115. (make-string indent ?*) " END\n"))
  116. (end-of-line -1))
  117. (define-key org-mode-map "\C-c\C-xt" 'org-inlinetask-insert-task)
  118. (defun org-inlinetask-outline-regexp ()
  119. "Return string matching an inline task heading.
  120. The number of levels is controlled by `org-inlinetask-min-level'."
  121. (let ((nstars (if org-odd-levels-only
  122. (1- (* org-inlinetask-min-level 2))
  123. org-inlinetask-min-level)))
  124. (format "^\\(\\*\\{%d,\\}\\)[ \t]+" nstars)))
  125. (defun org-inlinetask-in-task-p ()
  126. "Return true if point is inside an inline task."
  127. (save-excursion
  128. (let* ((stars-re (org-inlinetask-outline-regexp))
  129. (task-beg-re (concat stars-re "\\(?:.*\\)"))
  130. (task-end-re (concat stars-re "\\(?:END\\|end\\)[ \t]*$")))
  131. (beginning-of-line)
  132. (or (looking-at task-beg-re)
  133. (and (re-search-forward "^\\*+[ \t]+" nil t)
  134. (progn (beginning-of-line) (looking-at task-end-re)))))))
  135. (defun org-inlinetask-goto-beginning ()
  136. "Go to the beginning of the inline task at point."
  137. (end-of-line)
  138. (re-search-backward (org-inlinetask-outline-regexp) nil t)
  139. (when (org-looking-at-p (concat (org-inlinetask-outline-regexp) "END[ \t]*$"))
  140. (re-search-backward (org-inlinetask-outline-regexp) nil t)))
  141. (defun org-inlinetask-goto-end ()
  142. "Go to the end of the inline task at point."
  143. (beginning-of-line)
  144. (cond
  145. ((org-looking-at-p (concat (org-inlinetask-outline-regexp) "END[ \t]*$"))
  146. (forward-line 1))
  147. ((org-looking-at-p (org-inlinetask-outline-regexp))
  148. (forward-line 1)
  149. (when (org-inlinetask-in-task-p)
  150. (re-search-forward (org-inlinetask-outline-regexp) nil t)
  151. (forward-line 1)))
  152. (t
  153. (re-search-forward (org-inlinetask-outline-regexp) nil t)
  154. (forward-line 1))))
  155. (defun org-inlinetask-get-task-level ()
  156. "Get the level of the inline task around.
  157. This assumes the point is inside an inline task."
  158. (save-excursion
  159. (end-of-line)
  160. (re-search-backward (org-inlinetask-outline-regexp) nil t)
  161. (- (match-end 1) (match-beginning 1))))
  162. (defvar backend) ; dynamically scoped into the next function
  163. (defun org-inlinetask-export-handler ()
  164. "Handle headlines with level larger or equal to `org-inlinetask-min-level'.
  165. Either remove headline and meta data, or do special formatting."
  166. (goto-char (point-min))
  167. (let* ((nstars (if org-odd-levels-only
  168. (1- (* 2 (or org-inlinetask-min-level 200)))
  169. (or org-inlinetask-min-level 200)))
  170. (re1 (format "^\\(\\*\\{%d,\\}\\) .*\n" nstars))
  171. (re2 (concat "^[ \t]*" org-keyword-time-regexp))
  172. headline beg end stars content indent)
  173. (while (re-search-forward re1 nil t)
  174. (setq headline (match-string 0)
  175. stars (match-string 1)
  176. content nil)
  177. (replace-match "")
  178. (while (looking-at re2)
  179. (delete-region (point) (1+ (point-at-eol))))
  180. (while (looking-at org-drawer-regexp)
  181. (setq beg (point))
  182. (if (re-search-forward org-property-end-re nil t)
  183. (delete-region beg (1+ (match-end 0)))))
  184. (setq beg (point))
  185. (when (and (re-search-forward "^\\(\\*+\\) " nil t)
  186. (= (length (match-string 1)) (length stars))
  187. (progn (goto-char (match-end 0))
  188. (looking-at "END[ \t]*$")))
  189. (setq content (buffer-substring beg (1- (point-at-bol))))
  190. (delete-region beg (1+ (match-end 0))))
  191. (goto-char beg)
  192. (when org-inlinetask-export
  193. (when (string-match org-complex-heading-regexp headline)
  194. (setq headline (concat
  195. (if (match-end 2)
  196. (concat
  197. (format
  198. "@<span class=\"%s %s\"> %s@</span>"
  199. (if (member (match-string 2 headline)
  200. org-done-keywords)
  201. "done" "todo")
  202. (match-string 2 headline)
  203. (match-string 2 headline))
  204. " ") "")
  205. (match-string 4 headline)))
  206. (when content
  207. (if (not (string-match "\\S-" content))
  208. (setq content nil)
  209. (if (string-match "[ \t\n]+\\'" content)
  210. (setq content (substring content 0 (match-beginning 0))))
  211. (setq content (org-remove-indentation content))
  212. (if latexp (setq content (concat "\\nbsp\\\\ \\quad " content)))))
  213. (insert (make-string (org-inlinetask-get-current-indentation) ?\ )
  214. "- ")
  215. (setq indent (make-string (current-column) ?\ ))
  216. (insert headline " :: ")
  217. (if content
  218. (insert (if htmlp " " (concat "\n" indent))
  219. (mapconcat 'identity (org-split-string content "\n")
  220. (concat "\n" indent)) "\n")
  221. (insert "\n"))
  222. (insert indent)
  223. (backward-delete-char 2)
  224. (insert "THISISTHEINLINELISTTEMINATOR\n"))))))
  225. (defun org-inlinetask-get-current-indentation ()
  226. "Get the indentation of the last non-while line above this one."
  227. (save-excursion
  228. (beginning-of-line 1)
  229. (skip-chars-backward " \t\n")
  230. (beginning-of-line 1)
  231. (or (org-at-item-p)
  232. (looking-at "[ \t]*"))
  233. (goto-char (match-end 0))
  234. (current-column)))
  235. (defun org-inlinetask-fontify (limit)
  236. "Fontify the inline tasks."
  237. (let* ((nstars (if org-odd-levels-only
  238. (1- (* 2 (or org-inlinetask-min-level 200)))
  239. (or org-inlinetask-min-level 200)))
  240. (re (concat "^\\(\\*\\)\\(\\*\\{"
  241. (format "%d" (- nstars 3))
  242. ",\\}\\)\\(\\*\\* .*\\)")))
  243. (while (re-search-forward re limit t)
  244. (add-text-properties (match-beginning 1) (match-end 1)
  245. '(face org-warning font-lock-fontified t))
  246. (add-text-properties (match-beginning 2) (match-end 2)
  247. '(face org-hide font-lock-fontified t))
  248. (add-text-properties (match-beginning 3) (match-end 3)
  249. '(face shadow font-lock-fontified t)))))
  250. (defun org-inlinetask-remove-END-maybe ()
  251. "Remove an END line when present."
  252. (when (looking-at (format "\\([ \t]*\n\\)*\\*\\{%d,\\}[ \t]+END[ \t]*$"
  253. org-inlinetask-min-level))
  254. (replace-match "")))
  255. (defun org-inlinetask-remove-terminator ()
  256. (let (beg end)
  257. (save-excursion
  258. (goto-char (point-min))
  259. (while (re-search-forward "THISISTHEINLINELISTTEMINATOR\n" nil t)
  260. (setq beg (match-beginning 0) end (match-end 0))
  261. (save-excursion
  262. (beginning-of-line 1)
  263. (and (looking-at "<p\\(ara\\)?>THISISTHEINLINELISTTEMINATOR[ \t\n]*</p\\(ara\\)?>")
  264. (setq beg (point) end (match-end 0))))
  265. (delete-region beg end)))))
  266. (eval-after-load "org-exp"
  267. '(add-hook 'org-export-preprocess-after-tree-selection-hook
  268. 'org-inlinetask-export-handler))
  269. (eval-after-load "org"
  270. '(add-hook 'org-font-lock-hook 'org-inlinetask-fontify))
  271. (eval-after-load "org-html"
  272. '(add-hook 'org-export-html-final-hook 'org-inlinetask-remove-terminator))
  273. (eval-after-load "org-latex"
  274. '(add-hook 'org-export-latex-final-hook 'org-inlinetask-remove-terminator))
  275. (eval-after-load "org-ascii"
  276. '(add-hook 'org-export-ascii-final-hook 'org-inlinetask-remove-terminator))
  277. (eval-after-load "org-docbook"
  278. '(add-hook 'org-export-docbook-final-hook 'org-inlinetask-remove-terminator))
  279. (provide 'org-inlinetask)
  280. ;;; org-inlinetask.el ends here