org-indent.el 17 KB

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