org-inlinetask.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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.7
  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. This in handled by
  39. ;; `org-inlinetask-export' and `org-inlinetask-export-templates'
  40. ;; variables.
  41. ;;
  42. ;; Special fontification of inline tasks, so that they can be immediately
  43. ;; recognized. From the stars of the headline, only the first and the
  44. ;; last two will be visible, the others will be hidden using the
  45. ;; `org-hide' face.
  46. ;;
  47. ;; An inline task is identified solely by a minimum outline level, given
  48. ;; by the variable `org-inlinetask-min-level', default 15.
  49. ;;
  50. ;; If you need to have a time planning line (DEADLINE etc), drawers,
  51. ;; for example LOGBOOK of PROPERTIES, or even normal text as part of
  52. ;; the inline task, you must add an "END" headline with the same
  53. ;; number of stars.
  54. ;;
  55. ;; As an example, here are two valid inline tasks:
  56. ;;
  57. ;; **************** TODO a small task
  58. ;;
  59. ;; and
  60. ;;
  61. ;; **************** TODO another small task
  62. ;; DEADLINE: <2009-03-30 Mon>
  63. ;; :PROPERTIES:
  64. ;; :SOMETHING: or other
  65. ;; :END:
  66. ;; And here is some extra text
  67. ;; **************** END
  68. ;;
  69. ;; Also, if you want to use refiling and archiving for inline tasks,
  70. ;; The END line must be present to make things work properly.
  71. ;;
  72. ;; This package installs one new command:
  73. ;;
  74. ;; C-c C-x t Insert a new inline task with END line
  75. ;;; Code:
  76. (require 'org)
  77. (defgroup org-inlinetask nil
  78. "Options concerning inline tasks in Org mode."
  79. :tag "Org Inline Tasks"
  80. :group 'org-structure)
  81. (defcustom org-inlinetask-min-level 15
  82. "Minimum level a headline must have before it is treated as an inline task.
  83. It is strongly recommended that you set `org-cycle-max-level' not at all,
  84. or to a number smaller than this one. In fact, when `org-cycle-max-level' is
  85. not set, it will be assumed to be one less than the value of smaller than
  86. the value of this variable."
  87. :group 'org-inlinetask
  88. :type '(choice
  89. (const :tag "Off" nil)
  90. (integer)))
  91. (defcustom org-inlinetask-export t
  92. "Non-nil means export inline tasks.
  93. When nil, they will not be exported."
  94. :group 'org-inlinetask
  95. :type 'boolean)
  96. (defvar org-inlinetask-export-templates
  97. '((html "<pre class=\"inlinetask\"><b>%s%s</b><br />%s</pre>"
  98. '((unless (eq todo "")
  99. (format "<span class=\"%s %s\">%s%s</span> "
  100. class todo todo priority))
  101. heading content))
  102. (latex "\\begin\{description\}\n\\item[%s%s]~%s\\end\{description\}"
  103. '((unless (eq todo "") (format "\\textsc\{%s%s\} " todo priority))
  104. heading content))
  105. (ascii " -- %s%s%s"
  106. '((unless (eq todo "") (format "%s%s " todo priority))
  107. heading
  108. (unless (eq content "")
  109. (format "\n ¦ %s"
  110. (mapconcat 'identity (org-split-string content "\n")
  111. "\n ¦ ")))))
  112. (docbook "<variablelist>
  113. <varlistentry>
  114. <term>%s%s</term>
  115. <listitem><para>%s</para></listitem>
  116. </varlistentry>
  117. </variablelist>"
  118. '((unless (eq todo "") (format "%s%s " todo priority))
  119. heading content)))
  120. "Templates for inline tasks in various exporters.
  121. This variable is an alist in the shape of \(BACKEND STRING OBJECTS\).
  122. BACKEND is the name of the backend for the template \(ascii, html...\).
  123. STRING is a format control string.
  124. OBJECTS is a list of elements to be substituted into the format
  125. string. They can be of any type, from a string to a form
  126. returning a value (thus allowing conditional insertion). A nil
  127. object will be substituted as the empty string. Obviously, there
  128. must be at least as many objects as %-sequences in the format
  129. string.
  130. Moreover, the following special keywords are provided: `todo',
  131. `priority', `heading', `content', `tags'. If some of them are not
  132. defined in an inline task, their value is the empty string.
  133. As an example, valid associations are:
  134. \(html \"<ul><li>%s <p>%s</p></li></ul>\" \(heading content\)\)
  135. or, with the additional package \"todonotes\" for LaTeX,
  136. \(latex \"\\todo[inline]{\\textbf{\\textsf{%s %s}}\\linebreak{} %s}\"
  137. '\(\(unless \(eq todo \"\"\)
  138. \(format \"\\textsc{%s%s}\" todo priority\)\)
  139. heading content\)\)\)")
  140. (defvar org-odd-levels-only)
  141. (defvar org-keyword-time-regexp)
  142. (defvar org-drawer-regexp)
  143. (defvar org-complex-heading-regexp)
  144. (defvar org-property-end-re)
  145. (defcustom org-inlinetask-default-state nil
  146. "Non-nil means make inline tasks have a TODO keyword initially.
  147. This should be the state `org-inlinetask-insert-task' should use by
  148. default, or nil of no state should be assigned."
  149. :group 'org-inlinetask
  150. :type '(choice
  151. (const :tag "No state" nil)
  152. (string :tag "Specific state")))
  153. (defun org-inlinetask-insert-task (&optional no-state)
  154. "Insert an inline task.
  155. If prefix arg NO-STATE is set, ignore `org-inlinetask-default-state'."
  156. (interactive "P")
  157. ;; Error when inside an inline task, except if point was at its very
  158. ;; beginning, in which case the new inline task will be inserted
  159. ;; before this one.
  160. (when (and (org-inlinetask-in-task-p)
  161. (not (and (org-inlinetask-at-task-p) (bolp))))
  162. (error "Cannot nest inline tasks"))
  163. (or (bolp) (newline))
  164. (let ((indent org-inlinetask-min-level))
  165. (if org-odd-levels-only
  166. (setq indent (- (* 2 indent) 1)))
  167. (insert (make-string indent ?*)
  168. (if (or no-state (not org-inlinetask-default-state))
  169. " \n"
  170. (concat " " org-inlinetask-default-state " \n"))
  171. (make-string indent ?*) " END\n"))
  172. (end-of-line -1))
  173. (define-key org-mode-map "\C-c\C-xt" 'org-inlinetask-insert-task)
  174. (defun org-inlinetask-outline-regexp ()
  175. "Return string matching an inline task heading.
  176. The number of levels is controlled by `org-inlinetask-min-level'."
  177. (let ((nstars (if org-odd-levels-only
  178. (1- (* org-inlinetask-min-level 2))
  179. org-inlinetask-min-level)))
  180. (format "^\\(\\*\\{%d,\\}\\)[ \t]+" nstars)))
  181. (defun org-inlinetask-at-task-p ()
  182. "Return true if point is at beginning of an inline task."
  183. (save-excursion
  184. (beginning-of-line)
  185. (and (looking-at (concat (org-inlinetask-outline-regexp) "\\(.*\\)"))
  186. (not (string-match "^end[ \t]*$" (downcase (match-string 2)))))))
  187. (defun org-inlinetask-in-task-p ()
  188. "Return true if point is inside an inline task."
  189. (save-excursion
  190. (beginning-of-line)
  191. (let* ((case-fold-search t)
  192. (stars-re (org-inlinetask-outline-regexp))
  193. (task-beg-re (concat stars-re "\\(?:.*\\)"))
  194. (task-end-re (concat stars-re "END[ \t]*$")))
  195. (or (org-looking-at-p task-beg-re)
  196. (and (re-search-forward "^\\*+[ \t]+" nil t)
  197. (progn (beginning-of-line) (org-looking-at-p task-end-re)))))))
  198. (defun org-inlinetask-goto-beginning ()
  199. "Go to the beginning of the inline task at point."
  200. (end-of-line)
  201. (let ((case-fold-search t)
  202. (inlinetask-re (org-inlinetask-outline-regexp)))
  203. (re-search-backward inlinetask-re nil t)
  204. (when (org-looking-at-p (concat inlinetask-re "END[ \t]*$"))
  205. (re-search-backward inlinetask-re nil t))))
  206. (defun org-inlinetask-goto-end ()
  207. "Go to the end of the inline task at point."
  208. (beginning-of-line)
  209. (let ((case-fold-search t)
  210. (inlinetask-re (org-inlinetask-outline-regexp)))
  211. (cond
  212. ((org-looking-at-p (concat inlinetask-re "END[ \t]*$"))
  213. (forward-line 1))
  214. ((org-looking-at-p inlinetask-re)
  215. (forward-line 1)
  216. (when (org-inlinetask-in-task-p)
  217. (re-search-forward inlinetask-re nil t)
  218. (forward-line 1)))
  219. (t
  220. (re-search-forward inlinetask-re nil t)
  221. (forward-line 1)))))
  222. (defun org-inlinetask-get-task-level ()
  223. "Get the level of the inline task around.
  224. This assumes the point is inside an inline task."
  225. (save-excursion
  226. (end-of-line)
  227. (re-search-backward (org-inlinetask-outline-regexp) nil t)
  228. (- (match-end 1) (match-beginning 1))))
  229. (defun org-inlinetask-promote ()
  230. "Promote the inline task at point.
  231. If the task has an end part, promote it. Also, prevents level from
  232. going below `org-inlinetask-min-level'."
  233. (interactive)
  234. (if (not (org-inlinetask-in-task-p))
  235. (error "Not in an inline task")
  236. (save-excursion
  237. (let* ((lvl (org-inlinetask-get-task-level))
  238. (next-lvl (org-get-valid-level lvl -1))
  239. (diff (- next-lvl lvl))
  240. (down-task (concat (make-string next-lvl ?*)))
  241. beg)
  242. (if (< next-lvl org-inlinetask-min-level)
  243. (error "Cannot promote an inline task at minimum level")
  244. (org-inlinetask-goto-beginning)
  245. (setq beg (point))
  246. (replace-match down-task nil t nil 1)
  247. (org-inlinetask-goto-end)
  248. (if (eobp) (beginning-of-line) (forward-line -1))
  249. (unless (= (point) beg)
  250. (replace-match down-task nil t nil 1)
  251. (when org-adapt-indentation
  252. (goto-char beg)
  253. (org-fixup-indentation diff))))))))
  254. (defun org-inlinetask-demote ()
  255. "Demote the inline task at point.
  256. If the task has an end part, also demote it."
  257. (interactive)
  258. (if (not (org-inlinetask-in-task-p))
  259. (error "Not in an inline task")
  260. (save-excursion
  261. (let* ((lvl (org-inlinetask-get-task-level))
  262. (next-lvl (org-get-valid-level lvl 1))
  263. (diff (- next-lvl lvl))
  264. (down-task (concat (make-string next-lvl ?*)))
  265. beg)
  266. (org-inlinetask-goto-beginning)
  267. (setq beg (point))
  268. (replace-match down-task nil t nil 1)
  269. (org-inlinetask-goto-end)
  270. (if (eobp) (beginning-of-line) (forward-line -1))
  271. (unless (= (point) beg)
  272. (replace-match down-task nil t nil 1)
  273. (when org-adapt-indentation
  274. (goto-char beg)
  275. (org-fixup-indentation diff)))))))
  276. (defvar org-export-current-backend) ; dynamically bound in org-exp.el
  277. (defun org-inlinetask-export-handler ()
  278. "Handle headlines with level larger or equal to `org-inlinetask-min-level'.
  279. Either remove headline and meta data, or do special formatting."
  280. (goto-char (point-min))
  281. (let* ((keywords-re (concat "^[ \t]*" org-keyword-time-regexp))
  282. (inline-re (concat (org-inlinetask-outline-regexp) ".*")))
  283. (while (re-search-forward inline-re nil t)
  284. (let ((headline (match-string 0))
  285. (beg (point-at-bol))
  286. (end (copy-marker (save-excursion
  287. (org-inlinetask-goto-end) (point))))
  288. content)
  289. ;; Delete SCHEDULED, DEADLINE...
  290. (while (re-search-forward keywords-re end t)
  291. (delete-region (point-at-bol) (1+ (point-at-eol))))
  292. (goto-char beg)
  293. ;; Delete drawers
  294. (while (re-search-forward org-drawer-regexp end t)
  295. (when (save-excursion (re-search-forward org-property-end-re nil t))
  296. (delete-region beg (1+ (match-end 0)))))
  297. ;; Get CONTENT, if any.
  298. (goto-char beg)
  299. (forward-line 1)
  300. (unless (= (point) end)
  301. (setq content (buffer-substring (point)
  302. (save-excursion (goto-char end)
  303. (forward-line -1)
  304. (point)))))
  305. ;; Remove the task.
  306. (goto-char beg)
  307. (delete-region beg end)
  308. (when (and org-inlinetask-export
  309. (assq org-export-current-backend
  310. org-inlinetask-export-templates))
  311. ;; Format CONTENT, if appropriate.
  312. (setq content
  313. (if (not (and content (string-match "\\S-" content)))
  314. ""
  315. ;; Ensure CONTENT has minimal indentation, a single
  316. ;; newline character at its boundaries, and isn't
  317. ;; protected.
  318. (when (string-match "`\\([ \t]*\n\\)+" content)
  319. (setq content (substring content (match-end 0))))
  320. (when (string-match "[ \t\n]+\\'" content)
  321. (setq content (substring content 0 (match-beginning 0))))
  322. (org-add-props (concat "\n" (org-remove-indentation content) "\n")
  323. '(org-protected nil))))
  324. (when (string-match org-complex-heading-regexp headline)
  325. (let* ((nil-to-str
  326. (function
  327. ;; Change nil arguments into empty strings.
  328. (lambda (el) (or (eval el) ""))))
  329. ;; Set up keywords provided to templates.
  330. (todo (or (match-string 2 headline) ""))
  331. (class (or (and (eq "" todo) "")
  332. (if (member todo org-done-keywords) "done" "todo")))
  333. (priority (or (match-string 3 headline) ""))
  334. (heading (or (match-string 4 headline) ""))
  335. (tags (or (match-string 5 headline) ""))
  336. ;; Read `org-inlinetask-export-templates'.
  337. (backend-spec (assq org-export-current-backend
  338. org-inlinetask-export-templates))
  339. (format-str (org-add-props (nth 1 backend-spec)
  340. '(org-protected t)))
  341. (tokens (cadr (nth 2 backend-spec)))
  342. ;; Build export string. Ensure it won't break
  343. ;; surrounding lists by giving it arbitrary high
  344. ;; indentation.
  345. (export-str (org-add-props
  346. (eval (append '(format format-str)
  347. (mapcar nil-to-str tokens)))
  348. '(original-indentation 1000))))
  349. ;; Ensure task starts a new paragraph.
  350. (unless (or (bobp)
  351. (save-excursion (forward-line -1)
  352. (looking-at "[ \t]*$")))
  353. (insert "\n"))
  354. (insert export-str)
  355. (unless (bolp) (insert "\n")))))))))
  356. (defun org-inlinetask-get-current-indentation ()
  357. "Get the indentation of the last non-while line above this one."
  358. (save-excursion
  359. (beginning-of-line 1)
  360. (skip-chars-backward " \t\n")
  361. (beginning-of-line 1)
  362. (or (org-at-item-p)
  363. (looking-at "[ \t]*"))
  364. (goto-char (match-end 0))
  365. (current-column)))
  366. (defun org-inlinetask-fontify (limit)
  367. "Fontify the inline tasks."
  368. (let* ((nstars (if org-odd-levels-only
  369. (1- (* 2 (or org-inlinetask-min-level 200)))
  370. (or org-inlinetask-min-level 200)))
  371. (re (concat "^\\(\\*\\)\\(\\*\\{"
  372. (format "%d" (- nstars 3))
  373. ",\\}\\)\\(\\*\\* .*\\)")))
  374. (while (re-search-forward re limit t)
  375. (add-text-properties (match-beginning 1) (match-end 1)
  376. '(face org-warning font-lock-fontified t))
  377. (add-text-properties (match-beginning 2) (match-end 2)
  378. '(face org-hide font-lock-fontified t))
  379. (add-text-properties (match-beginning 3) (match-end 3)
  380. '(face shadow font-lock-fontified t)))))
  381. (defun org-inlinetask-toggle-visibility ()
  382. "Toggle visibility of inline task at point."
  383. (let ((end (save-excursion
  384. (org-inlinetask-goto-end)
  385. (if (bolp) (1- (point)) (point))))
  386. (start (save-excursion
  387. (org-inlinetask-goto-beginning)
  388. (point-at-eol))))
  389. (cond
  390. ;; Nothing to show/hide.
  391. ((= end start))
  392. ;; Inlinetask was folded: expand it.
  393. ((get-char-property (1+ start) 'invisible)
  394. (outline-flag-region start end nil))
  395. (t (outline-flag-region start end t)))))
  396. (defun org-inlinetask-remove-END-maybe ()
  397. "Remove an END line when present."
  398. (when (looking-at (format "\\([ \t]*\n\\)*\\*\\{%d,\\}[ \t]+END[ \t]*$"
  399. org-inlinetask-min-level))
  400. (replace-match "")))
  401. (eval-after-load "org-exp"
  402. '(add-hook 'org-export-preprocess-before-backend-specifics-hook
  403. 'org-inlinetask-export-handler))
  404. (eval-after-load "org"
  405. '(add-hook 'org-font-lock-hook 'org-inlinetask-fontify))
  406. (provide 'org-inlinetask)
  407. ;;; org-inlinetask.el ends here