org-inlinetask.el 12 KB

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