PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/lisp/org/org-inlinetask.el

https://gitlab.com/wilfred/emacs
Emacs Lisp | 334 lines | 222 code | 33 blank | 79 comment | 3 complexity | 4058e561ed2252fc6577233964b5443c MD5 | raw file
  1. ;;; org-inlinetask.el --- Tasks independent of outline hierarchy
  2. ;; Copyright (C) 2009-2016 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. :type 'boolean)
  95. (defvar org-odd-levels-only)
  96. (defvar org-keyword-time-regexp)
  97. (defvar org-drawer-regexp)
  98. (defvar org-complex-heading-regexp)
  99. (defvar org-property-end-re)
  100. (defcustom org-inlinetask-default-state nil
  101. "Non-nil means make inline tasks have a TODO keyword initially.
  102. This should be the state `org-inlinetask-insert-task' should use by
  103. default, or nil of no state should be assigned."
  104. :group 'org-inlinetask
  105. :version "24.1"
  106. :type '(choice
  107. (const :tag "No state" nil)
  108. (string :tag "Specific state")))
  109. (defun org-inlinetask-insert-task (&optional no-state)
  110. "Insert an inline task.
  111. If prefix arg NO-STATE is set, ignore `org-inlinetask-default-state'."
  112. (interactive "P")
  113. ;; Error when inside an inline task, except if point was at its very
  114. ;; beginning, in which case the new inline task will be inserted
  115. ;; before this one.
  116. (when (and (org-inlinetask-in-task-p)
  117. (not (and (org-inlinetask-at-task-p) (bolp))))
  118. (error "Cannot nest inline tasks"))
  119. (or (bolp) (newline))
  120. (let* ((indent (if org-odd-levels-only
  121. (1- (* 2 org-inlinetask-min-level))
  122. org-inlinetask-min-level))
  123. (indent-string (concat (make-string indent ?*) " ")))
  124. (insert indent-string
  125. (if (or no-state (not org-inlinetask-default-state))
  126. "\n"
  127. (concat org-inlinetask-default-state " \n"))
  128. indent-string "END\n"))
  129. (end-of-line -1))
  130. (define-key org-mode-map "\C-c\C-xt" 'org-inlinetask-insert-task)
  131. (defun org-inlinetask-outline-regexp ()
  132. "Return string matching an inline task heading.
  133. The number of levels is controlled by `org-inlinetask-min-level'."
  134. (let ((nstars (if org-odd-levels-only
  135. (1- (* org-inlinetask-min-level 2))
  136. org-inlinetask-min-level)))
  137. (format "^\\(\\*\\{%d,\\}\\)[ \t]+" nstars)))
  138. (defun org-inlinetask-at-task-p ()
  139. "Return true if point is at beginning of an inline task."
  140. (save-excursion
  141. (beginning-of-line)
  142. (and (looking-at (concat (org-inlinetask-outline-regexp) "\\(.*\\)"))
  143. (not (string-match "^end[ \t]*$" (downcase (match-string 2)))))))
  144. (defun org-inlinetask-in-task-p ()
  145. "Return true if point is inside an inline task."
  146. (save-excursion
  147. (beginning-of-line)
  148. (let* ((case-fold-search t)
  149. (stars-re (org-inlinetask-outline-regexp))
  150. (task-beg-re (concat stars-re "\\(?:.*\\)"))
  151. (task-end-re (concat stars-re "END[ \t]*$")))
  152. (or (org-looking-at-p task-beg-re)
  153. (and (re-search-forward "^\\*+[ \t]+" nil t)
  154. (progn (beginning-of-line) (org-looking-at-p task-end-re)))))))
  155. (defun org-inlinetask-goto-beginning ()
  156. "Go to the beginning of the inline task at point."
  157. (end-of-line)
  158. (let ((case-fold-search t)
  159. (inlinetask-re (org-inlinetask-outline-regexp)))
  160. (re-search-backward inlinetask-re nil t)
  161. (when (org-looking-at-p (concat inlinetask-re "END[ \t]*$"))
  162. (re-search-backward inlinetask-re nil t))))
  163. (defun org-inlinetask-goto-end ()
  164. "Go to the end of the inline task at point.
  165. Return point."
  166. (save-match-data
  167. (beginning-of-line)
  168. (let* ((case-fold-search t)
  169. (inlinetask-re (org-inlinetask-outline-regexp))
  170. (task-end-re (concat inlinetask-re "END[ \t]*$")))
  171. (cond
  172. ((looking-at task-end-re) (forward-line))
  173. ((looking-at inlinetask-re)
  174. (forward-line)
  175. (cond
  176. ((looking-at task-end-re) (forward-line))
  177. ((looking-at inlinetask-re))
  178. ((org-inlinetask-in-task-p)
  179. (re-search-forward inlinetask-re nil t)
  180. (forward-line))))
  181. (t (re-search-forward inlinetask-re nil t)
  182. (forward-line)))
  183. (point))))
  184. (defun org-inlinetask-get-task-level ()
  185. "Get the level of the inline task around.
  186. This assumes the point is inside an inline task."
  187. (save-excursion
  188. (end-of-line)
  189. (re-search-backward (org-inlinetask-outline-regexp) nil t)
  190. (- (match-end 1) (match-beginning 1))))
  191. (defun org-inlinetask-promote ()
  192. "Promote the inline task at point.
  193. If the task has an end part, promote it. Also, prevents level from
  194. going below `org-inlinetask-min-level'."
  195. (interactive)
  196. (if (not (org-inlinetask-in-task-p))
  197. (error "Not in an inline task")
  198. (save-excursion
  199. (let* ((lvl (org-inlinetask-get-task-level))
  200. (next-lvl (org-get-valid-level lvl -1))
  201. (diff (- next-lvl lvl))
  202. (down-task (concat (make-string next-lvl ?*)))
  203. beg)
  204. (if (< next-lvl org-inlinetask-min-level)
  205. (error "Cannot promote an inline task at minimum level")
  206. (org-inlinetask-goto-beginning)
  207. (setq beg (point))
  208. (replace-match down-task nil t nil 1)
  209. (org-inlinetask-goto-end)
  210. (if (eobp) (beginning-of-line) (forward-line -1))
  211. (unless (= (point) beg)
  212. (replace-match down-task nil t nil 1)
  213. (when org-adapt-indentation
  214. (goto-char beg)
  215. (org-fixup-indentation diff))))))))
  216. (defun org-inlinetask-demote ()
  217. "Demote the inline task at point.
  218. If the task has an end part, also demote it."
  219. (interactive)
  220. (if (not (org-inlinetask-in-task-p))
  221. (error "Not in an inline task")
  222. (save-excursion
  223. (let* ((lvl (org-inlinetask-get-task-level))
  224. (next-lvl (org-get-valid-level lvl 1))
  225. (diff (- next-lvl lvl))
  226. (down-task (concat (make-string next-lvl ?*)))
  227. beg)
  228. (org-inlinetask-goto-beginning)
  229. (setq beg (point))
  230. (replace-match down-task nil t nil 1)
  231. (org-inlinetask-goto-end)
  232. (if (eobp) (beginning-of-line) (forward-line -1))
  233. (unless (= (point) beg)
  234. (replace-match down-task nil t nil 1)
  235. (when org-adapt-indentation
  236. (goto-char beg)
  237. (org-fixup-indentation diff)))))))
  238. (defun org-inlinetask-get-current-indentation ()
  239. "Get the indentation of the last non-while line above this one."
  240. (save-excursion
  241. (beginning-of-line 1)
  242. (skip-chars-backward " \t\n")
  243. (beginning-of-line 1)
  244. (or (org-at-item-p)
  245. (looking-at "[ \t]*"))
  246. (goto-char (match-end 0))
  247. (current-column)))
  248. (defvar org-indent-indentation-per-level) ; defined in org-indent.el
  249. (defface org-inlinetask
  250. (org-compatible-face 'shadow '((t (:bold t))))
  251. "Face for inlinetask headlines."
  252. :group 'org-faces)
  253. (defun org-inlinetask-fontify (limit)
  254. "Fontify the inline tasks down to LIMIT."
  255. (let* ((nstars (if org-odd-levels-only
  256. (1- (* 2 (or org-inlinetask-min-level 200)))
  257. (or org-inlinetask-min-level 200)))
  258. (re (concat "^\\(\\*\\)\\(\\*\\{"
  259. (format "%d" (- nstars 3))
  260. ",\\}\\)\\(\\*\\* .*\\)"))
  261. ;; Virtual indentation will add the warning face on the first
  262. ;; star. Thus, in that case, only hide it.
  263. (start-face (if (and (org-bound-and-true-p org-indent-mode)
  264. (> org-indent-indentation-per-level 1))
  265. 'org-hide
  266. 'org-warning)))
  267. (while (re-search-forward re limit t)
  268. (if org-inlinetask-show-first-star
  269. (add-text-properties (match-beginning 1) (match-end 1)
  270. `(face ,start-face font-lock-fontified t)))
  271. (add-text-properties (match-beginning
  272. (if org-inlinetask-show-first-star 2 1))
  273. (match-end 2)
  274. '(face org-hide font-lock-fontified t))
  275. (add-text-properties (match-beginning 3) (match-end 3)
  276. '(face org-inlinetask font-lock-fontified t)))))
  277. (defun org-inlinetask-toggle-visibility ()
  278. "Toggle visibility of inline task at point."
  279. (let ((end (save-excursion
  280. (org-inlinetask-goto-end)
  281. (if (bolp) (1- (point)) (point))))
  282. (start (save-excursion
  283. (org-inlinetask-goto-beginning)
  284. (point-at-eol))))
  285. (cond
  286. ;; Nothing to show/hide.
  287. ((= end start))
  288. ;; Inlinetask was folded: expand it.
  289. ((get-char-property (1+ start) 'invisible)
  290. (outline-flag-region start end nil)
  291. (org-cycle-hide-drawers 'children))
  292. (t (outline-flag-region start end t)))))
  293. (defun org-inlinetask-remove-END-maybe ()
  294. "Remove an END line when present."
  295. (when (looking-at (format "\\([ \t]*\n\\)*\\*\\{%d,\\}[ \t]+END[ \t]*$"
  296. org-inlinetask-min-level))
  297. (replace-match "")))
  298. (eval-after-load "org"
  299. '(add-hook 'org-font-lock-hook 'org-inlinetask-fontify))
  300. (provide 'org-inlinetask)
  301. ;;; org-inlinetask.el ends here