org-indent.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. ;;; org-indent.el --- Dynamic indentation for Org -*- 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. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;;
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;;
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  22. ;;
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;; This is an implementation of dynamic virtual indentation. It works
  27. ;; by adding text properties to a buffer to make sure lines are
  28. ;; indented according to outline structure.
  29. ;;
  30. ;; The process is synchronous, toggled at every buffer modification.
  31. ;; Though, the initialization (indentation of text already in the
  32. ;; buffer), which can take a few seconds in large buffers, happens on
  33. ;; idle time.
  34. ;;
  35. ;;; Code:
  36. (require 'org-macs)
  37. (org-assert-version)
  38. (require 'org-macs)
  39. (require 'org-compat)
  40. (require 'org)
  41. (require 'cl-lib)
  42. (declare-function org-inlinetask-get-task-level "org-inlinetask" ())
  43. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  44. (declare-function org-list-item-body-column "org-list" (item))
  45. (defvar org-inlinetask-show-first-star)
  46. (defgroup org-indent nil
  47. "Options concerning dynamic virtual outline indentation."
  48. :tag "Org Indent"
  49. :group 'org)
  50. (defvar org-indent-inlinetask-first-star (org-add-props "*" '(face org-warning))
  51. "First star of inline tasks, with correct face.")
  52. (defvar org-indent-agent-timer nil
  53. "Timer running the initialize agent.")
  54. (defvar org-indent-agentized-buffers nil
  55. "List of buffers watched by the initialize agent.")
  56. (defvar org-indent-agent-resume-timer nil
  57. "Timer to reschedule agent after switching to other idle processes.")
  58. (defvar org-indent-agent-active-delay '(0 2 0)
  59. "Time to run agent before switching to other idle processes.
  60. Delay used when the buffer to initialize is current.")
  61. (defvar org-indent-agent-passive-delay '(0 0 400000)
  62. "Time to run agent before switching to other idle processes.
  63. Delay used when the buffer to initialize isn't current.")
  64. (defvar org-indent-agent-resume-delay '(0 0 100000)
  65. "Minimal time for other idle processes before switching back to agent.")
  66. (defvar org-indent--initial-marker nil
  67. "Position of initialization before interrupt.
  68. This is used locally in each buffer being initialized.")
  69. (defvar org-indent-modified-headline-flag nil
  70. "Non-nil means the last deletion operated on a headline.
  71. It is modified by `org-indent-notify-modified-headline'.")
  72. (defcustom org-indent-boundary-char ?\s
  73. "The end of the virtual indentation strings, a single-character string.
  74. The default is just a space, but if you wish, you can use \"|\" or so.
  75. This can be useful on a terminal window - under a windowing system,
  76. it may be prettier to customize the `org-indent' face."
  77. :group 'org-indent
  78. :type 'character)
  79. (defcustom org-indent-mode-turns-off-org-adapt-indentation t
  80. "Non-nil means setting `org-indent-mode' will turn off indentation adaptation.
  81. For details see the variable `org-adapt-indentation'."
  82. :group 'org-indent
  83. :type 'boolean)
  84. (defcustom org-indent-mode-turns-on-hiding-stars t
  85. "Non-nil means setting `org-indent-mode' will turn on `org-hide-leading-stars'."
  86. :group 'org-indent
  87. :type 'boolean)
  88. (defcustom org-indent-indentation-per-level 2
  89. "Indentation per level in number of characters."
  90. :group 'org-indent
  91. :type 'integer)
  92. (defface org-indent '((t (:inherit org-hide)))
  93. "Face for outline indentation.
  94. The default is to make it look like whitespace. But you may find it
  95. useful to make it ever so slightly different."
  96. :group 'org-faces)
  97. (defvar org-indent--text-line-prefixes nil
  98. "Vector containing line prefixes strings for regular text.")
  99. (defvar org-indent--heading-line-prefixes nil
  100. "Vector containing line prefix strings for headlines.")
  101. (defvar org-indent--inlinetask-line-prefixes nil
  102. "Vector containing line prefix strings for inline tasks.")
  103. (defconst org-indent--deepest-level 50
  104. "Maximum theoretical headline depth.")
  105. (defun org-indent--compute-prefixes ()
  106. "Compute prefix strings for regular text and headlines."
  107. (setq org-indent--heading-line-prefixes
  108. (make-vector org-indent--deepest-level nil))
  109. (setq org-indent--inlinetask-line-prefixes
  110. (make-vector org-indent--deepest-level nil))
  111. (setq org-indent--text-line-prefixes
  112. (make-vector org-indent--deepest-level nil))
  113. (when (> org-indent-indentation-per-level 0)
  114. (dotimes (n org-indent--deepest-level)
  115. (let ((indentation (if (<= n 1) 0
  116. (* (1- org-indent-indentation-per-level)
  117. (1- n)))))
  118. ;; Headlines line prefixes.
  119. (let ((heading-prefix (make-string indentation ?*)))
  120. (aset org-indent--heading-line-prefixes
  121. n
  122. (org-add-props heading-prefix nil 'face 'org-indent))
  123. ;; Inline tasks line prefixes
  124. (aset org-indent--inlinetask-line-prefixes
  125. n
  126. (cond ((<= n 1) "")
  127. ((bound-and-true-p org-inlinetask-show-first-star)
  128. (concat org-indent-inlinetask-first-star
  129. (substring heading-prefix 1)))
  130. (t (org-add-props heading-prefix nil 'face 'org-indent)))))
  131. ;; Text line prefixes.
  132. (aset org-indent--text-line-prefixes
  133. n
  134. (org-add-props
  135. (concat (make-string (+ n indentation) ?\s)
  136. (and (> n 0)
  137. (char-to-string org-indent-boundary-char)))
  138. nil 'face 'org-indent))))))
  139. (defsubst org-indent-remove-properties (beg end)
  140. "Remove indentations between BEG and END."
  141. (with-silent-modifications
  142. (remove-text-properties beg end '(line-prefix nil wrap-prefix nil))))
  143. ;;;###autoload
  144. (define-minor-mode org-indent-mode
  145. "When active, indent text according to outline structure.
  146. Internally this works by adding `line-prefix' and `wrap-prefix'
  147. properties, after each buffer modification, on the modified zone.
  148. The process is synchronous. Though, initial indentation of
  149. buffer, which can take a few seconds on large buffers, is done
  150. during idle time."
  151. :lighter " Ind"
  152. (cond
  153. (org-indent-mode
  154. ;; mode was turned on.
  155. (setq-local indent-tabs-mode nil)
  156. (setq-local org-indent--initial-marker (copy-marker 1))
  157. (when org-indent-mode-turns-off-org-adapt-indentation
  158. ;; Don't turn off `org-adapt-indentation' when its value is
  159. ;; 'headline-data, just indent headline data specially.
  160. (or (eq org-adapt-indentation 'headline-data)
  161. (setq-local org-adapt-indentation nil)))
  162. (when org-indent-mode-turns-on-hiding-stars
  163. (setq-local org-hide-leading-stars t))
  164. (org-indent--compute-prefixes)
  165. (if (boundp 'filter-buffer-substring-functions)
  166. (add-hook 'filter-buffer-substring-functions
  167. (lambda (fun start end delete)
  168. (org-indent-remove-properties-from-string
  169. (funcall fun start end delete)))
  170. nil t)
  171. ;; Emacs >= 24.4.
  172. (add-function :filter-return (local 'filter-buffer-substring-function)
  173. #'org-indent-remove-properties-from-string))
  174. (add-hook 'after-change-functions 'org-indent-refresh-maybe nil 'local)
  175. (add-hook 'before-change-functions
  176. 'org-indent-notify-modified-headline nil 'local)
  177. (and font-lock-mode (org-restart-font-lock))
  178. (org-indent-remove-properties (point-min) (point-max))
  179. ;; Submit current buffer to initialize agent. If it's the first
  180. ;; buffer submitted, also start the agent. Current buffer is
  181. ;; pushed in both cases to avoid a race condition.
  182. (if org-indent-agentized-buffers
  183. (push (current-buffer) org-indent-agentized-buffers)
  184. (push (current-buffer) org-indent-agentized-buffers)
  185. (setq org-indent-agent-timer
  186. (run-with-idle-timer 0.2 t #'org-indent-initialize-agent))))
  187. (t
  188. ;; Mode was turned off (or we refused to turn it on)
  189. (kill-local-variable 'org-adapt-indentation)
  190. (setq org-indent-agentized-buffers
  191. (delq (current-buffer) org-indent-agentized-buffers))
  192. (when (markerp org-indent--initial-marker)
  193. (set-marker org-indent--initial-marker nil))
  194. (when (local-variable-p 'org-hide-leading-stars)
  195. (kill-local-variable 'org-hide-leading-stars))
  196. (if (boundp 'filter-buffer-substring-functions)
  197. (remove-hook 'filter-buffer-substring-functions
  198. (lambda (fun start end delete)
  199. (org-indent-remove-properties-from-string
  200. (funcall fun start end delete))))
  201. (remove-function (local 'filter-buffer-substring-function)
  202. #'org-indent-remove-properties-from-string))
  203. (remove-hook 'after-change-functions 'org-indent-refresh-maybe 'local)
  204. (remove-hook 'before-change-functions
  205. 'org-indent-notify-modified-headline 'local)
  206. (org-with-wide-buffer
  207. (org-indent-remove-properties (point-min) (point-max)))
  208. (and font-lock-mode (org-restart-font-lock))
  209. (redraw-display))))
  210. (defun org-indent-indent-buffer ()
  211. "Add indentation properties to the accessible part of the buffer."
  212. (interactive)
  213. (if (not (derived-mode-p 'org-mode))
  214. (error "Not in Org mode")
  215. (message "Setting buffer indentation. It may take a few seconds...")
  216. (org-indent-remove-properties (point-min) (point-max))
  217. (org-indent-add-properties (point-min) (point-max))
  218. (message "Indentation of buffer set.")))
  219. (defun org-indent-remove-properties-from-string (string)
  220. "Remove indentation properties from STRING."
  221. (remove-text-properties 0 (length string)
  222. '(line-prefix nil wrap-prefix nil) string)
  223. string)
  224. (defun org-indent-initialize-agent ()
  225. "Start or resume current buffer initialization.
  226. Only buffers in `org-indent-agentized-buffers' trigger an action.
  227. When no more buffer is being watched, the agent suppress itself."
  228. (when org-indent-agent-resume-timer
  229. (cancel-timer org-indent-agent-resume-timer))
  230. (setq org-indent-agentized-buffers
  231. (cl-remove-if-not #'buffer-live-p org-indent-agentized-buffers))
  232. (cond
  233. ;; Job done: kill agent.
  234. ((not org-indent-agentized-buffers) (cancel-timer org-indent-agent-timer))
  235. ;; Current buffer is agentized: start/resume initialization
  236. ;; somewhat aggressively.
  237. ((memq (current-buffer) org-indent-agentized-buffers)
  238. (org-indent-initialize-buffer (current-buffer)
  239. org-indent-agent-active-delay))
  240. ;; Else, start/resume initialization of the last agentized buffer,
  241. ;; softly.
  242. (t (org-indent-initialize-buffer (car org-indent-agentized-buffers)
  243. org-indent-agent-passive-delay))))
  244. (defun org-indent-initialize-buffer (buffer delay)
  245. "Set virtual indentation for the buffer BUFFER, asynchronously.
  246. Give hand to other idle processes if it takes longer than DELAY,
  247. a time value."
  248. (with-current-buffer buffer
  249. (when org-indent-mode
  250. (org-with-wide-buffer
  251. (let ((interruptp
  252. ;; Always nil unless interrupted.
  253. (catch 'interrupt
  254. (and org-indent--initial-marker
  255. (marker-position org-indent--initial-marker)
  256. (equal (marker-buffer org-indent--initial-marker)
  257. buffer)
  258. (org-indent-add-properties org-indent--initial-marker
  259. (point-max)
  260. delay)
  261. nil))))
  262. (move-marker org-indent--initial-marker interruptp)
  263. ;; Job is complete: un-agentize buffer.
  264. (unless interruptp
  265. (setq org-indent-agentized-buffers
  266. (delq buffer org-indent-agentized-buffers))))))))
  267. (defun org-indent-set-line-properties (level indentation &optional heading)
  268. "Set prefix properties on current line an move to next one.
  269. LEVEL is the current level of heading. INDENTATION is the
  270. expected indentation when wrapping line.
  271. When optional argument HEADING is non-nil, assume line is at
  272. a heading. Moreover, if it is `inlinetask', the first star will
  273. have `org-warning' face."
  274. (let* ((line (aref (pcase heading
  275. (`nil org-indent--text-line-prefixes)
  276. (`inlinetask org-indent--inlinetask-line-prefixes)
  277. (_ org-indent--heading-line-prefixes))
  278. level))
  279. (wrap
  280. (org-add-props
  281. (concat line
  282. (if heading (concat (make-string level ?*) " ")
  283. (make-string indentation ?\s)))
  284. nil 'face 'org-indent)))
  285. ;; Add properties down to the next line to indent empty lines.
  286. (add-text-properties (line-beginning-position) (line-beginning-position 2)
  287. `(line-prefix ,line wrap-prefix ,wrap)))
  288. (forward-line))
  289. (defun org-indent-add-properties (beg end &optional delay)
  290. "Add indentation properties between BEG and END.
  291. When DELAY is non-nil, it must be a time value. In that case,
  292. the process is asynchronous and can be interrupted, either by
  293. user request, or after DELAY. This is done by throwing the
  294. `interrupt' tag along with the buffer position where the process
  295. stopped."
  296. (save-match-data
  297. (org-with-wide-buffer
  298. (goto-char beg)
  299. (beginning-of-line)
  300. ;; Initialize prefix at BEG, according to current entry's level.
  301. (let* ((case-fold-search t)
  302. (limited-re (org-get-limited-outline-regexp))
  303. (level (or (org-current-level) 0))
  304. (time-limit (and delay (time-add nil delay))))
  305. ;; For each line, set `line-prefix' and `wrap-prefix'
  306. ;; properties depending on the type of line (headline, inline
  307. ;; task, item or other).
  308. (with-silent-modifications
  309. (while (and (<= (point) end) (not (eobp)))
  310. (cond
  311. ;; When in asynchronous mode, check if interrupt is
  312. ;; required.
  313. ((and delay (input-pending-p)) (throw 'interrupt (point)))
  314. ;; In asynchronous mode, take a break of
  315. ;; `org-indent-agent-resume-delay' every DELAY to avoid
  316. ;; blocking any other idle timer or process output.
  317. ((and delay (time-less-p time-limit nil))
  318. (setq org-indent-agent-resume-timer
  319. (run-with-idle-timer
  320. (time-add (current-idle-time) org-indent-agent-resume-delay)
  321. nil #'org-indent-initialize-agent))
  322. (throw 'interrupt (point)))
  323. ;; Headline or inline task.
  324. ((looking-at org-outline-regexp)
  325. (let* ((nstars (- (match-end 0) (match-beginning 0) 1))
  326. (type (or (looking-at-p limited-re) 'inlinetask)))
  327. (org-indent-set-line-properties nstars 0 type)
  328. ;; At an headline, define new value for LEVEL.
  329. (unless (eq type 'inlinetask) (setq level nstars))))
  330. ;; List item: `wrap-prefix' is set where body starts.
  331. ((org-at-item-p)
  332. (org-indent-set-line-properties
  333. level (org-list-item-body-column (point))))
  334. ;; Regular line.
  335. (t
  336. (org-indent-set-line-properties
  337. level
  338. (current-indentation)
  339. ;; When adapt indentation is 'headline-data, use
  340. ;; `org-indent--heading-line-prefixes' for setting
  341. ;; headline data indentation.
  342. (and (eq org-adapt-indentation 'headline-data)
  343. (or (org-at-planning-p)
  344. (org-at-clock-log-p)
  345. (looking-at-p org-property-start-re)
  346. (looking-at-p org-property-end-re)
  347. (looking-at-p org-property-re))))))))))))
  348. (defun org-indent-notify-modified-headline (beg end)
  349. "Set `org-indent-modified-headline-flag' depending on context.
  350. BEG and END are the positions of the beginning and end of the
  351. range of deleted text.
  352. This function is meant to be called by `before-change-functions'.
  353. Flag will be non-nil if command is going to modify or delete an
  354. headline."
  355. (when org-indent-mode
  356. (setq org-indent-modified-headline-flag
  357. (org-with-wide-buffer
  358. (goto-char beg)
  359. (save-match-data
  360. (or (and (org-at-heading-p) (< beg (match-end 0)))
  361. (re-search-forward
  362. (org-with-limited-levels org-outline-regexp-bol) end t)))))))
  363. (defun org-indent-refresh-maybe (beg end _)
  364. "Refresh indentation properties in an adequate portion of buffer.
  365. BEG and END are the positions of the beginning and end of the
  366. range of inserted text. DUMMY is an unused argument.
  367. This function is meant to be called by `after-change-functions'."
  368. (when org-indent-mode
  369. (save-match-data
  370. ;; If a headline was modified or inserted, set properties until
  371. ;; next headline.
  372. (org-with-wide-buffer
  373. (if (or org-indent-modified-headline-flag
  374. (save-excursion
  375. (goto-char beg)
  376. (beginning-of-line)
  377. (re-search-forward
  378. (org-with-limited-levels org-outline-regexp-bol)
  379. (save-excursion
  380. (goto-char end)
  381. ;; Extend to headline if END is within its
  382. ;; headline stars.
  383. (line-end-position))
  384. t)))
  385. (let ((end (save-excursion
  386. (goto-char end)
  387. (org-with-limited-levels (outline-next-heading))
  388. (point))))
  389. (setq org-indent-modified-headline-flag nil)
  390. (org-indent-add-properties beg end))
  391. ;; Otherwise, only set properties on modified area.
  392. (org-indent-add-properties beg end))))))
  393. (provide 'org-indent)
  394. ;; Local variables:
  395. ;; generated-autoload-file: "org-loaddefs.el"
  396. ;; End:
  397. ;;; org-indent.el ends here