org-indent.el 14 KB

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