org-inlinetask.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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.6
  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. (or (bolp) (newline))
  158. (let ((indent org-inlinetask-min-level))
  159. (if org-odd-levels-only
  160. (setq indent (- (* 2 indent) 1)))
  161. (insert (make-string indent ?*)
  162. (if (or no-state (not org-inlinetask-default-state))
  163. " \n"
  164. (concat " " org-inlinetask-default-state " \n"))
  165. (make-string indent ?*) " END\n"))
  166. (end-of-line -1))
  167. (define-key org-mode-map "\C-c\C-xt" 'org-inlinetask-insert-task)
  168. (defun org-inlinetask-outline-regexp ()
  169. "Return string matching an inline task heading.
  170. The number of levels is controlled by `org-inlinetask-min-level'."
  171. (let ((nstars (if org-odd-levels-only
  172. (1- (* org-inlinetask-min-level 2))
  173. org-inlinetask-min-level)))
  174. (format "^\\(\\*\\{%d,\\}\\)[ \t]+" nstars)))
  175. (defun org-inlinetask-at-task-p ()
  176. "Return true if point is at beginning of an inline task."
  177. (save-excursion
  178. (beginning-of-line)
  179. (and (looking-at (concat (org-inlinetask-outline-regexp) "\\(.*\\)"))
  180. (not (string-match "^end[ \t]*$" (downcase (match-string 2)))))))
  181. (defun org-inlinetask-in-task-p ()
  182. "Return true if point is inside an inline task."
  183. (save-excursion
  184. (beginning-of-line)
  185. (let* ((case-fold-search t)
  186. (stars-re (org-inlinetask-outline-regexp))
  187. (task-beg-re (concat stars-re "\\(?:.*\\)"))
  188. (task-end-re (concat stars-re "END[ \t]*$")))
  189. (or (org-looking-at-p task-beg-re)
  190. (and (re-search-forward "^\\*+[ \t]+" nil t)
  191. (progn (beginning-of-line) (org-looking-at-p task-end-re)))))))
  192. (defun org-inlinetask-goto-beginning ()
  193. "Go to the beginning of the inline task at point."
  194. (end-of-line)
  195. (let ((case-fold-search t)
  196. (inlinetask-re (org-inlinetask-outline-regexp)))
  197. (re-search-backward inlinetask-re nil t)
  198. (when (org-looking-at-p (concat inlinetask-re "END[ \t]*$"))
  199. (re-search-backward inlinetask-re nil t))))
  200. (defun org-inlinetask-goto-end ()
  201. "Go to the end of the inline task at point."
  202. (beginning-of-line)
  203. (let ((case-fold-search t)
  204. (inlinetask-re (org-inlinetask-outline-regexp)))
  205. (cond
  206. ((org-looking-at-p (concat inlinetask-re "END[ \t]*$"))
  207. (forward-line 1))
  208. ((org-looking-at-p inlinetask-re)
  209. (forward-line 1)
  210. (when (org-inlinetask-in-task-p)
  211. (re-search-forward inlinetask-re nil t)
  212. (forward-line 1)))
  213. (t
  214. (re-search-forward inlinetask-re nil t)
  215. (forward-line 1)))))
  216. (defun org-inlinetask-get-task-level ()
  217. "Get the level of the inline task around.
  218. This assumes the point is inside an inline task."
  219. (save-excursion
  220. (end-of-line)
  221. (re-search-backward (org-inlinetask-outline-regexp) nil t)
  222. (- (match-end 1) (match-beginning 1))))
  223. (defun org-inlinetask-promote ()
  224. "Promote the inline task at point.
  225. If the task has an end part, promote it. Also, prevents level from
  226. going below `org-inlinetask-min-level'."
  227. (interactive)
  228. (if (not (org-inlinetask-in-task-p))
  229. (error "Not in an inline task")
  230. (save-excursion
  231. (let* ((lvl (org-inlinetask-get-task-level))
  232. (next-lvl (org-get-valid-level lvl -1))
  233. (diff (- next-lvl lvl))
  234. (down-task (concat (make-string next-lvl ?*)))
  235. beg)
  236. (if (< next-lvl org-inlinetask-min-level)
  237. (error "Cannot promote an inline task at minimum level")
  238. (org-inlinetask-goto-beginning)
  239. (setq beg (point))
  240. (replace-match down-task nil t nil 1)
  241. (org-inlinetask-goto-end)
  242. (if (eobp) (beginning-of-line) (forward-line -1))
  243. (unless (= (point) beg)
  244. (replace-match down-task nil t nil 1)
  245. (when org-adapt-indentation
  246. (goto-char beg)
  247. (org-fixup-indentation diff))))))))
  248. (defun org-inlinetask-demote ()
  249. "Demote the inline task at point.
  250. If the task has an end part, also demote it."
  251. (interactive)
  252. (if (not (org-inlinetask-in-task-p))
  253. (error "Not in an inline task")
  254. (save-excursion
  255. (let* ((lvl (org-inlinetask-get-task-level))
  256. (next-lvl (org-get-valid-level lvl 1))
  257. (diff (- next-lvl lvl))
  258. (down-task (concat (make-string next-lvl ?*)))
  259. beg)
  260. (org-inlinetask-goto-beginning)
  261. (setq beg (point))
  262. (replace-match down-task nil t nil 1)
  263. (org-inlinetask-goto-end)
  264. (if (eobp) (beginning-of-line) (forward-line -1))
  265. (unless (= (point) beg)
  266. (replace-match down-task nil t nil 1)
  267. (when org-adapt-indentation
  268. (goto-char beg)
  269. (org-fixup-indentation diff)))))))
  270. (defvar org-export-current-backend) ; dynamically bound in org-exp.el
  271. (defun org-inlinetask-export-handler ()
  272. "Handle headlines with level larger or equal to `org-inlinetask-min-level'.
  273. Either remove headline and meta data, or do special formatting."
  274. (goto-char (point-min))
  275. (let* ((keywords-re (concat "^[ \t]*" org-keyword-time-regexp))
  276. (inline-re (concat (org-inlinetask-outline-regexp) ".*")))
  277. (while (re-search-forward inline-re nil t)
  278. (let ((headline (match-string 0))
  279. (beg (point-at-bol))
  280. (end (copy-marker (save-excursion
  281. (org-inlinetask-goto-end) (point))))
  282. content)
  283. ;; Delete SCHEDULED, DEADLINE...
  284. (while (re-search-forward keywords-re end t)
  285. (delete-region (point-at-bol) (1+ (point-at-eol))))
  286. (goto-char beg)
  287. ;; Delete drawers
  288. (while (re-search-forward org-drawer-regexp end t)
  289. (when (save-excursion (re-search-forward org-property-end-re nil t))
  290. (delete-region beg (1+ (match-end 0)))))
  291. ;; Get CONTENT, if any.
  292. (goto-char beg)
  293. (forward-line 1)
  294. (unless (= (point) end)
  295. (setq content (buffer-substring (point)
  296. (save-excursion (goto-char end)
  297. (forward-line -1)
  298. (point)))))
  299. ;; Remove the task.
  300. (goto-char beg)
  301. (delete-region beg end)
  302. (when org-inlinetask-export
  303. ;; Format CONTENT, if appropriate.
  304. (setq content
  305. (if (not (and content (string-match "\\S-" content)))
  306. ""
  307. ;; Ensure CONTENT has minimal indentation, a single
  308. ;; newline character at its boundaries, and isn't
  309. ;; protected.
  310. (when (string-match "`\\([ \t]*\n\\)+" content)
  311. (setq content (substring content (match-end 0))))
  312. (when (string-match "[ \t\n]+\\'" content)
  313. (setq content (substring content 0 (match-beginning 0))))
  314. (org-add-props (concat "\n" (org-remove-indentation content) "\n")
  315. '(org-protected nil))))
  316. (when (string-match org-complex-heading-regexp headline)
  317. (let* ((nil-to-str
  318. (function
  319. ;; Change nil arguments into empty strings.
  320. (lambda (el) (or (eval el) ""))))
  321. ;; Set up keywords provided to templates.
  322. (todo (or (match-string 2 headline) ""))
  323. (class (or (and (eq "" todo) "")
  324. (if (member todo org-done-keywords) "done" "todo")))
  325. (priority (or (match-string 3 headline) ""))
  326. (heading (or (match-string 4 headline) ""))
  327. (tags (or (match-string 5 headline) ""))
  328. ;; Read `org-inlinetask-export-templates'.
  329. (backend-spec (assq org-export-current-backend
  330. org-inlinetask-export-templates))
  331. (format-str (org-add-props (nth 1 backend-spec)
  332. '(org-protected t)))
  333. (tokens (cadr (nth 2 backend-spec)))
  334. ;; Build export string. Ensure it won't break
  335. ;; surrounding lists by giving it arbitrary high
  336. ;; indentation.
  337. (export-str (org-add-props
  338. (eval (append '(format format-str)
  339. (mapcar nil-to-str tokens)))
  340. '(original-indentation 1000))))
  341. (insert export-str)
  342. (unless (bolp) (insert "\n")))))))))
  343. (defun org-inlinetask-get-current-indentation ()
  344. "Get the indentation of the last non-while line above this one."
  345. (save-excursion
  346. (beginning-of-line 1)
  347. (skip-chars-backward " \t\n")
  348. (beginning-of-line 1)
  349. (or (org-at-item-p)
  350. (looking-at "[ \t]*"))
  351. (goto-char (match-end 0))
  352. (current-column)))
  353. (defun org-inlinetask-fontify (limit)
  354. "Fontify the inline tasks."
  355. (let* ((nstars (if org-odd-levels-only
  356. (1- (* 2 (or org-inlinetask-min-level 200)))
  357. (or org-inlinetask-min-level 200)))
  358. (re (concat "^\\(\\*\\)\\(\\*\\{"
  359. (format "%d" (- nstars 3))
  360. ",\\}\\)\\(\\*\\* .*\\)")))
  361. (while (re-search-forward re limit t)
  362. (add-text-properties (match-beginning 1) (match-end 1)
  363. '(face org-warning font-lock-fontified t))
  364. (add-text-properties (match-beginning 2) (match-end 2)
  365. '(face org-hide font-lock-fontified t))
  366. (add-text-properties (match-beginning 3) (match-end 3)
  367. '(face shadow font-lock-fontified t)))))
  368. (defun org-inlinetask-toggle-visibility ()
  369. "Toggle visibility of inline task at point."
  370. (let ((end (save-excursion
  371. (org-inlinetask-goto-end)
  372. (if (bolp) (1- (point)) (point))))
  373. (start (save-excursion
  374. (org-inlinetask-goto-beginning)
  375. (point-at-eol))))
  376. (cond
  377. ;; Nothing to show/hide.
  378. ((= end start))
  379. ;; Inlinetask was folded: expand it.
  380. ((get-char-property (1+ start) 'invisible)
  381. (outline-flag-region start end nil))
  382. (t (outline-flag-region start end t)))))
  383. (defun org-inlinetask-remove-END-maybe ()
  384. "Remove an END line when present."
  385. (when (looking-at (format "\\([ \t]*\n\\)*\\*\\{%d,\\}[ \t]+END[ \t]*$"
  386. org-inlinetask-min-level))
  387. (replace-match "")))
  388. (eval-after-load "org-exp"
  389. '(add-hook 'org-export-preprocess-before-backend-specifics-hook
  390. 'org-inlinetask-export-handler))
  391. (eval-after-load "org"
  392. '(add-hook 'org-font-lock-hook 'org-inlinetask-fontify))
  393. (provide 'org-inlinetask)
  394. ;;; org-inlinetask.el ends here