org-indent.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. ;;; org-indent.el --- Dynamic indentation for Org -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2015 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. ;;
  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 <http://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. (eval-when-compile
  40. (require 'cl))
  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-hide-leading-stars-before-indent-mode nil
  70. "Used locally.")
  71. (defvar org-indent-modified-headline-flag nil
  72. "Non-nil means the last deletion operated on a headline.
  73. It is modified by `org-indent-notify-modified-headline'.")
  74. (defcustom org-indent-boundary-char ?\s
  75. "The end of the virtual indentation strings, a single-character string.
  76. The default is just a space, but if you wish, you can use \"|\" or so.
  77. This can be useful on a terminal window - under a windowing system,
  78. it may be prettier to customize the `org-indent' face."
  79. :group 'org-indent
  80. :type 'character)
  81. (defcustom org-indent-mode-turns-off-org-adapt-indentation t
  82. "Non-nil means setting the variable `org-indent-mode' will \
  83. turn off indentation adaptation.
  84. For details see the variable `org-adapt-indentation'."
  85. :group 'org-indent
  86. :type 'boolean)
  87. (defcustom org-indent-mode-turns-on-hiding-stars t
  88. "Non-nil means setting the variable `org-indent-mode' will \
  89. turn on `org-hide-leading-stars'."
  90. :group 'org-indent
  91. :type 'boolean)
  92. (defcustom org-indent-indentation-per-level 2
  93. "Indentation per level in number of characters."
  94. :group 'org-indent
  95. :type 'integer)
  96. (defface org-indent '((t (:inherit org-hide)))
  97. "Face for outline indentation.
  98. The default is to make it look like whitespace. But you may find it
  99. useful to make it ever so slightly different."
  100. :group 'org-faces)
  101. (defsubst org-indent-remove-properties (beg end)
  102. "Remove indentations between BEG and END."
  103. (org-with-silent-modifications
  104. (remove-text-properties beg end '(line-prefix nil wrap-prefix nil))))
  105. ;;;###autoload
  106. (define-minor-mode org-indent-mode
  107. "When active, indent text according to outline structure.
  108. Internally this works by adding `line-prefix' and `wrap-prefix'
  109. properties, after each buffer modification, on the modified zone.
  110. The process is synchronous. Though, initial indentation of
  111. buffer, which can take a few seconds on large buffers, is done
  112. during idle time."
  113. nil " Ind" nil
  114. (cond
  115. ((and org-indent-mode (featurep 'xemacs))
  116. (message "org-indent-mode does not work in XEmacs - refusing to turn it on")
  117. (setq org-indent-mode nil))
  118. ((and org-indent-mode
  119. (not (org-version-check "23.1.50" "Org Indent mode" :predicate)))
  120. (message "org-indent-mode can crash Emacs 23.1 - refusing to turn it on!")
  121. (ding)
  122. (sit-for 1)
  123. (setq org-indent-mode nil))
  124. (org-indent-mode
  125. ;; mode was turned on.
  126. (setq-local indent-tabs-mode nil)
  127. (setq-local org-indent--initial-marker (copy-marker 1))
  128. (when org-indent-mode-turns-off-org-adapt-indentation
  129. (setq-local org-adapt-indentation nil))
  130. (when org-indent-mode-turns-on-hiding-stars
  131. (setq-local org-hide-leading-stars-before-indent-mode
  132. org-hide-leading-stars)
  133. (setq-local org-hide-leading-stars t))
  134. (org-add-hook 'filter-buffer-substring-functions
  135. (lambda (fun start end delete)
  136. (org-indent-remove-properties-from-string
  137. (funcall fun start end delete)))
  138. nil t)
  139. (org-add-hook 'after-change-functions 'org-indent-refresh-maybe nil 'local)
  140. (org-add-hook 'before-change-functions
  141. 'org-indent-notify-modified-headline nil 'local)
  142. (and font-lock-mode (org-restart-font-lock))
  143. (org-indent-remove-properties (point-min) (point-max))
  144. ;; Submit current buffer to initialize agent. If it's the first
  145. ;; buffer submitted, also start the agent. Current buffer is
  146. ;; pushed in both cases to avoid a race condition.
  147. (if org-indent-agentized-buffers
  148. (push (current-buffer) org-indent-agentized-buffers)
  149. (push (current-buffer) org-indent-agentized-buffers)
  150. (setq org-indent-agent-timer
  151. (run-with-idle-timer 0.2 t #'org-indent-initialize-agent))))
  152. (t
  153. ;; mode was turned off (or we refused to turn it on)
  154. (kill-local-variable 'org-adapt-indentation)
  155. (setq org-indent-agentized-buffers
  156. (delq (current-buffer) org-indent-agentized-buffers))
  157. (when (markerp org-indent--initial-marker)
  158. (set-marker org-indent--initial-marker nil))
  159. (when (boundp 'org-hide-leading-stars-before-indent-mode)
  160. (setq-local org-hide-leading-stars
  161. org-hide-leading-stars-before-indent-mode))
  162. (remove-hook 'filter-buffer-substring-functions
  163. (lambda (fun start end delete)
  164. (org-indent-remove-properties-from-string
  165. (funcall fun start end delete))))
  166. (remove-hook 'after-change-functions 'org-indent-refresh-maybe 'local)
  167. (remove-hook 'before-change-functions
  168. 'org-indent-notify-modified-headline 'local)
  169. (org-with-wide-buffer
  170. (org-indent-remove-properties (point-min) (point-max)))
  171. (and font-lock-mode (org-restart-font-lock))
  172. (redraw-display))))
  173. (defun org-indent-indent-buffer ()
  174. "Add indentation properties to the accessible part of the buffer."
  175. (interactive)
  176. (if (not (derived-mode-p 'org-mode))
  177. (error "Not in Org mode")
  178. (message "Setting buffer indentation. It may take a few seconds...")
  179. (org-indent-remove-properties (point-min) (point-max))
  180. (org-indent-add-properties (point-min) (point-max))
  181. (message "Indentation of buffer set.")))
  182. (defun org-indent-remove-properties-from-string (string)
  183. "Remove indentation properties from STRING."
  184. (remove-text-properties 0 (length string)
  185. '(line-prefix nil wrap-prefix nil) string)
  186. string)
  187. (defun org-indent-initialize-agent ()
  188. "Start or resume current buffer initialization.
  189. Only buffers in `org-indent-agentized-buffers' trigger an action.
  190. When no more buffer is being watched, the agent suppress itself."
  191. (when org-indent-agent-resume-timer
  192. (cancel-timer org-indent-agent-resume-timer))
  193. (setq org-indent-agentized-buffers
  194. (cl-remove-if-not #'buffer-live-p org-indent-agentized-buffers))
  195. (cond
  196. ;; Job done: kill agent.
  197. ((not org-indent-agentized-buffers) (cancel-timer org-indent-agent-timer))
  198. ;; Current buffer is agentized: start/resume initialization
  199. ;; somewhat aggressively.
  200. ((memq (current-buffer) org-indent-agentized-buffers)
  201. (org-indent-initialize-buffer (current-buffer)
  202. org-indent-agent-active-delay))
  203. ;; Else, start/resume initialization of the last agentized buffer,
  204. ;; softly.
  205. (t (org-indent-initialize-buffer (car org-indent-agentized-buffers)
  206. org-indent-agent-passive-delay))))
  207. (defun org-indent-initialize-buffer (buffer delay)
  208. "Set virtual indentation for the buffer BUFFER, asynchronously.
  209. Give hand to other idle processes if it takes longer than DELAY,
  210. a time value."
  211. (with-current-buffer buffer
  212. (when org-indent-mode
  213. (org-with-wide-buffer
  214. (let ((interruptp
  215. ;; Always nil unless interrupted.
  216. (catch 'interrupt
  217. (and org-indent--initial-marker
  218. (marker-position org-indent--initial-marker)
  219. (org-indent-add-properties org-indent--initial-marker
  220. (point-max)
  221. delay)
  222. nil))))
  223. (move-marker org-indent--initial-marker interruptp)
  224. ;; Job is complete: un-agentize buffer.
  225. (unless interruptp
  226. (setq org-indent-agentized-buffers
  227. (delq buffer org-indent-agentized-buffers))))))))
  228. (defun org-indent-set-line-properties (level indentation &optional heading)
  229. "Set prefix properties on current line an move to next one.
  230. LEVEL is the current level of heading. INDENTATION is the
  231. expected indentation when wrapping line.
  232. When optional argument HEADING is non-nil, assume line is at
  233. a heading. Moreover, if is is `inlinetask', the first star will
  234. have `org-warning' face."
  235. (let* ((stars (if (<= level 1) ""
  236. (make-string (* (1- org-indent-indentation-per-level)
  237. (1- level))
  238. ?*)))
  239. (line
  240. (cond
  241. ((and (org-bound-and-true-p org-inlinetask-show-first-star)
  242. (eq heading 'inlinetask))
  243. (concat org-indent-inlinetask-first-star
  244. (org-add-props (substring stars 1) nil 'face 'org-hide)))
  245. (heading (org-add-props stars nil 'face 'org-hide))
  246. (t (concat (org-add-props (concat stars (make-string level ?*))
  247. nil 'face 'org-indent)
  248. (and (> level 0)
  249. (char-to-string org-indent-boundary-char))))))
  250. (wrap
  251. (org-add-props
  252. (concat stars
  253. (make-string level ?*)
  254. (if heading " "
  255. (make-string (+ indentation (min level 1)) ?\s)))
  256. nil 'face 'org-indent)))
  257. ;; Add properties down to the next line to indent empty lines.
  258. (add-text-properties (line-beginning-position) (line-beginning-position 2)
  259. `(line-prefix ,line wrap-prefix ,wrap)))
  260. (forward-line))
  261. (defun org-indent-add-properties (beg end &optional delay)
  262. "Add indentation properties between BEG and END.
  263. When DELAY is non-nil, it must be a time value. In that case,
  264. the process is asynchronous and can be interrupted, either by
  265. user request, or after DELAY. This is done by throwing the
  266. `interrupt' tag along with the buffer position where the process
  267. stopped."
  268. (save-match-data
  269. (org-with-wide-buffer
  270. (goto-char beg)
  271. (beginning-of-line)
  272. ;; Initialize prefix at BEG, according to current entry's level.
  273. (let* ((case-fold-search t)
  274. (limited-re (org-get-limited-outline-regexp))
  275. (level (or (org-current-level) 0))
  276. (time-limit (and delay (time-add (current-time) delay))))
  277. ;; For each line, set `line-prefix' and `wrap-prefix'
  278. ;; properties depending on the type of line (headline, inline
  279. ;; task, item or other).
  280. (org-with-silent-modifications
  281. (while (and (<= (point) end) (not (eobp)))
  282. (cond
  283. ;; When in asynchronous mode, check if interrupt is
  284. ;; required.
  285. ((and delay (input-pending-p)) (throw 'interrupt (point)))
  286. ;; In asynchronous mode, take a break of
  287. ;; `org-indent-agent-resume-delay' every DELAY to avoid
  288. ;; blocking any other idle timer or process output.
  289. ((and delay (time-less-p time-limit (current-time)))
  290. (setq org-indent-agent-resume-timer
  291. (run-with-idle-timer
  292. (time-add (current-idle-time) org-indent-agent-resume-delay)
  293. nil #'org-indent-initialize-agent))
  294. (throw 'interrupt (point)))
  295. ;; Headline or inline task.
  296. ((looking-at org-outline-regexp)
  297. (let* ((nstars (- (match-end 0) (match-beginning 0) 1))
  298. (type (or (org-looking-at-p limited-re) 'inlinetask)))
  299. (org-indent-set-line-properties nstars 0 type)
  300. ;; At an headline, define new value for LEVEL.
  301. (unless (eq type 'inlinetask) (setq level nstars))))
  302. ;; List item: `wrap-prefix' is set where body starts.
  303. ((org-at-item-p)
  304. (org-indent-set-line-properties
  305. level (org-list-item-body-column (point))))
  306. ;; Regular line.
  307. (t
  308. (org-indent-set-line-properties level (org-get-indentation))))))))))
  309. (defun org-indent-notify-modified-headline (beg end)
  310. "Set `org-indent-modified-headline-flag' depending on context.
  311. BEG and END are the positions of the beginning and end of the
  312. range of deleted text.
  313. This function is meant to be called by `before-change-functions'.
  314. Flag will be non-nil if command is going to modify or delete an
  315. headline."
  316. (when org-indent-mode
  317. (setq org-indent-modified-headline-flag
  318. (save-excursion
  319. (goto-char beg)
  320. (save-match-data
  321. (or (and (org-at-heading-p) (< beg (match-end 0)))
  322. (re-search-forward org-outline-regexp-bol end t)))))))
  323. (defun org-indent-refresh-maybe (beg end _)
  324. "Refresh indentation properties in an adequate portion of buffer.
  325. BEG and END are the positions of the beginning and end of the
  326. range of inserted text. DUMMY is an unused argument.
  327. This function is meant to be called by `after-change-functions'."
  328. (when org-indent-mode
  329. (save-match-data
  330. ;; If a headline was modified or inserted, set properties until
  331. ;; next headline.
  332. (if (or org-indent-modified-headline-flag
  333. (save-excursion
  334. (goto-char beg)
  335. (beginning-of-line)
  336. (re-search-forward org-outline-regexp-bol end t)))
  337. (let ((end (save-excursion
  338. (goto-char end)
  339. (org-with-limited-levels (outline-next-heading))
  340. (point))))
  341. (setq org-indent-modified-headline-flag nil)
  342. (org-indent-add-properties beg end))
  343. ;; Otherwise, only set properties on modified area.
  344. (org-indent-add-properties beg end)))))
  345. (provide 'org-indent)
  346. ;; Local variables:
  347. ;; generated-autoload-file: "org-loaddefs.el"
  348. ;; End:
  349. ;;; org-indent.el ends here