org-inlinetask.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. ;;; org-inlinetask.el --- Tasks independent of outline hierarchy
  2. ;; Copyright (C) 2009-2013 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,
  24. ;; including the ability to store meta data like scheduling dates,
  25. ;; TODO state, tags and properties. However, these nodes are treated
  26. ;; specially by the visibility cycling.
  27. ;;
  28. ;; Visibility cycling exempts these nodes from cycling. So whenever
  29. ;; their parent is opened, so are these tasks. This will only work
  30. ;; with `org-cycle', so if you are also using other commands to
  31. ;; show/hide entries, you will occasionally find these tasks to behave
  32. ;; like all other outline nodes, seemingly splitting the text of the
  33. ;; parent into children.
  34. ;;
  35. ;; Special fontification of inline tasks, so that they can be
  36. ;; immediately recognized. From the stars of the headline, only the
  37. ;; first and the last two will be visible, the others will be hidden
  38. ;; using the `org-hide' face.
  39. ;;
  40. ;; An inline task is identified solely by a minimum outline level,
  41. ;; given by the variable `org-inlinetask-min-level', default 15.
  42. ;;
  43. ;; If you need to have a time planning line (DEADLINE etc), drawers,
  44. ;; for example LOGBOOK of PROPERTIES, or even normal text as part of
  45. ;; the inline task, you must add an "END" headline with the same
  46. ;; number of stars.
  47. ;;
  48. ;; As an example, here are two valid inline tasks:
  49. ;;
  50. ;; **************** TODO a small task
  51. ;;
  52. ;; and
  53. ;;
  54. ;; **************** TODO another small task
  55. ;; DEADLINE: <2009-03-30 Mon>
  56. ;; :PROPERTIES:
  57. ;; :SOMETHING: or other
  58. ;; :END:
  59. ;; And here is some extra text
  60. ;; **************** END
  61. ;;
  62. ;; Also, if you want to use refiling and archiving for inline tasks,
  63. ;; The END line must be present to make things work properly.
  64. ;;
  65. ;; Note that you should not try to use inline tasks within plain list,
  66. ;; visibility cycling is known to be problematic when doing so.
  67. ;;
  68. ;; This package installs one new command:
  69. ;;
  70. ;; C-c C-x t Insert a new inline task with END line
  71. ;;; Code:
  72. (require 'org)
  73. (defgroup org-inlinetask nil
  74. "Options concerning inline tasks in Org mode."
  75. :tag "Org Inline Tasks"
  76. :group 'org-structure)
  77. (defcustom org-inlinetask-min-level 15
  78. "Minimum level a headline must have before it is treated as an inline task.
  79. Don't set it to something higher than `29' or clocking will break since this
  80. is the hardcoded maximum number of stars `org-clock-sum' will work with.
  81. It is strongly recommended that you set `org-cycle-max-level' not at all,
  82. or to a number smaller than this one. In fact, when `org-cycle-max-level' is
  83. not set, it will be assumed to be one less than the value of smaller than
  84. the value of this variable."
  85. :group 'org-inlinetask
  86. :type '(choice
  87. (const :tag "Off" nil)
  88. (integer)))
  89. (defcustom org-inlinetask-show-first-star nil
  90. "Non-nil means display the first star of an inline task as additional marker.
  91. When nil, the first star is not shown."
  92. :tag "Org Inline Tasks"
  93. :group 'org-structure)
  94. (defvar org-odd-levels-only)
  95. (defvar org-keyword-time-regexp)
  96. (defvar org-complex-heading-regexp)
  97. (defvar org-property-end-re)
  98. (defcustom org-inlinetask-default-state nil
  99. "Non-nil means make inline tasks have a TODO keyword initially.
  100. This should be the state `org-inlinetask-insert-task' should use by
  101. default, or nil of no state should be assigned."
  102. :group 'org-inlinetask
  103. :version "24.1"
  104. :type '(choice
  105. (const :tag "No state" nil)
  106. (string :tag "Specific state")))
  107. (defun org-inlinetask-insert-task (&optional no-state)
  108. "Insert an inline task.
  109. If prefix arg NO-STATE is set, ignore `org-inlinetask-default-state'."
  110. (interactive "P")
  111. ;; Error when inside an inline task, except if point was at its very
  112. ;; beginning, in which case the new inline task will be inserted
  113. ;; before this one.
  114. (when (and (org-inlinetask-in-task-p)
  115. (not (and (org-inlinetask-at-task-p) (bolp))))
  116. (error "Cannot nest inline tasks"))
  117. (or (bolp) (newline))
  118. (let* ((indent (if org-odd-levels-only
  119. (1- (* 2 org-inlinetask-min-level))
  120. org-inlinetask-min-level))
  121. (indent-string (concat (make-string indent ?*) " ")))
  122. (insert indent-string
  123. (if (or no-state (not org-inlinetask-default-state))
  124. "\n"
  125. (concat org-inlinetask-default-state " \n"))
  126. indent-string "END\n"))
  127. (end-of-line -1))
  128. (define-key org-mode-map "\C-c\C-xt" 'org-inlinetask-insert-task)
  129. (defun org-inlinetask-outline-regexp ()
  130. "Return string matching an inline task heading.
  131. The number of levels is controlled by `org-inlinetask-min-level'."
  132. (let ((nstars (if org-odd-levels-only
  133. (1- (* org-inlinetask-min-level 2))
  134. org-inlinetask-min-level)))
  135. (format "^\\(\\*\\{%d,\\}\\)[ \t]+" nstars)))
  136. (defun org-inlinetask-at-task-p ()
  137. "Return true if point is at beginning of an inline task."
  138. (save-excursion
  139. (beginning-of-line)
  140. (and (looking-at (concat (org-inlinetask-outline-regexp) "\\(.*\\)"))
  141. (not (string-match "^end[ \t]*$" (downcase (match-string 2)))))))
  142. (defun org-inlinetask-in-task-p ()
  143. "Return true if point is inside an inline task."
  144. (save-excursion
  145. (beginning-of-line)
  146. (let* ((case-fold-search t)
  147. (stars-re (org-inlinetask-outline-regexp))
  148. (task-beg-re (concat stars-re "\\(?:.*\\)"))
  149. (task-end-re (concat stars-re "END[ \t]*$")))
  150. (or (org-looking-at-p task-beg-re)
  151. (and (re-search-forward "^\\*+[ \t]+" nil t)
  152. (progn (beginning-of-line) (org-looking-at-p task-end-re)))))))
  153. (defun org-inlinetask-goto-beginning ()
  154. "Go to the beginning of the inline task at point."
  155. (end-of-line)
  156. (let ((case-fold-search t)
  157. (inlinetask-re (org-inlinetask-outline-regexp)))
  158. (re-search-backward inlinetask-re nil t)
  159. (when (org-looking-at-p (concat inlinetask-re "END[ \t]*$"))
  160. (re-search-backward inlinetask-re nil t))))
  161. (defun org-inlinetask-goto-end ()
  162. "Go to the end of the inline task at point.
  163. Return point."
  164. (save-match-data
  165. (beginning-of-line)
  166. (let* ((case-fold-search t)
  167. (inlinetask-re (org-inlinetask-outline-regexp))
  168. (task-end-re (concat inlinetask-re "END[ \t]*$")))
  169. (cond
  170. ((looking-at task-end-re) (forward-line))
  171. ((looking-at inlinetask-re)
  172. (forward-line)
  173. (cond
  174. ((looking-at task-end-re) (forward-line))
  175. ((looking-at inlinetask-re))
  176. ((org-inlinetask-in-task-p)
  177. (re-search-forward inlinetask-re nil t)
  178. (forward-line))))
  179. (t (re-search-forward inlinetask-re nil t)
  180. (forward-line)))
  181. (point))))
  182. (defun org-inlinetask-get-task-level ()
  183. "Get the level of the inline task around.
  184. This assumes the point is inside an inline task."
  185. (save-excursion
  186. (end-of-line)
  187. (re-search-backward (org-inlinetask-outline-regexp) nil t)
  188. (- (match-end 1) (match-beginning 1))))
  189. (defun org-inlinetask-promote ()
  190. "Promote the inline task at point.
  191. If the task has an end part, promote it. Also, prevents level from
  192. going below `org-inlinetask-min-level'."
  193. (interactive)
  194. (if (not (org-inlinetask-in-task-p))
  195. (error "Not in an inline task")
  196. (save-excursion
  197. (let* ((lvl (org-inlinetask-get-task-level))
  198. (next-lvl (org-get-valid-level lvl -1))
  199. (diff (- next-lvl lvl))
  200. (down-task (concat (make-string next-lvl ?*)))
  201. beg)
  202. (if (< next-lvl org-inlinetask-min-level)
  203. (error "Cannot promote an inline task at minimum level")
  204. (org-inlinetask-goto-beginning)
  205. (setq beg (point))
  206. (replace-match down-task nil t nil 1)
  207. (org-inlinetask-goto-end)
  208. (if (eobp) (beginning-of-line) (forward-line -1))
  209. (unless (= (point) beg)
  210. (replace-match down-task nil t nil 1)
  211. (when org-adapt-indentation
  212. (goto-char beg)
  213. (org-fixup-indentation diff))))))))
  214. (defun org-inlinetask-demote ()
  215. "Demote the inline task at point.
  216. If the task has an end part, also demote it."
  217. (interactive)
  218. (if (not (org-inlinetask-in-task-p))
  219. (error "Not in an inline task")
  220. (save-excursion
  221. (let* ((lvl (org-inlinetask-get-task-level))
  222. (next-lvl (org-get-valid-level lvl 1))
  223. (diff (- next-lvl lvl))
  224. (down-task (concat (make-string next-lvl ?*)))
  225. beg)
  226. (org-inlinetask-goto-beginning)
  227. (setq beg (point))
  228. (replace-match down-task nil t nil 1)
  229. (org-inlinetask-goto-end)
  230. (if (eobp) (beginning-of-line) (forward-line -1))
  231. (unless (= (point) beg)
  232. (replace-match down-task nil t nil 1)
  233. (when org-adapt-indentation
  234. (goto-char beg)
  235. (org-fixup-indentation diff)))))))
  236. (defun org-inlinetask-get-current-indentation ()
  237. "Get the indentation of the last non-while line above this one."
  238. (save-excursion
  239. (beginning-of-line 1)
  240. (skip-chars-backward " \t\n")
  241. (beginning-of-line 1)
  242. (or (org-at-item-p)
  243. (looking-at "[ \t]*"))
  244. (goto-char (match-end 0))
  245. (current-column)))
  246. (defvar org-indent-indentation-per-level) ; defined in org-indent.el
  247. (defface org-inlinetask
  248. (org-compatible-face 'shadow '((t (:bold t))))
  249. "Face for inlinetask headlines."
  250. :group 'org-faces)
  251. (defun org-inlinetask-fontify (limit)
  252. "Fontify the inline tasks down to LIMIT."
  253. (let* ((nstars (if org-odd-levels-only
  254. (1- (* 2 (or org-inlinetask-min-level 200)))
  255. (or org-inlinetask-min-level 200)))
  256. (re (concat "^\\(\\*\\)\\(\\*\\{"
  257. (format "%d" (- nstars 3))
  258. ",\\}\\)\\(\\*\\* .*\\)"))
  259. ;; Virtual indentation will add the warning face on the first
  260. ;; star. Thus, in that case, only hide it.
  261. (start-face (if (and (org-bound-and-true-p org-indent-mode)
  262. (> org-indent-indentation-per-level 1))
  263. 'org-hide
  264. 'org-warning)))
  265. (while (re-search-forward re limit t)
  266. (if org-inlinetask-show-first-star
  267. (add-text-properties (match-beginning 1) (match-end 1)
  268. `(face ,start-face font-lock-fontified t)))
  269. (add-text-properties (match-beginning
  270. (if org-inlinetask-show-first-star 2 1))
  271. (match-end 2)
  272. '(face org-hide font-lock-fontified t))
  273. (add-text-properties (match-beginning 3) (match-end 3)
  274. '(face org-inlinetask font-lock-fontified t)))))
  275. (defun org-inlinetask-toggle-visibility ()
  276. "Toggle visibility of inline task at point."
  277. (let ((end (save-excursion
  278. (org-inlinetask-goto-end)
  279. (if (bolp) (1- (point)) (point))))
  280. (start (save-excursion
  281. (org-inlinetask-goto-beginning)
  282. (point-at-eol))))
  283. (cond
  284. ;; Nothing to show/hide.
  285. ((= end start))
  286. ;; Inlinetask was folded: expand it.
  287. ((get-char-property (1+ start) 'invisible)
  288. (outline-flag-region start end nil)
  289. (org-cycle-hide-drawers 'children))
  290. (t (outline-flag-region start end t)))))
  291. (defun org-inlinetask-remove-END-maybe ()
  292. "Remove an END line when present."
  293. (when (looking-at (format "\\([ \t]*\n\\)*\\*\\{%d,\\}[ \t]+END[ \t]*$"
  294. org-inlinetask-min-level))
  295. (replace-match "")))
  296. (eval-after-load "org"
  297. '(add-hook 'org-font-lock-hook 'org-inlinetask-fontify))
  298. (provide 'org-inlinetask)
  299. ;;; org-inlinetask.el ends here