org-inlinetask.el 17 KB

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