org-inlinetask.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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.5
  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 />\n%s\n</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]~\n%s\n\\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* ((nstars (if org-odd-levels-only
  276. (1- (* 2 (or org-inlinetask-min-level 200)))
  277. (or org-inlinetask-min-level 200)))
  278. (re1 (format "^\\(\\*\\{%d,\\}\\)[ \t]+.*\n" nstars))
  279. (re2 (concat "^[ \t]*" org-keyword-time-regexp))
  280. headline beg end stars content)
  281. (while (re-search-forward re1 nil t)
  282. (setq headline (match-string 0)
  283. stars (match-string 1)
  284. content nil)
  285. (replace-match "")
  286. (while (looking-at re2)
  287. (delete-region (point) (1+ (point-at-eol))))
  288. (while (looking-at org-drawer-regexp)
  289. (setq beg (point))
  290. (if (re-search-forward org-property-end-re nil t)
  291. (delete-region beg (1+ (match-end 0)))))
  292. (setq beg (point))
  293. (when (and (re-search-forward "^\\(\\*+\\)[ \t]+" nil t)
  294. (= (length (match-string 1)) (length stars))
  295. (progn (goto-char (match-end 0))
  296. (looking-at "END[ \t]*$")))
  297. (setq content (buffer-substring beg (1- (point-at-bol))))
  298. (delete-region beg (1+ (match-end 0))))
  299. (goto-char beg)
  300. (when org-inlinetask-export
  301. ;; content formatting
  302. (when content
  303. (if (not (string-match "\\S-" content))
  304. (setq content nil)
  305. (if (string-match "[ \t\n]+\\'" content)
  306. (setq content (substring content 0 (match-beginning 0))))
  307. (setq content (org-remove-indentation content))))
  308. ;; Prevent from protecting content if there's any
  309. (setq content (or (and content
  310. (org-add-props content '(org-protected nil)))
  311. ""))
  312. ;; grab elements to export
  313. (when (string-match org-complex-heading-regexp headline)
  314. (let* ((todo (or (match-string 2 headline) ""))
  315. (class (or (and (eq "" todo) "")
  316. (if (member todo org-done-keywords) "done" "todo")))
  317. (priority (or (match-string 3 headline) ""))
  318. (heading (or (match-string 4 headline) ""))
  319. (tags (or (match-string 5 headline) ""))
  320. (backend-spec (assq org-export-current-backend
  321. org-inlinetask-export-templates))
  322. (format-str (org-add-props (nth 1 backend-spec)
  323. '(org-protected t)))
  324. (tokens (cadr (nth 2 backend-spec)))
  325. (nil-to-str
  326. ;; Change nil arguments into empty strings
  327. (lambda (el) (or (eval el) "")))
  328. ;; Build and ensure export string will not break lists
  329. (export-str (org-add-props
  330. (eval (append '(format format-str)
  331. (mapcar nil-to-str tokens)))
  332. '(original-indentation 1000))))
  333. ;; Eventually insert it
  334. (insert export-str "\n")))))))
  335. (defun org-inlinetask-get-current-indentation ()
  336. "Get the indentation of the last non-while line above this one."
  337. (save-excursion
  338. (beginning-of-line 1)
  339. (skip-chars-backward " \t\n")
  340. (beginning-of-line 1)
  341. (or (org-at-item-p)
  342. (looking-at "[ \t]*"))
  343. (goto-char (match-end 0))
  344. (current-column)))
  345. (defun org-inlinetask-fontify (limit)
  346. "Fontify the inline tasks."
  347. (let* ((nstars (if org-odd-levels-only
  348. (1- (* 2 (or org-inlinetask-min-level 200)))
  349. (or org-inlinetask-min-level 200)))
  350. (re (concat "^\\(\\*\\)\\(\\*\\{"
  351. (format "%d" (- nstars 3))
  352. ",\\}\\)\\(\\*\\* .*\\)")))
  353. (while (re-search-forward re limit t)
  354. (add-text-properties (match-beginning 1) (match-end 1)
  355. '(face org-warning font-lock-fontified t))
  356. (add-text-properties (match-beginning 2) (match-end 2)
  357. '(face org-hide font-lock-fontified t))
  358. (add-text-properties (match-beginning 3) (match-end 3)
  359. '(face shadow font-lock-fontified t)))))
  360. (defun org-inlinetask-toggle-visibility ()
  361. "Toggle visibility of inline task at point."
  362. (let ((end (save-excursion
  363. (org-inlinetask-goto-end)
  364. (if (bolp) (1- (point)) (point))))
  365. (start (save-excursion
  366. (org-inlinetask-goto-beginning)
  367. (point-at-eol))))
  368. (cond
  369. ;; Nothing to show/hide.
  370. ((= end start))
  371. ;; Inlinetask was folded: expand it.
  372. ((get-char-property (1+ start) 'invisible)
  373. (outline-flag-region start end nil))
  374. (t (outline-flag-region start end t)))))
  375. (defun org-inlinetask-remove-END-maybe ()
  376. "Remove an END line when present."
  377. (when (looking-at (format "\\([ \t]*\n\\)*\\*\\{%d,\\}[ \t]+END[ \t]*$"
  378. org-inlinetask-min-level))
  379. (replace-match "")))
  380. (eval-after-load "org-exp"
  381. '(add-hook 'org-export-preprocess-before-backend-specifics-hook
  382. 'org-inlinetask-export-handler))
  383. (eval-after-load "org"
  384. '(add-hook 'org-font-lock-hook 'org-inlinetask-fontify))
  385. (provide 'org-inlinetask)
  386. ;;; org-inlinetask.el ends here