org-indent.el 17 KB

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