org-inlinetask.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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.4
  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. ;; If you need to have a time planning line (DEADLINE etc), drawers,
  49. ;; for example LOGBOOK of PROPERTIES, or even normal text as part of
  50. ;; the inline task, you must add an "END" headline with the same
  51. ;; number of stars.
  52. ;;
  53. ;; As an example, here are two valid inline tasks:
  54. ;;
  55. ;; **************** TODO a small task
  56. ;;
  57. ;; and
  58. ;;
  59. ;; **************** TODO another 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. ;;
  67. ;; Also, if you want to use refiling and archiving for inline tasks,
  68. ;; The END line must be present to make things work properly.
  69. ;;
  70. ;; This package installs one new command:
  71. ;;
  72. ;; C-c C-x t Insert a new inline task with END line
  73. ;;; Code:
  74. (require 'org)
  75. (defgroup org-inlinetask nil
  76. "Options concerning inline tasks in Org mode."
  77. :tag "Org Inline Tasks"
  78. :group 'org-structure)
  79. (defcustom org-inlinetask-min-level 15
  80. "Minimum level a headline must have before it is treated as an inline task.
  81. It is strongly recommended that you set `org-cycle-max-level' not at all,
  82. or to a number smaller than this one. In fact, when `org-cycle-max-level' is
  83. not set, it will be assumed to be one less than the value of smaller than
  84. the value of this variable."
  85. :group 'org-inlinetask
  86. :type '(choice
  87. (const :tag "Off" nil)
  88. (integer)))
  89. (defcustom org-inlinetask-export t
  90. "Non-nil means export inline tasks.
  91. When nil, they will not be exported."
  92. :group 'org-inlinetask
  93. :type 'boolean)
  94. (defvar org-inlinetask-export-templates
  95. '((html "<pre class=\"inlinetask\"><b>%s%s</b><br>%s</pre>"
  96. '((unless (eq todo "")
  97. (format "<span class=\"%s %s\">%s%s</span> "
  98. class todo todo priority))
  99. heading content))
  100. (latex "\\begin\{description\}\\item[%s%s]%s\\end\{description\}"
  101. '((unless (eq todo "") (format "\\textsc\{%s%s\} " todo priority))
  102. heading content))
  103. (ascii " -- %s%s%s"
  104. '((unless (eq todo "") (format "%s%s " todo priority))
  105. heading
  106. (unless (eq content "")
  107. (format "\n ¦ %s"
  108. (mapconcat 'identity (org-split-string content "\n")
  109. "\n ¦ ")))))
  110. (docbook "<variablelist>
  111. <varlistentry>
  112. <term>%s%s</term>
  113. <listitem><para>%s</para></listitem>
  114. </varlistentry>
  115. </variablelist>"
  116. '((unless (eq todo "") (format "%s%s " todo priority))
  117. heading content)))
  118. "Templates for inline tasks in various exporters.
  119. This variable is an alist in the shape of (BACKEND STRING OBJECTS).
  120. BACKEND is the name of the backend for the template (ascii, html...).
  121. STRING is a format control string.
  122. OBJECTS is a list of elements to be substituted into the format
  123. string. They can be of any type, from a string to a form
  124. returning a value (thus allowing conditional insertion). A nil
  125. object will be substituted as the empty string. Obviously, there
  126. must be at least as many objects as %-sequences in the format
  127. string.
  128. Moreover, the following special keywords are provided: `todo',
  129. `priority', `heading', `content', `tags'. If some of them are not
  130. defined in an inline task, their value is the empty string.
  131. As an example, valid associations are:
  132. (html \"<ul><li>%s <p>%s</p></li></ul>\" (heading content))
  133. or, with the additional package \"todonotes\" for LaTeX,
  134. (latex \"\\todo[inline]{\\textbf{\\textsf{%s %s}}\\linebreak{} %s}\"
  135. '((unless (eq todo \"\")
  136. (format \"\\textsc{%s%s}\" todo priority))
  137. heading content)))")
  138. (defvar org-odd-levels-only)
  139. (defvar org-keyword-time-regexp)
  140. (defvar org-drawer-regexp)
  141. (defvar org-complex-heading-regexp)
  142. (defvar org-property-end-re)
  143. (defcustom org-inlinetask-default-state nil
  144. "Non-nil means make inline tasks have a TODO keyword initially.
  145. This should be the state `org-inlinetask-insert-task' should use by
  146. default, or nil of no state should be assigned."
  147. :group 'org-inlinetask
  148. :type '(choice
  149. (const :tag "No state" nil)
  150. (string :tag "Specific state")))
  151. (defun org-inlinetask-insert-task (&optional no-state)
  152. "Insert an inline task.
  153. If prefix arg NO-STATE is set, ignore `org-inlinetask-default-state'."
  154. (interactive "P")
  155. (or (bolp) (newline))
  156. (let ((indent org-inlinetask-min-level))
  157. (if org-odd-levels-only
  158. (setq indent (- (* 2 indent) 1)))
  159. (insert (make-string indent ?*)
  160. (if (or no-state (not org-inlinetask-default-state))
  161. " \n"
  162. (concat " " org-inlinetask-default-state " \n"))
  163. (make-string indent ?*) " END\n"))
  164. (end-of-line -1))
  165. (define-key org-mode-map "\C-c\C-xt" 'org-inlinetask-insert-task)
  166. (defun org-inlinetask-outline-regexp ()
  167. "Return string matching an inline task heading.
  168. The number of levels is controlled by `org-inlinetask-min-level'."
  169. (let ((nstars (if org-odd-levels-only
  170. (1- (* org-inlinetask-min-level 2))
  171. org-inlinetask-min-level)))
  172. (format "^\\(\\*\\{%d,\\}\\)[ \t]+" nstars)))
  173. (defun org-inlinetask-in-task-p ()
  174. "Return true if point is inside an inline task."
  175. (save-excursion
  176. (beginning-of-line)
  177. (let* ((case-fold-search t)
  178. (stars-re (org-inlinetask-outline-regexp))
  179. (task-beg-re (concat stars-re "\\(?:.*\\)"))
  180. (task-end-re (concat stars-re "END[ \t]*$")))
  181. (or (org-looking-at-p task-beg-re)
  182. (and (re-search-forward "^\\*+[ \t]+" nil t)
  183. (progn (beginning-of-line) (org-looking-at-p task-end-re)))))))
  184. (defun org-inlinetask-goto-beginning ()
  185. "Go to the beginning of the inline task at point."
  186. (end-of-line)
  187. (let ((case-fold-search t)
  188. (inlinetask-re (org-inlinetask-outline-regexp)))
  189. (re-search-backward inlinetask-re nil t)
  190. (when (org-looking-at-p (concat inlinetask-re "END[ \t]*$"))
  191. (re-search-backward inlinetask-re nil t))))
  192. (defun org-inlinetask-goto-end ()
  193. "Go to the end of the inline task at point."
  194. (beginning-of-line)
  195. (let ((case-fold-search t)
  196. (inlinetask-re (org-inlinetask-outline-regexp)))
  197. (cond
  198. ((org-looking-at-p (concat inlinetask-re "END[ \t]*$"))
  199. (forward-line 1))
  200. ((org-looking-at-p inlinetask-re)
  201. (forward-line 1)
  202. (when (org-inlinetask-in-task-p)
  203. (re-search-forward inlinetask-re nil t)
  204. (forward-line 1)))
  205. (t
  206. (re-search-forward inlinetask-re nil t)
  207. (forward-line 1)))))
  208. (defun org-inlinetask-get-task-level ()
  209. "Get the level of the inline task around.
  210. This assumes the point is inside an inline task."
  211. (save-excursion
  212. (end-of-line)
  213. (re-search-backward (org-inlinetask-outline-regexp) nil t)
  214. (- (match-end 1) (match-beginning 1))))
  215. (defvar backend) ; dynamically scoped into the next function
  216. (defun org-inlinetask-export-handler ()
  217. "Handle headlines with level larger or equal to `org-inlinetask-min-level'.
  218. Either remove headline and meta data, or do special formatting."
  219. (goto-char (point-min))
  220. (let* ((nstars (if org-odd-levels-only
  221. (1- (* 2 (or org-inlinetask-min-level 200)))
  222. (or org-inlinetask-min-level 200)))
  223. (re1 (format "^\\(\\*\\{%d,\\}\\) .*\n" nstars))
  224. (re2 (concat "^[ \t]*" org-keyword-time-regexp))
  225. headline beg end stars content)
  226. (while (re-search-forward re1 nil t)
  227. (setq headline (match-string 0)
  228. stars (match-string 1)
  229. content nil)
  230. (replace-match "")
  231. (while (looking-at re2)
  232. (delete-region (point) (1+ (point-at-eol))))
  233. (while (looking-at org-drawer-regexp)
  234. (setq beg (point))
  235. (if (re-search-forward org-property-end-re nil t)
  236. (delete-region beg (1+ (match-end 0)))))
  237. (setq beg (point))
  238. (when (and (re-search-forward "^\\(\\*+\\) " nil t)
  239. (= (length (match-string 1)) (length stars))
  240. (progn (goto-char (match-end 0))
  241. (looking-at "END[ \t]*$")))
  242. (setq content (buffer-substring beg (1- (point-at-bol))))
  243. (delete-region beg (1+ (match-end 0))))
  244. (goto-char beg)
  245. (when org-inlinetask-export
  246. ;; content formatting
  247. (when content
  248. (if (not (string-match "\\S-" content))
  249. (setq content nil)
  250. (if (string-match "[ \t\n]+\\'" content)
  251. (setq content (substring content 0 (match-beginning 0))))
  252. (setq content (org-remove-indentation content))))
  253. (setq content (or content ""))
  254. ;; grab elements to export
  255. (when (string-match org-complex-heading-regexp headline)
  256. (let* ((todo (or (match-string 2 headline) ""))
  257. (class (or (and (eq "" todo) "")
  258. (if (member todo org-done-keywords) "done" "todo")))
  259. (priority (or (match-string 3 headline) ""))
  260. (heading (or (match-string 4 headline) ""))
  261. (tags (or (match-string 5 headline) ""))
  262. (backend-spec (assq backend org-inlinetask-export-templates))
  263. (format-str (nth 1 backend-spec))
  264. (tokens (cadr (nth 2 backend-spec)))
  265. ;; change nil arguments into empty strings
  266. (nil-to-str (lambda (el) (or (eval el) "")))
  267. ;; build and protect export string
  268. (export-str (org-add-props
  269. (eval (append '(format format-str)
  270. (mapcar nil-to-str tokens)))
  271. nil 'org-protected t)))
  272. ;; eventually insert it
  273. (insert export-str "\n")))))))
  274. (defun org-inlinetask-get-current-indentation ()
  275. "Get the indentation of the last non-while line above this one."
  276. (save-excursion
  277. (beginning-of-line 1)
  278. (skip-chars-backward " \t\n")
  279. (beginning-of-line 1)
  280. (or (org-at-item-p)
  281. (looking-at "[ \t]*"))
  282. (goto-char (match-end 0))
  283. (current-column)))
  284. (defun org-inlinetask-fontify (limit)
  285. "Fontify the inline tasks."
  286. (let* ((nstars (if org-odd-levels-only
  287. (1- (* 2 (or org-inlinetask-min-level 200)))
  288. (or org-inlinetask-min-level 200)))
  289. (re (concat "^\\(\\*\\)\\(\\*\\{"
  290. (format "%d" (- nstars 3))
  291. ",\\}\\)\\(\\*\\* .*\\)")))
  292. (while (re-search-forward re limit t)
  293. (add-text-properties (match-beginning 1) (match-end 1)
  294. '(face org-warning font-lock-fontified t))
  295. (add-text-properties (match-beginning 2) (match-end 2)
  296. '(face org-hide font-lock-fontified t))
  297. (add-text-properties (match-beginning 3) (match-end 3)
  298. '(face shadow font-lock-fontified t)))))
  299. (defun org-inlinetask-remove-END-maybe ()
  300. "Remove an END line when present."
  301. (when (looking-at (format "\\([ \t]*\n\\)*\\*\\{%d,\\}[ \t]+END[ \t]*$"
  302. org-inlinetask-min-level))
  303. (replace-match "")))
  304. (eval-after-load "org-exp"
  305. '(add-hook 'org-export-preprocess-after-tree-selection-hook
  306. 'org-inlinetask-export-handler))
  307. (eval-after-load "org"
  308. '(add-hook 'org-font-lock-hook 'org-inlinetask-fontify))
  309. (provide 'org-inlinetask)
  310. ;;; org-inlinetask.el ends here