org-indent.el 15 KB

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