org-inlinetask.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. ;;; org-inlinetask.el --- Tasks Independent of Outline Hierarchy -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. ;;
  4. ;; Author: Carsten Dominik <carsten.dominik@gmail.com>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; URL: https://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 <https://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 last
  37. ;; two will be visible, the others will be hidden using the `org-hide'
  38. ;; 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: another thing
  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-macs)
  73. (org-assert-version)
  74. (require 'org)
  75. (defgroup org-inlinetask nil
  76. "Options concerning inline tasks in Org mode."
  77. :tag "Org Inline Tasks"
  78. :group 'org-structure)
  79. (defcustom org-inlinetask-min-level 15
  80. "Minimum level a headline must have before it is treated as an inline task.
  81. Don't set it to something higher than `29' or clocking will break since this
  82. is the hardcoded maximum number of stars `org-clock-sum' will work with.
  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-show-first-star nil
  92. "Non-nil means display the first star of an inline task as additional marker.
  93. When nil, the first star is not shown."
  94. :tag "Org Inline Tasks"
  95. :group 'org-structure
  96. :type 'boolean)
  97. (defvar org-odd-levels-only)
  98. (defvar org-keyword-time-regexp)
  99. (defvar org-complex-heading-regexp)
  100. (defvar org-property-end-re)
  101. (defcustom org-inlinetask-default-state nil
  102. "Non-nil means make inline tasks have a TODO keyword initially.
  103. This should be the state `org-inlinetask-insert-task' should use by
  104. default, or nil if no state should be assigned."
  105. :group 'org-inlinetask
  106. :version "24.1"
  107. :type '(choice
  108. (const :tag "No state" nil)
  109. (string :tag "Specific state")))
  110. (defun org-inlinetask-insert-task (&optional no-state)
  111. "Insert an inline task.
  112. If prefix arg NO-STATE is set, ignore `org-inlinetask-default-state'.
  113. If there is a region wrap it inside the inline task."
  114. (interactive "P")
  115. ;; Error when inside an inline task, except if point was at its very
  116. ;; beginning, in which case the new inline task will be inserted
  117. ;; before this one.
  118. (when (and (org-inlinetask-in-task-p)
  119. (not (and (org-inlinetask-at-task-p) (bolp))))
  120. (user-error "Cannot nest inline tasks"))
  121. (or (bolp) (newline))
  122. (let* ((indent (if org-odd-levels-only
  123. (1- (* 2 org-inlinetask-min-level))
  124. org-inlinetask-min-level))
  125. (indent-string (concat (make-string indent ?*) " "))
  126. (rbeg (if (org-region-active-p) (region-beginning) (point)))
  127. (rend (if (org-region-active-p) (region-end) (point))))
  128. (goto-char rend)
  129. (insert "\n" indent-string "END\n")
  130. (goto-char rbeg)
  131. (unless (bolp) (insert "\n"))
  132. (insert indent-string
  133. (if (or no-state (not org-inlinetask-default-state))
  134. ""
  135. (concat org-inlinetask-default-state " "))
  136. (if (= rend rbeg) "" "\n"))
  137. (unless (= rend rbeg) (end-of-line 0))))
  138. (define-key org-mode-map "\C-c\C-xt" 'org-inlinetask-insert-task)
  139. (defun org-inlinetask-outline-regexp ()
  140. "Return string matching an inline task heading.
  141. The number of levels is controlled by `org-inlinetask-min-level'."
  142. (let ((nstars (if org-odd-levels-only
  143. (1- (* org-inlinetask-min-level 2))
  144. org-inlinetask-min-level)))
  145. (format "^\\(\\*\\{%d,\\}\\)[ \t]+" nstars)))
  146. (defun org-inlinetask-end-p ()
  147. "Return a non-nil value if point is on inline task's END part."
  148. (let ((case-fold-search t))
  149. (org-match-line (concat (org-inlinetask-outline-regexp) "END[ \t]*$"))))
  150. (defun org-inlinetask-at-task-p ()
  151. "Return non-nil if point is at beginning of an inline task."
  152. (and (org-match-line (concat (org-inlinetask-outline-regexp) "\\(.*\\)"))
  153. (not (org-inlinetask-end-p))))
  154. (defun org-inlinetask-in-task-p ()
  155. "Return true if point is inside an inline task."
  156. (save-excursion
  157. (beginning-of-line)
  158. (let ((case-fold-search t))
  159. (or (looking-at-p (concat (org-inlinetask-outline-regexp) "\\(?:.*\\)"))
  160. (and (re-search-forward "^\\*+[ \t]+" nil t)
  161. (org-inlinetask-end-p))))))
  162. (defun org-inlinetask-goto-beginning ()
  163. "Go to the beginning of the inline task at point."
  164. (end-of-line)
  165. (let ((case-fold-search t)
  166. (inlinetask-re (org-inlinetask-outline-regexp)))
  167. (re-search-backward inlinetask-re nil t)
  168. (when (org-inlinetask-end-p)
  169. (re-search-backward inlinetask-re nil t))))
  170. (defun org-inlinetask-goto-end ()
  171. "Go to the end of the inline task at point.
  172. Return point."
  173. (save-match-data
  174. (beginning-of-line)
  175. (let ((case-fold-search t)
  176. (inlinetask-re (org-inlinetask-outline-regexp)))
  177. (cond
  178. ((org-inlinetask-end-p)
  179. (forward-line))
  180. ((looking-at-p inlinetask-re)
  181. (forward-line)
  182. (cond
  183. ((org-inlinetask-end-p) (forward-line))
  184. ((looking-at-p inlinetask-re))
  185. ((org-inlinetask-in-task-p)
  186. (re-search-forward inlinetask-re nil t)
  187. (forward-line))
  188. (t nil)))
  189. (t
  190. (re-search-forward inlinetask-re nil t)
  191. (forward-line)))))
  192. (point))
  193. (defun org-inlinetask-get-task-level ()
  194. "Get the level of the inline task around.
  195. This assumes the point is inside an inline task."
  196. (save-excursion
  197. (end-of-line)
  198. (re-search-backward (org-inlinetask-outline-regexp) nil t)
  199. (- (match-end 1) (match-beginning 1))))
  200. (defun org-inlinetask-promote ()
  201. "Promote the inline task at point.
  202. If the task has an end part, promote it. Also, prevents level from
  203. going below `org-inlinetask-min-level'."
  204. (interactive)
  205. (if (not (org-inlinetask-in-task-p))
  206. (user-error "Not in an inline task")
  207. (save-excursion
  208. (let* ((lvl (org-inlinetask-get-task-level))
  209. (next-lvl (org-get-valid-level lvl -1))
  210. (diff (- next-lvl lvl))
  211. (down-task (concat (make-string next-lvl ?*)))
  212. beg)
  213. (if (< next-lvl org-inlinetask-min-level)
  214. (user-error "Cannot promote an inline task at minimum level")
  215. (org-inlinetask-goto-beginning)
  216. (setq beg (point))
  217. (replace-match down-task nil t nil 1)
  218. (org-inlinetask-goto-end)
  219. (if (and (eobp) (looking-back "END\\s-*" (line-beginning-position)))
  220. (beginning-of-line)
  221. (forward-line -1))
  222. (unless (= (point) beg)
  223. (looking-at (org-inlinetask-outline-regexp))
  224. (replace-match down-task nil t nil 1)
  225. (when (eq org-adapt-indentation t)
  226. (goto-char beg)
  227. (org-fixup-indentation diff))))))))
  228. (defun org-inlinetask-demote ()
  229. "Demote the inline task at point.
  230. If the task has an end part, also demote it."
  231. (interactive)
  232. (if (not (org-inlinetask-in-task-p))
  233. (user-error "Not in an inline task")
  234. (save-excursion
  235. (let* ((lvl (org-inlinetask-get-task-level))
  236. (next-lvl (org-get-valid-level lvl 1))
  237. (diff (- next-lvl lvl))
  238. (down-task (concat (make-string next-lvl ?*)))
  239. beg)
  240. (org-inlinetask-goto-beginning)
  241. (setq beg (point))
  242. (replace-match down-task nil t nil 1)
  243. (org-inlinetask-goto-end)
  244. (if (and (eobp) (looking-back "END\\s-*" (line-beginning-position)))
  245. (beginning-of-line)
  246. (forward-line -1))
  247. (unless (= (point) beg)
  248. (looking-at (org-inlinetask-outline-regexp))
  249. (replace-match down-task nil t nil 1)
  250. (when (eq org-adapt-indentation t)
  251. (goto-char beg)
  252. (org-fixup-indentation diff)))))))
  253. (defvar org-indent-indentation-per-level) ; defined in org-indent.el
  254. (defface org-inlinetask '((t :inherit shadow))
  255. "Face for inlinetask headlines."
  256. :group 'org-faces)
  257. (defun org-inlinetask-fontify (limit)
  258. "Fontify the inline tasks down to LIMIT."
  259. (let* ((nstars (if org-odd-levels-only
  260. (1- (* 2 (or org-inlinetask-min-level 200)))
  261. (or org-inlinetask-min-level 200)))
  262. (re (concat "^\\(\\*\\)\\(\\*\\{"
  263. (format "%d" (- nstars 3))
  264. ",\\}\\)\\(\\*\\* .*\\)"))
  265. ;; Virtual indentation will add the warning face on the first
  266. ;; star. Thus, in that case, only hide it.
  267. (start-face (if (and (bound-and-true-p org-indent-mode)
  268. (> org-indent-indentation-per-level 1))
  269. 'org-hide
  270. 'org-warning)))
  271. (while (re-search-forward re limit t)
  272. (if org-inlinetask-show-first-star
  273. (add-text-properties (match-beginning 1) (match-end 1)
  274. `(face ,start-face font-lock-fontified t)))
  275. (add-text-properties (match-beginning
  276. (if org-inlinetask-show-first-star 2 1))
  277. (match-end 2)
  278. '(face org-hide font-lock-fontified t))
  279. (add-text-properties (match-beginning 3) (match-end 3)
  280. '(face org-inlinetask font-lock-fontified t)))))
  281. (defun org-inlinetask-toggle-visibility (&optional state)
  282. "Toggle visibility of inline task at point.
  283. When optional argument STATE is `fold', fold unconditionally.
  284. When STATE is `unfold', unfold unconditionally."
  285. (let ((end (save-excursion
  286. (org-inlinetask-goto-end)
  287. (if (bolp) (1- (point)) (point))))
  288. (start (save-excursion
  289. (org-inlinetask-goto-beginning)
  290. (line-end-position))))
  291. (cond
  292. ;; Nothing to show/hide.
  293. ((= end start))
  294. ;; Inlinetask was folded: expand it.
  295. ((and (not (eq state 'fold))
  296. (or (eq state 'unfold)
  297. (org-fold-get-folding-spec 'headline (1+ start))))
  298. (org-fold-region start end nil 'headline))
  299. (t (org-fold-region start end t 'headline)))))
  300. (defun org-inlinetask-hide-tasks (state)
  301. "Hide inline tasks in buffer when STATE is `contents' or `children'.
  302. This function is meant to be used in `org-cycle-hook'."
  303. (pcase state
  304. (`contents
  305. (let ((regexp (org-inlinetask-outline-regexp)))
  306. (save-excursion
  307. (goto-char (point-min))
  308. (while (re-search-forward regexp nil t)
  309. (org-inlinetask-toggle-visibility 'fold)
  310. (org-inlinetask-goto-end)))))
  311. (`children
  312. (save-excursion
  313. (while
  314. (or (org-inlinetask-at-task-p)
  315. (and (outline-next-heading) (org-inlinetask-at-task-p)))
  316. (org-inlinetask-toggle-visibility 'fold)
  317. (org-inlinetask-goto-end))))))
  318. (defun org-inlinetask-remove-END-maybe ()
  319. "Remove an END line when present."
  320. (when (looking-at (format "\\([ \t]*\n\\)*\\*\\{%d,\\}[ \t]+END[ \t]*$"
  321. org-inlinetask-min-level))
  322. (replace-match "")))
  323. (add-hook 'org-font-lock-hook 'org-inlinetask-fontify)
  324. (add-hook 'org-cycle-hook 'org-inlinetask-hide-tasks)
  325. (provide 'org-inlinetask)
  326. ;;; org-inlinetask.el ends here