org-inlinetask.el 17 KB

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