org-indent.el 14 KB

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